cpsc 490 problem solving in computer science
play

CPSC 490: Problem Solving in Computer Science Assignment 2 is due - PowerPoint PPT Presentation

Lecture 5: Network flow, Maximum flow algorithms, Max-flow min-cut Henry Xia, Brandon Zhang based on CPSC 490 slides from 2014-2018 2019-01-17 University of British Columbia CPSC 490: Problem Solving in Computer Science Assignment 2 is due


  1. Lecture 5: Network flow, Maximum flow algorithms, Max-flow min-cut Henry Xia, Brandon Zhang based on CPSC 490 slides from 2014-2018 2019-01-17 University of British Columbia CPSC 490: Problem Solving in Computer Science

  2. • Assignment 2 is due Tuesday at noon. • Solutions to Assignment 1 have been posted on the course website. 1 Announcements

  3. • Each railroad has a cost to destroy. • You are given a map of the railroads in Europe. • What’s the cheapest way of blowing up railroads to disconnect the Soviet Union? 2 Cold War

  4. 3 Max flow Input : • A directed graph G = ( V , E ) • A source s ∈ V and sink t ∈ V • A capacity c e for each edge e ∈ E

  5. 4 exiting v Max flow A flow is an assignment of values f e to each edge e satisfying: • Capacity constraints: f e ≤ c e for all e ∈ E • Conservation of flow: For all v ∈ V except s and t , total flow entering v = total flow Output : A valid flow maximizing the total flow leaving s ( = total flow entering t ).

  6. 5 subject to maximize f q Max flow Formally: given variables f e ( e ∈ E ), we want to | f | = total flow leaving s  f e ≤ c e for all e ∈ E ;     f e ≥ 0 for all e ∈ E ; ∑ ∑ f p = for all v ∈ V , v / ∈ { s , t } .     p = u → v q = v → w

  7. 6 The blue lines represent flow paths. Max flow

  8. 7 Max flow We’ll label edges by x / y , indicating x units of flow on an edge with capacity y .

  9. Let’s try the following greedy algorithm: 1 2 3 8 Max flow – Solution attempt while there is an augmenting path: pick any augmenting path P increase the flow on edges of P An augmenting path is an s - t path for which all its edges have room for more flow.

  10. 9 Greedy fails! A greedy in our example network could produce a flow of 3. Does greedy work?

  11. But, we can fix it... Greedy fails! A greedy in our example network could produce a flow of 3. 9 Does greedy work?

  12. • We picked a “bad” augmenting path above, which blocked us from pushing more flow. 10 Residual graph • The idea of residual networks lets us “undo” these bad choices.

  13. satisfies the following: 11 Residual graph A residual graph G f = ( V , E f ) is a flow graph (depending on the current flow f ) that • V ( G f ) = V ( G ) • For each edge e = u → v ∈ E ( G ) , we consider the following edges: • Forward edge: edge u → v with capacity c f ( u , v ) = c e − f e • Backward edge: edge v → u with capacity c f ( v , u ) = f e • c f ( u , v ) is the residual capacity of an edge. • The edges of G f are all the edges described above with residual capacity > 0.

  14. Theorem. A flow f in a flow graph G is maximum if and only if there is no augmenting path in the residual graph G f . 12 Residual graph

  15. path in the residual graph G f . 12 Residual graph Theorem. A flow f in a flow graph G is maximum if and only if there is no augmenting

  16. 13 6 1 10 2 9 3 8 4 7 5 Greedy! Ford-Fulkerson algorithm while there is an augmenting path in the residual graph: pick an augmenting path P # augment the flow push = min(capacity[e] for e in P) for e in P: if e is a forward edge: increase the flow on e by push else : decrease the flow on the corresponding forward edge by push update the residual graph

  17. 14 Ford-Fulkerson example

  18. 14 Ford-Fulkerson example

  19. 14 Ford-Fulkerson example

  20. 14 Ford-Fulkerson example

  21. 14 Ford-Fulkerson example

  22. Bad! What’s the runtime of Ford-Fulkerson? 15 Runtime • O ( | E | ) time to find an augmenting path (say, using DFS). • If the capacities are integers, every augmenting path increases the flow by ≥ 1 unit. • Thus, need O ( | f | ) iterations. Time complexity: O ( | E || f | ) .

  23. 15 What’s the runtime of Ford-Fulkerson? Runtime • O ( | E | ) time to find an augmenting path (say, using DFS). • If the capacities are integers, every augmenting path increases the flow by ≥ 1 unit. • Thus, need O ( | f | ) iterations. Time complexity: O ( | E || f | ) . Bad!

  24. Note: • If the capacities aren’t rational, Ford-Fulkerson is not guaranteed to terminate. • Not even guaranteed to converge to the maximum flow. (Edmonds-Karp algorithm). • Fast in practice, and better bounds on bipartite/unit capacity graphs. 16 Runtime • If we use BFS to find augmenting paths, we get O (min( | E || f | , | V || E | 2 )) runtime • Dinic’s algorithm uses some extra tricks to improve runtime to O ( | V | 2 | E | ) • Best theoretical complexity to compute max flow: O ( | V || E | ) (Orlin, 2013).

  25. 17 5 1 8 11 7 12 6 13 14 10 4 15 3 16 2 17 18 9 Edmonds-Karp Algorithm (BFS part) def EdmondsKarp_BFS(graph, s, t): # find an augmenting path and update the flow initialize previous[v] to null ( for all vertices v) initialize flow_to[v] to inf ( for all vertices v) q = new queue containing s while q is not empty: u = dequeue(q) for edge e from u to v , where v is a neighbour of u: if flow[e] < capacity[e] and previous[v] is null: previous[v] = u flow_to[v] = min(flow_to[u], capacity[e] - flow[e]) enqueue(v) if previous[t] is null: # no more augmenting paths return 0 # iterate over edges by following previous[], starting from t and ending at null for edge e in augmenting path: flow[e] += flow_to[t] # increase flow on the edge in the path flow[opposite edge of e] -= flow_to[t] # decrease flow on the opposite edge return flow_to[t]

  26. 18 6 12 11 10 9 8 1 7 5 4 2 3 Edmonds-Karp Algorithm def EdmondsKarp(graph, s, t): # add backward edges for edge e in graph from u to v : op = edge from v to u , with capacity 0 add op to graph # keep finding augmenting paths max_flow = 0 current_flow = EdmondsKarp_BFS(graph, s, t) while current_flow > 0: max_flow += current_flow current_flow = EdmondsKarp_BFS(graph, s, t) return max_flow

  27. 19 12 7 9 6 10 11 5 4 8 13 3 14 2 15 16 1 Dinic’s Algorithm (DFS and BFS parts) def Dinic_BFS(s, t): # build the layer graph run BFS from s , but only considering edges e where flow[e] < capacity[e] store dist(s,v) = number of edges on path from s to v , for each vertex v return true if we can reach t from s , else false def Dinic_DFS(u, t, f): # try to push f flow from u to t if u == t: return f # because we can push f flow from t to t for any f pushed = 0 for edge e from u to v , (start iteration from current_edge [u]): if pushed >= f: break # check whether we still have flow to push # we only want to try pushing flow if v is in the layer after u if flow[e] < capacity[e] and dist(s,v) == dist(s,u) + 1: current_push = Dinic_DFS(v, t, min(f - pushed, capacity[e] - flow[e])) flow[e] += current_push flow[opposite edge of e] -= current_push pushed += current_push return pushed

  28. 20 1 11 2 10 3 9 4 8 5 7 6 Dinic’s Algorithm def Dinic(graph, s, t): # add backward edges for edge e in graph from u to v : op = edge from v to u , with capacity 0 add op to graph # find max flow max_flow = 0 while Dinic_BFS(s, t): # while we can find a layer graph from s to t # try to push infinite flow from s to t, and record how much is successfully pushed max_flow += DFS(s, t, inf) return max_flow

  29. To solve this problem, we’ll study cuts. We want the cheapest way to disconnect the Soviet Union from the rest of Europe. 21 Back to the Cold War...

  30. from T to S don’t contribute.) 22 Cuts A cut divides the nodes in a graph into partitions ( S , T ) (i.e. V = S ∪ T and S ∩ T = ∅ ). • In the context of network flow, we usually consider s - t cuts, where s ∈ S and t ∈ T . • The capacity of the cut is the sum of capacities of the edges going from S to T . (Edges

  31. 23 Cuts

  32. 23 Cuts

  33. minimum s - t cut. Proof 1. Linear programming duality! (Not very illuminating...) Proof 2. • Run Ford-Fulkerson to find a maximum flow f . • Let S nodes reachable from s in the residual graph G f , T V S . • Claim: the capacity of the cut S T is equal to f . • To see this, observe that all edges from S to T are saturated, and all edges from T to S have zero flow. • Easy to see that the size of any flow the capacity of any cut, so we’re done. 24 Max-flow min-cut Theorem. The maximum flow from s to t in a graph is equal to the capacity of the

  34. minimum s - t cut. Proof 1. Linear programming duality! (Not very illuminating...) Proof 2. • Run Ford-Fulkerson to find a maximum flow f . • Let S nodes reachable from s in the residual graph G f , T V S . • Claim: the capacity of the cut S T is equal to f . • To see this, observe that all edges from S to T are saturated, and all edges from T to S have zero flow. • Easy to see that the size of any flow the capacity of any cut, so we’re done. 24 Max-flow min-cut Theorem. The maximum flow from s to t in a graph is equal to the capacity of the

  35. minimum s - t cut. Proof 1. Linear programming duality! (Not very illuminating...) Proof 2. • Run Ford-Fulkerson to find a maximum flow f . • To see this, observe that all edges from S to T are saturated, and all edges from T to S have zero flow. 24 Max-flow min-cut Theorem. The maximum flow from s to t in a graph is equal to the capacity of the • Let S = nodes reachable from s in the residual graph G f , T = V \ S . • Claim: the capacity of the cut ( S , T ) is equal to | f | . • Easy to see that the size of any flow ≤ the capacity of any cut, so we’re done.

Recommend


More recommend