200 likes | 319 Views
CS 361 – Chapter 8. Final thoughts on minimum spanning trees and similar problems Flow networks Commitment: Please read section 8.2 Decide on presentation order. Flow network. Special case of a weighted, directed graph Each edge has a flow and a capacity
E N D
CS 361 – Chapter 8 • Final thoughts on minimum spanning trees and similar problems • Flow networks • Commitment: • Please read section 8.2 • Decide on presentation order
Flow network • Special case of a weighted, directed graph • Each edge has a flow and a capacity • Ex. Edge label “3/5” means the flow is 3 and capacity is 5 • Something is flowing through the graph (oil, packets, money, chocolate), from a source vertex to a sink vertex • Source has in-degree 0 • Sink has out-degree 0 • Source and sink are unique • The network must obey certain properties • For each edge: flow capacity • For each vertex: in flow = out flow except for source, sink
Observations • Look at example, such as p. 384 • Representation? • No cycles!
Total flow • A single edge e has a flow, f(e) • The entire graph has a flow |f| = total flow coming out of source, or going into sink • Big question: what is the maximum flow we can have in the network? Want a general algorithm. • Ex. On page 384 has a max flow of 14. Why is 15 not possible? • At |f| = 14, source v2 and source v3 are maxed out. • Source v1 has a flow of only 3 out of 7. Why?
Cut • Technique for finding maximum flow relies on notion of a “cut” through the network. • Cut = partition of the vertices into 2 sets. • One partition contains the source; the other contains sink. • Reminiscent of Prim’s algorithm, in which we temporarily partition the graph into vertices already covered by spanning and those not yet • Boundary of partition “cuts” or crosses the graph at various edges. • Edges crossed can be classified as forward or backward. (most are forward) • Capacity of cut: add capacities only of forward edges • Flow of cut: flow(forward) – flow(backward)
Cut properties • Network’s total flow = flow across any cut: |f| = f(cut) • Because source is on one side and sink is on other. • You can make a “first cut” like this: (source, everybody else). • Then, move a vertex from 2nd partition to the first. By “conservation rule”, the flow across the cut is the same. • Can continue moving cut thru the graph until 2nd partition has just the sink. • A cut’s flow can’t exceed its capacity: f(cut) c(cut) • Intutitive… simple algebra: • To compute f(cut), we sum flows across all its edges: |f| = f(cut) = sum(flows of forward) – sum(flows of backward) |f| = f(cut) sum(flows of forward) sum(capacities of forward) = sum(capacities of all edges) • Insight: max flow cut of min capacity.
Flow networks (2) • Review description and properties • cycles are allowed • How to find maximum flow: Ford-Fulkerson algorithm • Along the way, some helpful terminology
Residual capacity • How much capacity is left on some edge • Can be calculated once we are given some flow through the network/graph • RC(u, v) = capacity(u, v) – flow(u, v) • Ex. If an edge is labeled “7/9”, the corresponding RC is 9 – 7 = 2. • Can be zero, when we’ve maxed out capacity. • If no edge from u to v, we say capacity(u, v) = 0 • Only needs to be calculated for vertices that are adjacent.
Residual capacity (2) • Example • We calculate RC in both directions In either case, RC = cap – flow: • cap(a, b) = 20 cap(b, a) = 0 • flow(a, b) = 15 flow(b, a) = –15 • RC(a, b) = 5 RC(b, a) = 15 • Negative flow? Means the net flow is in the other (opposite) direction. • RC(a, b) = 5 means we can pump 5 more from a to b. RC(b, a) = 15 means we can pump 15 less from a to b. 15/20 a b
Residual network • This is a weighted, directed graph (thus, another flow network) computed based on original flow network. • The RN will sit alongside the flow network to assist in max flow computations later. • Use all vertices from original graph. • For all vertices u, v that are adjacent to each other, we compute the RC, and only draw edges where RC is positive. • Often, RN will show edges going in both directions between pairs of vertices. • Examples • Original edge “11/14” RN will show edges 3 and 11. • Original edges “1/4” and “0/10” other way RN will show edges 3 and 11.
Augmenting path • “Augmenting” happens to be at heart of max flow algorithm. • An augmenting path is a path from the source to the sink in the residual network. • We want to find the minimum residual capacity along this path. • This is the amount by which it’s safe to increase flow.
Algorithm • Ford-Fulkerson is the basic technique for finding the maximum flow. Further optimizations also exist. • To begin, it doesn’t matter what the edge flows are. In general, we can set them all to 0. maxFlow(G): set all edge flows in G to 0. RN = compute the residual network of G. while path from source to sink in RN: choose some path p from source to sink. min = lowest edge weight along p. Add min to edge flows along corresponding path in G. for each edge (u, v) in p: RN(u, v) -= min RN(v, u) += min // i.e. we recompute RN
Example • Work out the max flow for this flow network: • To begin, it turns out that the initial residual network (RN) is just the set of capacities of the graph. Why? • The routine is: Compute RN, find next augmenting path, recompute G and RN, etc.
Bipartite matching • Application of flow network • The problem: you have a set of people, and a set of jobs. Each person is certified to handle 1+ jobs, but can only perform 1 job. Is there a way to assign jobs to maximize how many jobs get done, e.g. all of them? • Ex. P1 J1; P2 J1, J2; P3 J2, J3; P4 J2, J3; P5 J3, J4, J5 • Introduce a source and sink vertex. • Many other problems require matching things in two sets, and desiring the maximum number of matches. What are some examples?
Circulation with demand • Generalization of flow network • What if we have multiple sources & sinks? • Each vertex has a “demand” amount. • Positive demand means you want to receive more flow than you produce. Effectively, you are a sink. • Negative demand means you want to produce more than you receive. You are a source. • What constraints do we have on such a network? • The problem: see if there exists a way to set the flow on each edge to satisfy all constraints. (Not interested in a maximum)
Example –3 • We can convert this into an ordinary flow network, by doing what? • Then solve Ford-Fulkerson, but what should its maximum flow be? 1/3 2/3 2/2 –3 2 2/2 2/2 4
Airline scheduling • Could the following flights all be accomplished by just 2 planes? (Assume all times are in same time zone) • BOS-DCA 6:00-7:00 • PHL-PIT 7:00-8:00 • DCA-LAX 8:00-11:00 • PHL-SFO 11:00-14:00 • SFO-SEA 14:30-15:30 • SLC-SEA 17:00-18:00 • *** It’s possible to formulate this kind of problem as a circulation with demand.