Finding a Minimum Spanning Tree of an Undirected, Connected Graph
A minimum spanning tree of an undirected graph is a tree formed from that graph's edges that connects all the vertices of that graph at the lowest total cost. You can make a spanning tree of a graph only if the graph is connected. There may be more than one spanning tree of a particular graph.
The number of edges in a minimum spanning tree of a graph will be the number of vertices it has - 1. A minimum spanning tree is a tree because it's acyclic. It's spanning because it reaches every vertex in the graph, and it's minimum for the obvious reason. If we need to wire a house with a minimum of cable, then a we need to find a minimum spanning tree of a graph of the electrical layout of the house.
Minimum Spanning Trees: Why Do We Care?
Many real situations can be modeled with graphs. And, many real situations can be solved by finding the minimum spanning tree of graphs.
My favorite example involves an electrician and a house. Imagine that a collection of electrical outlets have been installed in the walls of a house, and that the electicity enters the house at a single electrical box. We can model this situation as a graph, where each of the outlets and the electrical box is a node, and the walls are the edges. The length of each wall, or segment thereof, is the distance along the wall between two of the electrical connections.
As a result, the electrician may have many different routes he can use to wire the outlets -- they may be reachable by different paths along the walls. So, the electrician wants to find the path that requires the least amount of wire. This saves money, becuase less wire is needed. And, it saves time, because the runs along the wal are shorter and, as a consequence, take less time to install.
To solve this problem, the electrician can model the outlets and wall segments conecting them as a graph rooted at the electrical box. Then, the electrician can find the minimum spanning tree of the graph. The edges in this tree give the paths that the electrician should use to run the wires -- they will reach each node, while requiring the least amount of wire.
Imagine finding the minimum amount of sidewalk needed to get to every point of interest from the entrance of a park. Now think of the park entrance as the root of your minimum spanning tree. This will help you as you apply Prim's Algorithm.
Prim's grows the tree in successive stages. You start by choosing one vertex to be the root v, and add an edge (piece of sidewalk), and thus an associated vertex (a point of interest in the park), to the tree. At each stage, you add a vertex to the tree by choosing the vertex u such that the cost of getting from v to u is the smallest possible cost (in the case of the park, the cost is distance). At each stage, you say, "Where can I get from here?" and go down the shortest road possible from where you are.
Applying this algorithm until all vertices of the given graph are in the tree creates a minimum spanning tree of that graph. This may sound familiar. Prim's algorithm is essentially the same as Dijkstra's -- with a different cost function. They both proceed using the same greedy strategy.
Prim's finds the minimum spanning tree of the entire graph from s, so we use the Length field to record the cost of getting from a vertex v to its parent in the minimum spanning tree we're making.
Suppose we have the following graph:
We would build a table as follows:
Known Path Length 1 - - INF 2 - - INF 3 - - INF 4 - - INF 5 - - INF 6 - - INF 7 - - INF
Selecting vertex 1 and making it the root of our tree, we update its neighbors, 1, 2, 3, and 4. Vertex 1's cheapest place in the tree is known.
Known Path Length 1 Y 1 0 2 - 1 2 3 - 1 4 4 - 1 1 5 - - INF 6 - - INF 7 - - INF
Next we select vertex 4 (one of the neighbors of vertex 1). It's cheapest place in the tree is now known. Every vertex in the graph is adjacent to 4.
Vertex 1 is known (meaning that its in its optimal place in the tree), so we don't examine it. We don't change vertex 2, because its Length is 2, and the edge cost from 4 to 2 is 3. We update the rest.
Known Path Length 1 Y 1 0 2 - 1 2 3 - 4 2 4 Y 1 1 5 - 4 7 6 - 4 8 7 - 4 4
Next we select vertex 2 (another neighbor of 1) and make it known. We can't improve our tree in any way by going through vertex 2. We select vertex 3 (the last neighbor of 1) and make it known. The path from 3 to 6 is cheaper than the path from 4 to 6, so we update 6's fields.
2 and 3's cheapest places in the tree are now known.
Known Path Length 1 Y 1 0 2 Y 1 2 3 Y 4 2 4 Y 1 1 5 - 4 7 6 - 3 5 7 - 4 4
Next we select vertex 7 (neighbor of 4, the first chosen neighbor of 1). Its cheapest place in the tree is now known. Now we can adjust vertices 5 and 6. Selecting 5 and 6 doesn't provide any cheaper paths. After 5 and 6 are selected, the Prim's algorithm terminates.
Known Path Length 1 Y 1 0 2 Y 1 2 3 Y 4 2 4 Y 1 1 5 Y 7 6 6 Y 7 1 7 Y 4 4
To find the minimum spanning tree of the graph featured in the table, follow the Path fields from vertex 1.