TU/e Algorithms (2IL15) – Lecture 8

TU/e
Algorithms (2IL15) – Lecture 8
Algorithms (2IL15) – Lecture 8
MAXIMUM FLOW (part II)
1
Algorithms (2IL15) – Lecture 8
TU/e
Flow network
 directed graph with source and sink, and capacities on the edges
 if (u,v) in E then we cannot have (v,u) in E
Flow in a network
 must satisfy capacity constraints and “flow in = flow out”
2 /3
4 / 10
2 /2
2 /2
3 /3
s
3 /3
t
1/5
1 /1
2 /5
value = 4+3−1 = 6

1 /3
flow / capacity
value of flow: |f | = ∑v in V f (s,v) − ∑v in V f (v,s)
2
Algorithms (2IL15) – Lecture 8
TU/e
Ford-Fulkerson: increase flow using augmenting paths in residual network
1
0 /3
flow network
2 /2
s
1
0 /2
1
2 /3
2 /2
65 /6
10 /1
t
5 /5
5 /5
0 /3
residual network: contains edges with residual capacity > 0
augmenting path
2
2
s
1
5
2
1
2
1
5
t
5
3

residual capacity of original edge (u,v):
cf (u,v) = c(u,v) – f (u,v)

residual capacity of reverse edge (v,u):
cf (v,u) = f (u,v)
3
Algorithms (2IL15) – Lecture 8
TU/e
Ford-Fulkerson ( G, s, t )
1.
Initialize flow: set f (u,v) = 0 for each pair (u,v) in V x V
2.
Construct residual network Gf
3.
while there is an augmenting path p in the residual network Gf
4.
do // increase flow by augmenting flow along p
5.
cf (p) ← residual capacity of the path p
6.
for each edge (u,v) on the path p
7.
do if (u,v) in E
8.
then f (u,v) ← f (u,v) + cf (p)
9.
else
10.
f (v,u) ← f (v,u) − cf (p)
Update the residual network Gf
11. return f
need algorithm to find path between two given vertices (s and t)
4
Algorithms (2IL15) – Lecture 8
TU/e
Properties of Ford-Fulkerson:

Invariant: flow is valid

Flow increases at each iteration
Questions:

Are we sure we have max flow when algorithm terminates ?
want to prove:
no augmenting path
max flow
to prove this we have to look at cuts

Are we sure it always terminates?
not guaranteed if capacities are irrational and augmenting paths are
chosen in the “wrong” way

How many iterations before termination (no augmenting path) ?
5
Algorithms (2IL15) – Lecture 8
TU/e
Cuts of flow network G = (V,E)
cut (S,T) = partitioning of V into subsets S and T, with s in S and t in T
2 /3
4 / 10
2 /2
2 /2
3 /3
s
2 /2
t
1/5
1 /1
1 /5
0/3
flow f (S,T) across cut (S,T) =
=
∑u in S ∑v in T f(u,v) − ∑u in S ∑v in T f(v,u)
(2+2+3+1) − (1+1)
=
6
capacity c(S,T) of cut (S,T) = max flow across the cut
= ∑u in S ∑v in T c(u,v) = ( 2 + 3 + 3 + 5 ) = 13
6
Algorithms (2IL15) – Lecture 8
TU/e
Lemma: Flow across any cut is the same, and equals the value of the flow.
Max-flow min-cut Theorem: Let f be a flow in a flow network G. Then the
following conditions are equivalent:
(i) f is a maximum flow in G
(ii) residual network Gf contains no augmenting path
(iii) there is a cut (S,T) with |f | = c(S,T)
Consequence: maximum flow = capacity of minimum cut
This implies: if Ford-Fulkerson terminates it has found max flow
7
Algorithms (2IL15) – Lecture 8
TU/e
Properties of Ford-Fulkerson:

Invariant: flow is valid

Flow increases at each iteration
Questions:

Are we sure we have max flow when algorithm terminates ?
want to prove:

no augmenting path
max flow
Are we sure it always terminates?
not guaranteed if capacities are irrational and augmenting paths are
chosen in the “wrong” way

How many iterations before termination (no augmenting path) ?
8
Algorithms (2IL15) – Lecture 8
TU/e
Example where Ford-Fulkerson is not guaranteed to terminate
flow network
1
1
s
1
2
3
t
σ
σ = (√5 -1) / 2 ≈ 0.61…
so σj+2 = σj − σj+1 for all j
4
other edges have capacity C ≥ 2
 max flow = 1 + 2C
initial residual network
1
1
s
1
2
3
t
σ
4
round 0: send flow 1
9
Algorithms (2IL15) – Lecture 8
TU/e
1
σ = (√5 -1) / 2 ≈ 0.61…
1
1
2
s
t
3
σ
4
total flow after zero-th round: 1
residual network after zero-th round
1
other rounds: 4 iterations each
Invariant: after k-th round
1=σ0
s
so σj+2 = σj − σj+1 for all j

