






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: Algorithms; Subject: Computer Science; University: University of Illinois - Urbana-Champaign; Term: Unknown 1999;
Typology: Study notes
1 / 10
This page cannot be seen from the preview
Don't miss anything!







“Who are you?" said Lunkwill, rising angrily from his seat. “What do you want?" “I am Majikthise!" announced the older one. “And I demand that I am Vroomfondel!" shouted the younger one. Majikthise turned on Vroomfondel. “It’s alright," he explained angrily, “you don’t need to demand that." “Alright!" bawled Vroomfondel banging on an nearby desk. “I am Vroomfondel, and that is not a demand, that is a solid fact! What we demand is solid facts!" “No we don’t!" exclaimed Majikthise in irritation. “That is precisely what we don’t demand!" Scarcely pausing for breath, Vroomfondel shouted, “We don’t demand solid facts! What we demand is a total absence of solid facts. I demand that I may or may not be Vroomfondel!" — Douglas Adams, The Hitchhiker’s Guide to the Galaxy (1979) For a long time it puzzled me how something so expensive, so leading edge, could be so useless, and then it occurred to me that a computer is a stupid machine with the ability to do incredibly smart things, while computer pro- grammers are smart people with the ability to do incredibly stupid things. They are, in short, a perfect match. — Bill Bryson, Notes from a Big Country (1999)
One of the easiest applications of maximum flows is computing the maximum number of edge-disjoint paths between two specified vertices s and t in a directed graph G using maximum flows. A set of paths in G is edge-disjoint if each edge in G appears in at most one of the paths; several edge-disjoint paths may pass through the same vertex, however. If we give each edge capacity 1, then the maxflow from s to t assigns a flow of either 0 or 1 to every edge. Since any vertex of G lies on at most two saturated edges (one in and one out, or none at all), the subgraph S of saturated edges is the union of several edge-disjoint paths and cycles. Moreover, the number of paths is exactly equal to the value of the flow. Extracting the actual paths from S is easy—just follow any directed path in S from s to t , remove that path from S , and recurse. Conversely, we can transform any collection of k edge-disjoint paths into a flow by pushing one unit of flow along each path from s to t ; the value of the resulting flow is exactly k. It follows that the maxflow algorithm actually computes the largest possible set of edge-disjoint paths. The overall running time is O ( V E ), just like for maximum bipartite matchings. The same algorithm can also be used to find edge-disjoint paths in undirected graphs. We simply replace every undirected edge in G with a pair of directed edges, each with unit capacity, and compute a maximum flow from s to t in the resulting directed graph G ′^ using the Ford-Fulkerson algorithm. For
both edges from the flow without changing its value. Thus, without loss of generality, the maximum flow assigns a direction to every saturated edge, and we can extract the edge-disjoint paths by searching the graph of directed saturated edges.
Another natural application of maximum flows is finding large matchings in bipartite graphs. A matching is a subgraph in which every vertex has degree at most one, or equivalently, a collection of edges such that no two share a vertex. The problem is to find the matching with the maximum number of edges in a given bipartite graph.
We can solve this problem by reducing it to a maximum flow problem as follows. Let G be the given bipartite graph with vertex set U ∪ W , such that every edge joins a vertex in U to a vertex in W. We create a new directed graph G ′^ by (1) orienting each edge from U to W , (2) adding two new vertices s and t , (3) adding edges from s to every vertex in U , and (4) adding edges from each vertex in W to t. Finally, we assign every edge in G ′^ a capacity of 1. Any matching M in G can be transformed into a flow fM in G ′^ as follows: For each edge uw in M ,
resulting flow satisfies the capacity constraints. Moreover, the value of the resulting flow is equal to the number of edges in M. Conversely, consider any ( s , t )-flow f in G ′^ computed using the Ford-Fulkerson augmenting path algorithm. Because the edge capacities are integers, the Ford-Fulkerson algorithm assigns an integer flow to every edge. (This is easy to verify by induction, hint, hint.) Moreover, since each edge has unit capacity, the computed flow either saturates ( f ( e ) = 1) or avoids ( f ( e ) = 0) every edge in G ′. Finally, since at most one unit of flow can enter any vertex in U or leave any vertex in W , the saturated edges from U to W form a matching in G. The size of this matching is exactly | f |. Thus, the size of the maximum matching in G is equal to the value of the maximum flow in G ′, and provided we compute the maxflow using augmenting paths, we can convert the actual maxflow into a maximum matching. The maximum flow has value at most min{| U |, | W |} = O ( V ), so the Ford-Fulkerson
algorithm runs in O ( V E ) time.
s t
A maximum matching in a bipartite graph G , and the corresponding maximum flow in G ′.
Now suppose we want to find the matching with maximum weight in a bipartite graph with weighted edges. Given a bipartite graph G = ( U × W , E ) and a non-negative weight function w : E → IR, the goal is to compute a matching M whose total weight w ( M ) =
uw ∈ M w ( uw )^ is as large as possible. Max-weight matchings can’t be found directly using standard max-flow algorithms^1 , but we can modify the algorithm for maximum-cardinality matchings described above. It will be helpful to reinterpret the behavior of our earlier algorithm directly in terms of the original bipartite graph instead of the derived flow network. Our algorithm maintains a matching M , which is initially empty. We say that a vertex is matched if it is an endpoint of an edge in M. At each iteration, we find an alternating path π that starts and ends at unmatched vertices and alternates between edges in E \ M and edges in M. Equivalently, let GM be the directed graph obtained by orienting every edge in M from W to U , and every edge in E \ M from U to W. An alternating path is just a directed path in GM between two unmatched vertices. Any alternating path has odd length and has exactly one more edge in
(^1) However, max-flow algorithms can be modified to compute maximum weighted flows, where every edge has both a capacity and a weight, and the goal is to maximize
∑ u v w ( u v )^ f^ ( u v ).
Finally, since the number of red edges in Mi + 1 ⊕ Mi is one more than the number of blue edges, the number of paths that start with a red edge is exactly one more than the number of paths that start with a blue edge. The same reasoning as above implies that Mi + 1 ⊕ Mi does not contain a blue-first path, because we can pair it up with a red-first path. We conclude that Mi + 1 ⊕ Mi consists of a single alternating path π whose first edge is red. Since w ( Mi + 1 ) = w ( Mi ) − wi ( π ), the path π must be the one with minimum weight wi ( π ). É
We can find the alternating path πi using a single-source shortest path algorithm. Modify the residual graph Gi by adding zero-weight edges from a new source vertex s to every unmatched node in U , and from every unmatched node in W to a new target vertex t , exactly as in out unweighted matching algorithm. Then πi is the shortest path from s to t in this modified graph. Since Mi is the maximum-weight matching with i vertices, Gi has no negative cycles, so this shortest path is well-defined. We can compute the shortest path in Gi in O ( V E ) time using Shimbel’s algorithm, so the overall running time our algorithm is O ( V^2 E ). The residual graph Gi has negative-weight edges, so we can’t speed up the algorithm by replacing Shimbel’s algorithm with Dijkstra’s. However, we can use a variant of Johnson’s all-pairs shortest path algorithm to improve the running time to O ( V E + V^2 log V ). Let di ( v ) denote the distance from s to v in the residual graph Gi , using the distance function wi. Let w ˜ i denote the modified distance function
Let d ˜ i ( v ) denote the shortest path distance from s to v with respect to the distance function w ˜ i. Because w ˜ i is positive everywhere, we can quickly compute d ˜ i ( v ) for all v using Dijkstra’s algorithm. This gives us both the shortest alternating path πi and the distances di ( v ) = d ˜ i ( v ) + di − 1 ( v ) needed for the next iteration.
Now suppose each directed edge e in has both a capacity c ( e ) and a demand d ( e ) ≤ c ( e ), and we want a maximum flow that satisfies d ( e ) ≤ f ( e ) ≤ c ( e ). We call a flow that satisfies these constraints an admissible (or feasible ) flow. In our original setting, where d ( e ) = 0 for every edge e , the zero flow is admissible; however, in this more general setting, even determining whether an admissible flow exists is a nontrivial task. Perhaps the easiest way to find an admissible flow (or determine that none exists) is to reduce the problem to a standard maximum flow problem, as follows. The input consists of a directed graph G = ( V , E ), nodes s and t , demand function d : E → IR, and capacity function c : E → IR. Let D denote the sum of all edge demands in G : D :=
u v ∈ E
We construct a new graph G ′^ = ( V ′, E ′) from G by adding new source and target vertices s ′^ and t ′, adding edges from s ′^ to each vertex in V , adding edges from each vertex in V to t ′, and finally adding an edge from t to s. We also define a new capacity function c ′^ : E ′^ → IR as follows:
If this construction produces multiple edges from s ′^ to the same vertex v (or to t ′^ from the same vertex v ), we merge them into a single edge with the same total capacity.
s t
6..
4..
5..
0..
3..
2.. 5.. 7..
0..
s t
13
6
5
5
7
13
6 14
s'^14 t'
4 7
∞
10 3
5 3
10 3
8 11
A flow network G with demands and capacities (written d .. c ), and the transformed network G ′.
In G ′, the total capacity out of s ′^ and the total capacity into t ′^ are both equal to D. We call a flow with value exactly D a saturating flow, since it saturates all the edges leaving s ′^ or entering t ′. If G ′^ has a saturating flow, it must be a maximum flow, so we can find it using any max-flow algorithm.
Lemma 2. G has an admissible ( s , t )-flow if and only if G ′^ has a saturating ( s ′, t ′)-flow.
Proof: Let f : E → IR be an admissible ( s , t )-flow in the original graph G. Consider the following function f ′^ : E ′^ → IR:
u ∈ V
w ∈ V
d ( u → w ) for all v ∈ V
We easily verify that f ′^ is a saturating ( s ′, t ′)-flow in G. The admissibility of f implies that f ( e ) ≥ d ( e ) for every edge e ∈ E , so f ′( e ) ≥ 0 everywhere. Admissibility also implies f ( e ) ≤ c ( e ) for every edge e ∈ E , so f ′( e ) ≤ c ′( e ) everywhere. Tedious algebra implies that ∑
u ∈ V ′
w ∈ V ′
for every vertex v ∈ V (including s and t ). Thus, f ′^ is a legal ( s ′, t ′)-flow, and every edge out of s ′
target t ′, and injects the same amount of flow into v directly from the new source s ′. The same tedious algebra implies that for any saturating ( s ′, t ′)-flow f ′^ : E ′^ → IR for G ′, the function f = f ′| E + d is an admissible ( s , t )-flow in G. É
To simplify the transformation, let us assume without loss of generality that the total excess in the network is zero:
where ˜ t is a new target node, and set x ( t ) = −
v x ( v ). Similarly, if the total excess is negative, we add
v x ( v ). In both cases, every admissible flow in the modified graph corresponds to an admissible flow in the original graph. As before, we modify G to obtain a new graph G ′^ by adding a new source s ′, a new target t ′, an
edge leaving s ′^ or entering t ′^ is saturated; any saturating flow is a maximum flow. It is easy to check that saturating flows in G ′^ are in direct correspondence with admissible flows in G ; we leave details as an exercise (hint, hint).
Similar reductions allow us to solve several other variants of the maximum flow problem using the same path-augmentation algorithms. For example, we could associate capacities and lower bounds with the vertices instead of (or in addition to) the edges. We could associate a range of excesses with every node, instead of a single excess value. We can associate a cost c ( e ) with each edge, and then ask for the maximum-value flow f whose total cost
e c ( e )^ ·^ f^ ( e )^ is as small as possible. We could even apply all of these extensions at once: upper bounds, lower bounds, and cost functions for the flow through each edge, into each vertex, and out of each vertex.
Greedily covering the marked cells in a grid with four monotone paths.
(a) Describe an algorithm to find a monotone path that covers the largest number of marked cells. (b) There is a natural greedy heuristic to find a small cover by monotone paths: If there are any marked cells, find a monotone path π that covers the largest number of marked cells, unmark any cells covered by π those marked cells, and recurse. Show that this algorithm does not always compute an optimal solution. (c) Describe and analyze an efficient algorithm to compute the smallest set of monotone paths that covers every marked cell.
u
Describe and analyze an efficient algorithm to compute a feasible flow of maximum value.
∑^ σ
i = 1
w
u
As usual, we are interested in finding flows with maximum value, subject to capacity constraints on the edges. (In particular, we don’t care how much flow moves from any particular source to any particular target.)
? 8. A rooted tree is a directed acyclic graph, in which every vertex has exactly one incoming edge,
except for the root , which has no incoming edges. Equivalently, a rooted tree consists of a root vertex, which has edges pointing to the roots of zero or more smaller rooted trees. Describe a polynomial-time algorithm to compute, given two rooted trees A and B , the largest common rooted subtree of A and B. [Hint: Let LC S ( u , v ) denote the largest common subtree whose root in A is u and whose root in B is v. Your algorithm should compute LC S ( u , v ) for all vertices u and v using dynamic programming. This would be easy if every vertex had O ( 1 ) children, and still straightforward if the children of each node were ordered from left to right and the common subtree had to respect that ordering. But for unordered trees with large degree, you need another trick to combine recursive subproblems efficiently. Don’t waste your time trying to reduce the polynomial running time.]
© c Copyright 2008 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms for the most recent revision.