2. Overview
Flow Networks
Positive Flow and Net Flow
Flow Properties
Maximum Flow
Minimum Cut
Ford-Fulkerson Method
Edmonds-Karp Algorithm
Sample Applications
3. Flow Networks
Graphs that model materials that are transported
through conduits
Each edge represents a conduit: it has a capacity
that is an upper bound for the flow rate
Some vertices may produce materials: sources
Other vertices may consume materials: sinks
4. Flow Networks (2)
The simplest version of flow networks:
Think of edges as pipes of different sizes (capacities)
The flow is the effective quantity of liquid that is transported
through the pipes
Flow Networks are also called transportation networks
But you can transport a lot of things beside liquids, like
commodities, information, electricity, traffic
There are some interesting and difficult problems for flow
networks:
Maximum flow problem
Maximum flow with minimum cost problem
Circulation problem
Multi-commodity problem
5. Flow Networks (3)
G(V, E) – directed graph
The capacity function c: E → R+
c(u, v) = the capacity of the edge (u, v)
(u,v) E c(u,v) = 0
Assume we have a single source vertex: s
Assume we have a single sink vertex: t
Assume we have a path s .. v .. t vV
6. Positive Flow
We can define a function called positive flow
p: V x V R+
Properties of positive flow:
Capacity constraint:
u, vV: p(u, v) <= c(u, v)
Flow conservation:
uV {s, t}: flow_in(u) = flow_out(u)
uV {s, t}: vV(p(v, u)) = vV(p(u, v))
7. Positive Flow – Example
Notation on each edge: positive flow/capacity
Note that the two properties are respected
The flow is always lower than the capacity of each edge
The flow is conserved in each vertex
The source produces flow
The sink consumes flow
8. Cancellation for Positive Flow
Note that in the previous example we have:
1 unit of flow from x z
2 units of flow from z x
We can cancel 1 unit of flow in both directions
0 units of flow from x z
1 unit of flow from z x
Flow conservation is still respected (we cancel the same
amount of flow in each direction)
Capacity constraint is still respected (the flow is decreased)
In both cases, the “net flow” is 1 from z x
Without loss of generality, we may always define a
positive flow in a flow network such that for each (u, v),
u,vV, we have positive flow either from u v or from v
u, but not in both directions!
9. Net Flow
We can define a function called net flow
f: V x V R
Properties of net flow:
Capacity constraint:
u, vV: f(u, v) <= c(u, v)
Flow conservation:
uV {s, t}: total_net_flow_in(u) = 0
uV {s, t}: vV(f(u, v)) = 0
Skew symmetry:
u, vV: f(u, v) = - f(v, u)
10. Net Flow (2)
Conservation of net flow can be rewritten:
vV (f(u, v)) = 0
As f(u, v) might be either positive or negative
uV {s, t}: vV and f(u,v)<0(f(u, v)) + vV and f(u,v)>0 (f(u, v))
=0
uV {s, t}: vV and f(v,u)>0(f(v, u)) = vV and f(u,v)>0 (f(u, v))
“total positive flow entering u” = “total positive flow
leaving u”
“flow in” = “flow out”
11. Net Flow – Example
Only positive flow is illustrated in graphical
representations
Nevertheless, this does not mean that the (negative)
net flow does not exist in the other direction
12. Positive Flow and Net Flow
Differences
Net flow satisfies skew symmetry
p(u, v) >= 0, while
f(u, v) >= 0 or f(u, v) <= 0
13. Positive Flow and Net Flow (2)
Equivalence
We can define net flow using only positive flow and vice-
versa
f(u, v) = p(u, v) – p(v, u) u, vV
Need to show that using this definition, the net flow still
satisfies the three properties: capacity constraint,
conservation and skew symmetry
p(u, v) = f(u, v) if f(u, v) > 0
p(u, v) = 0 otherwise
Need to show that this definition satisfies the two
properties of positive flow: capacity constraint and
conservation
15. Value of the Flow
From now on, we shall only use net flow
Value of the flow = |f| = vV (f(s, v)) = vV (f(v, t))
In the example below: |f| = 3
16. Cancellation of (Net) Flow
Let’s say that f(u, v) = 5 and c(u, v) = 8
We can “ship” at most 3 units of flow from u v
=> f(u, v) = 8 = c(u, v)
But we can also “ship” up to 5 units of flow back from v
u
=> f(u, v) = 3 if we cancel 2 units of flow!
Called flow cancellation
If (v, u) E => f(v, u) <= c(v, u) = 0
We are not allowed to have a positive flow from v u
But we are allowed to have negative flow!
This negative flow is equal in absolute value with the
positive value from u v
17. Cancellation of (Net) Flow (2)
Due to cancellation, a flow only gives us this so-
called “net” effect. We cannot reconstruct actual
“shipments” of products from a net flow.
A flow f(u, v) = 5 and f(v, u) = -5 can mean either:
5 units u v 6 units u v 8 units u v
0 units v u 1 units v u 3 units v u
18. Flow Operations
Extend the definition of the flow to sets of vertices
X,Y V
f(X,Y) = ΣxXΣyY f(x,y) = total flow between X and Y
Possible operations:
XV f(X,X) = 0;
X,YV f(X,Y) = -f(Y,X);
X,Y,ZV si Y⊆X
f(X Y, Z) = f(X,Z) - f(Y,Z);
f(Z, X Y) = f(Z,X) - f(Z,Y);
X,Y,ZV si XY=
f(X Y, Z) = f(X,Z) + f(Y,Z);
f(Z, X Y) = f(Z,X) + f(Z,Y)
|f| = f(s,V) = f(V,t)
19. Examples (1)
X 1/2
a b
1/8 2/2
4/4 3/3
s t
3/9 Y
c d
3/5
f(X,Y) = ΣxXΣyY f(x,y)
f(X,X) = f(s,a) + f(a,s) + f(s,b) + f(b,s) + f(a,b) + f(b,a) = 0
f(X,Y) = f(b,c) + f(b,t) = -f(c,b) - f(t,b) = -f(Y,X)
Proiectarea Algoritmilor 2010
20. Examples (2)
X 1/2
a b
1/8 2/2
Y
s 4/4 3/3 t
c 3/9 Z
3/5 d
X, Y, Z V si Y ⊆ X
f(X Y, Z) = f(X, Z) - f(Y, Z)
f(Z, X Y) = f(Z, X) - f(Z, Y)
f(X Y, Z) = 0 = f(b,t) + f(c,d) - f(b,t) - f(c,d) = f(X,Z) - f(Y,Z)
f(Z, X Y) = 0 = f(t,b) + f(d,c) - f(t,b) - f(d,c) = f(Z,X) - f(Z,Y)
Proiectarea Algoritmilor 2010
21. Examples (3)
Z
X 1/2
a b
1/8 2/2
s 4/4 3/3 t
3/9 Y
c d
3/5
X, Y, Z V si X Y =
f(XY, Z) = f(X,Z) + f(Y,Z)
f(Z, XY) = f(Z,X) + f(Z,Y)
f(X Y, Z) = f(s,b) + f(a,b) + f(t,b) + f(d,c) = f(X,Z) + f(Y,Z)
f(Z, X Y) = f(b,a) + f(b,s) + f(b,t) + f(c,d) = f(Z,X) + f(Z,Y)
Proiectarea Algoritmilor 2010
22. Examples (4)
1/2
a b
1/8 2/2
4/4 3/3
s t
3/9
c d
3/5
f(s, V) = f(V, t)
f(s, V) = f(s,a) + f(s,b) = 5 = f(d,t) + f(b,t) = f(V, t)
Proiectarea Algoritmilor 2010
23. Maximum Flow Problem
Given G(V, E), c: E R+, sV and tV
Find the maximum flow that can be “transported” from s
to t
We start with an initial void flow on all edges
We want to compute the net flow between each pair of
vertices that determines a maximum flow!
Various methods are used
Only look at one of the simplest:
Ford-Fulkerson and Edmonds-Karp
Uses the notion of residual network
24. Residual Network
Given G(V, E), c: E R+, sV and tV
Knowing a given flow in the graph, f: E R
We can compute the residual capacity for any pair of
vertices:
cf(u, v) = c(u, v) – f(u, v) >= 0 (due to capacity constraint)
Residual means: how much flow we can still transport from u
v
We can compute the residual network of the graph:
It is a graph Gf = (V, Ef)
Ef = {(u, v) V x V | cf(u, v) > 0}
It is a graph that contains edges that have residual capacity
(that can still transport flow!)
How many edges does Gf have? Find the minimum and the
maximum value!
26. Augmenting Path
Any path from the residual graph admits more flow
from the start vertex to the end vertex
Therefore, we want to find a path from the source s
to the sink t in the residual graph: augmenting path
Look for p = s..t in Gf
If we can find this augmenting path, compute how
much flow we can send along it!
We are constrained by the minimum residual capacity of
any edge along the augmenting path
cf(p) = min(cf(u, v) | (u, v) is on p)
27. Augmenting Path – Example
p = <(s, w), (w, y), (y, z), (z, t)>
Is it unique? Can you find another?
28. Summing Flows
If the graph G has a flow f
And we can find another flow f’
We can sum up the two flows to find the new flow:
f(u,v) + f’(u,v)
The value of the new flow is |f| + |f’|
Therefore, if we can find an augmenting path, p, in the residual
network of a graph, we can also find another flow, fp: V x V
R
fp(u,v) = cf(p) if (u,v) is on p
fp(u,v) = -cf(p) if (v,u) is on p
fp(u,v) = 0
We can thus define a new flow in G: |f’| = |f| + |fp| > |f|
Thus, we can improve the existing flow!
29. Graph Cuts
A cut (S, T) of a flow network G(V, E) is a partitionof
the vertices into two sets S (sS) and T = V S (t
T)
Already know cuts for minimum spanning trees
The flow across the cut is f(S, T)
The capacity of the cut is c(S, T) = summing up the
capacities of all the edges starting from a vertex in S
and ending in a vertex in T
A minimum cut in G is a cut whose capacity is
minimum over all the possible cuts in G
30. Graph Cuts – Example
S = {s, w, y}, T = {x, z, t}
f(S, T) = f(w, x) + f (y, x) + f(y, z) = 2 + (-1) + 2 = 3
Includes positive flow that goes either direction across the
cut
But only takes into account net flow that goes from any
vertex in S to any vertex in T
c(S, T) = c(w, x) + c(y, z) = 2 + 3 = 5
31. Max-Flow Min-Cut Theorem
The following statements are equivalent:
f is a maximum flow in G
There is no augmenting path in Gf
|f| = c(S, T) for all minimum cuts (S, T)
This theorem gives us a method to compute the
maximum flow: push flow along augmenting paths
until you cannot find any
This theorem also tells us how to compute a
minimum cut once the maximum flow is known
When f is a maximum flow, all the vertices reachable from
s in Gf are in S, the others are in T => (S, T) is a minimum
cut
32. Ford-Fulkerson Method
1. Start with a void flow
2. Compute the residual graph for the given flow
3. If there is an augmenting path in this residual graph
a) Increase the flow along it
b) Go to step 2
4. The maximum flow has been reached!
The disadvantage of this method is that it does not
specify how to look for the augmenting path in step 3
Any method can be used
Pick a random augmenting path
Use any search algorithms to compute the path: DFS, BFS
Be greedy: look for the fattest pipe each time! How?
33. Ford-Fulkerson Method (2)
If the augmenting path is not picked smartly, then the
worst case running time for the algorithm is O(E*|fmax|)
Why?
Each augmenting path has at most O(E) edges that will
cause a change in the value of the flow along them at
each step!
Maximum number of steps is fmax if a each step cf(p)=1
See the example on the next slides!
It is not good that the running time depends on fmax
What happens if the capacities of the edges are irrational
number? => so is fmax
34. Exemplu Ford – Fulkerson (1)
1000 B 1000
0/1000 B 0/1000
1
G
0/1 Gf A D
A D
1000 C 1000
0/1000 C 0/1000
Residual Path: A-B-C-D; Cf = 1
1
1/1000 B 0/1000 B 1000
999 G
1/1 Gf A 1 D
A D 999
0/1000 C 1/1000 1000 C 1
Residual Path : A-B-C-D; Cf = 1
1/1000 B 1/1000 1 B 999 1
Gf 999 G
A 0/1 D A 1 D
999
1/1000 C 1/1000 999 C 1
1
Proiectarea Algoritmilor 2010
Residual Path : A-B-C-D; Cf = 1
35. Exemplu Ford – Fulkerson (2)
2
2/1000 B 1/1000 B 999 1
998 G
1/1 Gf 1
A D A D
998
999 C
1/1000 C 2/1000 1 2
… Residual Path: A-C-B-D; Cf = 1
1000/1000 B 1000/1000 B
1000 1000
0/1
Gf
A D A 1 D
1000/1000 C 1000/1000 1000 C 1000
Residual Path: Ø
How many steps are needed to reach the maximum flow?
Proiectarea Algoritmilor 2010
36. Edmonds-Karp Algorithm
Is a variant of the Ford-Fulkerson algorithm that
guarantees to have a running time that is
independent of fmax
Always picks the augmenting path using BFS on Gf
Source vertex for BFS is s
You may stop when t is reached
This way, the length of the augmenting paths is
increasing when running the algorithm
We always pick an augmenting path with an equal or
greater length than the previous one
37. Edmonds-Karp Algorithm (2)
Edmonds–Karp(G, s, t)
FOREACH (u,v) in E
f(u,v) = f(v,u) = 0 // O(E)
WHILE (1) // O(E*V)
p = find-augmenting-path-using-bfs(G, f, s, t) // O(E)
IF (p == null)
BREAK
cf(p) = min{cf(u,v) | (u,v) in p} // O(E)
FOREACH (u,v) in p // O(E)
f(u,v) = f(u,v) + cf(p)
f(v,u) = -f(u,v)
RETURN f
Complexity: O(V*E2)
38. Edmonds-Karp – Example
1000 B 1000
0/1000 B 0/1000
1
G
0/1 Gf A D
A D
1000 C 1000
0/1000 C 0/1000
Residual Path: A-B-D; Cf = 1000
1000/1000 B 1000/1000 1000 B
1000
0/1 Gf 1
G
A D A D
0/1000 C 1/1000 1000 C 1000
Residual Path: A-C-D; Cf = 1000
1000/1000 B 1000/1000
1000 B
1000
0/1 Gf
A D 1
A D
1000/1000 C 1000/1000 1000 1000
C
Proiectarea Algoritmilor 2010
Residual Path: Ø
39. Application: Maximum Bipartite Matching
Bipartite graph: we can partition the graph into two sets
of vertices L, R such as L U R = V and for all (u, v)E,
uL, vR
Matching: a subset of edges, M, such that for all vV at
most an edge in M is incident in v
Need to transform this problem into a maximum flow
40. Application: The Escape Problem
http://webcourse.cs.technion.ac.il/234247/Spring2006/ho/
WCFiles/The%20Escape%20Problem.pdf
41. Conclusions
Flow networks are met very often in real life
A lot of applications and interesting problems to be solved:
maximum flow is only one of them
It is important to know the main concepts of flow networks:
positive flow, net flow, cuts, augmenting networks, etc.
Ford-Fulkerson gives us a generic method to compute the
maximum flow in a graph
Edmonds-Karp is a variant of FF that runs in O(V*E2)
There are other algorithms that work better
A lot of problems can be solved using maximum flow, but you
need to be able to “transform” them into a flow problem!