total flow = 1 + 2 ∑1≤i ≤ 2k σ i
σ = σ1

res.cap. (2,1) = σ2k
4

res.cap (2,3) = 0

res.cap. (4,3) = σ2k+1
2
3
t
10
Algorithms (2IL15) – Lecture 8
TU/e
1
σ = (√5 -1) / 2 ≈ 0.61…
1
1
2
s
t
3
σ
4
so σj+2 = σj − σj+1 for all j
2k+1
total flow: … + 2σ
σ2k+1
residual capacity of original
edge before iteration
σ2k
s
2
1
0
σ2k+2
3
t
s
2
first iteration: send flow σ2k+1
σ2k+1
3
t
0
σ2k+1
4
1
4
second iteration: send flow σ2k+1
11
Algorithms (2IL15) – Lecture 8
TU/e
1
σ = (√5 -1) / 2 ≈ 0.61…
1
1
2
s
t
3
σ
4
so σj+2 = σj − σj+1 for all j
total flow: … + 2σ2k+1 + σ2k+2
1
s
σ2k+2
0
2
σ2k+2
3
t
s
2
third iteration: send flow σ2k+2
σ2k+1
3
t
0
σ2k+1
4
1
4
second iteration: send flow σ2k+1
12
Algorithms (2IL15) – Lecture 8
TU/e
1
σ = (√5 -1) / 2 ≈ 0.61…
1
1
2
s
t
3
σ
4
so σj+2 = σj − σj+1 for all j
2k+2
total flow: … + 2σ2k+1 + 2σ
σ2k+2
1
1
s
σ2k+2
0
2
0
3
t
s
2
third iteration: send flow σ2k+2
3
t
σ2k+3
σ2k+1
4
σ2k+2
4
fourth iteration: send flow σ2k+2
13
Algorithms (2IL15) – Lecture 8
TU/e
1
σ = (√5 -1) / 2 ≈ 0.61…
1
1
2
s
t
3
σ
4
so σj+2 = σj − σj+1 for all j
total flow: … + 2σ2k+1 + 2σ2k+2
1
1
s
σ2k+2
0
2
0
3
t
s
2
after fourth iteration
3
t
σ2k+3
σ2k+3
4
σ2k+2
4
fourth iteration: send flow σ2k+2
14
Algorithms (2IL15) – Lecture 8
TU/e
1
σ = (√5 -1) / 2 ≈ 0.61…
1
1
2
s
t
3
σ
4
total flow: … + 2σ2k+1 + 2σ2k+2
residual network after (k+1)-st round
1
s
so σj+2 = σj − σj+1 for all j
σ2k+2
0
2
other rounds: 4 iterations each
Invariant: after k-th round
3
t
σ2k+3
4

total flow = 1 + 2 ∑1≤i ≤ 2k σ i

res.cap. (2,1) = σ2k

res.cap (2,3) = 0

res.cap. (4,3) = σ2k+1

total flow converges to 1 + 2(1+ σ) < 1 + 2C

Ford-Fulkerson does not terminate
15
Algorithms (2IL15) – Lecture 8
TU/e
Ford-Fulkerson for integral capacities
 Invariant: flow is always integral
 Flow increases at each iteration
Then
number of iterations ≤ OPT
( = value of max flow )
How much time do we need for one iteration?
 Find s-to-t path in Gf : O(|E|)
Theorem: On flow network G=(V,E) with integral capacities, Ford-Fulkerson
runs in time O( OPT∙ |E| ), where OPT is the value of a maximum flow.
16
Algorithms (2IL15) – Lecture 8
TU/e
 Can’t we get a running time that does not depend on OPT ?
 What about non-integral capacities?
