Algorithms (VI) Greedy Algorithms. Guoqiang Li. School of Software, Shanghai Jiao Tong University

Size: px
Start display at page:

Download "Algorithms (VI) Greedy Algorithms. Guoqiang Li. School of Software, Shanghai Jiao Tong University"

Transcription

1 Algorithms (VI) Greedy Algorithms Guoqiang Li School of Software, Shanghai Jiao Tong University

2 Review of the Previous Lecture

3 Lengths on Edges BFS treats all edges as having the same length. It is rarely true in applications where shortest paths are to be found. Every edge e E with a length l e. If e = (u, v), we will sometimes also write l(u, v) or l uv

4 Alarm Clocks Set an alarm clock for node s at time 0. Repeat until there are no more alarms: Say the next alarm goes off at time T, for node y. Then: The distance from s to u is T. For each neighbor v of u in G: If there is no alarm yet for v, set one for time T + l(u, v). If v s alarm is set for later than T + l(u, v), then reset it to this earlier time. G: G :

5 Priority Queue Priority queue is a data structure usually implemented by heap. Insert: Add a new element to the set. Decrease-key: Accommodate the decrease in key value of a particular element. Delete-min: Return the element with the smallest key, and remove it from the set. Make-queue: Build a priority queue out of the given elements, with the given key values. (In many implementations, this is significantly faster than inserting the elements one by one.) The first two let us set alarms, and the third tells us which alarm is next to go off.

6 Dijkstra s Shortest-Path Algorithm DIJKSTRA(G, l, s) input : Graph G = (V, E), directed or undirected; positive edge length {l e e E}; Vertex s V output: For all vertices u reachable from s, dist(u) is the set to the distance from s to u for all u V do dist(u) = ; prev(u) = nil; end dist(s) = 0; H =makequeue(v)\\ using dist-values as keys; while H is not empty do u=deletemin(h); for all edge (u, v) E do if dist(v) > dist(u) + l(u, v) then dist(v) = dist(u) + l(u, v); prev(v) = u; decreasekey (H,v); end end end

7 An Alternative Derivation Initialize dist(s) = 0, other dist(.) to ; R = { }\\ the known region ; while R V do Pick the node v R with smallest dist(.); Add v to R; for all edge (v, z) E do if dist(z) > dist(v) + l(v, z) then dist[z] = dist[v] + l(v, z); end end end

8 Running Time Since makequeue takes at most as long as V insert operations, we get a total of V deletemin and V + E insert/decreasekey operations. The time needed for these varies by implementation; for instance, a binary heap gives an overall running time of O(( V + E ) log V )

9 Which Heap is Best Implementation deletemin insert/decreasekey Array O( V ) O(1) Binary heap O(log V ) O(log V ) d-ary heap d log V log V O( ) O( ) log d log d Fibonacci heap O(log V ) O(1) (amortized) V deletemin+( V + E ) insert O( V 2 ) O(( V + E ) log V ) (d V + E ) log V O( ) log d O( V log V + E )

10 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 1. An insert or decreasekey is fast, because it just involves adjusting a key value, an O(1) operation. To deletemin, on the other hand, requires a linear-time scan of the list.

11 Binary Heap To insert, place the new element at the bottom of the tree (in the first available position), and let it bubble up. The number of swaps is at most the height of the tree log 2 n, when there are n elements. A decreasekey is similar, except the element is already in the tree, so we let it bubble up from its current position. To deletemin, return the root value. To then remove this element from the heap, take the last node in the tree (in the rightmost position in the bottom row) and place it at the root. Then let it shift down. Again this takes O(log n) time.

12 The Implementation of Binary Heap The regularity of a complete binary tree makes it easy to represent using an array. The tree nodes have a natural ordering: row by row, starting at the root and moving left to right within each row. If there are n nodes, this ordering specifies their positions 1, 2,..., n within the array. Moving up and down the tree is easily simulated on the array, using the fact that node number j has parent j/2 and children 2j and 2j + 1.

13 d-ary heap A d-ary heap is identical to a binary heap, except that nodes have d children. This reduces the height of a tree with n elements to Θ(log d n) = Θ((log n)/(log d)) Inserts are therefore speeded up by a factor of Θ(log d). Deletemin operations, however, take a little longer, namely O(d log d n).

14 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 v. This no longer holds when edge lengths can be negative. Q: What needs to be changed in order to accommodate this new complication? A crucial invariant of Dijkstra s algorithm is that the dist values it maintains are always either overestimates or exactly correct. They start off at, and the only way they ever change is by updating along an edge: UPDATE ((u, v) E) dist(v) = min{dist(v), dist(u) + l(u, v)};

15 Update UPDATE ((u, v) E) dist(v) = min{dist(v), dist(u) + l(u, v)}; Let s u 1 u 2 u 3... u k k be a shortest path from s to t. This path can have at most V 1 edges (why?). If the sequence of updates performed includes (s, u 1 ), (u 1, u 2 ),..., (u k, t), in that order (though not necessarily consecutively), then by 1 the distance to t will be correctly computed. It doesn t matter what other updates occur on these edges, or what happens in the rest of the graph, because updates are safe.

16 Bellman-Ford Algorithm SHORTEST-PATHS(G, l, s) input : Graph G = (V, E), edge length{l e e E}; Vertex s V output: For all vertices u reachable from s, dist(u) is the set to the distance from s to u for all u V do dist(u) = ; prev(u) = nil; end dists = 0; repeat V 1 times: for e E do UPDATE(e); end Running time:o( V E )

17 Graphs without Negative Edges There are two subclasses of graphs that automatically exclude the possibility of negative cycles: graphs without negative edges, and graphs without cycles. We already know how to efficiently handle the former. We will now see how the single-source shortest-path problem can be solved in just linear time on directed acyclic graphs. As before, we need to perform a sequence of updates that includes every shortest path as a subsequence. In any path of a dag, the vertices appear in increasing linearized order.

18 A Shortest-Path Algorithm for DAG DAG-SHORTEST-PATHS(G, l, s) input : Graph G = (V, E), edge length{l e e E}; Vertex s V output: For all vertices u reachable from s, dist(u) is the set to the distance from s to u for all u V do dist(u) = ; prev(u) = nil; end dists = 0; linearize G; for each u V in linearized order do for all e E do UPDATE(e); end end

19 Chapter V. Greedy Algorithms

20 Build a Network Suppose you are asked to network a collection of computers by linking selected pairs of them. 4 A C E B 4 D 6 F

21 Build a Network Suppose you are asked to network a collection of computers by linking selected pairs of them. This translates into a graph problem in which 4 A C E B 4 D 6 F

22 Build a Network Suppose you are asked to network a collection of computers by linking selected pairs of them. This translates into a graph problem in which nodes are computers, 4 A C E B 4 D 6 F

23 Build a Network Suppose you are asked to network a collection of computers by linking selected pairs of them. This translates into a graph problem in which nodes are computers, undirected edges are potential links, each with a maintenance cost. 4 A C E B 4 D 6 F

24 Build a Network The goal is to A 1 C E B 4 D F

25 Build a Network The goal is to pick enough of these edges that the nodes are connected, the total maintenance cost is minimum. A 1 C E B 4 D F

26 Build a Network The goal is to pick enough of these edges that the nodes are connected, the total maintenance cost is minimum. One immediate observation is that the optimal set of edges cannot contain a cycle. A 1 C E B 4 D F

27 Properties of the Optimal Solutions Lemma (1) Removing a cycle edge cannot disconnect a graph.

28 Properties of the Optimal Solutions Lemma (1) Removing a cycle edge cannot disconnect a graph. So the solution must be connected and acyclic: undirected graphs of this kind are called trees.

29 Properties of the Optimal Solutions Lemma (1) Removing a cycle edge cannot disconnect a graph. So the solution must be connected and acyclic: undirected graphs of this kind are called trees. A tree with minimum total weight, is a minimum spanning tree, MST.

30 Properties of the Optimal Solutions Lemma (1) Removing a cycle edge cannot disconnect a graph. So the solution must be connected and acyclic: undirected graphs of this kind are called trees. A tree with minimum total weight, is a minimum spanning tree, MST. Input: An undirected graph G = (V, E); edge weights w e

31 Properties of the Optimal Solutions Lemma (1) Removing a cycle edge cannot disconnect a graph. So the solution must be connected and acyclic: undirected graphs of this kind are called trees. A tree with minimum total weight, is a minimum spanning tree, MST. Input: An undirected graph G = (V, E); edge weights w e Output: A tree T = (V, E ) with E E that minimizes weight(t) = e E w e

32 Trees Lemma (2) A tree on n nodes has n 1 edges.

33 Trees Lemma (2) A tree on n nodes has n 1 edges. To build the tree one edge at a time, starting from an empty graph.

34 Trees Lemma (2) A tree on n nodes has n 1 edges. To build the tree one edge at a time, starting from an empty graph. Each of the n nodes is disconnected from the others, in a connected component by itself.

35 Trees Lemma (2) A tree on n nodes has n 1 edges. To build the tree one edge at a time, starting from an empty graph. Each of the n nodes is disconnected from the others, in a connected component by itself. As edges are added, these components merge. Since each edge unites two different components, exactly n 1 edges are added by the time the tree is fully formed.

36 Trees Lemma (2) A tree on n nodes has n 1 edges. To build the tree one edge at a time, starting from an empty graph. Each of the n nodes is disconnected from the others, in a connected component by itself. As edges are added, these components merge. Since each edge unites two different components, exactly n 1 edges are added by the time the tree is fully formed. When a particular edge (u, v) comes up, we can be sure that u and v lie in separate connected components, for otherwise there would already be a path between them and this edge would create a cycle.

37 Trees Lemma (3) Any connected, undirected graph G = (V, E) with E = V 1 is a tree.

38 Trees Lemma (3) Any connected, undirected graph G = (V, E) with E = V 1 is a tree. It is the converse of Lemma (2). We just need to show that G is acyclic.

