Spanning Tree
Spanning Tree
A spanning tree is a subset of Graph G, which has all the vertices covered with minimum
possible number of edges. Hence, a spanning tree does not have cycles and it cannot be
disconnected.
By this definition, we can draw a conclusion that every connected and undirected Graph
G has at least one spanning tree. A disconnected graph does not have any spanning tree,
as it cannot be spanned to all its vertices.
We now understand that one graph can have more than one spanning tree. Following are
a few properties of the spanning tree connected to graph G −
In a weighted graph, a minimum spanning tree is a spanning tree that has minimum
weight than all other spanning trees of the same graph. In real-world situations, this weight
can be measured as distance, congestion, traffic load or any arbitrary value denoted to
the edges.
The graph contains 9 vertices and 14 edges. So, the minimum spanning tree formed
will be having (9 – 1) = 8 edges.
Step 1: Pick edge 7-6. No cycle is formed, include it.
Step 6: Pick edge 8-6. Since including this edge results in the cycle, discard it. Pick
edge 2-3: No cycle is formed, include it.
Step 7: Pick edge 7-8. Since including this edge results in the cycle, discard it. Pick
edge 0-7. No cycle is formed, include it.
Step 8: Pick edge 1-2. Since including this edge results in the cycle, discard it. Pick
edge 3-4. No cycle is formed, include it.
Step 2: All the edges connecting the incomplete MST and other vertices are the edges
{0, 1} and {0, 7}. Between these two the edge with minimum weight is {0, 1}. So include
the edge and vertex 1 in the MST.
Step 3: The edges connecting the incomplete MST to other vertices are {0, 7}, {1, 7}
and {1, 2}. Among these edges the minimum weight is 8 which is of the edges {0, 7} and
{1, 2}. Let us here include the edge {0, 7} and the vertex 7 in the MST. [We could have
also included edge {1, 2} and vertex 2 in the MST].
Step 4: The edges that connect the incomplete MST with the fringe vertices are {1, 2},
{7, 6} and {7, 8}. Add the edge {7, 6} and the vertex 6 in the MST as it has the least
weight (i.e., 1).
Step 5: The connecting edges now are {7, 8}, {1, 2}, {6, 8} and {6, 5}. Include edge {6,
5} and vertex 5 in the MST as the edge has the minimum weight (i.e., 2) among them.
Step 6: Among the current connecting edges, the edge {5, 2} has the minimum weight.
So include that edge and the vertex 2 in the MST.
Step 7: The connecting edges between the incomplete MST and the other edges are
{2, 8}, {2, 3}, {5, 3} and {5, 4}. The edge with minimum weight is edge {2, 8} which has
weight 2. So include this edge and the vertex 8 in the MST.
Step 8: See here that the edges {7, 8} and {2, 3} both have same weight which are
minimum. But 7 is already part of MST. So we will consider the edge {2, 3} and include
that edge and vertex 3 in the MST.
Step 9: Only the vertex 4 remains to be included. The minimum weighted edge from the
incomplete MST to 4 is {3, 4}.
The final structure of the MST is as follows and the weight of the edges of the MST is
(4 + 8 + 1 + 2 + 4 + 2 + 7 + 9) = 37.
Advantages:
1. Prim’s algorithm is guaranteed to find the MST in a connected, weighted graph.
2. It has a time complexity of O(E log V) using a binary heap or Fibonacci heap,
where E is the number of edges and V is the number of vertices.
3. It is a relatively simple algorithm to understand and implement compared to some
other MST algorithms.
Disadvantages:
1. Like Kruskal’s algorithm, Prim’s algorithm can be slow on dense graphs with many
edges, as it requires iterating over all edges at least once.
2. Prim’s algorithm relies on a priority queue, which can take up extra memory and
slow down the algorithm on very large graphs.
3. The choice of starting node can affect the MST output, which may not be desirable
in some applications.
Prim’s algorithm prefer list data Kruskal’s algorithm prefer heap data
structures. structures.