Minimum Spanning Tree
The cost of the spanning tree is the sum of the weights of all the edges in the tree. There can be many spanning trees.
Minimum spanning tree is the spanning tree where the cost is minimum among all the spanning trees. There also can be many minimum spanning trees.
Minimum spanning tree has direct application in the design of networks. It is used in algorithms approximating the travelling salesman problem, multi-terminal minimum cut problem and minimum-cost weighted perfect matching. Other practical applications are Cluster Analysis, Handwriting recognition, Image segmentation etc.
Algorithms for finding the Minimum Spanning Tree:
Kruskal's Algorithm
Kruskal’s Algorithm builds the spanning tree by adding edges one by one into a growing spanning tree. Kruskal's algorithm follows greedy approach as in each iteration it finds an edge which has least weight and add it to the growing spanning tree.
Algorithm Steps:
Sort the graph edges with respect to their weights.
Start adding edges to the MST from the edge with the smallest weight until the edge of the largest weight.
Only add edges which doesn't form a cycle , edges which connect only disconnected components.
How to check if 2 vertices are connected or not ?
This could be done using DFS which starts from the first vertex, then check if the second vertex is visited or not. But DFS will make time complexity large as it has an order of O(V+E)
where V
is the number of vertices, E
is the number of edges. So the best solution is "Disjoint Sets".
Disjoint sets are sets whose intersection is the empty set so it means that they don't have any element in common.
Time Complexity: |
In Kruskal’s algorithm, most time consuming operation is sorting because the total complexity of the Disjoint-Set operations will be O(E log V)
, which is the overall Time Complexity of the algorithm.
Kruskal’s algorithm is preferred for sparse graphs. Sparse graph is a graph in which the number of edges is close to the minimal number of edges.
Prism's Algorithm
Prim’s Algorithm also use Greedy approach to find the minimum spanning tree. In Prim’s Algorithm we grow the spanning tree from a starting position. Unlike an edge in Kruskal's, we add vertex to the growing spanning tree in Prim's.
Algorithm Steps:
Maintain two disjoint sets of vertices. One containing vertices that are in the growing spanning tree and other that are not in the growing spanning tree.
Select the cheapest vertex that is connected to the growing spanning tree and is not in the growing spanning tree and add it into the growing spanning tree. This can be done using Priority Queues. Insert the vertices, that are connected to growing spanning tree, into the Priority Queue.
Check for cycles. To do that, mark the nodes which have been already selected and insert only those nodes in the Priority Queue that are not marked.
Time Complexity: |
The time complexity of the Prim’s Algorithm is O((V+E) log V)
because each vertex is inserted in the priority queue only once and insertion in priority queue take logarithmic time.
Prim’s Algorithm is preferred for dense graphs. Dense graph is a graph in which the number of edges is close to the maximal number of edges.
Last updated