=========================================================================== CSC 373H Lecture Summary for Week 9 Winter 2006 =========================================================================== Network Flow (continued) Ford-Fulkerson algorithm: start with any flow f (e.g., f(e) = 0 for all e in E) while there is an augmenting path P augment f using P output f Cuts, getting maximum flow: - A "cut" is a partition of V into V_s, V_t (i.e., V = V_s U V_t and no vertex belongs to both V_s and V_t) such that s in V_s, t in V_t; . an edge (u,v) with u in V_s, v in V_t is a "forward" edge; . an edge (u,v) with u in V_t, v in V_s is a "backward" edge. - Example. - Lemma: For any cut X, flow for network |f| = flow across cut f(X), where f(X) = sum_{e forward} f(e) - sum_{e backward} f(e). - Lemma: For any cut X, f(X) <= capacity of cut c(X) (c(X) = sum of capacities of forward edges). - Corollary: For any flow f, any cut X, |f| <= c(X). Theorem (Ford-Fulkerson): |f| is maximum (and equal to c(X) for some cut) iff there is no augmenting path. Proof: (=>) augment (<=) Construct a cut X as follows: - s in V_s; - if (u,v) in E with u in V_s, v not in V_s, f(u,v) < c(u,v), then add v to V_s; - if (u,v) in E with v in V_s, u not in V_s, f(u,v) > 0, then add u to V_s. Let V_t = V \ V_s. Since there is no augmenting path, t in V_t. By definition of X, every edge crossing X has the property that f(e) = c(e) for forward edges and f(e) = 0 for backward edges. Hence, f(X) = |f| = c(X). Corollary (max-flow/min-cut theorem): For any network, the maximum flow equals the minimum cut capacity. Choosing augmenting paths efficiently: - Example where it could take 2 * 10^10 augmentations to find max flow of 2 * 10^10. In worst-case, Ford-Fulkerson takes time Theta(m f^*), where f^* is the value of a max flow -- this is not polytime. - Edmonds-Karp algorithm: use BFS (modified to consider only augmenting edges) to find augmenting paths; guaranteed to find an augmenting path with smallest # of edges in time O(m). Possible to prove that no more than O(mn) augmentations are required to find max. flow. Total time: O(nm^2) = O(n^5). - Modification: perform complete BFS, use all augmenting paths w.r.t. BFS tree, then repeat. Worst-case time down to O(n^2m) = O(n^4). - "Preflow" algorithms: don't use augmenting paths; instead, push as much as possible along individual edges then go back to fix conservation. More complicated to explain and write down correctly, but cuts down time to O(n^3). Applications: - Multi-source, multi-sink network: Add "super-source" with edges of capacity "oo" to each source and "super-sink" with edges of capacity "oo" from each sink (instead of using "oo", can set capacity to sum of outgoing/incoming capacities). Max flow in resulting network = max flow in original network because . any flow in original network can be extended to a flow in resulting network (for new edges from super-source to source, set flow equal to total flow out of source; for new edges from sink to super-sink, set flow equal to total flow into sink); . any flow in resulting network induces flow in original network (flow out of every source and into every sink not limited because of "infinite" capacities on new edges). - Maximum matching: Input: An undirected bipartite graph G=(V_1,V_2,E) -- one where every edge is between V_1 and V_2 (i.e., no edge has both endpoints in the same "side"). Output: A disjoint subset of edges of maximum size (i.e., no edge in a matching shares an endpoint with any other edge in the matching). Given input graph, create network by turning every original edge into a directed edge (from V_1 to V_2) with capacity 1; add source with edges of capacity 1 to each vertex in V_1, sink with edges of capacity 1 from each vertex in V_2. . Any matching in graph yields flow in network: set flow = 1 for graph edges in matching, 0 for graph edges out of matching; set flow = 1 for new edges to/from matched vertices, 0 for new edges to/from unmatched vertices. . Any flow in network yields matching in graph: pick edges with flow = 1 (leave out edges with flow = 0). For this correspondence, size of matching = value of flow. Hence, any max flow in network yields a maximum matching in graph (because a larger matching would give a larger flow). - Teaching assignment: Input: Set of profs p_1, ..., p_n and courses c_1, ..., c_m; each prof has teaching load L (number of courses that must be taught) and subset of courses that the prof is capable of teaching. Output: Assignment of profs to courses so that: . each prof assigned exactly L courses . each course assigned exactly 1 prof. Is it possible to assign profs to courses to satisfy all constraints? If so, how? Given input, create network with vertices p_1,...,p_n, c_1,...,c_m, source s, sink t, and edges (s,p_i) of capacity L for each p_i, edges (c_j,t) of capacity 1 for each c_j, edges (p_i,c_j) of capacity 1 for each p_i,c_j such that p_i can teach c_j. . Any assignment of profs to courses yields flow in network: set f(p_i,c_j) = 1 if p_i assigned c_j, 0 otherwise; set f(s,p_i) = number of courses assigned to p_i; set f(c_j,t) = number of profs assigned to c_j. . Any flow in network yields assignment of profs to courses: assign p_i to c_j if f(p_i,c_j) = 1. Find max flow f in network. If |f| = Ln = m, then it is possible to assign profs to courses to satisfy all constraints; otherwise it isn't. If it is not possible, max flow yields max assignment possible. This could be used for example to determine set of courses that can be offered, or maximum teaching load for profs. Note this can easily accomodate slightly more general problem where each prof p_i has teaching load L_i which can change from prof to prof, and/or where each course can be taught by more than one prof (in multiple sections). - Project selection: See section 7.11 in textbook.