We should not use just any augmenting path, but a specific one:
Edmonds-Karp: always use shortest augmenting path
(one with minimum number of edges)
17
Algorithms (2IL15) – Lecture 8
TU/e
Lemma: The distances δf (s,v) for v ≠ s,t do not decrease when flow is
augmented (and, hence, Gf is modified) in the Edmonds-Karp algorithm.
δf (s,v) = distance from s to v in Gf
Proof. Assume not true.
f = flow before the augmentation
g = flow after augmentation
v = vertex with minimum δg (s,v) whose distance decreases
s
u
v
shortest path in Gg
Claim 1: δf (s,u) ≤ δg (s,u)
Claim 2: (u,v) not in Ef
Proof of Claim 2: otherwise δf (s,v) ≤ δf (s,u) + 1
≤ δg (s,u) + 1
= δg (s,v)
18
Algorithms (2IL15) – Lecture 8
TU/e
Lemma: The distances δf (s,v) for v ≠ s,t do not decrease when flow is
augmented (and, hence, Gf is modified) in the Edmonds-Karp algorithm.
δf (s,v) = distance from s to v in Gf
Proof. Assume not true.
f = flow before the augmentation
g = flow after augmentation
v = vertex with minimum δg (s,v) whose distance decreases
s
u
v
shortest path in Gg
shortest path in Gf
Claim 1: δf (s,u) ≤ δg (s,u)
Claim 2: (u,v) not in Ef
But then augmentation has increased flow along (v,u)
and we get
δf (s,v) = δf (s,u) − 1
≤ δg(s,u) − 1
= δg(s,v) − 2
contradiction
19
Algorithms (2IL15) – Lecture 8
TU/e
Theorem: The number of iterations in Edmonds-Karp is O( |V |∙ |E| ).
Proof.
Definition: (u,v) is critical in Gf if
augmenting path:
s
u
cf (u,v) = cf (p)
v
t
Claim: If (u,v) is critical in Gf and later in Gg then δg(s,u) ≥ δf (s,u) + 2
Proof of Claim:
 if (u,v) is critical in Gf then δf (s,v) = δf (s,u) + 1 and (u,v) disappears
 if (u,v) re-appears when Gh is handled, then (v,u) on augmenting path
and so δh(s,u) = δh(s,v) + 1
 Hence δg(s,u) ≥ δh (s,u) = δh(s,v) + 1 ≥ δf (s,v) + 1 = δf (s,u) + 2
because distances never decrease
20
Algorithms (2IL15) – Lecture 8
TU/e
Theorem: The number of iterations in Edmonds-Karp is O ( |V |∙ |E| ).
Proof.
Definition: (u,v) is critical in Gf if
augmenting path:
s
u
cf (u,v) = cf (p)
v
t
Claim: If (u,v) is critical in Gf and later in Gg then δg(s,u) ≥ δf (s,u) + 2
 min distance = 0
(u,v) can be critical at most |V | / 2 times
 max distance is |V|−2
at most
2 |E| ∙ ( |V |/2 ) = |E| ∙ |V | iterations
21
Algorithms (2IL15) – Lecture 8
TU/e
Theorem: The running time of Edmonds-Karp is O ( |V |∙ |E|2 ).
There are faster algorithms for max flow

push-relabel algorithm with relabel-to-front: O( |V |3 ) (in book)

or O( |V |∙ |E| log ( |V |2 / |E|) ),
or …
22
TU/e
Algorithms (2IL15) – Lecture 8
Applications of Max-Flow: some examples
 “robustness” of s-to-t connectivity
 maximum bipartite matching
 assigning jobs
 …
Many applications are based on the following lemma.
Lemma. Let G=(V,E) be a flow network with integral capacities.
Then there exists a max flow such that the flow along every edge
is integral, and the Ford-Fulkerson method computes such a flow.
23
TU/e
Algorithms (2IL15) – Lecture 8
Applications of Max Flow: example I
“Robustness” of s-to-t paths
24
Algorithms (2IL15) – Lecture 8
TU/e
“Robustness” of s-to-t connectivity


G = (V,E) directed graph
s, t: two nodes in the graph
Question: how many edge-disjoint simple paths are there from s to t ?
s
t
25
Algorithms (2IL15) – Lecture 8
TU/e
How many edge-disjoint simple paths are there from s to t ?
flow = 1
1
1
1
1
1
1
1
s
flow = 0
1
1
1
1
1
1
1
1
t
1
1
1
1.
Turn G into flow network G* by assigning capacity 1 to every edge
(and removing vertices that are not on any s-to-t path).
2.
Compute max flow f in G* using Ford-Fulkerson method.
NB: Ford-Fulkerson method computes integral flow
3.
If only max number of edge-disjoint paths is required, then return |f |
else compute paths themselves using f.
26
Algorithms (2IL15) – Lecture 8
TU/e
Lemma. Max number of edge-disjoint s-to-t paths in G = value of max flow in G*.
Proof.

If we have M edge-disjoint paths, we can send 1 unit of flow along each path:
−
capacity constraint
−
flow in = flow out
Hence,

