Which Minimum Spanning Tree Algorithm is better?
Last Updated :
23 Jul, 2025
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
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem