The Wayback Machine - https://web.archive.org/web/20241122230523/https://www.geeksforgeeks.org/what-is-minimum-spanning-tree-mst/
Open In App

What is Minimum Spanning Tree (MST)

Last Updated : 12 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

A minimum spanning tree (MST) is defined as a spanning tree that has the minimum weight among all the possible spanning trees

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 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.

Minimum Spanning Tree (MST)

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.
  • There is a fixed number of edges in the spanning tree which is equal to one less than the total number of vertices ( E = V-1 ).
  • The spanning tree should not be disconnected, as in 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. 

Minimum Spanning Tree:

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.

  • Let’s look at the MST of the above example Graph,
     

Minimum Spanning Tree

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:

This is one of the popular algorithms for finding the minimum spanning tree from a connected, undirected graph. This is a greedy algorithm. The algorithm workflow is as below:

  • First, it sorts all the edges of the graph by their weights, 
  • Then starts the iterations of finding the spanning tree. 
  • At each iteration, the algorithm adds the next lowest-weight edge one by one, such that the edges picked until now does not form a cycle.

This algorithm can be implemented efficiently using a DSU ( Disjoint-Set ) data structure to keep track of the connected components of the graph. This is used in a variety of practical applications such as network design, clustering, and data analysis.

Follow the article on “Kruskal’s Minimum Spanning Tree Algorithm” for a better understanding and implementation of the algorithm.

Prim’s Minimum Spanning Tree Algorithm:

This is also a greedy algorithm. This algorithm has the following workflow:

  • It starts by selecting an arbitrary vertex and then adding it to the MST. 
  • Then, it repeatedly checks for the minimum edge weight that connects one vertex of MST to another vertex that is not yet in the MST. 
  • This process is continued until all the vertices are included in the MST. 

To efficiently select the minimum weight edge for each iteration, this algorithm uses priority_queue to store the vertices sorted by their minimum edge weight currently. It also simultaneously keeps track of the MST using an array or other data structure suitable considering the data type it is storing.

This algorithm can be used in various scenarios such as image segmentation based on color, texture, or other features. For Routing, as in finding the shortest path between two points for a delivery truck to follow.

Follow the article on “Prim’s Minimum Spanning Tree Algorithm” for a better understanding and implementation of this algorithm.

Boruvka’s Minimum Spanning Tree Algorithm:

This is also a graph traversal algorithm used to find the minimum spanning tree of a connected, undirected graph. This is one of the oldest algorithms. The algorithm works by iteratively building the minimum spanning tree, starting with each vertex in the graph as its own tree. In each iteration, the algorithm finds the cheapest edge that connects a tree to another tree, and adds that edge to the minimum spanning tree. This is almost similar to the Prim’s algorithm for finding the minimum spanning tree. The algorithm has the following workflow:

  • Initialize a forest of trees, with each vertex in the graph as its own tree.
  • For each tree in the forest: 
    • Find the cheapest edge that connects it to another tree. Add these edges to the minimum spanning tree.
    • Update the forest by merging the trees connected by the added edges.
  • Repeat the above steps until the forest contains only one tree, which is the minimum spanning tree.

The algorithm can be implemented using a data structure such as a priority queue to efficiently find the cheapest edge between trees. Boruvka’s algorithm is a simple and easy-to-implement algorithm for finding minimum spanning trees, but it may not be as efficient as other algorithms for large graphs with many edges.

Follow the article on “Boruvka’s Minimum Spanning Tree Algorithm” for a better understanding and implementation of this algorithm.

To knowing more about the properties and characteristics of Minimum Spanning Tree, click here.

Applications of Minimum Spanning Trees:

  • Network design: Spanning trees can be used in network design to find the minimum number of connections required to connect all nodes. Minimum spanning trees, in particular, can help minimize the cost of the connections by selecting the cheapest edges.
  • Image processing: Spanning trees can be used in image processing to identify regions of similar intensity or color, which can be useful for segmentation and classification tasks.
  • Biology: Spanning trees and minimum spanning trees can be used in biology to construct phylogenetic trees to represent evolutionary relationships among species or genes.
  • Social network analysis: Spanning trees and minimum spanning trees can be used in social network analysis to identify important connections and relationships among individuals or groups.

Some popular interview problems on MST

1. Find the Minimum Cost to Connect All Cities Practice
2. Detect Cycle in an Undirected Graph Practice
3. Bridge in a Graph Practice
4. Minimum Product Spanning Tree Practice
5. Maximum Spanning Tree using Kruskal’s Algorithm Practice
6. Second Minimum Spanning Tree Practice
7. Steiner Tree Practice
8. Find the weight of the minimum spanning tree Practice
9. Second Best Minimum Spanning Tree Practice
10. Minimum spanning tree cost of given Graphs Practice

What is Minimum Spanning Tree (MST) – FAQs

Can there be multiple minimum-spanning trees for a given graph?

Yes, a graph can have multiple minimum spanning trees if there are multiple sets of edges with the same minimum total weight.

