





















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






















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.
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 edge’s weight. What is the cheapest possible network?
A
B
C
D
E
F
4
1
4
(^3 4 2 )
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 we. Output: A tree T = (V, E′), with E′^ ⊆ E, that minimizes
weight(T ) =
e∈E′
we.
In the preceding example, the minimum spanning tree has a cost of 16:
A
B
C
D
E
F
1
4
2 5
4
However, this is not the only optimal solution. Can you spot another?
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.
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.
The first two succeed, but the third, B − D, would produce a cycle if added. So we ignore it and move along. The final result is a tree with cost 14, the minimum possible.
The correctness of Kruskal’s method follows from a certain cut property , which is general enough to also justify a whole slew of other minimum spanning tree algorithms.
Figure 5.1 The minimum spanning tree found by Kruskal’s algorithm.
B
A (^6 )
3
2 D 4 F
C E
5 4 1 2 4
B
A
D^ F
C E
142 Algorithms
Figure 5.2 T ∪ {e}. The addition of e (dotted) to T (solid lines) produces a cycle. This cycle must contain at least one other edge, shown here as e′, across the cut (S, V − S).