Greedy Algorithms Greedy Strategy: Make a Locally Optimal Choice, Or Simply, What Appears Best at the Moment R
Total Page:16
File Type:pdf, Size:1020Kb
Overview Kruskal Dijkstra Human Compression Overview Kruskal Dijkstra Human Compression Overview One of the strategies used to solve optimization problems CSE 548: (Design and) Analysis of Algorithms Multiple solutions exist; pick one of low (or least) cost Greedy Algorithms Greedy strategy: make a locally optimal choice, or simply, what appears best at the moment R. Sekar Often, locally optimality 6) global optimality So, use with a great deal of care Always need to prove optimality If it is unpredictable, why use it? It simplifies the task! 1 / 35 2 / 35 Overview Kruskal Dijkstra Human Compression Overview Kruskal Dijkstra Human Compression Making change When does a Greedy algorithm work? Given coins of denominations 25cj, 10cj, 5cj and 1cj, make change for x cents (0 < x < 100) using minimum number of coins. Greedy choice property Greedy solution The greedy (i.e., locally optimal) choice is always consistent with makeChange(x) some (globally) optimal solution if (x = 0) return What does this mean for the coin change problem? Let y be the largest denomination that satisfies y ≤ x Optimal substructure Issue bx=yc coins of denomination y The optimal solution contains optimal solutions to subproblems. makeChange(x mod y) Implies that a greedy algorithm can invoke itself recursively after Show that it is optimal making a greedy choice. Is it optimal for arbitrary denominations? 3 / 35 4 / 35 Chapter 5 Overview Kruskal Dijkstra Human Compression Overview Kruskal Dijkstra Human Compression Knapsack Problem Greedy algorithmsFractional Knapsack A sack that can hold a maximum of x lbs Greedy choice property Proof by contradiction: Start with the assumption that there is an You have a choice of items you can pack in the sack optimal solution that does not include the greedy choice, and show a A game like chess can be won only by thinking ahead: a player who is focused entirely on Maximize the combined “value” of items in the sack contradiction. item calories/lb weightimmediate advantage is easy to defeat. But in many other games, such as Scrabble, it is bread 1100 5possible to do quite well byOptimal simply substructure making whichever move seems best at the moment and not After taking as much of the item with jth maximal value/weight, butter 3300 worrying1 too much about future consequences. tomato 80 1 suppose that the knapsack can hold y more lbs. cucumber 55 2 This sort of myopic behaviorThen the optimal is easy solution and for the convenient, problem includes making the optimal it an attractive algorithmic 0-1 knapsack: Take all of one item or none at allstrategy. Greedy algorithmschoice of build how to fill up a knapsack a solution of size y piecewith the byremaining piece, items. always choosing the next Fractional knapsack: Fractional quantities acceptablepiece that offers the mostDoes obvious not work forand 0-1 immediateknapsack because bene greedyfi choicet. Although property such an approach can be Greedy choice: pick item that maximizes calories/lbdisastrous for some computationaldoes not hold. tasks, there are many for which it is optimal. Ourfirst Will a greedy algorithm work, with x = 5? example is that5 / 35 of minimum0-1 knapsack spanning is NP-hard, trees. but a pseudo-polynomial algorithm is 6 / 35 available. Overview Kruskal Dijkstra Human Compression Minimal Spanning Tree 5.1Kruskal’s Algorithm Minimum spanningOverview Kruskal Dijkstra trees Human Compression Minimal Spanning Tree Kruskal’s Algorithm Spanning Tree Minimal Spanning Tree (MST) Suppose you are asked to network a collection of computers by linking selected pairs of them. A subgraph of a graph G = (V; E) that includes:This translates into a graphA spanning problem tree with minimal in which cost. Formally: nodes are computers, undirected edges are Input: An undirected graph G = (V; E), a cost function w : E ! . All the vertices V in the graph potential links, and the goal is to pick enough of these edges thatR the nodes are connected. But this is not all; each linkOutput: also A tree hasT = a (V maintenance; E0) such that E0 ⊆ cost,E that minimizesreflected in that edge’s weight. What A subset of E such that these edges form a tree P 0 w(e) is the cheapest possible network?e2E We consider connected undirected graphs, where the second condition for MST can be replaced by 1 A maximal subset of E such that the subgraph has no cycles A C E 4 3 4 A subset of E with jVj − 1 edges such that the subgraph is 4 2 5 connected A subset of E such that there is a unique path between any two B 4 D 6 F vertices in the subgraph One immediate7 / 35 observation is that the optimal set of edges cannot8 / 35contain a cycle, because removing an edge from this cycle would reduce the cost without compromising connectivity: Property 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. The particular tree we want is the one with minimum total weight, known as the minimum spanning tree. Here is its formal definition. Input: An undirected graphG=(V, E); edge weightsw e. 133 Output: A treeT=(V, E ), withE E, that minimizes � � ⊆ � weight(T) = wChaptere. 5 e E� ∈ Overview Kruskal Dijkstra Human Compression Minimal Spanning Tree Kruskal’s Algorithm Overview Kruskal Dijkstra Human Compression Minimal Spanning Tree Kruskal’s Algorithm In the preceding example,Minimal the Spanning minimum Tree spanning (MST) treeGreedy has a cost of 16: algorithmsKruskal’s algorithm 1 A C E 4 Start with the empty set of edges 2 A game5 like chess can beRepeat: won add only lightest byedge thinking that doesn’t ahead create a: cycle a player who is focused entirely on immediate advantage isAdds easy edges toB— defeat.C, C—D, C But—F, A— inD, manyE—F other games, such as Scrabble, it is B 4 D possibleF to do quite well by simply making whichever move seems best at the moment and not worrying too much about future consequences. However, this is not the only optimal solution. Can you spotThis another? sort of myopic behavior is easy and convenient, making it an attractive algorithmic strategy. Greedy algorithms build up a solution piece by piece, always choosing the next piece that offers the most obvious and immediate benefit. Although such an approach can be 5.1.1 A greedy approach disastrous for some computational tasks, there are many for which it is optimal. Ourfirst Kruskal’s minimum spanning tree algorithm starts withexample the empty is that9 / 35 graph of minimum and then spanning selects trees. 10 / 35 edges fromE according to the following rule. Overview Kruskal Dijkstra Human Compression Minimal Spanning Tree 5.1Kruskal’s Algorithm Minimum spanningOverview Kruskal Dijkstra trees Human Compression Minimal Spanning Tree Kruskal’s Algorithm Repeatedly add the next lightest edge that doesn’t produce a cycle. Kruskal’s algorithm Kruskal’s: Correctness (by induction) Suppose you are asked to network a collection of computers by linking selected pairs of them. In other words, it constructs the tree edge by edge and, apartThis translates from taking into care a tographInduction avoid problemHypothesis: cycles,The in first whichi edges selected nodes by Kruskal’s arecomputers, algorithm are undirected edges are simply picks whichever edge is cheapest at the moment.potential This is links, a greedy and thealgorithm:included goal isin some to every pickminimal enoughspanning tree ofT these edges that the nodes are connected. MST(V; E; w) Base case: trivial — the empty set of edges is always in any MST. decision it makes is the one with the most obvious immediateBut this advantage. is not all; each linkInduction also step: hasShow a that maintenancei+1th edge chosen bycost, Kruskal’s refl is ectedin the MST inT that edge’s weight. What X = φ Figure 5.1 shows an example. We start with an empty graph and then attemptfrom induction to hypothesis, add i.e., prove greedy choice property. Q = priorityQueue(E) // from min to max weightis the cheapest possible network? Let e = (v; w) be the edge chosen at i + 1th step of Kruskal’s. edges in increasing orderwhile of weightQ is nonempty (ties are broken arbitrarily): T is a spanning tree: must1 include a unique path from v to w e = deleteMin(Q) At least one edgeA e0 on this path isC not in X, the set ofE edges chosen in if e connects two disconnected components in (V; X) the first i steps by Kruskal’s. (Otherwise, v and4 w will already be B C, C D, B D, C F, D F, E F, A D, A B, C E, A C. 3 4 − − − X = X−[ feg − − − − − − connected in4X and so e won’t be chosen2 by Kruskal’s.) 5 Since neither e nor e0 are in X, and Kruskal’s chose e, w(e0) ≥ w(e). Replace e0 by e in T to get another spanning tree T 0. Either Thefirst two succeed, but the third,B D, would produce a cycle if added. So we ignore0 itB D F − w(T ) < w(T), a contradiction4 to the assumption6 T is minimal; or and move along. Thefinal result is a tree with cost 14, the minimum possible. w(T 0) = w(T), and we have another MST T 0 consistent with X [ feg. In both cases, we have completed the induction step. One immediate11 / 35 observation is that the optimal set of edges cannot12 / 35contain a cycle, because The correctness of Kruskal’s method follows from aremoving certain cut an property edge from, which this iscycle general would reduce the cost without compromising connectivity: enough to also justify a whole slew of other minimum spanning tree algorithms.