What is a spanning tree?
A spanning tree is a sub-graph of a connected, undirected graph. It has all the vertices of a graph with the minimum possible number of edges. If a vertex is skipped, then it will not be a spanning tree. The edges of a spanning tree may or may not have weights.
Properties of a spanning tree
A spanning tree has the following features:
- There can be multiple minimum spanning trees of the same weight having the minimum number of edges.
- If all edge weights of a graph are equal, then every spanning tree of the graph will be minimum.
- If every edge has a different weight, then only one minimum spanning tree exists.
- A connected graph can have multiple spanning trees.
- A disconnected graph cannot have a spanning tree.
- The spanning tree is acyclic.
- If a complete graph has n vertices, the spanning tree will have n-1 edges.
- If a single new edge is added to the spanning tree, it will lose its property of acyclicity.
- If a single edge is removed from a spanning tree, the tree will lose its connectivity property.
Minimum spanning tree
A minimum spanning tree (MST) or minimum weight spanning tree is a spanning tree that connects every vertex with the minimum total cost. In brief, it is a spanning tree whose sum of weighted edges is the least. Like spanning trees, a graph can have multiple minimum spanning trees. For instance, consider the following graph:
Here, figure 1 shows a weighted graph, and figure 2 shows one of the possible minimum spanning trees. The sum of the minimum weight edges of the spanning tree in figure 2 is 10.
A minimum spanning tree is used for the following:
- Finding paths in a map.
- Designing networks like water supply networks.
- To measure traffic load.
Minimum spanning tree algorithms
There are two methods (algorithms) to construct a minimum spanning tree.
- Kruskal’s minimum spanning tree
- Prim’s minimum spanning tree
Both algorithms are based on the greedy approach.
Kruskal’s minimum spanning tree algorithm
Kruskal’s algorithm is used to determine a minimum spanning tree for a weighted undirected graph. The algorithm adds the next edge that does not form a cycle in the MST at every step.
Algorithm
- Sort the edges of the graph G in the order of increasing (non-decreasing) weight.
- Sequentially add each minimum weight edge that does not form a cycle until n-1 edges are used.
- Exit the algorithm.
Pseudocode
All MST algorithms need to check if a cycle is formed after adding a new edge to the graph. The best way to find out if a cycle is formed or not is using the Union-Find algorithm. This algorithm divides vertices into clusters to check if the vertices belong to the same cluster. Eventually, this helps identify if a loop is created or not.
Kruskal’s MST algorithm follows the same method. Following is the pseudocode for Kruskal’s minimum spanning algorithm.
MST-KRUSKAL(G,w)
A = ∅
for each vertex v ∈ V [G]
do MAKE-SET (v)
sort edges of E into increasing order by weight w
do if FIND-SET (u) ≠ FIND-SET(v):
Then A = A ∪ {(u, v)}
UNION(u, v)
return A
Example
Consider the following graph:
Now, by using Kruskal’s algorithm to find the minimum spanning tree for the graph, the solution will be as follows:
Step 1: Kruskal’s algorithm selects the edge having the least weight at every stage. Here, the least-weighted edge is AE with weight 1. So, the algorithm will select that edge.
Step 2: The next least-weighted edge is CD with weight 2.
Step 3: The next least-weighted edge is AC with weight 3. So, the algorithm will pick the edge AC and add it to the tree.
Step 4: Finally, the next least-weighted edge is AD with weight 4, but it creates a cycle. So, the algorithm will pick the edge AB with weight 5 and add it to the tree. Now all nodes are visited. Hence, the algorithm will terminate. The total cost of the spanning tree will be 1+2+3+5 = 11.
Prim’s minimum spanning algorithm
Prim’s minimum spanning tree algorithm is also a greedy algorithm. Unlike Kruskal’s algorithm, Prim’s MST algorithm considers each node a single tree and continues adding new nodes to the spanning tree from a given graph.
The algorithm takes a graph as an input and determines the subset edges of the graph such that they satisfy the following conditions:
- Form a tree that has all the vertices in the graph.
- The sum of weights is the minimum among all possible trees that can be formed from the graph.
After selecting an edge, it proceeds to the other endpoint of the edge.
In other words, the algorithm finds a subset of edges that create a tree comprising of all vertices and the total weight of all the edges is minimum. It is a greedy algorithm because it begins with one vertex and selects the least weighted connection from the graph and adds it to the tree, at each step.
Algorithm
The process to find the MST, using Prim’s algorithm is as follows:
- Create a minimum spanning tree that tracks the vertices already included in the MST.
- Give key values to all nodes in the input graph. Initialize the key values as infinite.
- For the first vertex, assign the key value as 0 so that the vertex is selected first.
- Until the minimum spanning tree set does not include all vertices:
- Select vertex u, which is not included in the MST set and has the minimum key value. Add vertex u to the MST set.
- Update the key value of all adjacent nodes of vertex u. For updations, iterate across all adjacent nodes. For every adjacent vertex v, if the weight of (u,v) is less than the previous key value of v, update the key value as a weight of (u,v).
Pseudocode
MST-PRIM (G, w, r)
for each u ∈ V [G]
do key [u] = ∞
π [u] = NIL
key[r] = 0
Q = V[G]
while Q ? ∅
do u = EXTRACT - MIN (Q)
for each v ∈ Adj [u]
do if v ∈ Q and w (u, v) < key [v]
then π [v] = u
key[v] = w(u,v)
Example
Consider the graph below:
Now, by using Prim’s algorithm to find the minimum spanning tree for the graph, the solution will be as follows:
Step 1: Prim’s algorithm begins by selecting any arbitrary node and marks the adjacent vertex in each iteration. Here, the algorithm begins with node B and marks the least-weighted adjacent edge which is AB.
Step 2: Next, the algorithm selects the least-weighted adjacent edge which is BC.
Step 3: Lastly, the algorithm selects the least-weighted adjacent edge that does not form a cycle which is AD.
So, the total weight of the minimum spanning tree will be 1+2+4 = 7.
Context and Applications
Minimum spanning tree is an essential topic in data structures. This topic is studied by students pursuing courses like:
- Bachelors in Technology (Computer Science)
- Bachelors in Science (Software Development)
- Masters in Science (Software Development)
- Masters in Technology (Computer Science)
Practice Problems
- How many minimum spanning trees can be created for a graph?
- 100
- Many
- Two
- One
Answer: Option b
Explanation: One undirected, connected graph can have multiple spanning and minimum spanning trees.
2. Which of the following approach is used in Kruskal and Prim’s algorithm to find the minimum spanning tree?
- Breadth-first search
- Greedy algorithm
- Cut property vertex
- Node sorting algorithm
Answer: Option b
Explanation: Both Kruskal and Prim’s algorithms follow the greedy approach to generate minimum spanning trees.
3. Which of the following is false for a spanning tree of graph G?
- It is a tree that spans G
- It is a subgraph of G
- It consists of every vertex of the graph
- It can be cyclic or acyclic
Answer: Option d
Explanation: A graph can have several spanning trees, and each spanning tree is a subgraph of the graph. Spanning trees also contain every vertex of the graph and are always acyclic.
4. Which of the following is false for a spanning tree of graph G?
- Spanning trees do not have cycles
- MST has same number of edges as that of the graph
- Spanning tree has a cycle.
- Both b and c
Answer: Option d
Explanation: Minimum spanning trees have n-1 edges when the graph has a total number of n edges. Also, they do not have cycles. Spanning tree is acyclic.
5. Which of the following is not an algorithm to find MST?
Kruskal’s algorithm
- Prim’s algorithm
- Bellman-Ford algorithm
- All of the above
Answer: Option c
Explanation: Prim’s algorithm and Kruskal’s algorithm are the greedy algorithms used to find MST of a given graph. Bellman-Ford algorithm is used to find the shortest path from one point to other vertices.
Related Concepts
- Dijkstra’s shortest pathfinding algorithm
- Non-deterministic polynomial-time hardness (NP-hardness)
- Greedy algorithms
- Sorting in linear-time
- Asymptotic analysis
Want more help with your computer science homework?
*Response times may vary by subject and question complexity. Median response time is 34 minutes for paid subscribers and may be longer for promotional offers.
Search. Solve. Succeed!
Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.
Minimum Spanning Tree Homework Questions from Fellow Students
Browse our recently answered Minimum Spanning Tree homework questions.
Search. Solve. Succeed!
Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.