Outline of the Algorithm
Outline of the Algorithm
Let’s restate the algorithm in graph terms (as opposed to cable TV terms): Start with a vertex, and put it in the tree. Then repeatedly do the following:
1. Find all the edges from the newest vertex to other vertices that aren’t in the tree. Put these edges in the priority queue.
2. Pick the edge with the lowest weight, and add this edge and its destination vertex to the tree.
Repeat these steps until all the vertices are in the tree. At that point, you’re done. In step 1, newest means most recently installed in the tree. The edges for this step
can be found in the adjacency matrix. After step 1, the list will contain all the edges from vertices in the tree to vertices on the fringe.
Extraneous Edges
In maintaining the list of links, we went to some trouble to remove links that led to
a town that had recently become connected. If we didn’t do this, we would have ended up installing unnecessary cable links.
In a programming algorithm we must likewise make sure that we don’t have any edges in the priority queue that lead to vertices that are already in the tree. We could go through the queue looking for and removing any such edges each time we added
a new vertex to the tree. As it turns out, it is easier to keep only one edge from the tree to a given fringe vertex in the priority queue at any given time. That is, the queue should contain only one edge to each category 2 vertex.
You’ll see that this is what happens in the GraphW Workshop applet. There are fewer edges in the priority queue than you might expect—just one entry for each category 2 vertex. Step through the minimum spanning tree for Figure 14.1 and verify that this is what happens. Table 14.1 shows how edges with duplicate destinations have been removed from the priority queue.
678 CHAPTER 14 Weighted Graphs
TABLE 14.1 Edge Pruning
Step
Pruned Edge
Number
Duplicate Removed List
Unpruned Edge
(in Priority
List
Queue)
from Priority Queue
1 AB6, AD4
AB6, AD4
2 DE12, DC8, DB7, AB6
DB7(AB6) 3 DE12, BC10, DC8, BE7
DE12, DC8, AB6
DC8, BE7
DE12(BE7), BC10(DC8)
4 BC10, DC8, EF7, EC5
EF7, EC5
BC10(EC5), DC8(EC5)
5 EF7, CF6
CF6
EF7
Remember that an edge consists of a letter for the source (starting) vertex of the edge, a letter for the destination (ending vertex), and a number for the weight. The second column in this table corresponds to the lists you kept when constructing the cable TV system. It shows all edges from category 1 vertices (those in the tree) to category 2 vertices (those with at least one known edge from a category 1 vertex).
The third column is what you see in the priority queue when you run the GraphW applet. Any edge with the same destination vertex as another edge, and which has a greater weight, has been removed.
The fourth column shows the edges that have been removed and, in parentheses, the edge with the smaller weight that superseded it and remains in the queue. Remember that as you go from step to step the last entry on the list is always removed because this edge is added to the tree.