390 likes | 681 Views
Lecture15-Dinic ’ s Algorithm. The Dinic ’ s Algorithm for Maximum Flow Problem. Dinic’s vs Ford-Fulkerson ’s. Dinic’s algorithm is an implementation of the Ford-Fulkerson’s method Dinic’s algorithm needs O( V 2 E ) time. Idea of Dinic’s Algorithm (1970).
E N D
Lecture15-Dinic’s Algorithm The Dinic’s Algorithm for Maximum Flow Problem
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
Idea of Dinic’s Algorithm (1970) • Start with an initial flow, e.g., the zero-flow • Repeat the following procedure until no more flow can be added • 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 vif f(u, v) = c(u, v). Otherwise, it is called unsaturated from u to v.
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}, i0. • 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.
V1 V2 V3 V0 s b d t a c Layers 18:17 b a 18:17 19:16 5:1 t 8:2 8:0 4:0 s 3: 2 20:13 18:14 13:12 c d
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 = V0V1…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?
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 = (V0V1…VL,E1E2… 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).
V1 V2 V3 V0 s b d t 1 a c 3 1 1 1 4 7 1 Layered network 18:17 b a 18:17 19:16 5:1 t 8:2 8:0 4:0 s 3: 2 20:13 18:14 13:12 c d
Computes the Gf’s Maximal Flow • A maximal flow ff of Gf is a flow which satisfies that, for each path v0v1v2…vL where viVi, 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 1:1 1:0
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 vu 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 = min1iL{Cf(ei)-ff(ei)} Step 5.2: For each i (1iL), set ff(ei)ff(ei) + , and if ff(ei) = Cf(ei), mark ei ‘blocked’. Step 5.3: Goto Step 2.
V1 V2 V3 V0 S(s,a)(a,b)(b,t) Augment path sabt Block (a,b)(b,t) b d t s 1:0 a c 3:0 1:0 1:0 1:0 4:0 7:0 1:0 V1 V2 V3 V0 b d t S(s,a)(a,d)(d,t) Augment path sadt Block (a,d) s 1:1 a c 3:1 1:0 1:1 1:0 4:0 7:1 1:0 V1 V2 V3 V0 S(s,a), both (a,b), (a,d) are blocked, Pop(S,(s,a)) Block (s,a), b d t s 1:1 a c 3:2 1:1 1:1 1:0 4:0 7:1 1:0
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) b d t s 1:1 a c 3:2 1:1 1:1 1:0 4:0 7:1 1:0 S(s,c) Both (c,b)(c,d) are Blocked Pop(S,(s,c)) Block(s,c) b d t s 1:1 a c 3:2 1:1 1:1 1:0 4:1 7:2 1:1 b d t s 1:1 There are no unblocked edges leaving s Stop! a c 3:2 1:1 1:1 1:0 4:1 7:2 1:1
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, …
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.
An Example 18:0 b a 16:0 19:0 5:0 8:0 t 4:0 s 8:0 3:0 20:0 18:0 13:0 c d 18:16 b a 19:16 16:16 t s 3:0 20:0 18:0 13:0 c d
18:16 b a 19:16 16:16 t s 3:0 20:13 18:13 13:13 c d 18:16 b a 19:16 16:16 t s 3:0 20:13 18:13 13:13 c d 18:16 b a 16:16 19:16 5:0 8:0 t 4:0 s 8:0 3:0 20: 13 18:13 13:13 c d
2:0 b a 3:0 t s 8:0 3:0 5: 0 5:0 c d 2:2 b a 3:2 t s 8:2 3:0 5: 2 5:0 c d 2:2 b a 3:2 t s 8:5 3:3 5:5 5:3 c d
18:18 b a 16:16 19:18 5:0 8:0 t 4:0 s 8:5 3:3 20: 18 18:16 13:13 c d a 1 T=, stop s 2 c
An example a 1000 1000 t 1 s 1000 1000 b
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)!
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 , in the (k+1)’ st phase • 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|.
K+1--phase s v1’ v2’ va’ 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.
K+1--phase s v1’ v2’ va’ va+1’ t … … K--phase s v1’ v2’ va’ va+1’ t … 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.