## 1. Overview

In this tutorial, **we’ll discuss the minimum spanning tree and how to find the total number of minimum spanning trees in a graph.**

## 2. Definition of a Spanning Tree

Let’s start with a formal definition of a spanning tree. Then we’ll define the minimum spanning tree based on that.

**For a given graph ****, a spanning tree can be defined as the subset of **** which covers all the vertices of **** with the minimum number of edges****.**

Let’s simplify this further. Say we have a graph with the vertex set , and the edge set . A spanning tree on is a subset of where and . So the spanning tree contains all the vertices of the given graph but not all the edges.

Also, we should note that a spanning tree covers all the vertices of a given graph so it can’t be disconnected. By the general property, a spanning tree can’t contain any cycles.

A minimum spanning tree (MST) can be defined on an undirected weighted graph. An MST follows the same definition of a spanning tree. The only catch here is that we need to select the minimum number of edges to cover all the vertices in a given graph in such a way that **the total edge weights of the selected edges are at a minimum**.

Now, let’s try a graph with . Like a spanning tree, a minimum spanning tree will also contain all the vertices of the graph . Hence, . The edge set of is the subset of with an objective function:

Here, denotes the total number of edges in the minimum spanning tree . The objective function denotes the sum of all the edge weights in , and it should be a minimum among all other spanning trees.

## 3. Some Examples

In this section, let’s take a graph and construct spanning trees and associated minimum spanning trees to understand the concepts more clearly.

First, let’s take an undirected weighted graph:

Here, we’ve taken an undirected weighted graph . Now from the graph , we’ll construct a couple of spanning trees by following the definition of a spanning tree.

Also, we should note that while building the spanning tree, we won’t bother with the edge weights:

Here we’ve constructed four spanning trees from the graph . Each of the spanning trees covers all the vertices of the graph and none have a cycle.

Now let’s discuss how we can find the minimum spanning tree for the graph . So as per the definition, a minimum spanning tree is a spanning tree with the minimum edge weights among all other spanning trees in the graph.

To find the minimum spanning tree, we need to calculate the sum of edge weights in each of the spanning trees. The sum of edge weights in are and . Hence, has the smallest edge weights among the other spanning trees. **Therefore, **** is a minimum spanning tree in the graph ****.**

## 4. Properties

Let’s list out a couple of properties of a spanning tree. As a minimum spanning tree is also a spanning tree, these properties will also be true for a minimum spanning tree.

**In a spanning tree, the number of edges will always be** .

For example, let’s have another look at the spanning trees , and . The original graph has vertices, and each of the spanning trees contains four edges.

**A spanning tree doesn’t contain any loops or cycles.**

We can see none of the spanning trees and contain any loops or cycles.

**If we remove any one edge from the spanning tree, it will make it disconnected.**

Let’s consider the spanning tree . If we remove any of the edges, it will make it disconnected.

**If we add one edge in a spanning tree, then it will create a cycle.**

Again we’re considering the spanning tree . If we add any new edge let’s say the edge or , it will create a cycle in .

## 5. Total Number of MSTs

In this section, we’ll discuss two algorithms to find the total number of minimum spanning trees in a graph.

### 5.1. When the Graph Is a Complete Graph

If the given graph is complete, then finding the total number of spanning trees is equal to the counting trees with a different label. Using Cayley’s formula, we can solve this problem. According to Cayley?s formula, a graph with vertices can have different labeled trees.

Therefore, we can say that **the total number of spanning trees in a complete graph would be equal to** .

Now to find the minimum spanning tree among all the spanning trees, we need to calculate the total edge weight for each spanning tree. A minimum spanning tree is a spanning tree with the smallest edge weight among all the spanning trees.

Now let’s see the pseudocode:

Here, the variable denotes the total number of spanning trees in the graph. The variable is an array that stores the edge list of spanning trees with their weights. Next, we calculated the sum of edge weights for each spanning trees and stored it in . The minimum value in corresponds to the minimum spanning tree.

Finally, we use the variable to denote the total number of minimum spanning trees in the graph.

### 5.2. When the Graph Is Not Complete

If the given graph is not complete, then we can use the Matrix Tree algorithm to find the total number of minimum spanning trees.

