Skip to content

Commit cc2d83c

Browse files
author
Tushar Roy
committed
ford fulkerson for python
1 parent 5683d98 commit cc2d83c

2 files changed

Lines changed: 119 additions & 71 deletions

File tree

‎python/graph/fordfulkerson.py‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#ford fulkerson method edomonds karp algorithm
2+
# java code https://github.com/mission-peace/interview/blob/master/src/com/interview/graph/FordFulkerson.java
3+
from queue import Queue
4+
import sys
5+
6+
def max_flow(capacity, s, t):
7+
flow = [[0 for x in range(len(capacity))] for x in range(len(capacity[0]))]
8+
max_flow = 0
9+
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:
18+
u = parent[v]
19+
flow[u][v] += min_capacity
20+
flow[v][u] -= min_capacity
21+
v = u
22+
23+
return max_flow
24+
25+
def bfs(capacity, flow, s, t):
26+
visited = set()
27+
queue = Queue()
28+
queue.put(s)
29+
visited.add(s)
30+
parent = {}
31+
min_capacity = sys.maxsize
32+
while not queue.empty():
33+
u = queue.get()
34+
for v in range(len(capacity)):
35+
if v not in visited and capacity[u][v] - flow[u][v] > 0:
36+
parent[v] = u
37+
if min_capacity > capacity[u][v] - flow[u][v]:
38+
min_capacity = capacity[u][v] - flow[u][v]
39+
if v == t:
40+
return (parent, min_capacity)
41+
visited.add(v)
42+
queue.put(v)
43+
return (parent, 0)
44+
45+
if __name__ == '__main__':
46+
capacity = [[0, 3, 0, 3, 0, 0, 0],
47+
[0, 0, 4, 0, 0, 0, 0],
48+
[3, 0, 0, 1, 2, 0, 0],
49+
[0, 0, 0, 0, 2, 6, 0],
50+
[0, 1, 0, 0, 0, 0, 1],
51+
[0, 0, 0, 0, 0, 0, 9],
52+
[0, 0, 0, 0, 0, 0, 0]]
53+
54+
capacity1 = [[0, 16, 13, 0, 0, 0],
55+
[0, 0, 10, 12, 0, 0],
56+
[0, 4, 0, 0, 14, 0],
57+
[0, 0, 9, 0, 0, 20],
58+
[0, 0, 0, 7, 0, 4],
59+
[0, 0, 0, 0, 0, 0]]
60+
max_val = max_flow(capacity1, 0 ,5)
61+
print(max_val)
Lines changed: 58 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,86 @@
11
package com.interview.graph;
22

3-
import java.util.HashMap;
4-
import java.util.LinkedList;
5-
import java.util.Map;
6-
import java.util.Queue;
3+
import java.util.*;
74