39 Trees Lemma (3) Any connected, undirected graph G = (V, E) with E = V 1 is a tree. It is the converse of Lemma (2). We just need to show that G is acyclic. While the graph contains a cycle, remove one edge from this cycle.

40 Trees Lemma (3) Any connected, undirected graph G = (V, E) with E = V 1 is a tree. It is the converse of Lemma (2). We just need to show that G is acyclic. While the graph contains a cycle, remove one edge from this cycle. The process terminates with some graph G = (V, E ), E E, which is acyclic and, by Lemma (1), is also connected.

41 Trees Lemma (3) Any connected, undirected graph G = (V, E) with E = V 1 is a tree. It is the converse of Lemma (2). We just need to show that G is acyclic. While the graph contains a cycle, remove one edge from this cycle. The process terminates with some graph G = (V, E ), E E, which is acyclic and, by Lemma (1), is also connected. Therefore G is a tree, whereupon E = V 1 by Lemma (2). So E = E, no edges were removed, and G was acyclic to start with.

42 Trees Lemma (4) An undirected graph is a tree if and only if there is a unique path between any pair of nodes.

43 Trees Lemma (4) An undirected graph is a tree if and only if there is a unique path between any pair of nodes. In a tree, any two nodes can only have one path between them; for if there were two paths, the union of these paths would contain a cycle.

44 Trees Lemma (4) An undirected graph is a tree if and only if there is a unique path between any pair of nodes. In a tree, any two nodes can only have one path between them; for if there were two paths, the union of these paths would contain a cycle. On the other hand, if a graph has a path between any two nodes, then it is connected. If these paths are unique, then the graph is also acyclic.

45 A Greedy Approach

46 A Greedy Approach Kruskal s minimum spanning tree algorithm starts with the empty graph and then selects edges from E according to the following rule.

47 A Greedy Approach Kruskal s minimum spanning tree algorithm starts with the empty graph and then selects edges from E according to the following rule. Repeatedly add the next lightest edge that doesn t produce a cycle.

48 A Greedy Approach Kruskal s minimum spanning tree algorithm starts with the empty graph and then selects edges from E according to the following rule. Repeatedly add the next lightest edge that doesn t produce a cycle. Example: Starting with an empty graph and then attempt to add edges in increasing order of weight B C; C D; B D; C F; D F; E F; A D; A B; C E; A C A 6 5 C E A C E B 2 D 4 F B D F

49 The Cut Property Lemma: Suppose edges X are part of a minimum spanning tree (MST) of G = (V, E). Pick any subset of nodes S for which X does not cross between S and V\S, and let e be the lightest edge across this partition. Then X {e} is part of some MST.

50 The Cut Property A cut is any partition of the vertices into two groups, S and V\S. S V S e e

51 The Cut Property A cut is any partition of the vertices into two groups, S and V\S. The cut property says that it is always safe to add the lightest edge across any cut (that is, between a vertex in S and one in V\S), provided X has no edges across the cut. S e e V S

52 Proof of the Cut Property Edges X are part of some MST T;

53 Proof of the Cut Property Edges X are part of some MST T; if the new edge e also happens to be part of T, then there is nothing to prove.

54 Proof of the Cut Property Edges X are part of some MST T; if the new edge e also happens to be part of T, then there is nothing to prove. So assume e is not in T.

55 Proof of the Cut Property Edges X are part of some MST T; if the new edge e also happens to be part of T, then there is nothing to prove. So assume e is not in T. We will construct a different MST T containing X {e} by altering T slightly, changing just one of its edges.

56 Proof of the Cut Property Edges X are part of some MST T; if the new edge e also happens to be part of T, then there is nothing to prove. So assume e is not in T. We will construct a different MST T containing X {e} by altering T slightly, changing just one of its edges. Add edge e to T.

57 Proof of the Cut Property Edges X are part of some MST T; if the new edge e also happens to be part of T, then there is nothing to prove. So assume e is not in T. We will construct a different MST T containing X {e} by altering T slightly, changing just one of its edges. Add edge e to T. Since T is connected, it already has a path between the endpoints of e, so adding e creates a cycle.

58 Proof of the Cut Property Edges X are part of some MST T; if the new edge e also happens to be part of T, then there is nothing to prove. So assume e is not in T. We will construct a different MST T containing X {e} by altering T slightly, changing just one of its edges. Add edge e to T. Since T is connected, it already has a path between the endpoints of e, so adding e creates a cycle. This cycle must also have some other edge e across the cut (S, V\S).

59 Proof of the Cut Property Edges X are part of some MST T; if the new edge e also happens to be part of T, then there is nothing to prove. So assume e is not in T. We will construct a different MST T containing X {e} by altering T slightly, changing just one of its edges. Add edge e to T. Since T is connected, it already has a path between the endpoints of e, so adding e creates a cycle. This cycle must also have some other edge e across the cut (S, V\S). If we now remove e T = T {e}\{e } which we will show to be a tree.

60 Proof of the Cut Property Edges X are part of some MST T; if the new edge e also happens to be part of T, then there is nothing to prove. So assume e is not in T. We will construct a different MST T containing X {e} by altering T slightly, changing just one of its edges. Add edge e to T. Since T is connected, it already has a path between the endpoints of e, so adding e creates a cycle. This cycle must also have some other edge e across the cut (S, V\S). If we now remove e T = T {e}\{e } which we will show to be a tree. T is connected by Lemma (1), since e is a cycle edge.

61 Proof of the Cut Property Edges X are part of some MST T; if the new edge e also happens to be part of T, then there is nothing to prove. So assume e is not in T. We will construct a different MST T containing X {e} by altering T slightly, changing just one of its edges. Add edge e to T. Since T is connected, it already has a path between the endpoints of e, so adding e creates a cycle. This cycle must also have some other edge e across the cut (S, V\S). If we now remove e T = T {e}\{e } which we will show to be a tree. T is connected by Lemma (1), since e is a cycle edge. And it has the same number of edges as T; so by Lemma (2) and Lemma (3), it is also a tree.

62 Proof of the Cut Property T is a minimum spanning tree, since

63 Proof of the Cut Property T is a minimum spanning tree, since weight(t ) = weight(t) + w(e) w(e )

64 Proof of the Cut Property T is a minimum spanning tree, since weight(t ) = weight(t) + w(e) w(e ) Both e and e cross between S and V\S, and e is the lightest edge of this type.

65 Proof of the Cut Property T is a minimum spanning tree, since weight(t ) = weight(t) + w(e) w(e ) Both e and e cross between S and V\S, and e is the lightest edge of this type. Therefore w(e) w(e ), and weight(t ) weight(t)

66 Proof of the Cut Property T is a minimum spanning tree, since weight(t ) = weight(t) + w(e) w(e ) Both e and e cross between S and V\S, and e is the lightest edge of this type. Therefore w(e) w(e ), and weight(t ) weight(t) Since T is an MST, it must be the case that weight(t ) = weight(t) and that T is also an MST.

67 An Example of Cut Property (a) A 1 C 3 E 2 B D 4 F (b) A C E A C E Edges X: MST T: B D F B D F (c) The cut: A B C D e E F MST T : A B C D E F S V S

68 Kruskal s Algorithm KRUSKAL(G, w) input : A connected undirected graph G = (V, E), with edge weight w e output: A minimum spanning tree defined by the edges X for all u V do makeset (u); end X = { }; Sort the edges E by weight; for all (u, v) E in increasing order of weight do if find (u) find (v) then add (u, v) to X; union (u,v) end end

69 Kruskal s Algorithm makeset(x) create a singleton set containing x V find(x) find the set that x belong to 2 E union(x, y) merge the sets containing x and y V 1

70 Data Structure: Disjoint Sets

71 A Data Structure for Disjoint Sets Union by rank We store a set is by a directed tree.

72 A Data Structure for Disjoint Sets Union by rank We store a set is by a directed tree. Nodes of the tree are elements of the set, arranged in no particular order, and each has parent pointers that eventually lead up to the root of the tree.

73 A Data Structure for Disjoint Sets Union by rank We store a set is by a directed tree. Nodes of the tree are elements of the set, arranged in no particular order, and each has parent pointers that eventually lead up to the root of the tree. This root element is a convenient representative, or name, for the set.

74 A Data Structure for Disjoint Sets Union by rank We store a set is by a directed tree. Nodes of the tree are elements of the set, arranged in no particular order, and each has parent pointers that eventually lead up to the root of the tree. This root element is a convenient representative, or name, for the set. It is distinguished from the other elements by the fact that its parent pointer is a self-loop.

75 A Data Structure for Disjoint Sets Union by rank We store a set is by a directed tree. Nodes of the tree are elements of the set, arranged in no particular order, and each has parent pointers that eventually lead up to the root of the tree. This root element is a convenient representative, or name, for the set. It is distinguished from the other elements by the fact that its parent pointer is a self-loop. In addition to a parent pointer π, each node also has a rank that, for the time being, should be interpreted as the height of the subtree hanging from that node.

76 Union by Rank MAKESET(x) π(x) = x; rank (x)=0;

77 Union by Rank MAKESET(x) π(x) = x; rank (x)=0; FIND(x) while x π(x) do x = π(x); end return(x);

78 Union by Rank MAKESET(x) π(x) = x; rank (x)=0; FIND(x) while x π(x) do x = π(x); end return(x); MAKESET is a constant-time operation.

79 Union by Rank MAKESET(x) π(x) = x; rank (x)=0; FIND(x) while x π(x) do x = π(x); end return(x); MAKESET is a constant-time operation. FIND follows parent pointers to the root of the tree and therefore takes time proportional to the height of the tree.

80 Union by Rank MAKESET(x) π(x) = x; rank (x)=0; FIND(x) while x π(x) do x = π(x); end return(x); MAKESET is a constant-time operation. 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 trees shallow.

81 Union UNION(x, y) r x =find(x); r y =find(y); if r x = r y then return; if rank(r x )>rank(r y ) then π(r y ) = r x ; end else π(r x ) = r y ; if rank(r x )=rank(r y ) then rank(r y )=rank(r y )+1; end

82 An Example Aftermakeset(A),makeset(B),...,makeset(G): A 0 B 0 C 0 D 0 E 0 F 0 0 G After union(a, D), union(b, E), union(c, F ): D 1 E 1 F 1 G 0 A 0 B 0 C 0

83 An Example After union(c, G), union(e, A): D 2 F 1 E 1 A 0 0 C 0 G B 0 After union(b, G): D 2 E 1 F 1 A 0 B 0 C 0 G 0

84 Properties Lemma (1) For any non-root x, rank(x) < rank(π(x)).

85 Properties Lemma (1) For any non-root x, rank(x) < rank(π(x)). Proof Sketch: By design, the rank of a node is exactly the height of the subtree rooted at that node. This means, for instance, that as you move up a path toward a root node, the rank values along the way are strictly increasing.

86 Properties Lemma (2) Any root node of rank k has least 2 k nodes in its tree.

87 Properties Lemma (2) Any root node of rank k has least 2 k nodes in its tree. Proof Sketch: A root node with rank k is created by the merger of two trees with roots of rank k 1. By induction to get the results.

88 Properties Lemma (3) If there are n elements overall, there can be at most n/2 k nodes of rank k.

89 Properties Lemma (3) If there are n elements overall, there can be at most n/2 k nodes of rank k. Proof Sketch:

90 Properties Lemma (3) If there are n elements overall, there can be at most n/2 k nodes of rank k. Proof Sketch: A node of rank k has at least 2 k descendants. Any internal node was once a root, and neither its rank nor its set of descendants has changed since then. Different rank-k nodes cannot have common descendants. Any element has at most one ancestor of rank k.

91 The Analysis With the data structure as presented so far, the total time for Kruskal s algorithm becomes

92 The Analysis With the data structure as presented so far, the total time for Kruskal s algorithm becomes O( E log V ) for sorting the edges (log V log E ),

93 The Analysis With the data structure as presented so far, the total time for Kruskal s algorithm becomes O( E log V ) for sorting the edges (log V log E ), O( E log V ) for the union and find operations that dominate the rest of the algorithm.

94 The Analysis With the data structure as presented so far, the total time for Kruskal s algorithm becomes O( E log V ) for sorting the edges (log V log E ), O( E log V ) for the union and find operations that dominate the rest of the algorithm. But what if the edges are given to us sorted? Or if the weights are small (say, O( E )) so that sorting can be done in linear time?

95 The Analysis With the data structure as presented so far, the total time for Kruskal s algorithm becomes O( E log V ) for sorting the edges (log V log E ), O( E log V ) for the union and find operations that dominate the rest of the algorithm. But what if the edges are given to us sorted? Or if the weights are small (say, O( E )) so that sorting can be done in linear time? Then the data structure part becomes the bottleneck!

96 The Analysis With the data structure as presented so far, the total time for Kruskal s algorithm becomes O( E log V ) for sorting the edges (log V log E ), O( E log V ) for the union and find operations that dominate the rest of the algorithm. But what if the edges are given to us sorted? Or if the weights are small (say, O( E )) so that sorting can be done in linear time? Then the data structure part becomes the bottleneck! The main question:

97 The Analysis With the data structure as presented so far, the total time for Kruskal s algorithm becomes O( E log V ) for sorting the edges (log V log E ), O( E log V ) for the union and find operations that dominate the rest of the algorithm. But what if the edges are given to us sorted? Or if the weights are small (say, O( E )) so that sorting can be done in linear time? Then the data structure part becomes the bottleneck! The main question: How can we perform union s and find s faster than log n?

98 Path Compression FIND(x) if x π(x) then π(x)=find(π(x)); return(π(x));

99 Path Compression A 3 A 3 B 0 C 1 E 2 B 0 C 1 E 2 F 1 I 0 D 0 1 F G 1 H 0 0 D G 1 H 0 J 0 I 0 J 0 K 0 K 0 3 A B 0 C 1 E 2 F 1 I 0 K 0 G 1 0 D H 0 J 0

100 Path Compression

101 Path Compression The benefit of this simple alteration is long-term rather than instantaneous and thus necessitates a particular kind of analysis.

102 Path Compression The benefit of this simple alteration is long-term rather than instantaneous and thus necessitates a particular kind of analysis. we need to look at sequences of find and union operations, starting from an empty data structure, and determine the average time per operation.

103 Path Compression The benefit of this simple alteration is long-term rather than instantaneous and thus necessitates a particular kind of analysis. we need to look at sequences of find and union operations, starting from an empty data structure, and determine the average time per operation. This amortized cost turns out to be just barely more than O(1), down from the earlier O(log n).

104 Prim s Algorithm

105 A General Kruskal s Algorithm X = { }; repeat until X = V 1; pick a set S V for which X has no edges between S and V S; let e E be the minimum-weight edge between S and V S; X = X {e};

106 Prim s Algorithm A popular alternative to Kruskal s algorithm is Prim s, in which the intermediate set of edges X always forms a subtree, and S is chosen to be the set of this tree s vertices.

107 Prim s Algorithm A popular alternative to Kruskal s algorithm is Prim s, in which the intermediate set of edges X always forms a subtree, and S is chosen to be the set of this tree s vertices. On each iteration, the subtree defined by X grows by one edge, namely, the lightest edge between a vertex in S and a vertex outside S. We can equivalently think of S as growing to include the vertex v S of smallest cost: cost(v) = min w(u, v) u S

108 The Algorithm PRIM(G, w) input : A connected undirected graph G = (V, E), with edge weights w e output: A minimum spanning tree defined by the array prev for all u V do cost(u) = ; prev(u) = nil; end pick any initial node u 0; cost(u 0) = 0; H =makequeue(v)\\ using cost-values as keys; while H is not empty do v=deletemin(h); for each (v, z) E do if cost(z) > w(v, z) then cost(v) = w(v, z); prev(z) = v; decreasekey (H,z); end end end

109 Dijkstra s Algorithm DIJKSTRA(G, l, s) input : Graph G = (V, E), directed or undirected; positive edge length {l e e E}; Vertex s V output: For all vertices u reachable from s, dist(u) is the set to the distance from s to u for all u V do dist(u) = ; prev(u) = nil; end dist(s) = 0; H =makequeue(v)\\ using dist-values as keys; while H is not empty do u=deletemin(h); for all edge (u, v) E do if dist(v) > dist(u) + l(u, v) then dist(v) = dist(u) + l(u, v); prev(v) = u; decreasekey (H,v); end end end

110 Huffman Encoding

111 MP3 Audio Compression 1 It is digitized by sampling at regular intervals, yielding a sequence of real numbers s 1, s 2,..., s T.

112 MP3 Audio Compression 1 It is digitized by sampling at regular intervals, yielding a sequence of real numbers s 1, s 2,..., s T. 2 Each real-valued sample s t is quantized: approximated by a nearby number from a finite set Γ This set is carefully chosen to exploit human perceptual limitations, with the intention that the approximating sequence is indistinguishable from s 1, s 2,..., s T by the human ear.

113 MP3 Audio Compression 1 It is digitized by sampling at regular intervals, yielding a sequence of real numbers s 1, s 2,..., s T. 2 Each real-valued sample s t is quantized: approximated by a nearby number from a finite set Γ This set is carefully chosen to exploit human perceptual limitations, with the intention that the approximating sequence is indistinguishable from s 1, s 2,..., s T by the human ear. 3 The resulting string of length T over alphabet Γ is encoded in binary.

114 Binary Encoding Let s look at a toy example in which T is 130 million and the alphabet Γ consists of just four values, denoted by the symbols A, B, C, D.

115 Binary Encoding Let s look at a toy example in which T is 130 million and the alphabet Γ consists of just four values, denoted by the symbols A, B, C, D. Q: What is the most economical way to write this long string in binary?

116 Binary Encoding Let s look at a toy example in which T is 130 million and the alphabet Γ consists of just four values, denoted by the symbols A, B, C, D. Q: What is the most economical way to write this long string in binary? The obvious choice is to use 2 bits per symbol.

117 Binary Encoding Let s look at a toy example in which T is 130 million and the alphabet Γ consists of just four values, denoted by the symbols A, B, C, D. Q: What is the most economical way to write this long string in binary? The obvious choice is to use 2 bits per symbol. codeword 00 for A, 01 for B, 10 for C, and 11 for D

118 Binary Encoding Let s look at a toy example in which T is 130 million and the alphabet Γ consists of just four values, denoted by the symbols A, B, C, D. Q: What is the most economical way to write this long string in binary? The obvious choice is to use 2 bits per symbol. codeword 00 for A, 01 for B, 10 for C, and 11 for D 260 megabits are needed in total.

119 Binary Encoding Let s look at a toy example in which T is 130 million and the alphabet Γ consists of just four values, denoted by the symbols A, B, C, D. Q: What is the most economical way to write this long string in binary? The obvious choice is to use 2 bits per symbol. codeword 00 for A, 01 for B, 10 for C, and 11 for D 260 megabits are needed in total. Q: Can there possibly be a better encoding than this?

120 Binary Encoding In search of inspiration, we take a closer look at our particular sequence and find that the four symbols are not equally abundant.

121 Binary Encoding In search of inspiration, we take a closer look at our particular sequence and find that the four symbols are not equally abundant. Symbol A B C D Frequency 70 million 3 million 20 million 37 million

122 Binary Encoding In search of inspiration, we take a closer look at our particular sequence and find that the four symbols are not equally abundant. Symbol A B C D Frequency 70 million 3 million 20 million 37 million Is there some sort of variable-length encoding, in which just one bit is used for the frequently occurring symbol A, possibly at the expense of needing three or more bits for less common symbols?

