Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def bfs(n, adj_mat, s, t, parent):
- visited =[False]*n
- queue=[]
- queue.append(s)
- visited[s] = True
- while queue:
- u = queue.pop(0)
- for ind, val in enumerate(adj_mat[u]):
- if visited[ind] == False and val > 0 :
- queue.append(ind)
- visited[ind] = True
- parent[ind] = u
- return True if visited[t] else False
- def fordfulkerson(n, adj_mat, s, t):
- parent = [-1]*(n)
- max_flow = 0
- while bfs(n, adj_mat, s, t, parent) :
- path_flow = float("Inf")
- u = t
- while(u != s):
- path_flow = min(path_flow, adj_mat[parent[u]][u])
- u = parent[u]
- max_flow += path_flow
- v = sink
- while(v != source):
- u = parent[v]
- adj_mat[u][v] -= path_flow
- adj_mat[v][u] += path_flow
- v = parent[v]
- return max_flow
- n=int(input("ENter the numbers of nodes: "))
- adj_mat = [[0]*(n+1) for i in range(n+1)]
- m=int(input("ENter number of edges: "))
- for _ in range(m):
- x, y, f=eval(input("Enter edge and capacity: "))
- adj_mat[x][y]=f
- source, sink = map(int, input("Enter source and sink: "))
- print("MAX: ", fordfulkerson(n, adj_mat, source, sink)
Add Comment
Please, Sign In to add comment