Let’s first see the pseudocode then we’ll discuss the steps in detail:

**The first step of the algorithm is to create an adjacency matrix from the given graph.** Here denotes an adjacency matrix and is the dimension of the matrix. We should note that in the adjacency matrix, **we’ll not consider the edge weights.**

**The next step is to create a degree matrix from the graph**. We can create a degree matrix from the adjacency matrix. We need to replace all the diagonal elements with the degree of the vertices in the graph and all other elements to zero. The variable denotes the degree matrix corresponding to the graph. **Again, we’re not considering edge weights here.**

Now **we calculate the Laplacian matrix** by subtracting the adjacency matrix from the degree matrix. The variable represents the Laplacian matrix of the given graph.

To find the total number of spanning trees in the given graph, **we need to calculate the cofactor of any elements in the Laplacian matrix.** This number is equivalent to the total number of the spanning trees in the graph. **The general formula of calculation cofactor in a matrix is:** , where is the index of the matrix.

In this algorithm, we’ve decided to calculate the cofactor for the value and , which is denoted by the variable . One can choose any value for .

Next, we store the edge list of each spanning tree with their weights in . Like the previous algorithm, we calculate the sum of edge weights of each spanning tree denoted by . Parallelly, we also store the sum of weights in the list . The smallest entry in is the minimum spanning tree.

To find the total number of minimum spanning trees, we find the occurrence of the smallest entry in . The variable gives us the total number of minimum spanning trees in the given graph.

## 6. Running the Algorithm on a Graph

In this section, we’ll take two graphs: one is a complete graph, and the other one is not a complete graph. For both of the graphs, we’ll run our algorithm and find the number of minimum spanning tree exists in the given graph.

First, let’s take a complete undirected weighted graph:

We’ve taken a graph with vertices. According to our algorithm, the total number of spanning trees in would be: . Let’s list out the spanning trees:

Now to find the minimum spanning tree among the spanning trees, we need to calculate the weights of each spanning tree: , , . We can see that the spanning tree has the smallest weight among all the spanning trees. Therefore, the number of minimum spanning trees in is .

Next, let’s take a graph which is not a complete graph:

We’re taking a graph here with vertices. Now the first step is to construct the adjacency matrix of without taking the edge weights:

Then we’ll construct a degree matrix corresponding to the graph . Again we’ll not consider the edge weights here:

Next, we’ll create a Laplacian matrix by subtracting the adjacency matrix from the degree matrix:

We’re done with the Laplacian matrix. The next step is to calculate any of the positive cofactors from Laplacian matrix. The general formula is : . Let’s calculate for and :

Hence, the number of spanning trees in the graph is :

We’re going to calculate the sum of edge weights for each of the spanning tree here. The weights of the spanning trees are: .

So clearly, the smallest edge weight among the spinning trees is . Now in our algorithm, we defined a variable that counts the occurrence of the smallest edge weight in the list where all the weights of the spanning trees are stored. We can see the edge weight occurs three times in the , which corresponds to .

**Therefore, we applied our algorithm on the graph **** and found out that the total number of spanning trees in ** **is **** and the total number of minimum spanning trees is** .

## 7. Time Complexity Analysis

In the case of a complete graph, the time complexity of the algorithm depends on the loop where we’re calculating the sum of the edge weights of each spanning tree. The loop runs for all the vertices in the graph. Hence **the time complexity of the algorithm would be** .

In case the given graph is not complete, we presented the matrix tree algorithm. Among all the operations, the most expensive one is finding the determining of the matrix. It takes time. Therefore **the total time complexity of the algorithm would be** .

## 8. Applications of MST

An important application of the minimum spanning tree is to find the paths on the map.

The minimum spanning tree is used to design networks like telecommunication networks, water supply networks, and electrical grids.

Practical applications like cluster analysis, image segmentation, handwriting recognition all use the minimum spanning tree concept.

## 9. Conclusion

In this tutorial, we’ve discussed how to find the total number of spanning trees and minimum spanning trees in a graph.

We’ve presented two algorithms for two different cases and explained each step in detail. To verify the presented algorithms, we tested it by running the algorithms on two sample graphs.