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

## Minimum spanning tree algorithms

There are two methods (algorithms) to construct a minimum spanning tree.

1. Kruskal’s minimum spanning tree
2. 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:
1. Select vertex u, which is not included in the MST set and has the minimum key value. Add vertex u to the MST set.
2. 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

1. How many minimum spanning trees can be created for a graph?
1. 100
2. Many
3. Two
4. One

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?

2. Greedy algorithm
3. Cut property vertex
4. Node sorting algorithm

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?

1. It is a tree that spans G
2. It is a subgraph of G
3. It consists of every vertex of the graph
4. It can be cyclic or acyclic

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?

1. Spanning trees do not have cycles
2. MST has same number of edges as that of the graph
3. Spanning tree has a cycle.
4. Both b and c

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

1. Prim’s algorithm
2. Bellman-Ford algorithm
3. All of the above

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.

• 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?

We've got you covered with step-by-step solutions to millions of textbook problems, subject matter experts on standby 24/7 when you're stumped, and more.
Check out a sample computer science Q&A solution here!

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

Tagged in
EngineeringComputer Science