Minimum Spanning Tree
Minimum Spanning Tree
A Spanning tree is a subset to a connected graph G, where all the edges are connected, i.e, we
can traverse to any edge from a particular edge with or without intermediates. Also, a
spanning tree must not have any cycle in it. Thus we can say that if there are n vertices in a
connected graph then the no. of edges that a spanning tree may have is n-1.
Prim’s Algorithm
Prim's Algorithm is a greedy algorithm that is used to find the minimum spanning tree from a
graph. Prim's algorithm finds the subset of edges that includes every vertex of the graph such
that the sum of the weights of the edges can be minimized.
Prim's algorithm starts with the single node and explores all the adjacent nodes with all the
connecting edges at every step. The edges with the minimal weights causing no cycles in the
graph got selected.
The set mstSet is initially empty and keys assigned to vertices are {0, INF, INF, INF, INF, INF,
INF, INF} where INF indicates infinite. Now pick the vertex with the minimum key value. The
vertex 0 is picked, include it in mstSet. So mstSet becomes {0}. After including to mstSet,
update key values of adjacent vertices. Adjacent vertices of 0 are 1 and 7. The key values of 1
and 7 are updated as 4 and 8. Following subgraph shows vertices and their key values, only
the vertices with finite key values are shown. The vertices included in MST are shown in green
color.
Pick the vertex with minimum key value and not already
included in MST (not in mstSET). The vertex 1 is picked
and added to mstSet. So mstSet now becomes {0, 1}.
Update the key values of adjacent vertices of 1. The key
value of vertex 2 becomes 8.
Pick the vertex with minimum key value and not already
included in MST (not in mstSET). We can either pick vertex
7 or vertex 2, let vertex 7 is picked. So mstSet now
becomes {0, 1, 7}. Update the key values of adjacent
vertices of 7. The key value of vertex 6 and 8 becomes
finite (1 and 7 respectively).
Pick the vertex with minimum key value and not already
included in MST (not in mstSET). Vertex 6 is picked. So
mstSet now becomes {0, 1, 7, 6}. Update the key values of
adjacent vertices of 6. The key value of vertex 5 and 8 are
updated.
Below are the steps for finding MST using Kruskal’s algorithm
1. Sort all the edges in non-decreasing order of their weight.
2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If cycle is
not formed, include this edge. Else, discard it.
3. Repeat step#2 until there are (V-1) edges in the spanning tree.
The graph contains 9 vertices and 14 edges. So, the minimum spanning tree formed will be
having (9 – 1) = 8 edges.
After sorting:
Weight Src Dest
1 7 6
2 8 2
2 6 5
4 0 1
4 2 5
6 8 6
7 2 3
7 7 8
8 0 7
8 1 2
9 3 4
10 5 4
11 1 7
14 3 5
Now pick all edges one by one from the sorted list of edges
1. Pick edge 7-6: No cycle is formed, include it.
2. Pick edge 8-2: No cycle is formed, include it.
8. Pick edge 7-8: Since including this edge results in the cycle, discard it.
9. Pick edge 0-7: No cycle is formed, include it.
10. Pick edge 1-2: Since including this edge results in the cycle, discard it.
11. Pick edge 3-4: No cycle is formed, include it.
Since the number of edges included equals (V – 1), the algorithm stops here.
Dijkstra’s shortest path algorithm
Dijkstra’s algorithm is very similar to Prim’s algorithm for minimum spanning tree. Like
Prim’s MST, we generate a SPT (shortest path tree) with a given source as a root. We maintain
two sets, one set contains vertices included in the shortest-path tree, other set includes
vertices not yet included in the shortest-path tree. At every step of the algorithm, we find a
vertex that is in the other set (set of not yet included) and has a minimum distance from the
source.
Algorithm
1) Create a set sptSet (shortest path tree set) that keeps track of vertices included in the
shortest-path tree, i.e., whose minimum distance from the source is calculated and finalized.
Initially, this set is empty.
2) Assign a distance value to all vertices in the input graph. Initialize all distance values as
INFINITE. Assign distance value as 0 for the source vertex so that it is picked first.
3) While sptSet doesn’t include all vertices
a) Pick a vertex u which is not there in sptSet and has a minimum distance value.
b) Include u to sptSet.
c) Update distance value of all adjacent vertices of u. To update the distance values,
iterate through all adjacent vertices. For every adjacent vertex v, if the sum of distance
value of u (from source) and weight of edge u-v, is less than the distance value of v,
then update the distance value of v.
The set sptSet is initially empty and distances assigned to vertices are {0, INF, INF, INF, INF, INF,
INF, INF} where INF indicates infinite. Now pick the vertex with a minimum distance value. The
vertex 0 is picked, include it in sptSet. So sptSet becomes {0}. After including 0 to sptSet, update
distance values of its adjacent vertices. Adjacent vertices of 0 are 1 and 7. The distance values of
1 and 7 are updated as 4 and 8. The following subgraph shows vertices and their distance
values, only the vertices with finite distance values are shown. The vertices included in SPT are
shown in green colour.