This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: Chapter 5 Greedy algorithms A game like chess can be won only by thinking ahead : a player who is focused entirely on immediate advantage is easy to defeat. But in many other games, such as Scrabble, it is possible to do quite well by simply making whichever move seems best at the moment and not worrying too much about future consequences. This 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 disastrous for some computational tasks, there are many for which it is optimal. Our first example is that of minimum spanning trees. 5.1 Minimum spanning trees 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, and the goal is to pick enough of these edges that the nodes are connected. But this is not all; each link also has a maintenance cost, reflected in that edges weight. What is the cheapest possible network? A B C D E F 4 1 4 3 4 2 5 6 4 One immediate observation is that the optimal set of edges cannot contain 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. 139 140 Algorithms Input: An undirected graph G = ( V, E ) ; edge weights w e . Output: A tree T = ( V, E ) , with E E , that minimizes weight ( T ) = X e E w e . In the preceding example, the minimum spanning tree has a cost of 16: A B C D E F 1 4 2 5 4 However, this is not the only optimal solution. Can you spot another? 5.1.1 A greedy approach Kruskals 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 doesnt produce a cycle. In other words, it constructs the tree edge by edge and, apart from taking care to avoid cycles, simply picks whichever edge is cheapest at the moment. This is a greedy algorithm: every decision it makes is the one with the most obvious immediate advantage. Figure 5.1 shows an example. We start with an empty graph and then attempt to add edges in increasing order of weight (ties are broken arbitrarily): B- C, C- D, B- D, C- F, D- F, E- F, A- D, A- B, C- E, A- C....
View Full Document
This note was uploaded on 12/15/2009 for the course CS 473 taught by Professor Viswanathan during the Spring '08 term at University of Illinois at Urbana–Champaign.
- Spring '08