[prev] 77 [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 visited[] from s to t do
|  |  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

Shortest augmenting path can be found by standard BFS