400 likes | 421 Views
Algorithms (2IL15) – Lecture 8 MAXIMUM FLOW (part II). 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”
E N D
Algorithms (2IL15) – Lecture 8 MAXIMUM FLOW (part II)
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” • value of flow: |f | = ∑v in Vf (s,v) − ∑v in Vf (v,s) 2 / 3 2 / 2 4 / 10 2 / 2 3 / 3 t s 3 / 3 1 / 1 5 1 / 2 / 5 1 / 3 value = 4+3−1 = 6 flow / capacity
augmenting path 2 2 1 0 / 3 2 2 s 0 / 2 t 2 / 2 2 / 3 1 1 2 / 2 5 s t 5 5 5 / 6 1 0 / 5 / 5 3 6 5 / 5 1 1 1 1 0 / 3 flow network Ford-Fulkerson: increase flow using augmenting paths in residual network residual network: contains edges with residual capacity > 0 • 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)
Ford-Fulkerson ( G, s, t ) • Initialize flow: set f (u,v) = 0 for each pair (u,v) in V x V • Construct residual network Gf • while there is an augmenting path p in the residual network Gf • do // increase flow by augmenting flow along p • cf (p) ← residual capacity of the path p • for each edge (u,v) on the path p • do if (u,v) in E • thenf (u,v) ← f (u,v) + cf (p) • else f (v,u) ← f (v,u) − cf(p) • Update the residual network Gf • returnf need algorithm to find path between two given vertices (s and t)
Properties of Ford-Fulkerson: • Invariant: flowis 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) ?
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 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 Tc(u,v) = ( 2 + 3 + 3 + 5 ) = 13 2 / 3 2 / 2 4 / 10 2 / 2 3 / 3 t s 2 / 2 1 / 1 5 1 / 1 / 5 0 / 3
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 Gfcontains 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
Properties of Ford-Fulkerson: • Invariant: flowis 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) ?
3 3 s s t t 1 1 4 4 2 2 1 1 σ round 0: send flow 1 flow network 1 σ = (√5 -1) / 2 ≈ 0.61… so σj+2 =σj − σj+1 for all j 1 Example where Ford-Fulkerson is not guaranteed to terminate σ other edges have capacity C ≥ 2 max flow = 1 + 2C initial residual network
1 1 3 3 σ s s t t 1 1 4 4 2 2 σ = (√5 -1) / 2 ≈ 0.61… so σj+2 =σj − σj+1 for all j total flow after zero-th round: 1 residual network after zero-th round other rounds: 4 iterations each Invariant: after k-th round • total flow = 1 + 2 ∑1≤i ≤ 2kσi • res.cap. (2,1) =σ2k • res.cap (2,3) = 0 • res.cap. (4,3) =σ2k+1 1=σ0 σ = σ1
1 1 3 3 3 σ s s s t t t 1 1 1 4 4 4 2 2 2 σ2k+2 σ2k+1 0 first iteration: send flow σ2k+1 second iteration: send flow σ2k+1 σ = (√5 -1) / 2 ≈ 0.61… so σj+2 =σj − σj+1 for all j total flow: … + σ2k+1 total flow: … + 2σ2k+1 residual capacity of original edge before iteration σ2k 0 σ2k+1
1 1 3 3 3 σ s s s t t t 1 1 1 4 4 4 2 2 2 σ2k+2 σ2k+1 σ2k+2 0 0 σ2k+1 third iteration: send flow σ2k+2 σ = (√5 -1) / 2 ≈ 0.61… so σj+2 =σj − σj+1 for all j total flow: … + 2σ2k+1 + σ2k+2 total flow: … + 2σ2k+1 second iteration: send flow σ2k+1
1 1 3 3 3 σ s s s t t t 1 1 1 4 4 4 2 2 2 0 σ2k+2 σ2k+2 0 σ2k+3 σ2k+1 fourth iteration: send flow σ2k+2 third iteration: send flow σ2k+2 σ = (√5 -1) / 2 ≈ 0.61… so σj+2 =σj − σj+1 for all j total flow: … + 2σ2k+1 + σ2k+2 total flow: … + 2σ2k+1 + 2σ2k+2
1 1 3 3 3 σ s s s t t t 1 1 1 4 4 4 2 2 2 σ2k+2 0 σ2k+3 fourth iteration: send flow σ2k+2 σ = (√5 -1) / 2 ≈ 0.61… so σj+2 =σj − σj+1 for all j total flow: … + 2σ2k+1 + 2σ2k+2 0 σ2k+2 σ2k+3 after fourth iteration
1 1 3 3 σ s s t t 1 1 4 4 2 2 σ2k+2 0 σ2k+3 • total flow converges to 1 + 2(1+ σ) < 1 + 2C • Ford-Fulkerson does not terminate σ = (√5 -1) / 2 ≈ 0.61… so σj+2 =σj − σj+1 for all j total flow: … + 2σ2k+1 + 2σ2k+2 residual network after (k+1)-st round other rounds: 4 iterations each Invariant: after k-th round • total flow = 1 + 2 ∑1≤i ≤ 2kσi • res.cap. (2,1) =σ2k • res.cap (2,3) = 0 • res.cap. (4,3) =σ2k+1
Ford-Fulkerson for integral capacities • Invariant: flowis 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.
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)
shortest path in Gg s u v 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. Proof. Assume not true. f = flow before the augmentation g = flow after augmentation v = vertex with minimum δg(s,v) whose distance decreases 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) δf(s,v) =distance from s to v in Gf
shortest path in Gf 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. Proof. Assume not true. f = flow before the augmentation g = flow after augmentation v = vertex with minimum δg(s,v) whose distance decreases 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 δf(s,v) =distance from s to v in Gf shortest path in Gg s u v contradiction
because distances never decrease Theorem:The number of iterations in Edmonds-Karp is O( |V |∙ |E| ). Proof. Definition: (u,v) is criticalin Gf if Claim: If (u,v) is critical in Gf and later in Ggthenδ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 Ghis 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 cf(u,v) = cf (p) t augmenting path: s u v
Theorem:The number of iterations in Edmonds-Karp is O ( |V |∙ |E| ). Proof. Definition: (u,v) is criticalin Gf if Claim: If (u,v) is critical in Gf and later in Ggthenδg(s,u) ≥ δf (s,u) + 2 • min distance = 0 • max distance is |V|−2 cf(u,v) = cf (p) t augmenting path: s u v (u,v) can be critical at most |V | / 2 times at most 2 |E| ∙ ( |V |/2 ) = |E| ∙ |V | iterations
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 …
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.
Applications of Max Flow: example I “Robustness” of s-to-t paths
“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
flow = 1 flow = 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 How many edge-disjoint simple paths are there from s to t ? s t • 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). • Compute max flow f in G* using Ford-Fulkerson method. NB: Ford-Fulkerson method computes integral flow • If only max number of edge-disjoint paths is required, then return |f | else compute paths themselves using f.
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)
flow = 1 flow = 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Finding edge-disjoint simple paths from an integral flow f • Let G = (V,E) be the directed graph containing edges with flow =1. • x ← |f | // x = number of disjoint paths we can still find • while x > 0 • do Find a simple s-to-t path in G. • Remove edges on the path from G. • x ← x − 1 s t Question: do we have flow=0 for every edge after algorithm?
“Robustness” of s-to-t connectivity Question: What if we also want vertex-disjointness (except at s and t)?
Applications of Max Flow: example II Maximum matching in bipartite graphs
G = (V,E) undirected graph Matching: subset ME such that no two edges have a common vertex U 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 )
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 = LUR L R
s t Maximum matching for bipartite graphs • Turn graph G into a flow network G* • 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 1 1 1 1 1 1 1 1 1 1 L R
Applications of Max Flow: example III Assigning jobs to people
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 )
Modeling the problem as a “matching” problem construct bipartite graph G = ( L U R, E ) J1 • L = { P1, … , Pm } set of people • R = { J1, … , Jm } set of jobs • E = { (Pj, Ji ) : person Pj can do job Ji } P1 J2 P2 J3 When can all jobs be done such that no person does more than three jobs? • “matching” where each Piis incident to at most 3 edges and each Jj to exactly 1 edge P3 J4 P4 J5
s t Solving the matching problem • Turn graph G into a flow network G* • Compute max flow in G* with integral flow values all jobs can be done if and only if value of max flow = # jobs J1 1 P1 1 1 J2 3 1 1 P2 3 1 J3 1 3 1 P3 3 1 J4 1 1 P4 J5
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 integralf 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 Jiis 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
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
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)