Maximum Flow

Lecture15-Dinic’s Algorithm
1. The Dinic’s Algorithm for Maximum
Flow Problem
 2004 SDU
Dinic’s
vs
Ford-Fulkerson’s
Dinic’s algorithm is an implementation of the
Ford-Fulkerson’s method
Dinic’s algorithm needs O(V2E) time
 2004 SDU
2
Idea of Dinic’s Algorithm (1970)
1. Start with an initial flow, e.g., the zero-flow
2. Repeat the following procedure until no more flow can be
added
1.
2.
3.
Create a layered network based on the current flow
Compute a maximal flow of the layered network
Update the current flow based on the maximal flow
Call (u, v) saturated from u to v if f(u, v) = c(u, v).
Otherwise, it is called unsaturated from u to v.
 2004 SDU
3
The Layered network
The layers of the layered network can be obtained
from the following O(E) layer-algorithm according to
current flow.
 Step 1: V0 {s}, i0.
 Step 2: T  {v | v  Vj for j  i there is some vertex u in Vi
such that (u, v) is unsaturated from u to v}
 Step 3: If T = , then Stop (the current flow is a maximum
flow).
 Step 4: If t  T, then L i + 1, VL  {t}, Stop.
 Step 5: i  i +1, Vi  T, goto Step 2.
 2004 SDU
4
Layers
18:17
a
b
18:17
19:16
5:1
8:0
s
4:0
20:13
18:14
13:12
c
V0
s
 2004 SDU
d
V1
V2
a
b
c
t
8:2
3: 2
V3
t
d
5
A Key Lemma
If the layer-algorithm stops at Step 3, then the current flow in
network G is a maximum flow.
 Proof: Suppose V0, V1, …, Vi are the layers formed when the algorithm
terminates at Step 3. Let S = V0V1…Vi, then
– each pair (u, v) (S, SC) is saturated from u to v, i.e, f(u, v) = c(u, v)
– Thus, F = F(S, SC)=C(S,SC), indicating that f is a maximum flow.
Note that if stops at Step4, then there must be some augmenting
paths, why?
 2004 SDU
6
The Layered network
Each Vi (0  i  L) formed in the layer-algorithm is called a layer.
Each Ei (1  i  L)={(u,v)| (u,v) (Vi-1, Vi) and is unsaturated
from u to v}
Gf = (V0V1…VL, E1E2… Ef, Cf, s, t) is called the layered
network of the network G according to a flow f on G, where the
capacity function Cf of Gf is defined:
 For each edge (u, v)  Ei (1  i  L), cf (u, v) = c(u, v) – f(u, v).
 2004 SDU
7
Layered network
18:17
b
a
18:17
19:16
5:1
8:0
s
4:0
t
8:2
3: 2
20:13
18:14
13:12
c
V1
V0
3
s
 2004 SDU
d
4
a
c
V2
1
1
1
1
b
d
V3
1
7
t
8
Computes the Gf’s Maximal Flow
A maximal flow ff of Gf is a flow which satisfies
that, for each path v0v1v2…vL where vi Vi, v0 =
s,and vL = t, there is at least one edge e = (vi-1, vi)
such that ff(e) = cf(e).
A maximal flow may not be a maximum flow.
1:0
1:1
1:0
1:1
t
s
1:0
 2004 SDU
1:0
1:1
9
Computes the Gf’s Maximal Flow
in O(VE) Time
Step 1: Mark each edge e in Gf as ‘unblocked’ and set ff(e) 0.
Step 2: v s, set Stack S
Step 3: If there is no edge (v, w) in Gf that is marked ‘unblocked’
Step 3.1: If s = v, then Stop. (current flow is maximal)
Step 3.2: Let e = (u, v) = Pop(S)
Step 3.3: Mark e ‘blocked’ and set v u
Step 3.4: Goto Step 3.
Step 4: Select an edge e = (v, w) that is marked ‘unblocked’ and w is in the next layer,
Push(S, e) and set v w, if v  t, then goto Step 3. If v = t, go to Step5.
Step 5: The edges in Stack S form an augmenting path e1e2…eL from s to t
Step 5.1: Let  = min1iL{Cf(ei)-ff(ei)}
Step 5.2: For each i (1iL), set ff(ei)ff(ei) + , and if ff(ei) = Cf(ei), mark ei
‘blocked’.
Step 5.3: Goto Step 2.
 2004 SDU
10
V1
V0
3:0
s
4:0
V0
3:1
s
4:0
V0
3:2
s
4:0
 2004 SDU
V2
V3
1:0
b
1:0 1:0
a
c
1:0
d
V1
V2
a
1:1
b
1:0 1:0
c
1:0
1:0
7:0
1:1
7:1
d
V2
a
1:1
b
1:1 1:0
1:0
t
V3
V1
c
S(s,a)(a,b)(b,t)
Augment path sabt
Block (a,b)(b,t)
d
S(s,a)(a,d)(d,t)
Augment path sadt
Block (a,d)
t
V3
1:1
7:1
t
S(s,a), both (a,b),
(a,d) are blocked,
Pop(S,(s,a))
Block (s,a),
11
3:2
s
4:0
3:2
s
4:1
3:2
s
4:1
 2004 SDU
