Skip to content

Commit 2f53847

Browse files
committed
1 parent ab5f262 commit 2f53847

File tree

3 files changed

+115
-4
lines changed

3 files changed

+115
-4
lines changed

networking_flow/Ford-Fulkerson.py

-4
This file was deleted.

networking_flow/Ford_Fulkerson.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Ford-Fulkerson Algorithm for Maximum Flow Problem
2+
"""
3+
Description:
4+
(1) Start with initial flow as 0;
5+
(2) Choose augmenting path from source to sink and add path to flow;
6+
"""
7+
8+
def BFS(graph, s, t, parent):
9+
# Return True if there is node that has not iterated.
10+
visited = [False]*len(graph)
11+
queue=[]
12+
queue.append(s)
13+
visited[s] = True
14+
15+
while queue:
16+
u = queue.pop(0)
17+
for ind in range(len(graph[u])):
18+
if visited[ind] == False and graph[u][ind] > 0:
19+
queue.append(ind)
20+
visited[ind] = True
21+
parent[ind] = u
22+
23+
return True if visited[t] else False
24+
25+
def FordFulkerson(graph, source, sink):
26+
# This array is filled by BFS and to store path
27+
parent = [-1]*(len(graph))
28+
max_flow = 0
29+
while BFS(graph, source, sink, parent) :
30+
path_flow = float("Inf")
31+
s = sink
32+
33+
while(s != source):
34+
# Find the minimum value in select path
35+
path_flow = min (path_flow, graph[parent[s]][s])
36+
s = parent[s]
37+
38+
max_flow += path_flow
39+
v = sink
40+
41+
while(v != source):
42+
u = parent[v]
43+
graph[u][v] -= path_flow
44+
graph[v][u] += path_flow
45+
v = parent[v]
46+
return max_flow
47+
48+
graph = [[0, 16, 13, 0, 0, 0],
49+
[0, 0, 10 ,12, 0, 0],
50+
[0, 4, 0, 0, 14, 0],
51+
[0, 0, 9, 0, 0, 20],
52+
[0, 0, 0, 7, 0, 4],
53+
[0, 0, 0, 0, 0, 0]]
54+
55+
source, sink = 0, 5
56+
print(FordFulkerson(graph, source, sink))

networking_flow/Minimum_cut.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Minimum cut on Ford_Fulkerson algorithm.
2+
3+
def BFS(graph, s, t, parent):
4+
# Return True if there is node that has not iterated.
5+
visited = [False]*len(graph)
6+
queue=[]
7+
queue.append(s)
8+
visited[s] = True
9+
10+
while queue:
11+
u = queue.pop(0)
12+
for ind in range(len(graph[u])):
13+
if visited[ind] == False and graph[u][ind] > 0:
14+
queue.append(ind)
15+
visited[ind] = True
16+
parent[ind] = u
17+
18+
return True if visited[t] else False
19+
20+
def mincut(graph, source, sink):
21+
# This array is filled by BFS and to store path
22+
parent = [-1]*(len(graph))
23+
max_flow = 0
24+
res = []
25+
temp = [i[:] for i in graph] # Record orignial cut, copy.
26+
while BFS(graph, source, sink, parent) :
27+
path_flow = float("Inf")
28+
s = sink
29+
30+
while(s != source):
31+
# Find the minimum value in select path
32+
path_flow = min (path_flow, graph[parent[s]][s])
33+
s = parent[s]
34+
35+
max_flow += path_flow
36+
v = sink
37+
38+
while(v != source):
39+
u = parent[v]
40+
graph[u][v] -= path_flow
41+
graph[v][u] += path_flow
42+
v = parent[v]
43+
44+
for i in range(len(graph)):
45+
for j in range(len(graph[0])):
46+
if graph[i][j] == 0 and temp[i][j] > 0:
47+
res.append((i,j))
48+
49+
return res
50+
51+
graph = [[0, 16, 13, 0, 0, 0],
52+
[0, 0, 10 ,12, 0, 0],
53+
[0, 4, 0, 0, 14, 0],
54+
[0, 0, 9, 0, 0, 20],
55+
[0, 0, 0, 7, 0, 4],
56+
[0, 0, 0, 0, 0, 0]]
57+
58+
source, sink = 0, 5
59+
print(mincut(graph, source, sink))

0 commit comments

Comments
 (0)