85
/**
9-
http://www.geeksforgeeks.org/ford-fulkerson-algorithm-for-maximum-flow-problem/
6+
* Date 04/14/2014
7+
* @author Tushar Roy
8+
*
9+
* Ford fulkerson method edmonds karp algorithm for finding max flow
10+
*
11+
* Time complexity is O(VE^2)
12+
*
13+
* References:
14+
* http://www.geeksforgeeks.org/ford-fulkerson-algorithm-for-maximum-flow-problem/
15+
* https://en.wikipedia.org/wiki/Edmonds%E2%80%93Karp_algorithm
1016
*/
11-
public class FordFulkerson<T> {
17+
public class FordFulkerson {
1218

13-
public int maxFlow(Graph<T> graph, Vertex<T> s, Vertex<T> t){
14-
15-
int[][] matrix = new int[graph.getAllVertex().size()][graph.getAllVertex().size()];
16-
for(Edge<T> edge : graph.getAllEdges()){
17-
matrix[(int)edge.getVertex1().getId()][(int)edge.getVertex2().getId()] = edge.getWeight();
18-
}
19-
20-
21-
Map<Integer,Integer> parent = new HashMap<Integer,Integer>();
22-
23-
int max_flow = 0;
24-
while(BFS(matrix,parent,(int)s.getId(),(int)t.getId())){
25-
26-
int min_flow = 10000;
27-
int v = (int)t.getId();
28-
while(parent.containsKey(v)){
29-
int u = parent.get(v);
30-
if(min_flow > matrix[u][v]){
31-
min_flow = matrix[u][v];
32-
}
33-
v = u;
19+
public int maxFlow(int capacity[][], int s, int t){
20+
int flow[][] = new int[capacity.length][capacity[0].length];
21+
Map<Integer,Integer> parent = new HashMap<>();
22+
int maxFlow = 0;
23+
while(true){
24+
int minCapacity = BFS(capacity, flow, parent, s, t);
25+
if(minCapacity == 0) {
26+
break;
3427
}
35-
36-
max_flow += min_flow;
37-
38-
v = (int)t.getId();
39-
while(parent.containsKey(v)){
28+
maxFlow += minCapacity;
29+
30+
int v = t;
31+
while(v != s){
4032
int u = parent.get(v);
41-
matrix[u][v] -= min_flow;
42-
matrix[v][u] += min_flow;
33+
flow[u][v] += minCapacity;
34+
flow[v][u] -= minCapacity;
4335
v = u;
4436
}
4537
}
46-
return max_flow;
38+
return maxFlow;
4739
}
4840

49-
private boolean BFS(int[][] matrix, Map<Integer,Integer> parent,
41+
private int BFS(int[][] capacity, int[][] flow, Map<Integer,Integer> parent,
5042
int s, int t){
51-
52-
Map<Integer,Boolean> visited = new HashMap<Integer,Boolean>();
53-
Queue<Integer> queue = new LinkedList<Integer>();
43+
44+
Set<Integer> visited = new HashSet<>();
45+
Queue<Integer> queue = new LinkedList<>();
5446
queue.add(s);
55-
visited.put(s, true);
56-
while(queue.size() != 0){
47+
visited.add(s);
48+
int minCapacity = Integer.MAX_VALUE;
49+
while(!queue.isEmpty()){
5750
int u = queue.poll();
58-
for(int v =0 ; v < matrix.length; v++){
59-
if(!visited.containsKey(v) && matrix[u][v] > 0){
51+
for(int v = 0; v < flow.length; v++){
52+
if(!visited.contains(v) && capacity[u][v] - flow[u][v] > 0){
6053
parent.put(v, u);
54+
if (minCapacity > capacity[u][v] - flow[u][v]) {
55+
minCapacity = capacity[u][v] - flow[u][v];
56+
}
6157
if(v == t){
62-
return true;
58+
return minCapacity;
6359
}
64-
visited.put(v, true);
60+
visited.add(v);
6561
queue.add(v);
6662
}
6763
}
6864
}
69-
return false;
65+
return 0;
7066
}
7167

7268
public static void main(String args[]){
73-
74-
Graph<Integer> graph = new Graph<Integer>(true);
75-
76-
Vertex<Integer> s = graph.addSingleVertex(0);
77-
Vertex<Integer> t = graph.addSingleVertex(7);
78-
graph.addEdge(0, 1,10);
79-
graph.addEdge(1, 2,9);
80-
graph.addEdge(2, 7,10);
81-
graph.addEdge(0, 3,5);
82-
graph.addEdge(3, 4,8);
83-
graph.addEdge(4, 7,10);
84-
graph.addEdge(0, 5,15);
85-
graph.addEdge(5, 6,16);
86-
graph.addEdge(6, 7,10);
87-
graph.addEdge(1, 3,4);
88-
graph.addEdge(3, 5,4);
89-
graph.addEdge(1, 4,15);
90-
graph.addEdge(2, 4,15);
91-
graph.addEdge(6, 3,6);
92-
graph.addEdge(4, 6,15);
93-
94-
95-
FordFulkerson<Integer> ff = new FordFulkerson<Integer>();
96-
int result = ff.maxFlow(graph, s, t);
97-
System.out.print(result);
69+
FordFulkerson ff = new FordFulkerson();
70+
int[][] capacity = {{0, 3, 0, 3, 0, 0, 0},
71+
{0, 0, 4, 0, 0, 0, 0},
72+
{3, 0, 0, 1, 2, 0, 0},
73+
{0, 0, 0, 0, 2, 6, 0},
74+
{0, 1, 0, 0, 0, 0, 1},
75+
{0, 0, 0, 0, 0, 0, 9},
76+
{0, 0, 0, 0, 0, 0, 0}};
77+
78+
int [][] capacity1 = {{0, 16, 13, 0, 0, 0},
79+
{0, 0, 10, 12, 0, 0},
80+
{0, 4, 0, 0, 14, 0},
81+
{0, 0, 9, 0, 0, 20},
82+
{0, 0, 0, 7, 0, 4},
83+
{0, 0, 0, 0, 0, 0}};
84+
System.out.print("Maximum capacity " + ff.maxFlow(capacity1, 0, 5));
9885
}
9986
}

0 commit comments

Comments
 (0)