Open In App

Which Minimum Spanning Tree Algorithm is better?

Last Updated : 23 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  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 the cycle is not formed, include this edge. Else, discard it.
  3. 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:

  1. Determine an arbitrary vertex as the starting vertex of the MST.
  2. Follow steps 3 to 5 till there are vertices that are not included in the MST (known as fringe vertex).
  3. Find edges connecting any tree vertex with the fringe vertices.
  4. Find the minimum among these edges.
  5. Add the chosen edge to the MST if it does not form any cycle.
  6. 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:

  1. Input is a connected, weighted and un-directed graph.
  2. Initialize all vertices as individual components (or sets).
  3. Initialize MST as empty.
  4. 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.
  5. 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.

Dense-Graphsdrawio

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.

SparseGraphsdrawio

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads