**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 onlyif 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 atreebecause it's acyclic. It'sspanningbecause it reaches every vertex in the graph, and it'sminimumfor 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.

**Prim's Algorithm**

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 vertexusuch that the cost of getting fromvtouis the smallest possible cost (in the case of the park, the cost isdistance). 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 parentin the minimum spanning tree we're making.Suppose we have the following graph:

We would build a table as follows:

KnownPathLength1 - - INF 2 - - INF 3 - - INF 4 - - INF 5 - - INF 6 - - INF 7 - - INF Selecting vertex

1and making it the root of our tree, we update its neighbors,1,2,3, and4. Vertex1's cheapest place in the tree is known.

KnownPathLength1 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 vertex1). It's cheapest place in the tree is now known. Every vertex in the graph is adjacent to4.Vertex

1is known (meaning that its in its optimal place in the tree), so we don't examine it. We don't change vertex2, because its Length is 2, and the edge cost from4to2is 3. We update the rest.

KnownPathLength1 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 of1) and make it known. We can't improve our tree in any way by going through vertex2. We select vertex3(the last neighbor of1) and make it known. The path from3to6is cheaper than the path from4to6, so we update6's fields.

2and3's cheapest places in the tree are now known.

KnownPathLength1 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 of4, the first chosen neighbor of1). Its cheapest place in the tree is now known. Now we can adjust vertices5and6. Selecting5and6doesn't provide any cheaper paths. After5and6are selected, the Prim's algorithm terminates.

KnownPathLength1 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.