Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures & Algorithms Network Flow Richard Newman based on book by R. Sedgewick.

Similar presentations


Presentation on theme: "Data Structures & Algorithms Network Flow Richard Newman based on book by R. Sedgewick."— Presentation transcript:

1 Data Structures & Algorithms Network Flow Richard Newman based on book by R. Sedgewick

2 Network Flow Problems Weighted, digraph G, or network May have cost per unit flow for edges May have maximum flow per edge May have max production rates May have required consumption rate per sink

3 Distribution Problems Merchandise distribution – Sources with production rates – Sinks with consumption rates – Distribution centers Input rate = output rate – Channels with maximum rate and unit cost for distribution

4 Merchandise distribution Get product to retail locations cheaply 6 7 2 4 5 3 1 89 7 6 7 2 4 5 3 1 89 2 4 65 3 798 00 Retail Locations Factories Distribution Centers

5 Transportation Problems Communications – Max total data rate between a source and sink – Cheapest way to move a given amount of data from s to t Traffic flow – Minimize evacuation time – Minimize total cost

6 Transportation Problem No channel capacity restrictions Get product to retail locations cheaply 67 24 5 31 89 7 67 24 5 31 89 24 65 3 798 00 Demand Supply Channels

7 Matching Problems Job placement – Interviews + job offers – Maximize number of placements Min-distance point matching – Two sets A and B of N points each – Find set of N segments matching an element from A with one from B that has lowest cost

8 Matching Problem Maximize placements (matching) 67 24 5 31 89 7 67 24 5 31 89 24 65 3 798 00 Applicant Employer Offers

9 Cut Problems Network reliability – What is minimum number of lines that must be cut to disconnect two switches? Supply line cutting – What is the minimum supply line destruction required to ensure no troops get supplies?

10 Cut Problem How few edges must be cut to disrupt delivery May have edge weights also 6 7 2 4 5 3 1 89 7 6 7 2 4 5 3 1 89 2 4 65 3 798 00 Troops Supplies Delivery paths

11 Network Flow Problems Generic problems Maxflow – What is maximum flow between s and t? Mincost-flow – What is cheapest cost way to achieve a particular flow?

12 Network Flow Flow Networks Maxflow Algorithms Maxflow Reductions Mincost Flows Network Simplex Algorithm

13 Flow Networks Defn 22.1: A network with a single source and a single sink is an s-t network

14 Flow Networks Defn 22.2: A flow network is an s-t network with positive edge weights, called capacities. A flow in a flow network is a set of non-negative edge weights called edge flows satisfying: No edge flow exceeds capacity In flow = out flow for interior nodes

15 Flow Network Maximize flow subject to capacity and conservation of flow 2 4 5 31 0 Refinery Oil Field Pipelines and Valves 2 3 2 3 3 1 1 1 3 1 1 1 1 Is this OK?

16 Flow Network Can we do better? 2 4 5 31 0 Refinery Oil Field Pipelines and Valves 2 3 2 3 3 1 1 1 2 1 1 1 1 1 1

17 Flow Network Are we done? Is this OK? 2 4 5 31 0 Refinery Oil Field Pipelines and Valves 2 3 2 3 3 1 1 1 2 1 1 1 2 1 1 1

18 Flow Network Now are we done? Yep 2 4 5 31 0 Refinery Oil Field Pipelines and Valves 2 3 2 3 3 1 1 1 2 1 2 1 2 2 1 1

19 Flow Network Sum of flows into a node is called inflow Sum of flows out of a node is called outflow Conservation of flow: except for source and sink, inflow = outflow Feasible flow = obeys constraints (max flow and conservation of flow)

20 Flow Network Set outflow from sink to zero Set inflow to source to zero Outflow of source = inflow of sink This is called network's value

21 Maximum Flow Given an s-t network, find a flow such that no other flow from s to t has a larger value. A flow like this is called a maxflow. Problem of finding one is called the maxflow problem.

22 Augmenting Path Maximum Flow First algorithm like this due to Ford and Fulkerson Iteratively: Find a feasible path from s to t Find the max residual capacity on it Saturate the path by adding flow along it equal to the minimum residual capacity

23 Ford-Fulkerson Find path with capacity left: 0-1-3-5 Find minimum residual capacity: Add flow to saturate: 2 2 4 5 31 0 t s 2 3 2 3 3 1 1 1 2 2 2 2 New flow

24 Ford-Fulkerson Find path with capacity left: 0-2-4-5 Find minimum residual capacity: Add flow to saturate: 1 2 4 5 31 0 t s 2 3 2 3 3 1 1 1 1 1 1 2 2 2 1 Saturated edge, flow Unsaturated flow

25 Ford-Fulkerson Find path with capacity left: Wait a doggone second... Consider flow in one direction as capacity in reverse direction! 2 4 5 31 0 t s 2 3 2 3 3 1 1 1 1 1 1 2 2 2 There are none! “Reverse edge”

