[prev] 178 [next]

Edmonds-Karp Algorithm (cont)

Algorithm:

flow[][]  // V×V array of current flow
visited[] /* array of predecessor nodes on shortest path
             from source to sink in residual network */

maxflow(G):
|  Input  flow network G with source s and sink t
|  Output maximum flow value
|
|  initialise flow[v][w]=0 for all vertices v, w
|  maxflow=0
|        
|  while ∃shortest augmenting path from s to t do  /* Run BFS on "residual network"
|  |                                                  given by capacity[v][w] > flow[v][w]
|  |                                                  to find a shortest path "visited[]" */
|  |  df = maximum additional flow via visited[]
|  |  // adjust flow so as to represent residual graph
|  |  v=t
|  |  while v≠s do
|  |  |  flow[visited[v]][v] = flow[visited[v]][v] + df;
|  |  |  flow[v][visited[v]] = flow[v][visited[v]] - df;
|  |  |  v=visited[v]
|  |  end while
|  |  maxflow=maxflow+df
|  end while
|  return maxflow