123 Binary Encoding In search of inspiration, we take a closer look at our particular sequence and find that the four symbols are not equally abundant. Symbol A B C D Frequency 70 million 3 million 20 million 37 million Is there some sort of variable-length encoding, in which just one bit is used for the frequently occurring symbol A, possibly at the expense of needing three or more bits for less common symbols? A danger with having codewords of different lengths is that the resulting encoding may not be uniquely decipherable.

124 Binary Encoding In search of inspiration, we take a closer look at our particular sequence and find that the four symbols are not equally abundant. Symbol A B C D Frequency 70 million 3 million 20 million 37 million Is there some sort of variable-length encoding, in which just one bit is used for the frequently occurring symbol A, possibly at the expense of needing three or more bits for less common symbols? A danger with having codewords of different lengths is that the resulting encoding may not be uniquely decipherable. {0, 01, 11, 001}

125 Prefix-Free Encoding We will avoid this problem by insisting on the prefix-free property: no codeword can be a prefix of another codeword.

126 Prefix-Free Encoding We will avoid this problem by insisting on the prefix-free property: no codeword can be a prefix of another codeword. Any prefix-free encoding can be represented by a full binary tree - a binary tree in which every node has either zero or two children.

127 Prefix-Free Encoding We will avoid this problem by insisting on the prefix-free property: no codeword can be a prefix of another codeword. Any prefix-free encoding can be represented by a full binary tree - a binary tree in which every node has either zero or two children. The symbols are at the leaves, and where each codeword is generated by a path from root to leaf, interpreting left as 0 and right as 1.

128 Prefix-Free Encoding We will avoid this problem by insisting on the prefix-free property: no codeword can be a prefix of another codeword. Any prefix-free encoding can be represented by a full binary tree - a binary tree in which every node has either zero or two children. The symbols are at the leaves, and where each codeword is generated by a path from root to leaf, interpreting left as 0 and right as 1. Decoding is unique: a string of bits is decrypted by starting at the root, reading the string from left to right to move downward, and, whenever a leaf is reached, outputting the corresponding symbol and returning to the root.

129 Prefix-Free Encoding 0 1 Symbol Codeword A 0 B 100 C 101 D 11 A [70] [23] [60] D [37] B [3] C [20]

130 Prefix-Free Encoding 0 1 Symbol Codeword A 0 B 100 C 101 D 11 A [70] [23] [60] D [37] B [3] C [20] The total size of the binary string drops to 213 megabits, a 17% improvement.

131 Optimal Encoding

132 Optimal Encoding Q: How do we find the optimal coding tree, given the frequencies f 1, f 2,..., f n of n symbols?

133 Optimal Encoding Q: How do we find the optimal coding tree, given the frequencies f 1, f 2,..., f n of n symbols? We want a tree whose leaves each correspond to a symbol and which minimizes the overall length of the encoding, n cost of tree = f i (depth of i th symbol in tree) i=1

134 Optimal Encoding Q: How do we find the optimal coding tree, given the frequencies f 1, f 2,..., f n of n symbols? We want a tree whose leaves each correspond to a symbol and which minimizes the overall length of the encoding, n cost of tree = f i (depth of i th symbol in tree) i=1 The number of bits required for a symbol is exactly its depth in the tree.

135 Optimal Encoding Q: How do we find the optimal coding tree, given the frequencies f 1, f 2,..., f n of n symbols? We want a tree whose leaves each correspond to a symbol and which minimizes the overall length of the encoding, n cost of tree = f i (depth of i th symbol in tree) i=1 The number of bits required for a symbol is exactly its depth in the tree. There is another way to write this cost function that is very helpful.

136 Optimal Encoding Q: How do we find the optimal coding tree, given the frequencies f 1, f 2,..., f n of n symbols? We want a tree whose leaves each correspond to a symbol and which minimizes the overall length of the encoding, n cost of tree = f i (depth of i th symbol in tree) i=1 The number of bits required for a symbol is exactly its depth in the tree. There is another way to write this cost function that is very helpful. We can define the frequency of any internal node to be the sum of the frequencies of its descendant leaves.

137 Optimal Encoding Q: How do we find the optimal coding tree, given the frequencies f 1, f 2,..., f n of n symbols? We want a tree whose leaves each correspond to a symbol and which minimizes the overall length of the encoding, n cost of tree = f i (depth of i th symbol in tree) i=1 The number of bits required for a symbol is exactly its depth in the tree. There is another way to write this cost function that is very helpful. We can define the frequency of any internal node to be the sum of the frequencies of its descendant leaves. The cost of a tree is the sum of the frequencies of all leaves and internal nodes, except the root.

138 Huffman Encoding We start constructing the tree greedily:

139 Huffman Encoding We start constructing the tree greedily: find the two symbols with the smallest frequencies, say i and j,

140 Huffman Encoding We start constructing the tree greedily: find the two symbols with the smallest frequencies, say i and j, and make them children of a new node, which then has frequency f i + f j. To keep the notation simple, let s just assume these are f 1 and f 2.

141 Huffman Encoding We start constructing the tree greedily: find the two symbols with the smallest frequencies, say i and j, and make them children of a new node, which then has frequency f i + f j. To keep the notation simple, let s just assume these are f 1 and f 2. By the second formulation of the cost function, any tree in which f 1 and f 2 are sibling-leaves has cost f 1 + f 2 plus the cost for a tree with n 1 leaves of frequencies (f 1 + f 2 ), f 3, f 4,..., f n.

142 Huffman Encoding We start constructing the tree greedily: find the two symbols with the smallest frequencies, say i and j, and make them children of a new node, which then has frequency f i + f j. To keep the notation simple, let s just assume these are f 1 and f 2. By the second formulation of the cost function, any tree in which f 1 and f 2 are sibling-leaves has cost f 1 + f 2 plus the cost for a tree with n 1 leaves of frequencies (f 1 + f 2 ), f 3, f 4,..., f n. The latter problem is just a smaller version of the one we started with. So we pull f 1 and f 2 off the list of frequencies, insert (f 1 + f 2 ), and loop.

143 The Algorithm Huffman(f ) input : An array f [1,..., n] of frequencies output: An encoding tree with n leaves Let H be a priority queue of integers, ordered by f ; for i = 1 to n do Insert(H,i); end for k = to 2n do i=deletemin (H); j=deletemin (H); create a node numbered k with children i, j; f [k] = f [i] + f [j]; Insert(H, k); end

144 Set Cover

145 The Problem A county is in its early stages of planning and is deciding where to put schools. (a) c (b) c d b a e f d b a e f k g k g i h i h j j

146 The Problem A county is in its early stages of planning and is deciding where to put schools. There are only two constraints: (a) c (b) c d b a e f d b a e f k g k g i h i h j j

147 The Problem A county is in its early stages of planning and is deciding where to put schools. There are only two constraints: each school should be in a town, and no one should have to travel more than 30 miles to reach one of them. (a) c (b) c d b a e f d b a e f k g k g i h i h j j

148 The Problem A county is in its early stages of planning and is deciding where to put schools. There are only two constraints: each school should be in a town, and no one should have to travel more than 30 miles to reach one of them. Q: What is the minimum number of schools needed? (a) c (b) c d b a e f d b a e f k g k g i h i h j j

149 The Problem This is a typical set cover problem.

150 The Problem This is a typical set cover problem. For each town x, let S x be the set of towns within 30 miles of it.

151 The Problem This is a typical set cover problem. For each town x, let S x be the set of towns within 30 miles of it. A school at x will essentially cover these other towns.

152 The Problem This is a typical set cover problem. For each town x, let S x be the set of towns within 30 miles of it. A school at x will essentially cover these other towns. The question is then, how many sets S x must be picked in order to cover all the towns in the county?

153 Set Cover Problem

154 Set Cover Problem Set Cover Input: A set of elements B, sets S 1,..., S m B Output: A selection of the S i whose union is B. Cost: Number of sets picked.

155 Set Cover Problem Set Cover Input: A set of elements B, sets S 1,..., S m B Output: A selection of the S i whose union is B. Cost: Number of sets picked. This problem lends itself immediately to a greedy solution:

156 Set Cover Problem Set Cover Input: A set of elements B, sets S 1,..., S m B Output: A selection of the S i whose union is B. Cost: Number of sets picked. This problem lends itself immediately to a greedy solution: Repeat until all elements of B are covered: Pick the set S i with the largest number of uncovered elements.

157 Set Cover Problem Set Cover Input: A set of elements B, sets S 1,..., S m B Output: A selection of the S i whose union is B. Cost: Number of sets picked. This problem lends itself immediately to a greedy solution: Repeat until all elements of B are covered: Pick the set S i with the largest number of uncovered elements. The greedy algorithm doesn t always find the best solution!

158 The Example c d b a e f k g i h j

159 Performance Ratio

160 Performance Ratio Lemma Suppose B contains n elements and that the optimal cover consists of k sets. Then the greedy algorithm will use at most k ln n sets.

161 Performance Ratio Lemma Suppose B contains n elements and that the optimal cover consists of k sets. Then the greedy algorithm will use at most k ln n sets. Proof

162 Performance Ratio Lemma Suppose B contains n elements and that the optimal cover consists of k sets. Then the greedy algorithm will use at most k ln n sets. Proof Let n t be the number of elements still not covered after t iterations of the greedy algorithm (so n 0 = n).

163 Performance Ratio Lemma Suppose B contains n elements and that the optimal cover consists of k sets. Then the greedy algorithm will use at most k ln n sets. Proof Let n t be the number of elements still not covered after t iterations of the greedy algorithm (so n 0 = n). Since these remaining elements are covered by the optimal k sets, there must be some set with at least n t /k of them.

