Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Edmonds–Karp algorithm
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Pseudocode== '''algorithm''' EdmondsKarp '''is''' '''input''': graph ''(graph[v] should be the list of edges coming out of vertex v in the'' '' original graph '''and''' their corresponding constructed reverse edges'' '' which are used for push-back flow.'' '' Each edge should have a capacity 'cap', flow, source 's' and sink 't' '' '' as parameters, as well as a pointer to the reverse edge 'rev'.)'' s ''(Source vertex)'' t ''(Sink vertex)'' '''output''': flow ''(Value of maximum flow)'' flow := 0 ''(Initialize flow to zero)'' '''repeat''' ''(Run a breadth-first search (bfs) to find the shortest s-t path.'' '' We use 'pred' to store the edge taken to get to each vertex,'' '' so we can recover the path afterwards)'' q := '''queue'''() q.push(s) pred := '''array'''(graph.length) '''while''' '''not''' empty(q) '''and''' pred[t] = null cur := q.pop() '''for''' Edge e '''in''' graph[cur] '''do''' '''if''' pred[e.t] = '''null''' '''and''' e.t ≠ s '''and''' e.cap > e.flow '''then''' pred[e.t] := e q.push(e.t) '''if''' '''not''' (pred[t] = null) '''then''' ''(We found an augmenting path.'' '' See how much flow we can send)'' df := '''∞''' '''for''' (e := pred[t]; e ≠ null; e := pred[e.s]) '''do''' df := '''min'''(df, e.cap - e.flow) ''(And update edges by that amount)'' '''for''' (e := pred[t]; e ≠ null; e := pred[e.s]) '''do''' e.flow := e.flow + df e.rev.flow := e.rev.flow - df flow := flow + df '''until''' pred[t] = null ''(i.e., until no augmenting path was found)'' '''return''' flow
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)