Skip to content

Commit 2e81566

Browse files
author
Tushar Roy
committed
update in ford fulkerson python code
1 parent ab8a5eb commit 2e81566

2 files changed

Lines changed: 39 additions & 36 deletions

File tree

‎python/graph/fordfulkerson.py‎

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,56 @@
33
from queue import Queue
44
import sys
55

6-
def max_flow(capacity, s, t):
7-
flow = [[0 for x in range(len(capacity))] for x in range(len(capacity[0]))]
6+
def max_flow(capacity, source, sink):
7+
residual_capacity = [x[:] for x in capacity]
8+
augmented_paths = []
89
max_flow = 0
910
while True:
10-
parent,min_capacity = bfs(capacity, flow, s, t)
11-
if min_capacity == 0:
12-
break;
13-
14-
max_flow += min_capacity
15-
16-
v = t;
17-
while v != s:
11+
found_augmented_path, parent = bfs(residual_capacity, source, sink)
12+
if not found_augmented_path:
13+
break
14+
augmented_path = []
15+
v = sink
16+
min_capacity = sys.maxsize
17+
while not v == source:
18+
augmented_path.append(v)
1819
u = parent[v]
19-
flow[u][v] += min_capacity
20-
flow[v][u] -= min_capacity
20+
if min_capacity > residual_capacity[u][v]:
21+
min_capacity = residual_capacity[u][v]
2122
v = u
23+
augmented_path.append(source)
24+
augmented_paths.append(augmented_path[::-1])
25+
max_flow += min_capacity
26+
27+
v = sink
28+
while not v == source:
29+
u = parent[v]
30+
residual_capacity[u][v] -= min_capacity
31+
residual_capacity[v][u] += min_capacity
32+
v = u
2233

34+
print("Augmented path")
35+
print(augmented_paths)
2336
return max_flow
24-
25-
def bfs(capacity, flow, s, t):
37+
38+
def bfs(residual_capacity, source, sink):
2639
visited = set()
2740
queue = Queue()
28-
queue.put(s)
29-
visited.add(s)
3041
parent = {}
31-
min_capacity = {}
32-
min_capacity[s] = sys.maxsize;
42+
queue.put(source)
43+
visited.add(source)
44+
found_augmented_path = False
3345
while not queue.empty():
3446
u = queue.get()
35-
for v in range(len(capacity)):
36-
residual_capacity = capacity[u][v] - flow[u][v]
37-
if v not in visited and residual_capacity > 0:
47+
for v in range(len(residual_capacity)):
48+
if v not in visited and residual_capacity[u][v] > 0:
3849
parent[v] = u
39-
if (residual_capacity < min_capacity[u]):
40-
min_capacity[v] = residual_capacity
41-
else:
42-
min_capacity[v] = min_capacity[u]
43-
if v == t:
44-
return (parent, min_capacity[v])
4550
visited.add(v)
4651
queue.put(v)
47-
return (parent, 0)
52+
if v == sink:
53+
found_augmented_path = True
54+
break;
55+
return found_augmented_path, parent
4856

4957
if __name__ == '__main__':
5058
capacity = [[0, 3, 0, 3, 0, 0, 0],
@@ -55,11 +63,5 @@ def bfs(capacity, flow, s, t):
5563
[0, 0, 0, 0, 0, 0, 9],
5664
[0, 0, 0, 0, 0, 0, 0]]
5765

58-
capacity1 = [[0, 16, 13, 0, 0, 0],
59-
[0, 0, 10, 12, 0, 0],
60-
[0, 4, 0, 0, 14, 0],
61-
[0, 0, 9, 0, 0, 20],
62-
[0, 0, 0, 7, 0, 4],
63-
[0, 0, 0, 0, 0, 0]]
64-
max_val = max_flow(capacity1, 0 ,5)
66+
max_val = max_flow(capacity, 0, 6)
6567
print(max_val)

‎src/com/interview/graph/FordFulkerson.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public int maxFlow(int capacity[][], int source, int sink){
8686
* Prints all the augmented path which contribute to max flow
8787
*/
8888
private void printAugmentedPaths(List<List<Integer>> augmentedPaths) {
89+
System.out.println("Augmented paths");
8990
augmentedPaths.forEach(path -> {
9091
path.forEach(i -> System.out.print(i + " "));
9192
System.out.println();
@@ -134,6 +135,6 @@ public static void main(String args[]){
134135
{0, 0, 0, 0, 0, 0, 9},
135136
{0, 0, 0, 0, 0, 0, 0}};
136137

137-
System.out.println("Maximum capacity " + ff.maxFlow(capacity, 0, 6));
138+
System.out.println("\nMaximum capacity " + ff.maxFlow(capacity, 0, 6));
138139
}
139140
}

0 commit comments

Comments
 (0)