164 Performance Ratio Lemma Suppose B contains n elements and that the optimal cover consists of k sets. Then the greedy algorithm will use at most k ln n sets. Proof Let n t be the number of elements still not covered after t iterations of the greedy algorithm (so n 0 = n). Since these remaining elements are covered by the optimal k sets, there must be some set with at least n t /k of them. Therefore, the greedy strategy will ensure that n t+1 n t n t k = n t(1 1 k ) which by repeated application implies n t n 0 (1 1 k )t

165 Performance Ratio A more convenient bound can be obtained from the useful inequality 1 x e x for all x with equality if and only if x = 0,

166 Performance Ratio A more convenient bound can be obtained from the useful inequality 1 x e x for all x with equality if and only if x = 0, Thus n t n 0 (1 1 k )t < n 0 (e 1 k ) t = ne t k

167 Performance Ratio A more convenient bound can be obtained from the useful inequality 1 x e x for all x with equality if and only if x = 0, Thus n t n 0 (1 1 k )t < n 0 (e 1 k ) t = ne t k At t = k ln n, therefore, n t is strictly less than ne ln n = 1, which means no elements remain to be covered.

168 SAT solver

169 What Is SAT Given a propositional formula in CNF, find an assignment to boolean variables that makes the formula true:

170 What Is SAT Given a propositional formula in CNF, find an assignment to boolean variables that makes the formula true: ω 1 = x 2 x 3 ω 2 = x 1 x 4 ω 3 = x 2 x 4

171 What Is SAT Given a propositional formula in CNF, find an assignment to boolean variables that makes the formula true: ω 1 = x 2 x 3 ω 2 = x 1 x 4 ω 3 = x 2 x 4 A = {x 1 = 0; x 2 = 1; x 3 = 0; x 4 = 1; }

172 Satisfiability The instances of Satisfiability or SAT:

173 Satisfiability The instances of Satisfiability or SAT: (x y z)(x y)(y z)(z x)(x y z)

174 Satisfiability The instances of Satisfiability or SAT: (x y z)(x y)(y z)(z x)(x y z) That is, a Boolean formula in conjunctive normal form (CNF).

175 Satisfiability The instances of Satisfiability or SAT: (x y z)(x y)(y z)(z x)(x y z) That is, a Boolean formula in conjunctive normal form (CNF). It is a collection of clauses (the parentheses),

176 Satisfiability The instances of Satisfiability or SAT: (x y z)(x y)(y z)(z x)(x y z) That is, a Boolean formula in conjunctive normal form (CNF). It is a collection of clauses (the parentheses), each consisting of the disjunction (logical or, denoted ) of several literals;

177 Satisfiability The instances of Satisfiability or SAT: (x y z)(x y)(y z)(z x)(x y z) That is, a Boolean formula in conjunctive normal form (CNF). It is a collection of clauses (the parentheses), each consisting of the disjunction (logical or, denoted ) of several literals; a literal is either a Boolean variable (such as x) or the negation of one (such as x).

178 Satisfiability The instances of Satisfiability or SAT: (x y z)(x y)(y z)(z x)(x y z) That is, a Boolean formula in conjunctive normal form (CNF). It is a collection of clauses (the parentheses), each consisting of the disjunction (logical or, denoted ) of several literals; a literal is either a Boolean variable (such as x) or the negation of one (such as x). A satisfying truth assignment is an assignment of false or true to each variable so that every clause contains a literal whose value is true.

179 Satisfiability The instances of Satisfiability or SAT: (x y z)(x y)(y z)(z x)(x y z) That is, a Boolean formula in conjunctive normal form (CNF). It is a collection of clauses (the parentheses), each consisting of the disjunction (logical or, denoted ) of several literals; a literal is either a Boolean variable (such as x) or the negation of one (such as x). A satisfying truth assignment is an assignment of false or true to each variable so that every clause contains a literal whose value is true. The SAT problem is the following: given a Boolean formula in conjunctive normal form, either find a satisfying truth assignment or else report that none exists.

180 2-SAT Given a set of clauses, where each clause is the disjunction of two literals (a literal is a Boolean variable or the negation of a Boolean variable). You are looking for a way to assign a value true or false to each of the variables so that all clauses are satisfied. that is, there is at least one true literal in each clause. (x 1 x 2 ) (x 1 x 3 ) (x 1 x 2 ) (x 3 x 4 ) (x 1 x 4 )

181 2-SAT Given a set of clauses, where each clause is the disjunction of two literals (a literal is a Boolean variable or the negation of a Boolean variable). You are looking for a way to assign a value true or false to each of the variables so that all clauses are satisfied. that is, there is at least one true literal in each clause. (x 1 x 2 ) (x 1 x 3 ) (x 1 x 2 ) (x 3 x 4 ) (x 1 x 4 ) Given an instance I of 2-Sat with n variables and m clauses, construct a directed graph G I = (V, E) as follows.

182 2-SAT Given a set of clauses, where each clause is the disjunction of two literals (a literal is a Boolean variable or the negation of a Boolean variable). You are looking for a way to assign a value true or false to each of the variables so that all clauses are satisfied. that is, there is at least one true literal in each clause. (x 1 x 2 ) (x 1 x 3 ) (x 1 x 2 ) (x 3 x 4 ) (x 1 x 4 ) Given an instance I of 2-Sat with n variables and m clauses, construct a directed graph G I = (V, E) as follows. G I has 2n nodes, one for each variable and its negation.

183 2-SAT Given a set of clauses, where each clause is the disjunction of two literals (a literal is a Boolean variable or the negation of a Boolean variable). You are looking for a way to assign a value true or false to each of the variables so that all clauses are satisfied. that is, there is at least one true literal in each clause. (x 1 x 2 ) (x 1 x 3 ) (x 1 x 2 ) (x 3 x 4 ) (x 1 x 4 ) Given an instance I of 2-Sat with n variables and m clauses, construct a directed graph G I = (V, E) as follows. G I has 2n nodes, one for each variable and its negation. G I has 2m edges: for each clause (α β) of I, G I has an edge from the negation of α to β, and one from the negation of β to α.

184 2-SAT Show that if G I has a strongly connected component containing both x and x for some variable x, then I has no satisfying assignment.

185 2-SAT Show that if G I has a strongly connected component containing both x and x for some variable x, then I has no satisfying assignment. If none of G I s strongly connected components contain both a literal and its negation, then the instance I must be satisfiable.

186 2-SAT Show that if G I has a strongly connected component containing both x and x for some variable x, then I has no satisfying assignment. If none of G I s strongly connected components contain both a literal and its negation, then the instance I must be satisfiable. Conclude that there is a linear-time algorithm for solving 2-SAT.

187 SAT Solver SAT solver decide whether a given propositional formula is satisfiable(i.e., whether there exists truth valuation for atomic propositions).

188 SAT Solver SAT solver decide whether a given propositional formula is satisfiable(i.e., whether there exists truth valuation for atomic propositions). 3-SAT (satisfiablity of boolean formula of length 3 clauses) is NP-complete; though often practically efficient.

189 SAT Solver SAT solver decide whether a given propositional formula is satisfiable(i.e., whether there exists truth valuation for atomic propositions). 3-SAT (satisfiablity of boolean formula of length 3 clauses) is NP-complete; though often practically efficient. SAT solver is to be said as the most successful formal tools.

Algorithms (IX) Yijia Chen Shanghai Jiaotong University

Algorithms (IX) Yijia Chen Shanghai Jiaotong University Algorithms (IX) Yijia Chen Shanghai Jiaotong University Review of the Previous Lecture Shortest paths in the presence of negative edges Negative edges Dijkstra s algorithm works in part because the shortest

More information

Algorithms (V) Path in Graphs. Guoqiang Li. School of Software, Shanghai Jiao Tong University

Algorithms (V) Path in Graphs. Guoqiang Li. School of Software, Shanghai Jiao Tong University Algorithms (V) Path in Graphs Guoqiang Li School of Software, Shanghai Jiao Tong University Review of the Previous Lecture Exploring Graphs EXPLORE(G, v) input : G = (V, E) is a graph; v V output: visited(u)

More information

Building a network. Properties of the optimal solutions. Trees. A greedy approach. Lemma (1) Lemma (2) Lemma (3) Lemma (4)

Building a network. Properties of the optimal solutions. Trees. A greedy approach. Lemma (1) Lemma (2) Lemma (3) Lemma (4) Chapter 5. Greedy algorithms Minimum spanning trees Building a network Properties of the optimal solutions Suppose you are asked to network a collection of computers by linking selected pairs of them.

More information

Chapter 5. Greedy algorithms

Chapter 5. Greedy algorithms Chapter 5. Greedy algorithms Minimum spanning trees Building a network Suppose you are asked to network a collection of computers by linking selected pairs of them. This translates into a graph problem

More information

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

Algorithms (VII) Yijia Chen Shanghai Jiaotong University Algorithms (VII) Yijia Chen Shanghai Jiaotong University Review of the Previous Lecture Depth-first search in undirected graphs Exploring graphs explore(g, v) Input: G = (V, E) is a graph; v V Output:

More information

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

Algorithms (VII) Yijia Chen Shanghai Jiaotong University Algorithms (VII) Yijia Chen Shanghai Jiaotong University Review of the Previous Lecture Depth-first search in undirected graphs Exploring graphs explore(g, v) Input: G = (V, E) is a graph; v V Output:

More information

looking ahead to see the optimum

looking ahead to see the optimum ! Make choice based on immediate rewards rather than looking ahead to see the optimum! In many cases this is effective as the look ahead variation can require exponential time as the number of possible

More information

CS Final - Review material

CS Final - Review material CS4800 Algorithms and Data Professor Fell Fall 2009 October 28, 2009 Old stuff CS 4800 - Final - Review material Big-O notation Though you won t be quizzed directly on Big-O notation, you should be able

More information

CS 161: Design and Analysis of Algorithms