Can Kruskal’s algorithm and Prim’s algorithm be used for directed graphs?

No, Kruskal’s algorithm and Prim’s algorithm are designed for undirected graphs only.

Can a disconnected graph have a minimum spanning tree?

No, a disconnected graph cannot have a spanning tree because it does not span all the vertices. Therefore, it also cannot have a minimum spanning tree.

Can a minimum spanning tree be found using Dijkstra’s algorithm?

No, Dijkstra’s algorithm is used to find the shortest path between two vertices in a weighted graph. It is not designed to find a minimum spanning tree.

What is the time complexity of Kruskal’s and Prim’s algorithms?

Both Kruskal’s and Prim’s algorithms have a time complexity of O(ElogE), where E is the number of edges in the graph.



Previous Article
Next Article

Similar Reads

Properties of Minimum Spanning Tree (MST)
For a connected and undirected graph, a spanning tree of that graph is a subgraph that is a tree and connects all the vertices together. A single graph can have multiple spanning trees. A Spanning Tree is a tree which have V vertices and V-1 edges. All nodes in a spanning tree are reachable from each other. A Minimum Spanning Tree(MST) or minimum w
4 min read
Kruskal’s Minimum Spanning Tree (MST) Algorithm
A minimum spanning tree (MST) or minimum weight spanning tree for a weighted, connected, undirected graph is a spanning tree with a weight less than or equal to the weight of every other spanning tree. To learn more about Minimum Spanning Tree, refer to this article. Introduction to Kruskal's Algorithm:Here we will discuss Kruskal's algorithm to fi
15+ min read
Prim’s Algorithm for Minimum Spanning Tree (MST)
Introduction to Prim's algorithm:We have discussed Kruskal's algorithm for Minimum Spanning Tree. Like Kruskal's algorithm, Prim’s algorithm is also a Greedy 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
15+ min read
Spanning Tree vs Minimum Spanning Tree
Spanning Tree (ST):A spanning tree of a graph is a subgraph that includes all the vertices of the original graph and is also a tree (a connected acyclic graph). The primary goal of a spanning tree is to connect all vertices with the minimum number of edges. Uses of Spanning Tree: STs are used in network design and routing algorithms to ensure conne
2 min read
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 algorithm was discovered by Boruvka in 1926, long b
1 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 graph with V nodes. Examples: Input: V = 4 Output: 13
4 min read
Find the minimum spanning tree with alternating colored edges
Given a graph with N nodes and M edges where each edge has a color (either black or green) and a cost associated with it. Find the minimum spanning tree of the graph such that every path in the tree is made up of alternating colored edges. Examples: Input: N = 3, M = 4 Output: 6 Input: N = 4, M = 6 Output: 4 Approach: The first observation we make
8 min read
Minimum Spanning Tree using Priority Queue and Array List
Given a bi-directed weighted (positive) graph without self-loops, the task is to generate the minimum spanning tree of the graph.Examples: Input: N = 9, E = 14, edges = {{0, 1, 4}, {0, 7, 8}, {1, 2, 8}, {1, 7, 11}, {2, 3, 7}, {2, 8, 2}, {2, 5, 4}, {3, 4, 9}, {3, 5, 14}, {4, 5, 10}, {5, 6, 2}, {6, 7, 1}, {6, 8, 6}, {7, 8, 7}} Output: ((A, B), Cost)
5 min read
Second Best Minimum Spanning Tree
Prerequisites - Graph, Spanning tree, Disjoint Set (Union - Find). A minimum spanning tree (MST) T, for a given graph G, spans over all vertices of a given graph and has minimum weight sum of all edges, out of all the possible spanning trees. Second best MST, T’, is a spanning tree with the second minimum weight sum of all edges, out of all spannin
15+ min read
Difference between Minimum Spanning Tree and Shortest Path
Spanning tree: A spanning tree (T) of an undirected graph(G) is a subgraph which is a tree that includes all the vertices of a graph (G) and the minimum number of edges required to connect the graph (G). And it is a known maximal set of edges with no cycles. Properties: If a graph(G) is not connected then it does not contain a spanning tree (i.e. i
4 min read
Check if an edge is a part of any Minimum Spanning Tree
Given a connected undirected weighted graph in the form of a 2D array where each row is of the type [start node, end node, weight] describing an edge, and also two integers (A, B). Return if the edge formed between (A, B) is a part of any of the Minimum Spanning Tree (MST) of the graph. Minimum Spanning Tree (MST): This is a special subgraph of the
13 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 edges in the tree. MST is a fundamental problem with d
3 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 spanning tree. A minimum spanning tree is completely diff
4 min read
Which Minimum Spanning Tree Algorithm is better?
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 TreeTh
6 min read
Kruskal's Minimum Spanning Tree using STL in C++
Given an undirected, connected and weighted graph, find Minimum Spanning Tree (MST) of the graph using Kruskal's algorithm. Input : Graph as an array of edges Output : Edges of MST are 6 - 7 2 - 8 5 - 6 0 - 1 2 - 5 2 - 3 0 - 7 3 - 4 Weight of MST is 37 Note : There are two possible MSTs, the other MST includes edge 1-2 in place of 0-7. We have disc
6 min read
Minimum Product Spanning Tree
Given a connected and undirected graph, a spanning tree of that graph is a subgraph that is a tree and connects all the vertices together. A single graph can have many different spanning trees. A minimum product spanning tree for a weighted, connected, and undirected graph is a spanning tree with a weight product less than or equal to the weight pr
15+ 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 cycle until there are V-1 edges in spanning tree, w
14 min read
Find the weight of the minimum spanning tree
Given a connected undirected weighted graph with N nodes and M edges. The task is to perform given queries and find the weight of the minimum spanning tree. Queries are of three types:   query(1) -> Find the weight of the minimum spanning tree.query(2, x, y) -> Change the weight of the edge between the nodes x and y to 0.query(3, x, y) ->
14 min read
Approximate solution for Travelling Salesman Problem using MST
We introduced Travelling Salesman Problem and discussed Naive and Dynamic Programming Solutions for the problem in the previous post,. Both of the solutions are infeasible. In fact, there is no polynomial time solution available for this problem as the problem is a known NP-Hard problem. There are approximate algorithms to solve the problem though.
3 min read
Why Prim’s and Kruskal's MST algorithm fails for Directed Graph?
Pre-requisites: Graph and its representations Greedy Algorithms | Set 5 (Prim’s Minimum Spanning Tree (MST)) Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2 Given a directed graph D = < V, E >, the task is to find the minimum spanning tree for the given directed graph Example: But the Prim's Minimum Spanning Tree and Kruskal's algor
2 min read
Find weight of MST in a complete graph with edge-weights either 0 or 1
Given an undirected weighted complete graph of N vertices. There are exactly M edges having weight 1 and rest all the possible edges have weight 0. The array arr[][] gives the set of edges having weight 1. The task is to calculate the total weight of the minimum spanning tree of this graph. Examples: Input: N = 6, M = 11, arr[][] = {(1 3), (1 4), (
10 min read
Prim’s MST for Adjacency List Representation | Greedy Algo-6
We recommend reading the following two posts as a prerequisite to this post. Greedy Algorithms | Set 5 (Prim’s Minimum Spanning Tree (MST)) Graph and its representationsWe have discussed Prim's algorithm and its implementation for adjacency matrix representation of graphs. The time complexity for the matrix representation is O(V^2). In this post, O
15+ min read
Construct MST from GCD
Given an array arr[] of N integers. and an integer K. Construct a weighted undirected graph of N vertices numbered from 0 to N-1. An edge between the vertices i and j (j>i) is added if any one of the following conditions is satisfied: If j = i + 1, add an edge of weight K between vertices i and j.If min(ai , ai+1 ,....., aj) = gcd(ai , ai+1 ,...
15+ min read
Boruvka's MST in Python
Boruvka's Algorithm is a Greedy Algorithm to find the Minimum Spanning Tree of a given Graph. In this article, we'll discuss about the basics of Boruvka's algorithm and its implementation in Python. Idea behind Boruvka's Algorithm:The idea behind Boruvka's Algorithm is similar to Prim's Algorithm. A Spanning tree means all vertices must be connecte
4 min read
Difference between Prim's and Kruskal's algorithm for MST
Minimum Spanning Tree (MST) is a fundamental concept in graph theory and has various applications in network design, clustering, and optimization problems. Two of the most commonly used algorithms to find the MST of a graph are Prim's and Kruskal's algorithms. Although both algorithms achieve the same goal, they do so in different ways. In this art
3 min read
Types of Spanning Tree Protocol (STP)
Prerequisite - Spanning Tree Protocol Spanning Tree Protocol (STP) is used to make a loop free network by monitoring the network to track all the links and shut down the least redundant ones. Root bridge is a switch in a single VLAN or whole topology (according to the type of STP standard used) which is responsible for distributing BPDUs and block
3 min read
Maximum Possible Edge Disjoint Spanning Tree From a Complete Graph
Give a complete graph with N-vertices. The task is to find out the maximum number of edge-disjoint spanning tree possible.Edge-disjoint Spanning Tree is a spanning tree where no two trees in the set have an edge in common. Examples: Input : N = 4 Output : 2 Input : N = 5 Output : 2 The maximum number of possible Edge-Disjoint Spanning tree from a c
3 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 reverse also i.e. (u, v) can also be printed as (v, u
11 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, 0, 6, 0}, {2, 0, 3, 8, 5}, {0, 3, 0, 0, 7}, {6, 8
15+ min read
Spanning Tree
In this article, we are going to cover one of the most commonly asked DSA topic which is the Spanning Tree with its definition, properties, and applications. Moreover, we will explore the Minimum Spanning Tree and various algorithms used to construct it. These algorithms have various pros and cons over each other depending on the use case of the pr
7 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg