Lectures on Network Flows COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski Lectures on Network Flows 1 Overview Previous lectures: • Dynamic programming – Weighted interval scheduling – Sequence alignment These lectures: • Network flows • Applications: largest matching in bipartite graphs Lectures on Network Flows 2 Network A directed graph G = (V,E) such that • each directed edge e has its nonnegative capacity denoted by ce • there is a node s (source) with no incoming edges • there is a node t (target) with no outgoing u edges 20 u,v - internal nodes 10 30 s 10 t 20 v Lectures on Network Flows 3 Flow s-t flow in G = (V,E) is a function f from E to R+ • capacity condition: for each e, 0 f(e) ce • conservation condition: for each internal node v, ∑e in v f(e) = ∑e out v f(e) • there is a node t (target) with no outgoing edges Property: ∑e in t f(e) = ∑e out s f(e) Network: u 20 u 10 30 s 10 t 20 v Flow: 20 10 s Lectures on Network Flows 10 10 t 20 v 5 Useful definitions Given s-t flow f in G = (V,E) and any subset of nodes S • f in(S) = ∑e in S f(e) • f out(S) = ∑e out S f(e) Property: f in(t) = f out(s) Example: f in(u,v) = f out(u,v) = 30 Network: u 20 u 10 30 s 10 t 20 v Flow: 20 10 s Lectures on Network Flows 10 10 t 20 v 7 Problem(s) • • What is the maximum value of f in(t) (flow) for a given graph G = (V,E) ? How to compute it efficiently? Assumption: capacities are positive integers. Example: f in(t) = f out(s) = 30 Network: u 20 u 30 s Flow: 20 10 10 t 20 v 10 s Lectures on Network Flows 10 10 t 20 v 8 Residual graph Assume that we are given a flow f in graph G. Residual graph Gf • The same nodes, internal and s,t • For each edge e in E with ce > f(e) we put weight ce - f(e) (residual capacity) • For each edge e = (u,v) in E we put weight f(e) to the backward edge (v,u) (residual capacity) u Network: u 20 10 30 s 10 t 20 v Residual u Graph: 20 10 20 t s 20 10 t s 0 20 20 10 v Lectures on Network Flows 9 v Flow: 20 0 Augmenting path & augmentation Assume that we are given a flow f in graph G, and the corresponding residual graph Gf 1. Find a new flow in residual graph - through a path with no repeating nodes, and value equal to the minimum capacity on the path (augmenting path) 2. Update residual graph along the path u Network: u 20 10 30 s 10 20 v New 20 10 flow: 1020 10 s t 10 20 v Lectures on Network Flows New residual u graph: 10 20 t 10 20 s 10 20 10 v t Ford-Fulkerson Algorithm • • Initialize f(e) = 0 for all e While there is s-t path P in residual graph – Augment f through path P and get new f and new residual graph Augment f through path P : • Find minimum capacity on the path • Go through the path and modify weights Network: u 20 10 30 s 10 20 v New u u New 10 residual 20 10 flow: 20 graph: t 1020 10 10 20 t s s 10 10 20 20 v v 12 Lectures on Network Flows t Analysis Correctness: maximum flow - proof later termination - each time the flow is an integer and advances by at least 1 (assumption about integer capacities) Time: O(mC) • at most C iterations, where C is the value of the maximum flow, m is the number of edges • each iteration takes O(m+n) steps - use DFS to find path P Network: u 20 10 30 s 10 t 20 v New u 10 residual 20 10 graph: 1020 10 10 20 t s s 10 10 20 20 v v New flow: 20 u Lectures on Network Flows 13 t Reminder: Depth-First Search (DFS) Given a directed graph G = (V,E) of diameter D and the root node Goal: find a directed rooted spanning tree such that each edge in graph G corresponds to the ancestor relation in the tree Recursive idea of the algorithm: Repeat until no neighbor of the root is free • Select a free out-neighbor v of the root and add the edge from root to v to partial DFS • Recursively find a DFS’ in graph G restricted to free nodes and node v as the root’ and add it to partial DFS root’ root Lectures on Network Flows root’’ 14 Implementing DFS Structures: – – – Adjacency list List (stack) S Array Discovered[1…n] Algorithm: • Set S = {root} • Consider the top element v in S – – For each out-neighbor w of node v, if w is not Discovered then put w into the stack S and start the next iteration for w as the top element Otherwise remove v from the stack, add edge (z,v) to partial DFS, where z is the current top element, and start the next iteration for z as the top element Remark: after considering the out-neighbor of node v we remove this neighbor from adjacency list to avoid considering it many times! root’ root Lectures on Network Flows root’’ 15 Flows vs. Cuts (A,B) - cut in graph G: • A,B is a partition of nodes, s in A, t in B c(A,B) = ∑e out A c(e) = ∑e in B c(e) is the capacity of this cut Property: Minimum cut is equal to the maximum flow Example: c(A,B) = 50 u 20 10 30 s 10 20 t 10 30 s 20 v u A 10 Lectures on Network Flows B t 20 v 16 Max Flow vs. Min Cut For any set A containing s we proceed in 3 steps: • value(f) = ∑v in A ∑e out v f(e) - ∑v in A ∑e in v f(e) • value(f) = f out(A) - f in(A) • c(A,B) f out(A) - f in(A) = value(f) Conclusion: Min-cut value(f) Example: c(A,B) = 50 and f out(A) = 30 u 20 10 30 s 10 20 t 20 v u A 10/10 30/10 s 10/10 20 Lectures on Network Flows v B t 17 FF-algorithm gives Max-flow Suppose FF-algorithm stopped with flow f : • Directed DFS tree rooted in s does not contain t • It means that cut-capacity between nodes in DFS and the remaining ones is 0 in residual graph • It follows that each edge in this cut has been reversed by augmenting flow, which means that c(DFS,DFS’) = value(f) • Using Min-cut value(f) we get that f is maximum u 20 u 10 30 s 10 20 v 20 10 Residual graph: t 10 20 t s DFS 10 2018 Lectures on Network Flows Min-cut v Conclusions Network flow algorithms: – Ford-Fulkerson algorithm in time O(mC) – Correspondence between max-flows and min-cuts Lectures on Network Flows 19 Exercises READING: • Chapter 7, Sections 7.1, 7.2, and 7.3 EXERCISES: • Modify FF-algorithm to work in time O(m2 log C) • Find an augmentation scheme to guarantee time O(mn) in FF-algorithm independently from integer C Lectures on Network Flows 20 Overview Previous lecture: • Network flows • Ford-Fulkerson algorithm • Max-flows versus Min-cuts This lecture: • Rational and real capacities in network • Applications: largest matching in bipartite graphs • Application to disjoint paths problem Lectures on Network Flows 21 Network Given a directed graph G = (V,E) such that • each directed edge e has its nonnegative capacity denoted by ce • there is a node s (source) with no incoming edges • there is a node t (target) with no outgoing u edges 20 u,v - internal nodes 10 30 s 10 t 20 v Lectures on Network Flows 22 Flow s-t flow in G = (V,E) is a function f from E to R+ • capacity condition: for each e, 0 f(e) ce • conservation condition: for each internal node v, ∑e in v f(e) = ∑e out v f(e) • there is a node t (target) with no outgoing edges Property: ∑e in t f(e) = ∑e out s f(e) Network: u 20 u 10 30 s 10 t 20 v Flow: 20 10 s Lectures on Network Flows 10 10 t 20 v 23 Problem What is a maximum value f in(t) = f out(s) (flow) for a given graph G = (V,E) ? How to compute it efficiently? Assumption: capacities are positive integers. Example: f in(t) = f out(s) = 30 Network: u 20 u 30 s Flow: 20 10 10 t 20 v 10 s Lectures on Network Flows 10 10 t 20 v 24 Ford-Fulkerson Algorithm • • Initialize f(e) = 0 for all e While there is s-t path P in residual graph – Augment f through path P and get new f and new residual graph Augment f through path P : • Find minimum capacity on the path • Go through the path and modify weights Network: u 20 10 30 s 10 20 v New u u New 10 residual 20 10 flow: 20 graph: t 1020 10 10 20 t s s 10 10 20 20 v v 25 Lectures on Network Flows t Non-integer capacities • Rational capacities - rescale them to integers by multiplying by the common multiply Real capacities - difficult to handle: • – – Min-cut = Max-flow FF-algorithm may work very slowly Graph: u 10 20 30 s 10 20 v New u u New 10 residual 20 10 flow: 20 graph: t t 10 20 10 10 20 t s s 10 10 20 20 v v Lectures on Network Flows 26 Flows vs. Cuts (A,B) - cut in graph G: • A,B is a partition of nodes, s in A, t in B c(A,B) = ∑e out A c(e) = ∑e in B c(e) is a capacity of the cut Property: Minimum cut is equal to the maximum flow Example: c(A,B) = 50 u 20 10 30 s 10 20 t 10 30 s 20 v u A 10 Lectures on Network Flows B t 20 v 27 Applications - largest matching Input: bipartite graph G=(V,W,E) Goal: largest set of non-incident edges (with different ends) Solution using flow algorithms: • Lets direct edges from V to W, introduce s connected to all nodes in V, t connected from all nodes in W • Capacities are 1 for all edges • Max-flow is the largest matching s Lectures on Network Flows t 28 Applications - disjoint paths Input: network graph G=(V,E) Goal: largest set of edge-disjoint paths from s to t Solution: Using flow algorithms, where each edge has capacity 1 t s Lectures on Network Flows 30 Disjoint paths cont. Suppose that k is the largest number of edge-disjoint paths from s to t. • It is also a flow: – – capacity condition is clear since we push flow with value 1 through each path, and conservation is satisfied since if a path comes into a node it also goes out Conclusion: the largest number of edge-disjoint paths from s to t is not bigger than Max-flow t s Lectures on Network Flows 31 Disjoint paths cont. Suppose that x is the value of Max-flow produced by FF-algorithm. How to construct x edge-disjoint paths from s to t ? By induction on the number of edges in the flow. For 0 edges trivial (nothing to do, no even a path) Assume j edges in the flow. Take one of them (s,v) and continue going using edges in the flow: • We go to t - done, since we have path, remove it from the graph and continue by induction • We make a cycle - reduce the flow along the cycle to zero, and the obtained is a flow having the same value but smaller number of edges - next continue by induction s t Lectures on Network Flows 32 Complexity Time of FF-algorithm: O(mn) ( since C = O(n) ) Time of extracting paths: each edge is considered once while extracting one path, hence total time O(mn) Total time: O(mn) Additional memory: O(m+n) for keeping paths t s Lectures on Network Flows 33 Conclusions Network flow algorithms: – Rational capacities are easy to deal with – Real capacities are difficult to compute although we can alternatively check Min-cut – Application to the largest matching problem in time O(mn) (n = C since capacities are 1) – Application to the edge-disjoint paths problem in time O(mn) (n = C since capacities are 1) Lectures on Network Flows 34 Textbook and Exercises READING: • Chapter 7, Sections 7.5, 7.6 and 7.7 EXERCISES: • Find a network with real capacities for which FFalgorithm works very slowly • Prove formally that FF-algorithm gives the largest matching in the last application • Design the algorithm for finding the largest number of edge-disjoint paths from s to t in undirected network • Design the algorithm for finding the largest number of node-disjoint paths from s to t in both directed and undirected networks Lectures on Network Flows 35