CS 161: Design and Analysis of Algorithms CS 161: Design and Analysis of Algorithms Greedy Algorithms 2: Minimum Spanning Trees Definition The cut property Prim s Algorithm Kruskal s Algorithm Disjoint Sets Tree A tree is a connected graph with

More information

2.1 Greedy Algorithms. 2.2 Minimum Spanning Trees. CS125 Lecture 2 Fall 2016

2.1 Greedy Algorithms. 2.2 Minimum Spanning Trees. CS125 Lecture 2 Fall 2016 CS125 Lecture 2 Fall 2016 2.1 Greedy Algorithms We will start talking about methods high-level plans for constructing algorithms. One of the simplest is just to have your algorithm be greedy. Being greedy,

More information

Lecture 4 Greedy algorithms

Lecture 4 Greedy algorithms dvanced lgorithms loriano Zini ree University of ozen-olzano aculty of omputer Science cademic Year 203-20 Lecture Greedy algorithms hess vs Scrabble In chess the player must think ahead winning strategy

More information

Greedy Approach: Intro

Greedy Approach: Intro Greedy Approach: Intro Applies to optimization problems only Problem solving consists of a series of actions/steps Each action must be 1. Feasible 2. Locally optimal 3. Irrevocable Motivation: If always

More information

CSC 373 Lecture # 3 Instructor: Milad Eftekhar

CSC 373 Lecture # 3 Instructor: Milad Eftekhar Huffman encoding: Assume a context is available (a document, a signal, etc.). These contexts are formed by some symbols (words in a document, discrete samples from a signal, etc). Each symbols s i is occurred

More information

Lecture Summary CSC 263H. August 5, 2016

Lecture Summary CSC 263H. August 5, 2016 Lecture Summary CSC 263H August 5, 2016 This document is a very brief overview of what we did in each lecture, it is by no means a replacement for attending lecture or doing the readings. 1. Week 1 2.

More information

CS521 \ Notes for the Final Exam

CS521 \ Notes for the Final Exam CS521 \ Notes for final exam 1 Ariel Stolerman Asymptotic Notations: CS521 \ Notes for the Final Exam Notation Definition Limit Big-O ( ) Small-o ( ) Big- ( ) Small- ( ) Big- ( ) Notes: ( ) ( ) ( ) ( )

More information

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms Lecturer: Shi Li Department of Computer Science and Engineering University at Buffalo Main Goal of Algorithm Design Design fast

More information

6.1 Minimum Spanning Trees

6.1 Minimum Spanning Trees CS124 Lecture 6 Fall 2018 6.1 Minimum Spanning Trees A tree is an undirected graph which is connected and acyclic. It is easy to show that if graph G(V,E) that satisfies any two of the following properties

More information

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

CIS 121 Data Structures and Algorithms Minimum Spanning Trees CIS 121 Data Structures and Algorithms Minimum Spanning Trees March 19, 2019 Introduction and Background Consider a very natural problem: we are given a set of locations V = {v 1, v 2,..., v n }. We want

More information

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018 CS Section # Shortest Paths and MSTs //08 Shortest Paths There are types of shortest paths problems: Single source single destination Single source to all destinations All pairs shortest path In today

More information

Minimum Spanning Trees

Minimum Spanning Trees CS124 Lecture 5 Spring 2011 Minimum Spanning Trees A tree is an undirected graph which is connected and acyclic. It is easy to show that if graph G(V,E) that satisfies any two of the following properties

More information

Analysis of Algorithms, I

Analysis of Algorithms, I Analysis of Algorithms, I CSOR W4231.002 Eleni Drinea Computer Science Department Columbia University Thursday, March 8, 2016 Outline 1 Recap Single-source shortest paths in graphs with real edge weights:

More information

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018 CS 4 Section # Shortest Paths and MSTs //08 Shortest Paths There are types of shortest paths problems: Single source single destination Single source to all destinations All pairs shortest path In today

More information

22 Elementary Graph Algorithms. There are two standard ways to represent a

22 Elementary Graph Algorithms. There are two standard ways to represent a VI Graph Algorithms Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths All-Pairs Shortest Paths 22 Elementary Graph Algorithms There are two standard ways to represent a graph

More information

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs Representations of Weighted Graphs (as Matrices) A B Algorithms and Data Structures: Minimum Spanning Trees 9.0 F 1.0 6.0 5.0 6.0 G 5.0 I H 3.0 1.0 C 5.0 E 1.0 D 28th Oct, 1st & 4th Nov, 2011 ADS: lects

More information

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 6: BFS and Dijkstra s

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 6: BFS and Dijkstra s CSE 101 Algorithm Design and Analysis Miles Jones mej016@eng.ucsd.edu Office 4208 CSE Building Lecture 6: BFS and Dijkstra s BREADTH FIRST SEARCH (BFS) Given a graph G and a starting vertex s, BFS computes

More information

CS 310 Advanced Data Structures and Algorithms

CS 310 Advanced Data Structures and Algorithms CS 0 Advanced Data Structures and Algorithms Weighted Graphs July 0, 07 Tong Wang UMass Boston CS 0 July 0, 07 / Weighted Graphs Each edge has a weight (cost) Edge-weighted graphs Mostly we consider only

More information

2pt 0em. Computer Science & Engineering 423/823 Design and Analysis of Algorithms. Lecture 04 Minimum-Weight Spanning Trees (Chapter 23)

2pt 0em. Computer Science & Engineering 423/823 Design and Analysis of Algorithms. Lecture 04 Minimum-Weight Spanning Trees (Chapter 23) 2pt 0em Computer Science & Engineering 423/823 Design and of s Lecture 04 Minimum-Weight Spanning Trees (Chapter 23) Stephen Scott (Adapted from Vinodchandran N. Variyam) 1 / 18 Given a connected, undirected

More information

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem Minimum Spanning Trees (Forests) Given an undirected graph G=(V,E) with each edge e having a weight w(e) : Find a subgraph T of G of minimum total weight s.t. every pair of vertices connected in G are

More information

Union Find and Greedy Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 8

Union Find and Greedy Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 8 Union Find and Greedy Algorithms CSE 101: Design and Analysis of Algorithms Lecture 8 CSE 101: Design and analysis of algorithms Union find Reading: Section 5.1 Greedy algorithms Reading: Kleinberg and

More information

Algorithms (I) Introduction. Guoqiang Li. School of Software, Shanghai Jiao Tong University

Algorithms (I) Introduction. Guoqiang Li. School of Software, Shanghai Jiao Tong University Algorithms (I) Introduction Guoqiang Li School of Software, Shanghai Jiao Tong University Instructor and Teaching Assistants Guoqiang LI Instructor and Teaching Assistants Guoqiang LI Homepage: http://basics.sjtu.edu.cn/

More information

Greedy Algorithms. Textbook reading. Chapter 4 Chapter 5. CSci 3110 Greedy Algorithms 1/63

Greedy Algorithms. Textbook reading. Chapter 4 Chapter 5. CSci 3110 Greedy Algorithms 1/63 CSci 3110 Greedy Algorithms 1/63 Greedy Algorithms Textbook reading Chapter 4 Chapter 5 CSci 3110 Greedy Algorithms 2/63 Overview Design principle: Make progress towards a solution based on local criteria

More information

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Computer Science & Engineering 423/823 Design and Analysis of Algorithms Computer Science & Engineering 423/823 Design and Analysis of Algorithms Lecture 05 Minimum-Weight Spanning Trees (Chapter 23) Stephen Scott (Adapted from Vinodchandran N. Variyam) sscott@cse.unl.edu Introduction

More information

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019 CS 341: Algorithms Douglas R. Stinson David R. Cheriton School of Computer Science University of Waterloo February 26, 2019 D.R. Stinson (SCS) CS 341 February 26, 2019 1 / 296 1 Course Information 2 Introduction

More information

Dijkstra s algorithm for shortest paths when no edges have negative weight.

Dijkstra s algorithm for shortest paths when no edges have negative weight. Lecture 14 Graph Algorithms II 14.1 Overview In this lecture we begin with one more algorithm for the shortest path problem, Dijkstra s algorithm. We then will see how the basic approach of this algorithm

More information

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29 Lecture 13 Connectedness in graphs Spanning trees in graphs Finding a minimal spanning tree Time costs of graph problems and NP-completeness Finding a minimal spanning tree: Prim s and Kruskal s algorithms

More information

Minimum Spanning Trees My T. UF

Minimum Spanning Trees My T. UF Introduction to Algorithms Minimum Spanning Trees @ UF Problem Find a low cost network connecting a set of locations Any pair of locations are connected There is no cycle Some applications: Communication

More information

Horn Formulae. CS124 Course Notes 8 Spring 2018

Horn Formulae. CS124 Course Notes 8 Spring 2018 CS124 Course Notes 8 Spring 2018 In today s lecture we will be looking a bit more closely at the Greedy approach to designing algorithms. As we will see, sometimes it works, and sometimes even when it

More information

CS483 Design and Analysis of Algorithms

CS483 Design and Analysis of Algorithms CS483 Design and Analysis of Algorithms Review: Chapters 4-8, DPV Instructor: Fei Li lifei@cs.gmu.edu with subject: CS483 Office hours: STII, Room 443, Friday 4:00pm - 6:00pm or by appointments Course

More information

CSE 431/531: Analysis of Algorithms. Greedy Algorithms. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo

CSE 431/531: Analysis of Algorithms. Greedy Algorithms. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo CSE 431/531: Analysis of Algorithms Greedy Algorithms Lecturer: Shi Li Department of Computer Science and Engineering University at Buffalo Main Goal of Algorithm Design Design fast algorithms to solve

More information

CSE 100: GRAPH ALGORITHMS

CSE 100: GRAPH ALGORITHMS CSE 100: GRAPH ALGORITHMS Dijkstra s Algorithm: Questions Initialize the graph: Give all vertices a dist of INFINITY, set all done flags to false Start at s; give s dist = 0 and set prev field to -1 Enqueue

More information

UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 8 Lecturer: David Wagner February 20, Notes 8 for CS 170

UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 8 Lecturer: David Wagner February 20, Notes 8 for CS 170 UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 8 Lecturer: David Wagner February 20, 2003 Notes 8 for CS 170 1 Minimum Spanning Trees A tree is an undirected graph that is connected

More information

22 Elementary Graph Algorithms. There are two standard ways to represent a

22 Elementary Graph Algorithms. There are two standard ways to represent a VI Graph Algorithms Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths All-Pairs Shortest Paths 22 Elementary Graph Algorithms There are two standard ways to represent a graph

More information

Design and Analysis of Algorithms

Design and Analysis of Algorithms CSE 101, Winter 018 D/Q Greed SP s DP LP, Flow B&B, Backtrack Metaheuristics P, NP Design and Analysis of Algorithms Lecture 8: Greed Class URL: http://vlsicad.ucsd.edu/courses/cse101-w18/ Optimization

More information

2 A Template for Minimum Spanning Tree Algorithms

2 A Template for Minimum Spanning Tree Algorithms CS, Lecture 5 Minimum Spanning Trees Scribe: Logan Short (05), William Chen (0), Mary Wootters (0) Date: May, 0 Introduction Today we will continue our discussion of greedy algorithms, specifically in

More information

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree Algorithms and Theory of Computation Lecture 5: Minimum Spanning Tree Xiaohui Bei MAS 714 August 31, 2017 Nanyang Technological University MAS 714 August 31, 2017 1 / 30 Minimum Spanning Trees (MST) A

More information

managing an evolving set of connected components implementing a Union-Find data structure implementing Kruskal s algorithm

managing an evolving set of connected components implementing a Union-Find data structure implementing Kruskal s algorithm Spanning Trees 1 Spanning Trees the minimum spanning tree problem three greedy algorithms analysis of the algorithms 2 The Union-Find Data Structure managing an evolving set of connected components implementing

More information

Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 3.. NIL. 2. error new key is greater than current key 6. CASCADING-CUT(, )

Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 3.. NIL. 2. error new key is greater than current key 6. CASCADING-CUT(, ) Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 1. if >. 2. error new key is greater than current key 3.. 4.. 5. if NIL and.

More information

Design and Analysis of Algorithms (I)

Design and Analysis of Algorithms (I) Design and Analysis of Algorithms (I) Introduction Guoqiang Li School of Software, Shanghai Jiao Tong University Instructor and Teaching Assistants Guoqiang LI Instructor and Teaching Assistants Guoqiang

More information

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved. Chapter 9 Greedy Technique Copyright 2007 Pearson Addison-Wesley. All rights reserved. Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that

More information

UNIT 3. Greedy Method. Design and Analysis of Algorithms GENERAL METHOD

UNIT 3. Greedy Method. Design and Analysis of Algorithms GENERAL METHOD UNIT 3 Greedy Method GENERAL METHOD Greedy is the most straight forward design technique. Most of the problems have n inputs and require us to obtain a subset that satisfies some constraints. Any subset

More information

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree Algorithms and Theory of Computation Lecture 5: Minimum Spanning Tree Xiaohui Bei MAS 714 August 31, 2017 Nanyang Technological University MAS 714 August 31, 2017 1 / 30 Minimum Spanning Trees (MST) A

More information

Week 12: Minimum Spanning trees and Shortest Paths

Week 12: Minimum Spanning trees and Shortest Paths Agenda: Week 12: Minimum Spanning trees and Shortest Paths Kruskal s Algorithm Single-source shortest paths Dijkstra s algorithm for non-negatively weighted case Reading: Textbook : 61-7, 80-87, 9-601

More information

Minimum Spanning Trees

Minimum Spanning Trees Minimum Spanning Trees Overview Problem A town has a set of houses and a set of roads. A road connects and only houses. A road connecting houses u and v has a repair cost w(u, v). Goal: Repair enough (and

More information

1 Format. 2 Topics Covered. 2.1 Minimal Spanning Trees. 2.2 Union Find. 2.3 Greedy. CS 124 Quiz 2 Review 3/25/18

1 Format. 2 Topics Covered. 2.1 Minimal Spanning Trees. 2.2 Union Find. 2.3 Greedy. CS 124 Quiz 2 Review 3/25/18 CS 124 Quiz 2 Review 3/25/18 1 Format You will have 83 minutes to complete the exam. The exam may have true/false questions, multiple choice, example/counterexample problems, run-this-algorithm problems,

More information

FINAL EXAM SOLUTIONS

FINAL EXAM SOLUTIONS COMP/MATH 3804 Design and Analysis of Algorithms I Fall 2015 FINAL EXAM SOLUTIONS Question 1 (12%). Modify Euclid s algorithm as follows. function Newclid(a,b) if a

More information

Algorithms for Data Science

Algorithms for Data Science Algorithms for Data Science CSOR W4246 Eleni Drinea Computer Science Department Columbia University Thursday, October 1, 2015 Outline 1 Recap 2 Shortest paths in graphs with non-negative edge weights (Dijkstra

More information

COP 4531 Complexity & Analysis of Data Structures & Algorithms

COP 4531 Complexity & Analysis of Data Structures & Algorithms COP 4531 Complexity & Analysis of Data Structures & Algorithms Lecture 9 Minimum Spanning Trees Thanks to the text authors who contributed to these slides Why Minimum Spanning Trees (MST)? Example 1 A

More information

CS3383 Unit 2: Greedy

CS3383 Unit 2: Greedy CS3383 Unit 2: Greedy David Bremner January 31, 2018 Contents Greedy Huffman Coding MST Huffman Coding DPV 5.2 http://jeffe.cs.illinois.edu/teaching/algorithms/ notes/07-greedy.pdf Huffman Coding is covered

More information

Single Source Shortest Path

Single Source Shortest Path Single Source Shortest Path A directed graph G = (V, E) and a pair of nodes s, d is given. The edges have a real-valued weight W i. This time we are looking for the weight and the shortest path from s

More information

Scribe: Virginia Williams, Sam Kim (2016), Mary Wootters (2017) Date: May 22, 2017

Scribe: Virginia Williams, Sam Kim (2016), Mary Wootters (2017) Date: May 22, 2017 CS6 Lecture 4 Greedy Algorithms Scribe: Virginia Williams, Sam Kim (26), Mary Wootters (27) Date: May 22, 27 Greedy Algorithms Suppose we want to solve a problem, and we re able to come up with some recursive

More information

( ) n 3. n 2 ( ) D. Ο

( ) n 3. n 2 ( ) D. Ο CSE 0 Name Test Summer 0 Last Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. The time to multiply two n n matrices is: A. Θ( n) B. Θ( max( m,n, p) ) C.

More information

CSC 373: Algorithm Design and Analysis Lecture 4

CSC 373: Algorithm Design and Analysis Lecture 4 CSC 373: Algorithm Design and Analysis Lecture 4 Allan Borodin January 14, 2013 1 / 16 Lecture 4: Outline (for this lecture and next lecture) Some concluding comments on optimality of EST Greedy Interval

More information

Announcements Problem Set 5 is out (today)!

Announcements Problem Set 5 is out (today)! CSC263 Week 10 Announcements Problem Set is out (today)! Due Tuesday (Dec 1) Minimum Spanning Trees The Graph of interest today A connected undirected weighted graph G = (V, E) with weights w(e) for each

More information

Three Graph Algorithms

Three Graph Algorithms Three Graph Algorithms Shortest Distance Paths Distance/Cost of a path in weighted graph sum of weights of all edges on the path path A, B, E, cost is 2+3=5 path A, B, C, E, cost is 2+1+4=7 How to find

More information

Three Graph Algorithms

Three Graph Algorithms Three Graph Algorithms Shortest Distance Paths Distance/Cost of a path in weighted graph sum of weights of all edges on the path path A, B, E, cost is 2+3=5 path A, B, C, E, cost is 2+1+4=7 How to find

More information

Graphs and Network Flows ISE 411. Lecture 7. Dr. Ted Ralphs

Graphs and Network Flows ISE 411. Lecture 7. Dr. Ted Ralphs Graphs and Network Flows ISE 411 Lecture 7 Dr. Ted Ralphs ISE 411 Lecture 7 1 References for Today s Lecture Required reading Chapter 20 References AMO Chapter 13 CLRS Chapter 23 ISE 411 Lecture 7 2 Minimum

More information

CS 6783 (Applied Algorithms) Lecture 5

CS 6783 (Applied Algorithms) Lecture 5 CS 6783 (Applied Algorithms) Lecture 5 Antonina Kolokolova January 19, 2012 1 Minimum Spanning Trees An undirected graph G is a pair (V, E); V is a set (of vertices or nodes); E is a set of (undirected)

More information

CS 561, Lecture 10. Jared Saia University of New Mexico

CS 561, Lecture 10. Jared Saia University of New Mexico CS 561, Lecture 10 Jared Saia University of New Mexico Today s Outline The path that can be trodden is not the enduring and unchanging Path. The name that can be named is not the enduring and unchanging

More information

Greedy Algorithms CSE 780

Greedy Algorithms CSE 780 Greedy Algorithms CSE 780 Reading: Sections 16.1, 16.2, 16.3, Chapter 23. 1 Introduction Optimization Problem: Construct a sequence or a set of elements {x 1,..., x k } that satisfies given constraints

More information

Algorithms for Minimum Spanning Trees

Algorithms for Minimum Spanning Trees Algorithms & Models of Computation CS/ECE, Fall Algorithms for Minimum Spanning Trees Lecture Thursday, November, Part I Algorithms for Minimum Spanning Tree Sariel Har-Peled (UIUC) CS Fall / 6 Sariel

More information

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17 01.433/33 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/2/1.1 Introduction In this lecture we ll talk about a useful abstraction, priority queues, which are

More information

UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 7 Lecturer: David Wagner February 13, Notes 7 for CS 170

UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 7 Lecturer: David Wagner February 13, Notes 7 for CS 170 UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 7 Lecturer: David Wagner February 13, 003 Notes 7 for CS 170 1 Dijkstra s Algorithm Suppose each edge (v, w) of our graph has a

More information

Minimum Spanning Trees and Union Find. CSE 101: Design and Analysis of Algorithms Lecture 7

Minimum Spanning Trees and Union Find. CSE 101: Design and Analysis of Algorithms Lecture 7 Minimum Spanning Trees and Union Find CSE 101: Design and Analysis of Algorithms Lecture 7 CSE 101: Design and analysis of algorithms Minimum spanning trees and union find Reading: Section 5.1 Quiz 1 is

More information

Greedy Algorithms CSE 6331

Greedy Algorithms CSE 6331 Greedy Algorithms CSE 6331 Reading: Sections 16.1, 16.2, 16.3, Chapter 23. 1 Introduction Optimization Problem: Construct a sequence or a set of elements {x 1,..., x k } that satisfies given constraints

More information

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I MCA 312: Design and Analysis of Algorithms [Part I : Medium Answer Type Questions] UNIT I 1) What is an Algorithm? What is the need to study Algorithms? 2) Define: a) Time Efficiency b) Space Efficiency

