realsdx

ford-gfg

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