26 Ford-Fulkerson What is “residual capacity” of 3-1? Find path with capacity left: What is the minimum RC on this path? Add flow to saturate... 2 4 5 31 0 t s 2 3 2 3 3 1 1 1 1 1 1 2 2 2 0-2-3-1-4-5 2 (the flow) 1 2 1 “Reverse edge”

27 Ford-Fulkerson Find path with capacity left: We are done. 2 4 5 31 0 t s 2 3 2 3 3 1 1 1 2 1 2 2 2 1 There is none. 1 1 1 1 1 1

28 Ford-Fulkerson Removing saturated edges partitions the network 2 4 5 31 0 t s 2 3 2 3 3 1 1 1 2 1 2 2 2 1 1 1 1 1 1 1

29 Ford-Fulkerson Removing saturated edges partitions the network Source s is in one part, t is in another Note: cut is subset of saturated edges 2 4 5 31 0 t s 3 3 3 2 2

30 s-t Cut Defn. 22.3: An s-t cut is a cut that places node s in one of its sets and node t in the other. Its capacity is the sum of its edge weights. The flow across an s-t cut is the sum of the flows across its s-t edges, less the sum of the flows across its t-s edges.

31 MinCut Problem Given an s-t network, find an s-t cut such that the capacity of no other cut is smaller. We call this a mincut. Maxflow-mincut theorem – min cut capacity and maxflow value are equal

32 Residual Network Given flow network G and a flow F, the residual network R for the flow has the same nodes, and one or two edges in R for each edge in the original: for edge (v,w) in G, let f be the flow and c be the capacity. If f 0 include (w,v) with capacity f.

33 Residual Network If flow is 0, then the original edge with original capacity is used For positive flows, decrease capacity by the flow (unless the residual capacity is zero – the edge is saturated – remove the edge) For positive flows, add a reverse directed edge with the flow as its capacity (can reduce flow that much).

34 Flow Network with Flow Make reverse edges with capacity = flow Decrease capacity by flow on forward edges 2 4 5 31 0 t s 2 3 2 3 3 1 1 1 2 2 2 Flow 2 2 2 Reverse edges

35 Residual Network 2 4 5 31 0 t s 0 3 0 1 3 1 1 1 Saturated edge Residual capacity 2 2 2 Reverse edges Make reverse edges with capacity = flow Decrease capacity by flow on forward edges Remove edges with zero residual capacity (saturated edges)

36 Residual Network 2 4 5 31 0 t s 3 1 3 1 1 1 Residual capacity 2 2 2 Reverse edges Residual network remains Note that reverse edge to s or from t don’t help, so can be omitted in practice

37 Variants on Ford-Fulkerson Shortest Augmenting Path Measured by number of edges Build “layer graph” – like BFS Which nodes can be reached by 1 edge, then by 2 edges, etc. Stop when t is reached and add flow to saturate edge on path to t

38 Variants on Ford-Fulkerson Maximum Flow Augmenting Path Measured by max flow along path Pick edges that give max flow to next layer Take max of (min flow along path to predecessor u, capacity (u,v)) to find max flow to v through u at that layer

39 Ford-Fulkerson Complexity Prop. 22.6: Let M be the maximum edge capacity in G. The number of augmenting paths needed by any implementation of F-F is at most VM. Every AP adds at least one unit of flow to every cut; any cut has at most V edges; hence the algorithm must terminate after VM passes since any cut must be saturated by then.

40 Ford-Fulkerson Complexity Cor: The time required to find a maxflow is O(VEM), which is O(V 2 M) for sparse networks. Linear (in edges) graph search per pass. Need extra lg V factor if using priority queue fringe implementation Actual performance is quite good

41 Ford-Fulkerson Complexity Prop. 22.7: The number of augmenting paths needed in the shortest augmenting path F-F algo is at most VE/2.

42 Ford-Fulkerson Complexity The length of the APs monotone non- decreasing. Every AP has a critical edge that is saturated in its pass. Each time edge e is the critical edge, the AP must be at least 2 hops longer. The longest path has < V edges

43 Ford-Fulkerson Complexity Prop. 22.7 (again): The number of augmenting paths needed in the shortest augmenting path F-F algo is at most VE/2. Cor: The time required to find a maxflow in a sparse network is O(V 3 ) O(E) time per pass, VE/2 passes, and if G is sparse, E is O(V).

44 Ford-Fulkerson Complexity Prop. 22.8: The number of augmenting paths needed in the maximal augmenting path F-F algo is at most 2E lg M. Cor: The time required to find a maxflow in a sparse network is O(V 2 lg M lg V)

45 Pre-flow Algorithms Push flow into nodes, then out

46 Network Flow Flow Networks Maxflow Algorithms Maxflow Reductions Mincost Flows Network Simplex Algorithm

