Question: Do you know why my algorithm is not work right? Give a working python logarithm for this problem. The code must print the same result

Do you know why my algorithm is not work right? Give a working python logarithm for this problem. The code must print the same result as the test case example.
Task is next:
Given is a data network with n computers and connections between them. Through each connection, information can be sent from one machine to another at a certain number of bits per second.
Your task is to find out what is the maximum amount of data that can be transferred from a given machine to another in one second.
Implement a Download class with the following methods:
add_link adds a connection from machine a to machine b that transfers x bits per second
calculate calculates the maximum possible transfer amount from machine a to machine b
Implement the class in the file download.py according to the following example.
class Download:
def __init__(self, n):
# TRUE
def add_link(self, a, b, x):
# TRUE
def calculate(self, a, b):
# TRUE
if __name__=="__main__":
d = Download(5)
print(d.calculate(3,4)) #print must be givn 0
d.add_link(5,3,6)
print(d.calculate(5,4)) #print must be givn 0
print(d.calculate(4,5)) #print must be givn 0
print(d.calculate(5,1)) #print must be givn 0
d.add_link(5,4,9)
d.add_link(1,2,10)
print(d.calculate(3,1)) #print must be givn 0
print(d.calculate(2,4)) #print must be givn 0
print(d.calculate(5,4)) #print must be givn 9
d.add_link(5,2,9)
print(d.calculate(1,5)) #print must be givn 0
d.add_link(3,5,2)
d.add_link(1,3,2)
d.add_link(5,4,9)
print(d.calculate(2,3)) #print must be givn 0
print(d.calculate(1,3)) #print must be givn 2
print(d.calculate(3,2)) #print must be givn 2
print(d.calculate(5,4)) #print must be givn 18
print(d.calculate(4,5)) #print must be givn 0
d.add_link(4,3,9)
print(d.calculate(4,5)) #print must be givn 2
print(d.calculate(2,4)) #print must be givn 0
print(d.calculate(4,5)) #print must be givn 2
d.add_link(5,1,6)
d.add_link(3,5,3)
d.add_link(4,5,2)
print(d.calculate(3,4)) #print must be givn 0
MY ALGORITHM:
class Download:
def __init__(self, n):
self.n = n
self.graph ={}
for i in range(1, n+1):
self.graph[i]={}
def add_link(self, a, b, capacity):
if b not in self.graph[a]:
self.graph[a][b]=0
self.graph[a][b]+= capacity
if a not in self.graph[b]:
self.graph[b][a]=0
def bfs(self, source, sink, parent):
visited = set()
queue =[source]
visited.add(source)
while queue:
current = queue.pop(0)
if current == sink:
return True
for adj in self.graph[current]:
if adj not in visited and self.graph[current][adj]>0:
queue.append(adj)
visited.add(adj)
parent[adj]= current
if adj == sink:
return True
return False
def calculate(self, source, sink):
parent ={}
max_flow =0
while self.bfs(source, sink, parent):
path_flow = float('Inf')
s = sink
while s != source:
path_flow = min(path_flow, self.graph[parent[s]][s])
s = parent[s]
v = sink
while v != source:
u = parent[v]
self.graph[u][v]-= path_flow
self.graph[v][u]+= path_flow
v = u
max_flow += path_flow
return max_flow

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!