Which Minimum Spanning Tree Algorithm is better?
Last Updated :
23 Nov, 2023
A spanning tree is defined as a tree-like sub-graph of a connected, undirected graph that includes all the graph's vertices. Or, in Layman’s words, it is a subset of the edges of the graph that forms a tree (acyclic) where every node of the graph is a part of the e tree. A fixed number of edges in the spanning tree .
Properties of a Spanning Tree
The spanning tree holds the below-mentioned principles:
- The number of vertices (V) in the graph and the spanning tree is the same.
- A fixed number of edges in the spanning tree equal to one less than the total number of vertices ( E = V-1 ).
- The spanning tree should not be disconnected; there should only be a single source of component, not more than that.
- The spanning tree should be acyclic, which means there would not be any cycle in the tree.
- The total cost (or weight) of the spanning tree is defined as the sum of the edge weights of all the edges of the spanning tree.
- There can be many possible spanning trees for a graph.
A minimum spanning tree (MST) is defined as a spanning tree that has the minimum weight among all the possible spanning trees.
The minimum spanning tree has all the properties of a spanning tree with an added constraint of having the minimum possible weights among all possible spanning trees. Like a spanning tree, there can also be many possible MSTs for a graph, To know more visit this article.
Algorithms to find Minimum Spanning Tree
There are several algorithms to find the minimum spanning tree from a given graph, some of them are listed below:
- Kruskal’s Minimum Spanning Tree Algorithm
- Prim's Minimum Spanning Tree Algorithm
- Boruvka’s Minimum Spanning Tree Algorithm
Kruskal's Minimum Spanning Tree Algorithm
In Kruskal’s algorithm, sort all edges of the given graph in increasing order. Then it keeps on adding new edges and nodes in the MST if the newly added edge does not form a cycle. It picks the minimum weighted edge at first and the maximum weighted edge at last. Thus we can say that it makes a locally optimal choice in each step in order to find the optimal solution. Hence this is a Greedy Algorithm.
Below are the steps for finding MST using Kruskal’s algorithm:
- Sort all the edges in non-decreasing order of their weight.
- Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If the cycle is not formed, include this edge. Else, discard it.
- Repeat step#2 until there are (V-1) edges in the spanning tree.
For more detailed explanation, go to this article.
Prim's Minimum Spanning Tree Algorithm
The algorithm starts with an empty spanning tree. The idea is to maintain two sets of vertices. The first set contains the vertices already included in the MST, and the other set contains the vertices not yet included. At every step, it considers all the edges that connect the two sets and picks the minimum weight edge from these edges. After picking the edge, it moves the other endpoint of the edge to the set containing MST. This is also a Greedy Algorithm.
Below are steps for finding MST using Prim's algorithm:
- Determine an arbitrary vertex as the starting vertex of the MST.
- Follow steps 3 to 5 till there are vertices that are not included in the MST (known as fringe vertex).
- Find edges connecting any tree vertex with the fringe vertices.
- Find the minimum among these edges.
- Add the chosen edge to the MST if it does not form any cycle.
- Return the MST and exit
For more detailed explanation, go to this article.
Boruvka's Minimum Spanning Tree Algorithm
Boruvka's algorithm is a greedy algorithm for finding a minimum spanning tree in a graph, or a minimum spanning forest in the case of a graph that is not connected. The idea is the same as Prim’s MST algorithm. Boruvka’s algorithm is the oldest minimum spanning tree algorithm that was discovered by Boruvka in 1926, long before computers even existed. The algorithm was published as a method of constructing an efficient electricity network.
Below are the steps for finding MST using Boruvka's algorithm:
- Input is a connected, weighted and un-directed graph.
- Initialize all vertices as individual components (or sets).
- Initialize MST as empty.
- While there are more than one components, do following for each component: (a) Find the closest weight edge that connects this component to any other component. (b) Add this closest edge to MST if not already added.
- Return MST.
Which Algorithm is the Better One?
The choice of the better algorithm depends on the characteristics of the graph and requirement of the application. However, Both Prim's and Kruskal's algorithms are efficient algorithms but they have different strengths and weaknesses. Here are some properties of Prim's Algorithm:
- Starts to build the MST from any vertex in the graph.
- Traverses one node more than one time to get the minimum distance.
- Has a time complexity of O(V²), which can be improved up to O(E log V) using Fibonacci heaps.
- Gives a connected component as it works only on connected graphs.
- Runs faster for dense graphs.
Some properties of Kruskal's Algorithm:
- Starts to build the MST from the vertex carrying minimum weight in the graph.
- Traverses one node only once.
- Has a time complexity of O(E log V).
- Can generate a forest (disconnected components) at any instant and can work on disconnected components.
- Runs faster for sparse graphs.
Prim's algorithm is generally faster than Kruskal's algorithm for Dense graphs, where the number of edges is close to the maximum possible number of edges. Prim's algorithm is also simpler to implement and easier to understand.
An Example of Dense Graphs
Kruskal's algorithm is generally faster than Prim's algorithm for sparse graphs, where the number of edges is much smaller than the maximum possible number of edges. Kruskal's algorithm is also more efficient in terms of memory usage.
An Example of Sparse Graphs
Conclusion:
So, neither algorithm is universally “better”. The choice between Prim’s and Kruskal’s algorithm depends on the specific characteristics and requirements of the problem you’re trying to solve. For example, if you’re working with a Dense graph, Prim’s algorithm might be more efficient, while for sparse graphs, Kruskal’s algorithm could be a better choice.
Similar Reads
Boruvka's algorithm for Minimum Spanning Tree
Following two algorithms are generally taught for Minimum Spanning Tree (MST) problem. Prim's algorithm Kruskal's algorithm There is a third algorithm called Boruvka's algorithm for MST which (like the above two) is also Greedy algorithm. The Boruvka's algorithm is the oldest minimum spanning tree a
1 min read
Kruskalâs Minimum Spanning Tree (MST) Algorithm
A minimum spanning tree (MST) or minimum weight spanning tree for a weighted, connected, and undirected graph is a spanning tree (no cycles and connects all vertices) that has minimum weight. The weight of a spanning tree is the sum of all edges in the tree. In Kruskal's algorithm, we sort all edges
9 min read
Reverse Delete Algorithm for Minimum Spanning Tree
Reverse Delete algorithm is closely related to Kruskal's algorithm. In Kruskal's algorithm what we do is : Sort edges by increasing order of their weights. After sorting, we one by one pick edges in increasing order. We include current picked edge if by including this in spanning tree not form any c
14 min read
Spanning Tree With Maximum Degree (Using Kruskal's Algorithm)
Given an undirected unweighted connected graph consisting of n vertices and m edges. The task is to find any spanning tree of this graph such that the maximum degree over all vertices is maximum possible. The order in which you print the output edges does not matter and an edge can be printed in rev
11 min read
Primâs Algorithm for Minimum Spanning Tree (MST)
Primâs algorithm is a Greedy algorithm like Kruskal's algorithm. This algorithm always starts with a single node and moves through several adjacent nodes, in order to explore all of the connected edges along the way. The algorithm starts with an empty spanning tree. The idea is to maintain two sets
15+ min read
What is Minimum Spanning Tree (MST)
A spanning tree is defined as a tree-like subgraph of a connected, undirected graph that includes all the vertices of the graph. Or, to say in Layman's words, it is a subset of the edges of the graph that forms a tree (acyclic) where every node of the graph is a part of the tree. The minimum spannin
7 min read
Maximum Spanning Tree using Primâs Algorithm
Given undirected weighted graph G, the task is to find the Maximum Spanning Tree of the Graph using Prim's Algorithm Prims algorithm is a Greedy algorithm which can be used to find the Minimum Spanning Tree (MST) as well as the Maximum Spanning Tree of a Graph. Examples: Input: graph[V][V] = {{0, 2,
15+ min read
Minimum Bottleneck Spanning Tree(MBST)
The minimum bottleneck spanning tree in an undirected graph is a tree whose most expensive edge is as minimum as possible. In this article, we will understand more about how to identify a minimum bottleneck spanning tree and understand that every minimum spanning tree is a minimum bottleneck spannin
4 min read
Applications of Minimum Spanning Tree
A Minimum Spanning Tree (MST) is a subset of the edges of a connected, undirected graph that connects all the vertices together, without any cycles and with the minimum possible total edge weight. It is a way to connect all the vertices in a graph in a way that minimizes the total weight of the edge
3 min read
Minimum spanning tree cost of given Graphs
Given an undirected graph of V nodes (V > 2) named V1, V2, V3, ..., Vn. Two nodes Vi and Vj are connected to each other if and only if 0 < | i - j | ? 2. Each edge between any vertex pair (Vi, Vj) is assigned a weight i + j. The task is to find the cost of the minimum spanning tree of such gra
4 min read