url
stringclasses
71 values
filename
stringclasses
71 values
success
bool
1 class
page_count
int64
1
917
pdf_text
stringlengths
0
4.81k
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
3.30. On page 97, we defined the binary relation “connected” on the set of vertices of a directed graph. Show that this is an equivalence relation (see Exercise 3.29), and conclude that it partitions the vertices into disjoint strongly connected components. 3.31. Biconnected components Let G = (V, E) be an undirected gr...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
108
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Chapter 4 Paths in graphs 4.1 Distances Depth-first search readily identifies all the vertices of a graph that can be reached from a designated starting point. It also finds explicit paths to these vertices, summarized in its search tree (Figure 4.1). However, these paths might not be the most economical ones possi- ble. ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 4.2 A physical model of a graph. B E S D C A S D E C B A Figure 4.3 Breadth-first search. procedure bfs(G, s) Input: Graph G = (V, E), directed or undirected; vertex s ∈V Output: For all vertices u reachable from s, dist(u) is set to the distance from s to u. for all u ∈V : dist(u) = ∞ dist(s) = 0 Q = [s] (queue ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 4.4 The result of breadth-first search on the graph of Figure 4.1. Order Queue contents of visitation after processing node [S] S [A C D E] A [C D E B] C [D E B] D [E B] E [B] B [ ] D A B C E S right thing. If S is the starting point and the nodes are ordered alphabetically, they get visited in the sequence shown...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 4.5 Edge lengths often matter. Francisco San Los Angeles Bakersfield Sacramento Reno Las Vegas 409 290 95 271 133 445 291 112 275 Also notice one stylistic difference from DFS: since we are only interested in distances from s, we do not restart the search in other connected components. Nodes not reachable from s...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 4.6 Breaking edges into unit-length pieces. C A B E D C E D B A 1 2 2 4 2 3 1 transformation. To construct the new graph G′, For any edge e = (u, v) of E, replace it by le edges of length 1, by adding le −1 dummy nodes between u and v. Graph G′ contains all the vertices V that interest us, and the distances betw...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
More generally, at any given moment the breadth-first search is advancing along certain edges of G, and there is an alarm for every endpoint node toward which it is moving, set to go off at the estimated time of arrival at that node. Some of these might be overestimates be- cause BFS may later find shortcuts, as a result...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
into account. This viewpoint gives a concrete appreciation of how and why the algorithm works, but there is a more direct, more abstract derivation that doesn’t depend upon BFS at all. We now start from scratch with this complementary interpretation. Figure 4.8 Dijkstra’s shortest-path algorithm. procedure dijkstra(G, ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 4.9 A complete run of Dijkstra’s algorithm, with node A as the starting point. Also shown are the associated dist values and the final shortest-path tree. B C D E A 4 1 3 2 4 1 3 5 2 A: 0 D: ∞ B: 4 E: ∞ C: 2 B C D E A 4 2 4 1 3 5 2 1 3 A: 0 D: 6 B: 3 E: 7 C: 2 B C D E A 4 1 3 2 4 1 3 5 2 A: 0 D: 5 B: 3 E: 6 C: 2 ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 4.10 Single-edge extensions of known shortest paths.                          ! "# $%&' ( () * *+ , ,- . ./ 0 0 1 1 s u R Known region v 4.4.2 An alternative derivation Here’s a plan for computing shortest paths: expand outward from the starting point s, steadil...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Initialize dist(s) to 0, other dist(·) values to ∞ R = { } (the ‘‘known region’’) while R ̸= V : Pick the node v ̸∈R with smallest dist(·) Add v to R for all edges (v, z) ∈E: if dist(z) > dist(v) + l(v, z): dist(z) = dist(v) + l(v, z) Incorporating priority queue operations gives us back Dijkstra’s algorithm (Figure 4....
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Which heap is best? The running time of Dijkstra’s algorithm depends heavily on the priority queue implemen- tation used. Here are the typical choices. Implementation deletemin insert/ decreasekey |V | × deletemin + (|V | + |E|) × insert Array O(|V |) O(1) O(|V |2) Binary heap O(log |V |) O(log |V |) O((|V | + |E|) log...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
4.5 Priority queue implementations 4.5.1 Array The simplest implementation of a priority queue is as an unordered array of key values for all potential elements (the vertices of the graph, in the case of Dijkstra’s algorithm). Initially, these values are set to ∞. An insert or decreasekey is fast, because it just invol...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 4.11 (a) A binary heap with 10 elements. Only the key values are shown. (b)–(d) The intermediate “bubble-up” steps in inserting an element with key 7. (e)–(g) The “sift-down” steps in a delete-min operation. (a) 3 5 10 12 11 6 8 15 20 13 (b) 3 5 10 12 11 6 8 15 20 13 7 (c) 3 5 10 11 6 8 15 20 13 12 7 (d) 3 5 11 ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 4.12 Dijkstra’s algorithm will not work if there are negative edges. S A B −2 3 4 4.6 Shortest paths in the presence of negative edges 4.6.1 Negative edges Dijkstra’s algorithm works in part because the shortest path from the starting point s to any node v must pass exclusively through nodes that are closer than...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 4.13 The Bellman-Ford algorithm for single-source shortest paths in general graphs. procedure shortest-paths(G, l, s) Input: Directed graph G = (V, E); edge lengths {le : e ∈E} with no negative cycles; vertex s ∈V Output: For all vertices u reachable from s, dist(u) is set to the distance from s to u. for all u ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 4.14 The Bellman-Ford algorithm illustrated on a sample graph. E B A G F D S C 3 1 1 −2 2 10 −1 −1 −4 1 8 Iteration Node 0 1 2 3 4 5 6 7 S 0 0 0 0 0 0 0 0 A ∞ 10 10 5 5 5 5 5 B ∞ ∞ ∞ 10 6 5 5 5 C ∞ ∞ ∞ ∞ 11 7 6 6 D ∞ ∞ ∞ ∞ ∞ 14 10 9 E ∞ ∞ 12 8 7 7 7 7 F ∞ ∞ 9 9 9 9 9 9 G ∞ 8 8 8 8 8 8 8 4.7 Shortest paths in dag...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 4.15 A single-source shortest-path algorithm for directed acyclic graphs. procedure dag-shortest-paths(G, l, s) Input: Dag G = (V, E); edge lengths {le : e ∈E}; vertex s ∈V Output: For all vertices u reachable from s, dist(u) is set to the distance from s to u. for all u ∈V : dist(u) = ∞ prev(u) = nil dist(s) = ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Exercises 4.1. Suppose Dijkstra’s algorithm is run on the following graph, starting at node A. A B C D E F G H 1 2 4 1 2 6 8 5 6 4 1 1 1 (a) Draw a table showing the intermediate distance values of all the nodes at each iteration of the algorithm. (b) Show the final shortest-path tree. 4.2. Just like the previous proble...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Input: Undirected graph G = (V, E) with unit edge lengths; nodes u, v ∈V . Output: The number of distinct shortest paths from u to v. 4.6. Prove that for the array prev computed by Dijkstra’s algorithm, the edges {u, prev[u]} (for all u ∈V ) form a tree. 4.7. You are given a directed graph G = (V, E) with (possibly neg...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 4.16 Operations on a binary heap. procedure insert(h, x) bubbleup(h, x, |h| + 1) procedure decreasekey(h, x) bubbleup(h, x, h−1(x)) function deletemin(h) if |h| = 0: return null else: x = h(1) siftdown(h, h(|h|), 1) return x function makeheap(S) h = empty array of size |S| for x ∈S: h(|h| + 1) = x for i = |S| do...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
4.16. Section 4.5.2 describes a way of storing a complete binary tree of n nodes in an array indexed by 1, 2, . . . , n. (a) Consider the node at position j of the array. Show that its parent is at position ⌊j/2⌋and its children are at 2j and 2j + 1 (if these numbers are ≤n). (b) What the corresponding indices when a c...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
4.19. Generalized shortest-paths problem. In Internet routing, there are delays on lines but also, more significantly, delays at routers. This motivates a generalized shortest-paths problem. Suppose that in addition to having edge lengths {le : e ∈E}, a graph also has vertex costs {cv : v ∈V }. Now define the cost of a p...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Let r∗be the maximum ratio achievable by a simple cycle. One way to determine r∗is by binary search: by first guessing some ratio r, and then testing whether it is too large or too small. Consider any positive r > 0. Give each edge (i, j) a weight of wij = rcij −pj. (a) Show that if there is a cycle of negative weight, ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
132
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Chapter 5 Greedy algorithms A game like chess can be won only by thinking ahead: a player who is focused entirely on immediate advantage is easy to defeat. But in many other games, such as Scrabble, it is possible to do quite well by simply making whichever move seems best at the moment and not worrying too much about ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Output: A tree T = (V, E′), with E′ ⊆E, that minimizes weight(T) = X e∈E′ we. In the preceding example, the minimum spanning tree has a cost of 16: A B C D E F 1 4 2 5 4 However, this is not the only optimal solution. Can you spot another? 5.1.1 A greedy approach Kruskal’s minimum spanning tree algorithm starts with th...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Trees A tree is an undirected graph that is connected and acyclic. Much of what makes trees so useful is the simplicity of their structure. For instance, Property 2 A tree on n nodes has n −1 edges. This can be seen by building the tree one edge at a time, starting from an empty graph. Initially each of the n nodes is ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 5.2 T ∪{e}. The addition of e (dotted) to T (solid lines) produces a cycle. This cycle must contain at least one other edge, shown here as e′, across the cut (S, V −S).                    e S V −S e′ 5.1.2 The cut property Say that in the process of building a minimum spa...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 5.3 The cut property at work. (a) An undirected graph. (b) Set X has three edges, and is part of the MST T on the right. (c) If S = {A, B, C, D}, then one of the minimum-weight edges across the cut (S, V −S) is e = {D, E}. X ∪{e} is part of MST T ′, shown on the right. (a) A B C E F D 2 2 3 3 4 1 1 2 1 (b) Edges...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 5.4 Kruskal’s minimum spanning tree algorithm. procedure kruskal(G, w) Input: A connected undirected graph G = (V, E) with edge weights we Output: A minimum spanning tree defined by the edges X for all u ∈V : makeset(u) X = {} Sort the edges E by weight for all edges {u, v} ∈E, in increasing order of weight: if ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
return x As can be expected, makeset is a constant-time operation. On the other hand, find follows parent pointers to the root of the tree and therefore takes time proportional to the height of the tree. The tree actually gets built via the third operation, union, and so we must make sure that this procedure keeps tree...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 5.6 A sequence of disjoint-set operations. Superscripts denote rank. After makeset(A), makeset(B), . . . , makeset(G): A0 B 0 C 0 D 0 E 0 F0 0 G After union(A, D), union(B, E), union(C, F): A0 B0 C0 G0 F1 E1 D1 After union(C, G), union(E, A): B 1 F1 C 0 G 0 E D2 A0 0 After union(B, G): A G0 F E1 0 C0 D2 B0 1 Pat...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 5.7 The effect of path compression: find(I) followed by find(K). B0 D0 I0 J0 K0 H0 C1 1 G1 A3 F E2 −→ B0 0 D K0 J0 I0 H0 C1 F 1 G1 A3 E2 −→ B0 D H0 J 0 I0 K0 G1 C1 F 1 E2 A 0 3 little more careful to maintain our data structure in good shape. As any housekeeper knows, a little extra effort put into routine maint...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
compression) only touch the insides of trees, whereas union’s only look at the top level. Thus path compression has no effect on union operations and leaves the top level unchanged. We now know that the ranks of root nodes are unaltered, but what about nonroot nodes? The key point here is that once a node ceases to be ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
A randomized algorithm for minimum cut We have already seen that spanning trees and cuts are intimately related. Here is another connection. Let’s remove the last edge that Kruskal’s algorithm adds to the spanning tree; this breaks the tree into two components, thus defining a cut (S, S) in the graph. What can we say ab...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 5.8 Prim’s algorithm: the edges X form a tree, and S consists of its vertices.                        ! ! " " " " # # # # $ $ % % e S V −S X growing to include the vertex v ̸∈S of smallest cost: cost(v) = min u∈S w(u, v). This is strongly reminiscent of Dijkstra’s...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 5.9 Top: Prim’s minimum spanning tree algorithm. Below: An illustration of Prim’s algorithm, starting at node A. Also shown are a table of cost/prev values, and the final MST. procedure prim(G, w) Input: A connected undirected graph G = (V, E) with edge weights we Output: A minimum spanning tree defined by the ar...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
5.2 Huffman encoding In the MP3 audio compression scheme, a sound signal is encoded in three steps. 1. It is digitized by sampling at regular intervals, yielding a sequence of real numbers s1, s2, . . . , sT . For instance, at a rate of 44,100 samples per second, a 50-minute symphony would correspond to T = 50 × 60 × 4...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 5.10 A prefix-free encoding. Frequencies are shown in square brackets. Symbol Codeword A 0 B 100 C 101 D 11 0 A [70] 1 [60] C [20] B [3] D [37] [23] In general, how do we find the optimal coding tree, given the frequencies f1, f2, . . . , fn of n symbols? To make the problem precise, we want a tree whose leaves ea...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
f1 f2 f3 f5 f4 f1 + f2 The latter problem is just a smaller version of the one we started with. So we pull f1 and f2 off the list of frequencies, insert (f1 + f2), and loop. The resulting algorithm can be described in terms of priority queue operations (as defined on page 114) and takes O(n log n) time if a binary heap ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
149
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Entropy The annual county horse race is bringing in three thoroughbreds who have never competed against one another. Excited, you study their past 200 races and summarize these as prob- ability distributions over four outcomes: first (“first place”), second, third, and other. Outcome Aurora Whirlwind Phantasm first 0.15...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
5.3 Horn formulas In order to display human-level intelligence, a computer must be able to perform at least some modicum of logical reasoning. Horn formulas are a particular framework for doing this, for expressing logical facts and deriving conclusions. The most primitive object in a Horn formula is a Boolean variable...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
while there is an implication that is not satisfied: set the right-hand variable of the implication to true if all pure negative clauses are satisfied: return the assignment else: return ‘‘formula is not satisfiable’’ For instance, suppose the formula is (w ∧y ∧z) ⇒x, (x ∧z) ⇒w, x ⇒y, ⇒x, (x ∧y) ⇒w, (w ∨x ∨y), (z). We ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 5.11 (a) Eleven towns. (b) Towns that are within 30 miles of each other. (a) h b k j i g f e a c d (b) h b k j i g f e a c d Repeat until all elements of B are covered: Pick the set Si with the largest number of uncovered elements. This is extremely natural and intuitive. Let’s see what it would do on our earlie...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
x 0 1 1 −x e−x Thus nt ≤ n0  1 −1 k t < n0(e−1/k)t = ne−t/k. At t = k ln n, therefore, nt is strictly less than ne−ln n = 1, which means no elements remain to be covered. The ratio between the greedy algorithm’s solution and the optimal solution varies from input to input but is always less than ln n. And there are c...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Exercises 5.1. Consider the following graph. A B C D E F G H 1 2 2 1 6 5 6 3 3 5 4 5 7 (a) What is the cost of its minimum spanning tree? (b) How many minimum spanning trees does it have? (c) Suppose Kruskal’s algorithm is run on this graph. In what order are the edges added to the MST? For each edge in this sequence, ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
5.7. Show how to find the maximum spanning tree of a graph, that is, the spanning tree of largest total weight. 5.8. Suppose you are given a weighted graph G = (V, E) with a distinguished vertex s and where all edge weights are positive and distinct. Is it possible for a tree of shortest paths from s and a minimum spann...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
(a) Code: {0, 10, 11} (b) Code: {0, 1, 00} (c) Code: {10, 01, 00} 5.16. Prove the following two properties of the Huffman encoding scheme. (a) If some character occurs with frequency more than 2/5, then there is guaranteed to be a codeword of length 1. (b) If all characters occur with frequency less than 1/3, then ther...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
(b) Now consider arbitrary distributions—that is, the probabilities pi are not restricted to pow- ers of 2. The most commonly used measure of the amount of randomness in the distribution is the entropy n X i=1 pi log 1 pi . For what distribution (over n outcomes) is the entropy the largest possible? The smallest possib...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Input: Undirected graph G = (V, E); edge weights we; subset of vertices U ⊂V Output: The lightest spanning tree in which the nodes of U are leaves (there might be other leaves in this tree as well). (The answer isn’t necessarily a minimum spanning tree.) Give an algorithm for this problem which runs in O(|E| log |V |) ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
5.28. A prefix-free encoding of a finite alphabet Γ assigns each symbol in Γ a binary codeword, such that no codeword is a prefix of another codeword. Show that such an encoding can be represented by a full binary tree in which each leaf corre- sponds to a unique element of Γ, whose codeword is generated by the path from ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Chapter 6 Dynamic programming In the preceding chapters we have seen some elegant design principles—such as divide-and- conquer, graph exploration, and greedy choice—that yield definitive algorithms for a variety of important computational tasks. The drawback of these tools is that they can only be used on very specific ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
left-to-right order of Figure 6.1, we can always be sure that by the time we get to a node v, we already have all the information we need to compute dist(v). We are therefore able to compute all distances in a single pass: initialize all dist(·) values to ∞ dist(s) = 0 for each v ∈V \{s}, in linearized order: dist(v) =...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 6.2 The dag of increasing subsequences. 5 2 8 3 9 7 6 6 Notice that (1) this graph G = (V, E) is a dag, since all edges (i, j) have i < j, and (2) there is a one-to-one correspondence between increasing subsequences and paths in this dag. Therefore, our goal is simply to find the longest path in the dag! Here is ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
same bookkeeping device we used for shortest paths in Chapter 4. While computing L(j), we should also note down prev(j), the next-to-last node on the longest path to j. The optimal subsequence can then be reconstructed by following these backpointers. Recursion? No, thanks. Returning to our discussion of longest increa...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Programming? The origin of the term dynamic programming has very little to do with writing code. It was first coined by Richard Bellman in the 1950s, a time when computer programming was an esoteric activity practiced by so few people as to not even merit a name. Back then programming meant “planning,” and “dynamic prog...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 6.3 The subproblem E(7, 5). P L Y N O M A L I O P O N N L A X E E T I For this to work, we need to somehow express E(i, j) in terms of smaller subproblems. Let’s see—what do we know about the best alignment between x[1 · · · i] and y[1 · · · j]? Well, its rightmost column can only be one of three things: x[i] − ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 6.4 (a) The table of subproblems. Entries E(i −1, j −1), E(i −1, j), and E(i, j −1) are needed to fill in E(i, j). (b) The final table of values found by dynamic programming. (a) i j −1 j i −1 m GOAL n (b) P O L Y N O M I A L 0 1 2 3 4 5 6 7 8 9 10 E 1 1 2 3 4 5 6 7 8 9 10 X 2 2 2 3 4 5 6 7 8 9 10 P 3 2 3 3 4 5 6 ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 6.5 The underlying dag, and a path of length 6. P O L Y N O M A L I E X P O N E N T A L I distances are given by shortest paths in the dag! To see this, set all edge lengths to 1, except for {(i −1, j −1) →(i, j) : x[i] = y[j]} (shown dotted in the figure), whose length is 0. The final answer is then simply the di...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Common subproblems Finding the right subproblem takes creativity and experimentation. But there are a few standard choices that seem to arise repeatedly in dynamic programming. i. The input is x1, x2, . . . , xn and a subproblem is x1, x2, . . . , xi. x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 The number of subproblems is therefor...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Of mice and men Our bodies are extraordinary machines: flexible in function, adaptive to new environments, and able to interact and reproduce. All these capabilities are specified by a program unique to each of us, a string that is 3 billion characters long over the alphabet {A, C, G, T}—our DNA. The DNA sequences of any...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
6.4 Knapsack During a robbery, a burglar finds much more loot than he had expected and has to decide what to take. His bag (or “knapsack”) will hold a total weight of at most W pounds. There are n items to pick from, of weight w1, . . . , wn and dollar value v1, . . . , vn. What’s the most valuable combination of items ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
K(0) = 0 for w = 1 to W: K(w) = max{K(w −wi) + vi : wi ≤w} return K(W) This algorithm fills in a one-dimensional table of length W + 1, in left-to-right order. Each entry can take up to O(n) time to compute, so the overall running time is O(nW). As always, there is an underlying dag. Try constructing it, and you will be...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Memoization In dynamic programming, we write out a recursive formula that expresses large problems in terms of smaller ones and then use it to fill out a table of solution values in a bottom-up manner, from smallest subproblem to largest. The formula also suggests a recursive algorithm, but we saw earlier that naive rec...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 6.6 A × B × C × D = (A × (B × C)) × D. (a) × × C D B A × 20 × 1 1 × 10 50 × 20 10 × 100 (b) × A B × C × 50 × 20 20 × 10 D 10 × 100 (c) A × (B × C) × 50 × 10 D 10 × 100 (d) (A × (B × C)) × D 50 × 100 6.5 Chain matrix multiplication Suppose that we want to multiply four matrices, A × B × C × D, of dimensions 50 × ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 6.7 (a) ((A × B) × C) × D; (b) A × ((B × C) × D); (c) (A × (B × C)) × D. D A C B A D B C D A C B (a) (b) (c) number is exponential in n (Exercise 2.13). We certainly cannot try each tree, and with brute force thus ruled out, we turn to dynamic programming. The binary trees of Figure 6.7 are suggestive: for a tre...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 6.8 We want a path from s to t that is both short and has few edges. B D C A S 1 2 1 2 1 4 T 5 3 5 and see how these too can be accommodated by our powerful algorithmic technique. Shortest reliable paths Life is complicated, and abstractions such as graphs, edge lengths, and shortest paths rarely capture the who...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
because it leads right back to the O(|V |2|E|) algorithm. One idea comes to mind: the shortest path u →w1 →· · · →wl →v between u and v uses some number of intermediate nodes—possibly none. Suppose we disallow intermediate nodes altogether. Then we can solve all-pairs shortest paths at once: the shortest path from u to...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 6.9 The optimal traveling salesman tour has length 10. B D C A E 2 2 2 3 4 2 4 2 1 3 before he returns home. Given the pairwise distances between cities, what is the best order in which to visit them, so as to minimize the overall distance traveled? Denote the cities by 1, . . . , n, the salesman’s hometown bein...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
dij. We must pick the best such i: C(S, j) = min i∈S:i̸=j C(S −{j}, i) + dij. The subproblems are ordered by |S|. Here’s the code. C({1}, 1) = 0 for s = 2 to n: for all subsets S ⊆{1, 2, . . . , n} of size s and containing 1: C(S, 1) = ∞ for all j ∈S, j ̸= 1: C(S, j) = min{C(S −{j}, i) + dij : i ∈S, i ̸= j} return minj...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 6.10 The largest independent set in this graph has size 3. 1 2 3 4 5 6 nodes {1, 4, 5} do not, because of the edge between 4 and 5. The largest independent set is {2, 3, 6}. Like several other problems we have seen in this chapter (knapsack, traveling salesman), finding the largest independent set in a graph is b...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 6.11 I(u) is the size of the largest independent set of the subtree rooted at u. Two cases: either u is in this independent set, or it isn’t. r u Exercises 6.1. A contiguous subsequence of a list S is a subsequence made up of consecutive elements of S. For instance, if S is 5, 15, −30, 10, −5, 40, 10, then 15, −...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Give an efficient algorithm to compute the maximum expected total profit subject to the given constraints. 6.4. You are given a string of n characters s[1 . . . n], which you believe to be a corrupted text document in which all punctuation has vanished (so that it looks something like “itwasthebestoftimes...”). You wish ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
6.8. Given two strings x = x1x2 · · · xn and y = y1y2 · · · ym, we wish to find the length of their longest common substring, that is, the largest k for which there are indices i and j with xixi+1 · · · xi+k−1 = yjyj+1 · · · yj+k−1. Show how to do this in time O(mn). 6.9. A certain string-processing language offers a pr...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
maximum sum of selling prices. You are free to make as many copies of a given product as you wish, or none if desired. 6.15. Suppose two teams, A and B, are playing a match to see who is the first to win n games (for some particular n). We can suppose that A and B are equally competent, so each has a 50% chance of winni...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 6.12 Two binary search trees for the keywords of a programming language. end begin else if while do then do then while begin if else end end 4% if 10% then 10% while 23% We want to organize them in a binary search tree, so that the keyword in the root is alphabetically bigger than all the keywords in the left su...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
6.22. Give an O(nt) algorithm for the following task. Input: A list of n positive integers a1, a2, . . . , an; a positive integer t. Question: Does some subset of the ai’s add up to t? (You can use each ai at most once.) (Hint: Look at subproblems of the form “does a subset of {a1, a2, . . . , ai} add up to s?” ) 6.23....
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
6.26. Sequence alignment. When a new gene is discovered, a standard approach to understanding its function is to look through a database of known genes and find close matches. The closeness of two genes is measured by the extent to which they are aligned. To formalize this, think of a gene as being a long string over an...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
6.30. Reconstructing evolutionary trees by maximum parsimony. Suppose we manage to sequence a particular gene across a whole bunch of different species. For concreteness, say there are n species, and the sequences are strings of length k over alphabet Σ = {A, C, G, T}. How can we use this information to reconstruct the...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Chapter 7 Linear programming and reductions Many of the problems for which we want algorithms are optimization tasks: the shortest path, the cheapest spanning tree, the longest increasing subsequence, and so on. In such cases, we seek a solution that (1) satisfies certain constraints (for instance, the path must use edg...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 7.1 (a) The feasible region for a linear program. (b) Contour lines of the objective function: x1 + 6x2 = c for different values of the profit c. (a)                     ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
It is a general rule of linear programs that the optimum is achieved at a vertex of the feasible region. The only exceptions are cases in which there is no optimum; this can happen in two ways: 1. The linear program is infeasible; that is, the constraints are so tight that it is impossible to satisfy all of them. For i...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 7.2 The feasible polyhedron for a three-variable linear program. x1 x3 x2 Optimum Let x1, x2, x3 denote the number of boxes of each chocolate produced daily, with x3 referring to Luxe. The old constraints on x1 and x2 persist, although the labor restriction now extends to x3 as well: the sum of all three variabl...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
the feasible region. The point of final contact is the optimal vertex: (0, 300, 100), with total profit $3100. How would the simplex algorithm behave on this modified problem? As before, it would move from vertex to vertex, along edges of the polyhedron, increasing profit steadily. A possi- ble trajectory is shown in Figur...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
How can we handle the fluctuations in demand? There are three ways: 1. Overtime, but this is expensive since overtime pay is 80% more than regular pay. Also, workers can put in at most 30% overtime. 2. Hiring and firing, but these cost $320 and $400, respectively, per worker. 3. Storing surplus production, but this costs...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 7.3 A communications network between three users A, B, and C. Bandwidths are shown. a c b 12 10 6 13 11 8 user A user B user C Well, almost. The optimum solution might turn out to be fractional; for instance, it might involve hiring 10.6 workers in the month of March. This number would have to be rounded to eith...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
bandwidth is exceeded and that each connection gets a bandwidth of at least 2 units. max 3xAB + 3x′ AB + 2xBC + 2x′ BC + 4xAC + 4x′ AC xAB + x′ AB + xBC + x′ BC ≤10 [edge (b, B)] xAB + x′ AB + xAC + x′ AC ≤12 [edge (a, A)] xBC + x′ BC + xAC + x′ AC ≤8 [edge (c, C)] xAB + x′ BC + x′ AC ≤6 [edge (a, b)] x′ AB + xBC + x′ ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Reductions Sometimes a computational task is sufficiently general that any subroutine for it can also be used to solve a variety of other tasks, which at first glance might seem unrelated. For instance, we saw in Chapter 6 how an algorithm for finding the longest path in a dag can, surprisingly, also be used for finding lo...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
We will now show that these various LP options can all be reduced to one another via simple transformations. Here’s how. 1. To turn a maximization problem into a minimization (or vice versa), just multiply the coefficients of the objective function by −1. 2a. To turn an inequality constraint like Pn i=1 aixi ≤b into an ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Matrix-vector notation A linear function like x1 + 6x2 can be written as the dot product of two vectors c = 1 6  and x = x1 x2  , denoted c · x or cT x. Similarly, linear constraints can be compiled into matrix-vector form: x1 ≤ 200 x2 ≤ 300 x1 + x2 ≤ 400 =⇒   1 0 0 1 1 1   | {z } x1 x2  ≤   200 300 400  ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 7.4 (a) A network with edge capacities. (b) A flow in the network. (a) s a b c d e t 3 3 4 10 1 2 5 5 2 1 1 (b) s a b c d e t 5 2 0 1 0 2 1 4 5 2 1 u: X (w,u)∈E fwu = X (u,z)∈E fuz. In other words, flow is conserved. The size of a flow is the total quantity sent from s to t and, by the conservation principle, is eq...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 7.5 An illustration of the max-flow algorithm. (a) A toy network. (b) The first path chosen. (c) The second path chosen. (d) The final flow. (e) We could have chosen this path first. (f) In which case, we would have to allow this second path. (a) s b a t 1 1 1 1 1 (b) s a t (c) s b t (d) s b a t 1 1 1 1 0 (e) s b a t...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
cf:  cuv −fuv if (u, v) ∈E and fuv < cuv fvu if (v, u) ∈E and fvu > 0 Thus we can equivalently think of simplex as choosing an s −t path in the residual network. By simulating the behavior of simplex, we get a direct algorithm for solving max-flow. It proceeds in iterations, each time explicitly constructing Gf, finding...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
L R t s e′ e We claim that size(f) = capacity(L, R). To see this, observe that by the way L is defined, any edge going from L to R must be at full capacity (in the current flow f), and any edge from R to L must have zero flow. (So, in the figure, fe = ce and fe′ = 0.) Therefore the net flow across (L, R) is exactly the capa...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 7.6 The max-flow algorithm applied to the network of Figure 7.4. At each iteration, the current flow is shown on the left and the residual network on the right. The paths chosen are shown in bold. Current flow Residual graph (a) s a b c d e t s a b c d e t 3 3 4 10 1 2 1 5 1 2 5 (b) s a b c d e t 1 1 1 1 1 s a b c ...
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 7.6 Continued Current Flow Residual Graph (e) s a b c d e t 1 2 2 5 4 5 1 s a b c d e t 3 10 1 1 1 2 2 1 5 4 5 1 1 (f) s a b c d e t 1 2 2 5 4 5 2 1 1 s a b c d e t 10 1 1 1 2 2 5 4 5 2 2 1 1 205
http://algorithmics.lsi.upc.edu/docs/Dasgupta-Papadimitriou-Vazirani.pdf
Dasgupta-Papadimitriou-Vazirani.pdf
true
318
Figure 7.7 An edge between two people means they like each other. Is it possible to pair everyone up happily? Alice Beatrice Carol Danielle GIRLS Chet Dan Bob Al BOYS 7.3 Bipartite matching Figure 7.7 shows a graph with four nodes on the left representing boys and four nodes on the right representing girls.1 There is a...