660 likes | 671 Vues
Explore network flow problems such as distribution, transportation, matching, and cut problems in data structures and algorithms. Learn about maximum flow, minimum cost flow, and flow networks using the Ford-Fulkerson algorithm.
E N D
Data Structures & Algorithms Network Flow Richard Newman based on book by R. Sedgewick
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
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
Merchandise distribution Factories 0 0 1 1 2 2 2 7 3 3 3 4 4 4 Distribution Centers 5 5 5 6 6 6 Retail Locations 7 7 7 8 8 8 9 9 9 Get product to retail locations cheaply
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
Transportation Problem 0 0 1 1 2 2 2 3 3 3 4 4 4 Supply 7 Channels Demand 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 No channel capacity restrictions Get product to retail locations cheaply
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
Matching Problem 0 0 1 1 2 2 2 3 3 3 4 4 4 Employer 7 Offers Applicant 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 Maximize placements (matching)
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?
Cut Problem Supplies 0 0 1 1 2 2 2 7 3 3 3 4 4 4 Delivery paths 5 5 5 6 6 6 Troops 7 7 7 8 8 8 9 9 9 How few edges must be cut to disrupt delivery May have edge weights also
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?
Network Flow • Flow Networks • Maxflow Algorithms • Maxflow Reductions • Mincost Flows • Network Simplex Algorithm
Flow Networks • Defn 22.1: A network with a single source and a single sink is an s-t network
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
Flow Network 3 1 3 1 2 2 1 0 5 3 1 1 Oil Field 3 1 3 Refinery 1 1 2 4 Pipelines and Valves Maximize flow subject to capacity and conservation of flow Is this OK?
Flow Network 1 3 1 3 1 2 2 1 1 0 5 2 1 1 Oil Field 3 1 3 Refinery 1 1 2 4 Pipelines and Valves Can we do better?
Flow Network 1 3 1 3 2 2 2 1 1 1 0 5 2 1 1 Oil Field 3 1 3 Refinery 1 1 2 4 Pipelines and Valves Are we done? Is this OK?
Flow Network 1 3 1 3 2 2 2 1 2 1 0 5 2 1 1 Oil Field 3 2 3 Refinery 1 1 2 4 Pipelines and Valves Now are we done? Yep
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)
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
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.
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
Ford-Fulkerson 2 3 1 3 2 2 2 1 2 s 0 5 t 1 New flow 3 3 1 2 4 Find path with capacity left: 0-1-3-5 Find minimum residual capacity: Add flow to saturate: 2 2
Ford-Fulkerson 2 3 Saturated edge, flow 1 3 2 2 2 2 1 s 0 5 t 1 1 Unsaturated flow 3 1 3 1 1 2 4 Find path with capacity left: 0-2-4-5 Find minimum residual capacity: Add flow to saturate: 1 1
Ford-Fulkerson 2 3 1 3 2 2 2 2 1 s 0 5 t 1 1 3 1 3 “Reverse edge” 1 1 2 4 Find path with capacity left: Wait a doggone second... Consider flow in one direction as capacity in reverse direction! There are none!
Ford-Fulkerson 2 3 1 3 2 2 2 2 2 1 s 0 5 t 1 1 3 1 3 “Reverse edge” 1 1 2 4 1 2 (the flow) What is “residual capacity” of 3-1? Find path with capacity left: What is the minimum RC on this path? Add flow to saturate... 0-2-3-1-4-5 1
Ford-Fulkerson 1 3 1 3 2 1 2 1 2 2 1 1 s 0 5 t 2 1 1 3 2 3 1 1 1 2 4 1 There is none. Find path with capacity left: We are done.
Ford-Fulkerson 1 3 1 3 2 1 2 1 2 2 1 1 s 0 5 t 2 1 1 3 2 3 1 1 1 2 4 1 Removing saturated edges partitions the network
Ford-Fulkerson 3 1 3 s 0 5 t 2 3 2 3 2 4 Removing saturated edges partitions the network Source s is in one part, t is in another Note: cut is subset of saturated edges
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.
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
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<c, include (v,w) with capacity c-f; if>0 include (w,v) with capacity f.
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).
Flow Network with Flow 2 3 1 3 2 2 2 2 1 2 2 2 Reverse edges s 0 5 t 1 3 3 Flow 1 2 4 Make reverse edges with capacity = flow Decrease capacity by flow on forward edges
Residual Network 1 Saturated edge 1 3 2 0 0 1 2 2 Reverse edges s 0 5 t 1 3 3 Residual capacity 1 2 4 Make reverse edges with capacity = flow Decrease capacity by flow on forward edges Remove edges with zero residual capacity (saturated edges)
Residual Network 1 1 3 2 1 2 2 Reverse edges s 0 5 t 1 3 3 Residual capacity 1 2 4 Residual network remains Note that reverse edge to s or from t don’t help, so can be omitted in practice
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
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
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.
Ford-Fulkerson Complexity • Cor: The time required to find a maxflow is O(VEM), which is O(V2M) 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
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.
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
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(V3) • O(E) time per pass, VE/2 passes, and if G is sparse, E is O(V).
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(V2 lg M lg V)
Network Flow • Flow Networks • Maxflow Algorithms • Maxflow Reductions • Mincost Flows • Network Simplex Algorithm
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!
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
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.
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.)
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