a
c
1:1
b
1:1 1:0
1:0
a
c
a
c
d
1:1
b
1:1 1:0
1:1
d
1:1
b
1:1 1:0
1:1
d
1:1
7:1
1:1
7:2
1:1
7:2
S(s,c)(c,b)
(b,t) is blocked
Pop(S,(c,b))
Block (c,b)
S(s,c)(c,d)(d,t)
Augment scdt
Block (c,d)
t
t
t
S(s,c)
Both (c,b)(c,d) are
Blocked
Pop(S,(s,c))
Block(s,c)
There are no unblocked
edges leaving s
Stop!
12
Compute the maximal flow
Why O(VE)?
– Each edge changes at most once from being unblocked to
blocked.
– Totally at most E such changes.
– Each time browsing  V edges (a tree rooted at the first node
for this browsing ), one such change takes place.
– So, browsing VE edges, E such changes will take place.
Why is the final flow maximal?
– All paths from s to t through each blocked edge have at least
one saturated edge. (By induction on order of the edge
becoming blocked.)
– In the end, all edges leaving s are blocked, so, …
 2004 SDU
13
The Dinic’s Algorithm
Step 1: Initialize the flow as zero flow f.
Step 2: Compute the layered network Gf according to
current flow f. If some T = , stop.
Step 3: Compute the maximal flow ff of Gf.
Step 4: Update the flow f:
for each (u,v ) Ef, f(u,v) = f(u,v)+ff(u,v)
f(v,u) = -f(u,v)
Step 5: Goto Step 2.
 2004 SDU
14
An Example
18:0
a
b
16:0
19:0
5:0
8:0
s
4:0
3:0
18:0
20:0
13:0
c
18:16
a
t
8:0
d
b
19:16
16:16
t
s
3:0
18:0
c
 2004 SDU
20:0
13:0
d
15
18:16
a
b
19:16
16:16
t
s
3:0
18:13
20:13
13:13
c
18:16
a
d
b
19:16
16:16
t
s
3:0
18:13
20:13
13:13
c
18:16
a
5:0
8:0
4:0
c
t
8:0
3:0
18:13
 2004 SDU
b
16:16
19:16
s
d
20: 13
13:13
d
16
2:0
a
b
3:0
t
s
8:0
3:0
5:0
5: 0
c
d
2:2
a
b
3:2
t
s
8:2
3:0
5:0
5: 2
c
d
2:2
a
b
3:2
t
s
3:3
5:3
c
 2004 SDU
8:5
5:5
d
17
18:18
a
16:16
19:18
5:0
8:0
s
b
4:0
3:3
18:16
20: 18
13:13
c
t
8:5
d
a
1
T=, stop
s
2
 2004 SDU
c
18
An example
a
1000
1000
t
1
s
1000
1000
b
 2004 SDU
19
Time Complexity
Initialization O(E)
Compute the layered subgraph O(E)
Compute the maximal flow of the layered
network O(VE)
Updating O(E)
Loops O(V), Why?
Total: O(V2E)!
 2004 SDU
20
Time Complexity
Why O(V) loops?
 Call Steps 2-4 a phase.
 Lemma: If the k+1st phase is not the last phase, then Lk+1 > Lk.
Proof:
– There is a path P  sv ' v '...v
, in the (k+1)’ st phase
1 2
L k 1 1 ' t
– If all vertices in P are in the k’st phase i.e., each va’ Vb, then by
induction proof, a  b, hence, Lk+1  Lk. Since Lk+1  Lk,, Lk+1>Lk
– If there is one va+1’is not in k st phase (so not t), suppose it is the first
one. Then va’ Vb, and a  b. va+1’ is not in Vb+1 implies that b+1=Lk. So,
Lk+1> a+1 b+1=Lk.
 The lemma implies number of phases is at most |V|.
 2004 SDU
21
K+1--phase
s
v1’
va’
v2’ …
va+1’ …
t
K--phase
s
v1’
v2 ’
v3’
…
va+1’ …
t
If all the vi’ appear in the k-phase, then either
each vi appears in former layers (in this case, (vi-1,vi)
may become unsaturated in phase k), or (vi-1, vi)
must be unsaturated also in phase k, then it must appear in Vi.
 2004 SDU
22
K+1--phase
s
v1’
v2’ …
va’
va+1’ …
…
va’
va+1’
t
t
K--phase
s
v1’
v2’
If not all the vi’ appear in the k-phase, then either
vi appears in former layers (in this case, (vi-1,vi)
may become unsaturated in phase k), or (vi-1, vi)
must be unsaturated also in phase k, then it must appear in Vi.
But why va+1’ does not appear in phase k? The reason is that the
layer next to the one in which va’ appears contains t.
 2004 SDU
23