More information

CSci 231 Final Review

CSci 231 Final Review CSci 231 Final Review Here is a list of topics for the final. Generally you are responsible for anything discussed in class (except topics that appear italicized), and anything appearing on the homeworks.

More information

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. Chapter 4 Greedy Algorithms Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. 1 4.5 Minimum Spanning Tree Minimum Spanning Tree Minimum spanning tree. Given a connected

More information

CSC Intro to Intelligent Robotics, Spring Graphs

CSC Intro to Intelligent Robotics, Spring Graphs CSC 445 - Intro to Intelligent Robotics, Spring 2018 Graphs Graphs Definition: A graph G = (V, E) consists of a nonempty set V of vertices (or nodes) and a set E of edges. Each edge has either one or two

More information

16 Greedy Algorithms

16 Greedy Algorithms 16 Greedy Algorithms Optimization algorithms typically go through a sequence of steps, with a set of choices at each For many optimization problems, using dynamic programming to determine the best choices

More information

CS161 - Minimum Spanning Trees and Single Source Shortest Paths

CS161 - Minimum Spanning Trees and Single Source Shortest Paths CS161 - Minimum Spanning Trees and Single Source Shortest Paths David Kauchak Single Source Shortest Paths Given a graph G and two vertices s, t what is the shortest path from s to t? For an unweighted

More information

Contents. CS 124 Final Exam Practice Problem 5/6/17. 1 Format and Logistics 2

Contents. CS 124 Final Exam Practice Problem 5/6/17. 1 Format and Logistics 2 CS 124 Final Exam Practice Problem 5/6/17 Contents 1 Format and Logistics 2 2 Topics Covered 2 2.1 Math Fundamentals.................................... 2 2.2 Graph Search........................................

More information

CSC 8301 Design & Analysis of Algorithms: Kruskal s and Dijkstra s Algorithms

CSC 8301 Design & Analysis of Algorithms: Kruskal s and Dijkstra s Algorithms CSC 8301 Design & Analysis of Algorithms: Kruskal s and Dijkstra s Algorithms Professor Henry Carter Fall 2016 Recap Greedy algorithms iterate locally optimal choices to construct a globally optimal solution

More information

Department of Computer Science and Engineering Analysis and Design of Algorithm (CS-4004) Subject Notes

Department of Computer Science and Engineering Analysis and Design of Algorithm (CS-4004) Subject Notes Page no: Department of Computer Science and Engineering Analysis and Design of Algorithm (CS-00) Subject Notes Unit- Greedy Technique. Introduction: Greedy is the most straight forward design technique.

More information

Round 3: Trees. Tommi Junttila. Aalto University School of Science Department of Computer Science. CS-A1140 Data Structures and Algorithms Autumn 2017

Round 3: Trees. Tommi Junttila. Aalto University School of Science Department of Computer Science. CS-A1140 Data Structures and Algorithms Autumn 2017 Round 3: Trees Tommi Junttila Aalto University School of Science Department of Computer Science CS-A1140 Data Structures and Algorithms Autumn 2017 Tommi Junttila (Aalto University) Round 3 CS-A1140 /

More information

Minimum Spanning Trees

Minimum Spanning Trees Minimum Spanning Trees 1 Minimum- Spanning Trees 1. Concrete example: computer connection. Definition of a Minimum- Spanning Tree Concrete example Imagine: You wish to connect all the computers in an office

More information

G205 Fundamentals of Computer Engineering. CLASS 21, Mon. Nov Stefano Basagni Fall 2004 M-W, 1:30pm-3:10pm

G205 Fundamentals of Computer Engineering. CLASS 21, Mon. Nov Stefano Basagni Fall 2004 M-W, 1:30pm-3:10pm G205 Fundamentals of Computer Engineering CLASS 21, Mon. Nov. 22 2004 Stefano Basagni Fall 2004 M-W, 1:30pm-3:10pm Greedy Algorithms, 1 Algorithms for Optimization Problems Sequence of steps Choices at

More information

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Computer Science & Engineering 423/823 Design and Analysis of Algorithms Computer Science & Engineering 423/823 Design and Analysis of Algorithms Lecture 07 Single-Source Shortest Paths (Chapter 24) Stephen Scott and Vinodchandran N. Variyam sscott@cse.unl.edu 1/36 Introduction

More information

Minimum Spanning Trees

Minimum Spanning Trees Minimum Spanning Trees Problem A town has a set of houses and a set of roads. A road connects 2 and only 2 houses. A road connecting houses u and v has a repair cost w(u, v). Goal: Repair enough (and no

More information

February 24, :52 World Scientific Book - 9in x 6in soltys alg. Chapter 3. Greedy Algorithms

February 24, :52 World Scientific Book - 9in x 6in soltys alg. Chapter 3. Greedy Algorithms Chapter 3 Greedy Algorithms Greedy algorithms are algorithms prone to instant gratification. Without looking too far ahead, at each step they make a locally optimum choice, with the hope that it will lead

More information

END-TERM EXAMINATION

END-TERM EXAMINATION (Please Write your Exam Roll No. immediately) Exam. Roll No... END-TERM EXAMINATION Paper Code : MCA-205 DECEMBER 2006 Subject: Design and analysis of algorithm Time: 3 Hours Maximum Marks: 60 Note: Attempt

More information

Algorithms IV. Dynamic Programming. Guoqiang Li. School of Software, Shanghai Jiao Tong University

Algorithms IV. Dynamic Programming. Guoqiang Li. School of Software, Shanghai Jiao Tong University Algorithms IV Dynamic Programming Guoqiang Li School of Software, Shanghai Jiao Tong University Dynamic Programming Shortest Paths in Dags, Revisited Shortest Paths in Dags, Revisited The special distinguishing

More information

Notes on Minimum Spanning Trees. Red Rule: Given a cycle containing no red edges, select a maximum uncolored edge on the cycle, and color it red.

Notes on Minimum Spanning Trees. Red Rule: Given a cycle containing no red edges, select a maximum uncolored edge on the cycle, and color it red. COS 521 Fall 2009 Notes on Minimum Spanning Trees 1. The Generic Greedy Algorithm The generic greedy algorithm finds a minimum spanning tree (MST) by an edge-coloring process. Initially all edges are uncolored.

More information

V Advanced Data Structures

V Advanced Data Structures V Advanced Data Structures B-Trees Fibonacci Heaps 18 B-Trees B-trees are similar to RBTs, but they are better at minimizing disk I/O operations Many database systems use B-trees, or variants of them,

More information

Algorithm Design (8) Graph Algorithms 1/2

Algorithm Design (8) Graph Algorithms 1/2 Graph Algorithm Design (8) Graph Algorithms / Graph:, : A finite set of vertices (or nodes) : A finite set of edges (or arcs or branches) each of which connect two vertices Takashi Chikayama School of

More information

Breadth-First Search, 1. Slides for CIS 675 DPV Chapter 4. Breadth-First Search, 3. Breadth-First Search, 2

Breadth-First Search, 1. Slides for CIS 675 DPV Chapter 4. Breadth-First Search, 3. Breadth-First Search, 2 Breadth-First Search, Slides for CIS DPV Chapter Jim Royer EECS October, 00 Definition In an undirected graph, the distance between two vertices is the length of the shortest path between them. (If there

More information

Context: Weighted, connected, undirected graph, G = (V, E), with w : E R.

Context: Weighted, connected, undirected graph, G = (V, E), with w : E R. Chapter 23: Minimal Spanning Trees. Context: Weighted, connected, undirected graph, G = (V, E), with w : E R. Definition: A selection of edges from T E such that (V, T ) is a tree is called a spanning

More information

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures COMP 3170 - Analysis of Algorithms & Data Structures Shahin Kamali Disjoin Sets and Union-Find Structures CLRS 21.121.4 University of Manitoba 1 / 32 Disjoint Sets Disjoint set is an abstract data type

More information

Treewidth and graph minors

Treewidth and graph minors Treewidth and graph minors Lectures 9 and 10, December 29, 2011, January 5, 2012 We shall touch upon the theory of Graph Minors by Robertson and Seymour. This theory gives a very general condition under

More information

Chapter 9 Graph Algorithms

Chapter 9 Graph Algorithms Chapter 9 Graph Algorithms 2 Introduction graph theory useful in practice represent many real-life problems can be slow if not careful with data structures 3 Definitions an undirected graph G = (V, E)

More information

Disjoint set (Union-Find)

Disjoint set (Union-Find) CS124 Lecture 6 Spring 2011 Disjoint set (Union-Find) For Kruskal s algorithm for the minimum spanning tree problem, we found that we needed a data structure for maintaining a collection of disjoint sets.

More information