47 Maxflow in General Networks Maximize the total outflow from sources in a network. (Zero by convention if no sources or sinks) Multiple sources Multiple sinks Still need feasible flows!

48 Maxflow in General Networks Prop. 22.14: The maxflow in general networks is equivalent to the maxflow problem for general networks. The general case subsumes the special case of s-t networks. Add dummy source connected to all sources and dummy sink to all sinks by high-capacity edges

49 Vertex-capacity Constraints Given a flow network, find a maxflow satisfying additional constraints that the flow through each node v must not exceed the capacity of that node.

50 Vertex-capacity Constraints Prop. 22.15: The maxflow with node capacity problem is equivalent to maxflow problem. Setting node capacity high subsumes the special case of s-t networks. Node capacity can be set to max of in-capacity and out-capacity (sum of in- and out-edge capacities, resp.)

51 Vertex-capacity Constraints Prop. 22.15: The maxflow with node capacity problem is equivalent to maxflow problem. Split each node into in-part and out- part, with all in-edge to in-part and all out-edge from out-part Make edge from in-part to out-part with edge capacity = node capacity in original problem

52 Maxflow in Acyclic Networks Maximize the total outflow from sources in an acyclic network. Seems like it might be easier But NO! Easy transformation from any network to acyclic network V nodes -> 2V + 2 nodes E edges -> E + 3V edges

53 Maxflow in General Networks Prop. 22.16: The maxflow in acyclic networks is equivalent to the maxflow problem for general networks. The construction allows a mincut in original to reveal a mincut in the transformed version, and vice versa, with transformed cut values. p427

54 Maxflow in Undirected Networks Prop. 22.17: The maxflow in undirected networks reduces to the maxflow problem for s-t networks. Just make two directed edges in the s-t network for every edge in the undirected network Make both edge capacities equal to the corresponding edge capacity in the undirected network

55 Maxflow in Undirected Networks Note that we did not say equivalent! It remains possible that finding maxflows in undirected networks is easier than finding them in directed networks.

56 Feasible Flow Given a flow network G, assign a weight to each node, interpreted as supply if positive, and demand if negative. A flow is feasible if it obeys flow constraints AND if the difference between the outflow sum and inflow sum for each node v = v’s weight Finding a feasible flow is FF problem

57 Feasible Flow Prop. 22.18: The feasible flow problem reduces to maxflow Make flow network with V more nodes. For supply nodes, make the new node a source with edge weight the same as the node weight For demand nodes make the new node a sink with edge capacity = -weight

58 Feasible Flow Prop. 22.18: The feasible flow problem reduces to maxflow Feasible flow exists in original network iff maxflow in constructed flow network saturates all edges between sources and companions and all edges between sinks and companions

59 Max-cardinality Bipartite Matching Given a bipartite graph G, find a set of edges of maximum cardinality such that each vertex is connected to at most one other vertex. A.k.a. Bipartite Matching Problem Direct solution by trying all possibilities results in combinatorial explosion!

60 Max-cardinality Bipartite Matching Prop. 22.19: Bipartite matching reduces to the maxflow problem. Make edges directed from set A to set B nodes Add two dummy nodes s and t Add edges from s to each node in A Add edges from each node in B to t Assign capacity 1 to all edges

61 Edge-Connectivity Prop. 22.20: The minimum number of edges whose removal disconnects two nodes in a digraph is equal to the number of edge-disjoint paths between the two nodes. Define a flow network with the same nodes and edges, with capacities all 1. The capacity of any s-t cut is equal to the cut’s cardinality

62 Connectivity Problems Edge connectivity Node connectivity Digraphs Undirected graphs Hence 4 problems Can do reductions….

63 Edge-Connectivity Prop. 22.21: The time required to determine the edge-connectivity of an undirected graph G is O(E 2 ) Compute min cut in corresponding s-t network with unit capacity edges. Edge connectivity for G is the minimum over all s-t pairs Note we only have to use a minimal degree vertex as s – faster

64 Flow Network Now are we done? Yep 2 4 5 31 0 Refinery Oil Field Pipelines and Valves 2 3 2 3 3 1 1 1 2 1 2 1 2 2 1 1

65 Network Flow Flow Networks Maxflow Algorithms Maxflow Reductions Mincost Flows Network Simplex Algorithm

66 Mincost Flows Flow network has cost also associated with each edge Defn 22.8: Flow cost of an edge is the product of the flow and the edge cost. The cost of a flow is the sum of the flow costs of that flow’s edges.

67 Mincost Flows Mincost Maxflow Find maxflow with minimum flow cost Mincost Feasible Flow Find feasible flow with minimum cost Prop 22.22: The mincost feasible flow and the mincost maxflow problems are equivalent.

68 Network Flow Flow Networks Maxflow Algorithms Maxflow Reductions Mincost Flows Network Simplex Algorithm


Download ppt "Data Structures & Algorithms Network Flow Richard Newman based on book by R. Sedgewick."

Similar presentations


Ads by Google