max number of edge-disjoint paths ≤ value of max flow
For any integral flow f, we can find |f | edge-disjoint simple paths (next slides).
Hence,
max number of edge-disjoint paths ≥ value of max flow
NB Max number of edge-disjoint paths = min number of edges to be destroyed
to disconnect s and t.
(Follows from max flow = min cut)
27
Algorithms (2IL15) – Lecture 8
TU/e
Finding edge-disjoint simple paths from an integral flow f
1. Let G = (V,E ) be the directed graph containing edges with flow =1.
2. x ← |f |
// x = number of disjoint paths we can still find
3. while x > 0
4.
do Find a simple s-to-t path in G.
5.
Remove edges on the path from G.
6.
x←x−1
flow = 1
1
1
1
1
1
1
1
s
flow = 0
1
1
1
1
1
1
1
1
t
1
1
1
Question: do we have flow=0 for every edge after algorithm?
28
TU/e
Algorithms (2IL15) – Lecture 8
“Robustness” of s-to-t connectivity
Question: What if we also want vertex-disjointness (except at s and t) ?
29
TU/e
Algorithms (2IL15) – Lecture 8
Applications of Max Flow: example II
Maximum matching in bipartite graphs
30
Algorithms (2IL15) – Lecture 8
TU/e
G = (V,E) undirected graph
U
Matching: subset M
E such that no two edges have a common vertex
Maximum matching = matching with maximum number of edges
( Maximal matching = matching to which we cannot add another edge
matching can be maximal without being maximum )
31
Algorithms (2IL15) – Lecture 8
TU/e
The maximum-matching problem

compute a maximum matching for a given graph G

we will study the problem for bipartite graphs G = (V,E) where V = L U R
L
R
32
Algorithms (2IL15) – Lecture 8
TU/e
Maximum matching for bipartite graphs
1.
Turn graph G into a flow network G*
2.
Compute max flow in G* with integral flow values
{ (u,v): u in L and v in R
and
f (u,v) = 1 } is maximum matching
1
1
1
1
1
s
1
1
1
1
1
t
1
1
1
1
1
L
R
33
TU/e
Algorithms (2IL15) – Lecture 8
Applications of Max Flow: example III
Assigning jobs to people
34
Algorithms (2IL15) – Lecture 8
TU/e
Assigning jobs to people

J1, … , Jn set of n jobs

P1, … , Pm set of m people

each person Pj has a list Lj of jobs that they are able to do

no person is allowed to do more than, say, 3 jobs
Questions:

can jobs be assigned such that all jobs are done ?

if yes, compute such an assignment

if not, would it help if jobs can be shared?
( condition: if some person does part βi of Ji, then ∑i βi ≤ 3 )
35
Algorithms (2IL15) – Lecture 8
TU/e
Modeling the problem as a “matching” problem
construct bipartite graph G = ( L U R, E )
J1
P1
J2

L = { P1, … , Pm } set of people

R = { J1, … , Jm } set of jobs

E = { (Pj, Ji ) : person Pj can do job Ji }
P2
J3
P3
J4
P4
J5
When can all jobs be done such that no
person does more than three jobs?
 “matching” where each Pi is incident to at
most 3 edges and each Jj to exactly 1 edge
36
Algorithms (2IL15) – Lecture 8
TU/e
Solving the matching problem
1.
Turn graph G into a flow network G*
2.
Compute max flow in G* with integral flow values
all jobs can be done
if and only if
J1
1
P1
1
s
1
1
P2
1
3
3
1
J2
3
3
value of max flow = # jobs
J3
t
1
P3
1
P4
1
1
J4
1
J5
37
Algorithms (2IL15) – Lecture 8
TU/e
Lemma. All jobs can be done
if and only if
value of max flow = # jobs.
Proof.

Suppose all jobs can be done.
// argue there exists flow f with |f | = # jobs, and that f is maximal

Suppose value of max flow = # jobs. Then there is integral f with |f | = # jobs.
If edge (Pj, Ji) has flow 1 in f, then assign Ji to Pj
Each Ji is assigned to exactly one person because
−
out-flow of Ji is 1 (otherwise total flow cannot be equal to # jobs)
−
flow in = flow out
−
flow is integral
Each person Pj is assigned at most three jobs, because
−
incoming flow is at most 3
−
flow is integral
38
Algorithms (2IL15) – Lecture 8
TU/e
Assigning jobs to people

J1, … , Jn set of n jobs

P1, … , Pm set of m people

each person Pj has a list Lj of jobs that they are able to do

no person is allowed to do more than, say, 3 jobs
Questions:

can jobs be assigned such that all jobs are done ?
check using max flow

if yes, compute such an assignment
assign Ji to Pj if flow between them

if not, would it help if jobs can be shared?
no: there is always an integral max flow
39
Algorithms (2IL15) – Lecture 8
TU/e
Max-Flow Summary
Flow network
 directed graph with source and sink, and capacities on the edges
 if (u,v) in E then we cannot have (v,u) in E
Flow in a network
 must satisfy capacity constraints and “flow in = flow out”
Ford-Fulkerson method
 iteratively increase flow using augmenting paths in residual graph
 Edmonds-Karp variant: use shortest augment path
 number of iterations O( |V| ∙ |E| )  running time O( |V| ∙ |E|2 )
Relation between flows and cuts
 for any flow f, the net flow across any cut is the same and equals |f |
 Max flow = min cut
Applications of max flow:
 often use that if all capacities are integral then there exists a max flow that is
integral everywhere (and Ford-Fulkerson method computes such a flow)
40