|
| 1 | +# Python program for implementation of Ford Fulkerson algorithm |
| 2 | + |
| 3 | +#from collections import defaultdict |
| 4 | + |
| 5 | +#This class represents a directed graph using adjacency matrix representation |
| 6 | +class Graph: |
| 7 | + def __init__(self,graph): |
| 8 | + self.graph = graph # residual graph |
| 9 | + self. ROW = len(graph) |
| 10 | + #self.COL = len(gr[0]) |
| 11 | + |
| 12 | + |
| 13 | + '''Returns true if there is a path from source 's' to sink 't' in |
| 14 | + residual graph. Also fills parent[] to store the path ''' |
| 15 | + def BFS(self,s, t, parent): |
| 16 | + |
| 17 | + # Mark all the vertices as not visited |
| 18 | + visited =[False]*(self.ROW) |
| 19 | + |
| 20 | + # Create a queue for BFS |
| 21 | + queue=[] |
| 22 | + |
| 23 | + # Mark the source node as visited and enqueue it |
| 24 | + queue.append(s) |
| 25 | + visited[s] = True |
| 26 | + |
| 27 | + # Standard BFS Loop |
| 28 | + while queue: |
| 29 | + |
| 30 | + #Dequeue a vertex from queue and print it |
| 31 | + u = queue.pop(0) |
| 32 | + |
| 33 | + # Get all adjacent vertices of the dequeued vertex u |
| 34 | + # If a adjacent has not been visited, then mark it |
| 35 | + # visited and enqueue it |
| 36 | + for ind, val in enumerate(self.graph[u]): |
| 37 | + if visited[ind] == False and val > 0 : |
| 38 | + queue.append(ind) |
| 39 | + visited[ind] = True |
| 40 | + parent[ind] = u |
| 41 | + |
| 42 | + # If we reached sink in BFS starting from source, then return |
| 43 | + # true, else false |
| 44 | + return True if visited[t] else False |
| 45 | + |
| 46 | + |
| 47 | + # Returns tne maximum flow from s to t in the given graph |
| 48 | + def FordFulkerson(self, source, sink): |
| 49 | + |
| 50 | + # This array is filled by BFS and to store path |
| 51 | + parent = [-1]*(self.ROW) |
| 52 | + |
| 53 | + max_flow = 0 # There is no flow initially |
| 54 | + |
| 55 | + # Augment the flow while there is path from source to sink |
| 56 | + while self.BFS(source, sink, parent) : |
| 57 | + |
| 58 | + # Find minimum residual capacity of the edges along the |
| 59 | + # path filled by BFS. Or we can say find the maximum flow |
| 60 | + # through the path found. |
| 61 | + path_flow = float("Inf") |
| 62 | + s = sink |
| 63 | + while(s != source): |
| 64 | + path_flow = min (path_flow, self.graph[parent[s]][s]) |
| 65 | + s = parent[s] |
| 66 | + |
| 67 | + # Add path flow to overall flow |
| 68 | + max_flow += path_flow |
| 69 | + |
| 70 | + # update residual capacities of the edges and reverse edges |
| 71 | + # along the path |
| 72 | + v = sink |
| 73 | + while(v != source): |
| 74 | + u = parent[v] |
| 75 | + self.graph[u][v] -= path_flow |
| 76 | + self.graph[v][u] += path_flow |
| 77 | + v = parent[v] |
| 78 | + |
| 79 | + return max_flow |
| 80 | + |
| 81 | + |
| 82 | +# Create a graph given in the above diagram |
| 83 | +graph=[[1000,1,0,0], |
| 84 | + [0,1,1,0], |
| 85 | + [0,0,0,1000], |
| 86 | + [0,0,0,0]] |
| 87 | +g = Graph(graph) |
| 88 | + |
| 89 | +source = 0; sink = 3 |
| 90 | + |
| 91 | +print ("The maximum possible flow is %d " % g.FordFulkerson(source, sink)) |
| 92 | + |
0 commit comments