mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-05 01:09:40 +08:00
psf/black code formatting (#1277)
This commit is contained in:

committed by
Christian Clauss

parent
07f04a2e55
commit
9eac17a408
@ -4,14 +4,15 @@ Description:
|
||||
(1) Start with initial flow as 0;
|
||||
(2) Choose augmenting path from source to sink and add path to flow;
|
||||
"""
|
||||
|
||||
|
||||
|
||||
def BFS(graph, s, t, parent):
|
||||
# Return True if there is node that has not iterated.
|
||||
visited = [False]*len(graph)
|
||||
queue=[]
|
||||
visited = [False] * len(graph)
|
||||
queue = []
|
||||
queue.append(s)
|
||||
visited[s] = True
|
||||
|
||||
|
||||
while queue:
|
||||
u = queue.pop(0)
|
||||
for ind in range(len(graph[u])):
|
||||
@ -21,36 +22,40 @@ def BFS(graph, s, t, parent):
|
||||
parent[ind] = u
|
||||
|
||||
return True if visited[t] else False
|
||||
|
||||
|
||||
|
||||
def FordFulkerson(graph, source, sink):
|
||||
# This array is filled by BFS and to store path
|
||||
parent = [-1]*(len(graph))
|
||||
max_flow = 0
|
||||
while BFS(graph, source, sink, parent) :
|
||||
parent = [-1] * (len(graph))
|
||||
max_flow = 0
|
||||
while BFS(graph, source, sink, parent):
|
||||
path_flow = float("Inf")
|
||||
s = sink
|
||||
|
||||
while(s != source):
|
||||
while s != source:
|
||||
# Find the minimum value in select path
|
||||
path_flow = min (path_flow, graph[parent[s]][s])
|
||||
path_flow = min(path_flow, graph[parent[s]][s])
|
||||
s = parent[s]
|
||||
|
||||
max_flow += path_flow
|
||||
max_flow += path_flow
|
||||
v = sink
|
||||
|
||||
while(v != source):
|
||||
while v != source:
|
||||
u = parent[v]
|
||||
graph[u][v] -= path_flow
|
||||
graph[v][u] += path_flow
|
||||
v = parent[v]
|
||||
return max_flow
|
||||
|
||||
graph = [[0, 16, 13, 0, 0, 0],
|
||||
[0, 0, 10 ,12, 0, 0],
|
||||
[0, 4, 0, 0, 14, 0],
|
||||
[0, 0, 9, 0, 0, 20],
|
||||
[0, 0, 0, 7, 0, 4],
|
||||
[0, 0, 0, 0, 0, 0]]
|
||||
|
||||
graph = [
|
||||
[0, 16, 13, 0, 0, 0],
|
||||
[0, 0, 10, 12, 0, 0],
|
||||
[0, 4, 0, 0, 14, 0],
|
||||
[0, 0, 9, 0, 0, 20],
|
||||
[0, 0, 0, 7, 0, 4],
|
||||
[0, 0, 0, 0, 0, 0],
|
||||
]
|
||||
|
||||
source, sink = 0, 5
|
||||
print(FordFulkerson(graph, source, sink))
|
||||
print(FordFulkerson(graph, source, sink))
|
||||
|
Reference in New Issue
Block a user