mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
Add doctests to networking_flow/minimum_cut.py (#1126)
This commit is contained in:
parent
c74fd0c9bf
commit
dc2b575274
|
@ -1,10 +1,19 @@
|
||||||
# Minimum cut on Ford_Fulkerson algorithm.
|
# Minimum cut on Ford_Fulkerson algorithm.
|
||||||
|
|
||||||
|
test_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],
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
def BFS(graph, s, t, parent):
|
def BFS(graph, s, t, parent):
|
||||||
# Return True if there is node that has not iterated.
|
# Return True if there is node that has not iterated.
|
||||||
visited = [False]*len(graph)
|
visited = [False] * len(graph)
|
||||||
queue=[]
|
queue = [s]
|
||||||
queue.append(s)
|
|
||||||
visited[s] = True
|
visited[s] = True
|
||||||
|
|
||||||
while queue:
|
while queue:
|
||||||
|
@ -17,25 +26,29 @@ def BFS(graph, s, t, parent):
|
||||||
|
|
||||||
return True if visited[t] else False
|
return True if visited[t] else False
|
||||||
|
|
||||||
|
|
||||||
def mincut(graph, source, sink):
|
def mincut(graph, source, sink):
|
||||||
# This array is filled by BFS and to store path
|
"""This array is filled by BFS and to store path
|
||||||
parent = [-1]*(len(graph))
|
>>> mincut(test_graph, source=0, sink=5)
|
||||||
|
[(1, 3), (4, 3), (4, 5)]
|
||||||
|
"""
|
||||||
|
parent = [-1] * (len(graph))
|
||||||
max_flow = 0
|
max_flow = 0
|
||||||
res = []
|
res = []
|
||||||
temp = [i[:] for i in graph] # Record orignial cut, copy.
|
temp = [i[:] for i in graph] # Record orignial cut, copy.
|
||||||
while BFS(graph, source, sink, parent) :
|
while BFS(graph, source, sink, parent):
|
||||||
path_flow = float("Inf")
|
path_flow = float("Inf")
|
||||||
s = sink
|
s = sink
|
||||||
|
|
||||||
while(s != source):
|
while s != source:
|
||||||
# Find the minimum value in select path
|
# 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]
|
s = parent[s]
|
||||||
|
|
||||||
max_flow += path_flow
|
max_flow += path_flow
|
||||||
v = sink
|
v = sink
|
||||||
|
|
||||||
while(v != source):
|
while v != source:
|
||||||
u = parent[v]
|
u = parent[v]
|
||||||
graph[u][v] -= path_flow
|
graph[u][v] -= path_flow
|
||||||
graph[v][u] += path_flow
|
graph[v][u] += path_flow
|
||||||
|
@ -44,16 +57,10 @@ def mincut(graph, source, sink):
|
||||||
for i in range(len(graph)):
|
for i in range(len(graph)):
|
||||||
for j in range(len(graph[0])):
|
for j in range(len(graph[0])):
|
||||||
if graph[i][j] == 0 and temp[i][j] > 0:
|
if graph[i][j] == 0 and temp[i][j] > 0:
|
||||||
res.append((i,j))
|
res.append((i, j))
|
||||||
|
|
||||||
return res
|
return res
|
||||||
|
|
||||||
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
|
if __name__ == "__main__":
|
||||||
print(mincut(graph, source, sink))
|
print(mincut(test_graph, source=0, sink=5))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user