Open in App
Not now

# Total number of Spanning Trees in a Graph

• Difficulty Level : Hard
• Last Updated : 29 Mar, 2023

If a graph is a complete graph with n vertices, then total number of spanning trees is n(n-2) where n is the number of nodes in the graph. In complete graph, the task is equal to counting different labeled trees with n nodes for which have Cayley’s formula.

What if graph is not complete?

• STEP 1: Create Adjacency Matrix for the given graph.
• STEP 2: Replace all the diagonal elements with the degree of nodes. For eg. element at (1,1) position of adjacency matrix will be replaced by the degree of node 1, element at (2,2) position of adjacency matrix will be replaced by the degree of node 2, and so on.
• STEP 3: Replace all non-diagonal 1’s with -1.
• STEP 4: Calculate co-factor for any element.
• STEP 5: The cofactor that you get is the total number of spanning tree for that graph.

Consider the following graph:

Adjacency Matrix for the above graph will be as follows:

After applying STEP 2 and STEP 3, adjacency matrix will look like

The co-factor for (1, 1) is 8. Hence total no. of spanning tree that can be formed is 8.

NOTE: Co-factor for all the elements will be same. Hence we can compute co-factor for any element of the matrix. This method is also known as Kirchhoff’s Theorem. It can be applied to complete graphs also.

let’s see another example to solve these problems by making use of the Laplacian matrix.

A Laplacian matrix L,
where L[i, i] is the degree of node i and L[i, j] = −1 if there is an edge between nodes i and j, and otherwise L[i, j] = 0.

Kirchhoff’s theorem provides a way to calculate the number of spanning trees for a given graph as a determinant of a special matrix.

consider the following graph,

the undirected graph

All possible spanning trees are as follows

three spanning trees

In order to calculate the number of spanning trees, construct a Laplacian matrix L, where L[i, i] is the degree of node i and L[i, j] = −1 if there is an edge between nodes i and j, and otherwise L[i, j] = 0.

for the above graph, The Laplacian matrix will look like this

The number of spanning trees equals the determinant of a matrix.

The Determinant of a matrix that can be obtained when we remove any row and any column from L.

For example, if we remove the first row and column, the result will be,

The determinant is always the same, regardless of which row and column we remove from L.

## C++

 `#include ` `using` `namespace` `std; `   `// C++ program to find number of spanning ` `// trees in a graph using Matrix ` `// Chain Multiplication ` `#define MAX 100 ` `#define MOD 1000000007 `   `// Matrix Multiplication ` `void` `multiply(``int` `A[MAX][MAX], ``int` `B[MAX][MAX], ``int` `C[MAX][MAX]) ` `{ ` `    ``for` `(``int` `i = 0; i < MAX; i++) ` `        ``for` `(``int` `j = 0; j < MAX; j++) ` `            ``for` `(``int` `k = 0; k < MAX; k++) ` `                ``C[i][j] = (C[i][j] + (A[i][k] * B[k][j])%MOD)%MOD;     ` `} `   `// Function to find Nth power of A ` `void` `power(``int` `A[MAX][MAX], ``int` `N, ``int` `result[MAX][MAX]) ` `{ ` `    ``int` `temp[MAX][MAX]; ` `    ``for` `(``int` `i = 0; i < MAX; i++) ` `        ``for` `(``int` `j = 0; j < MAX; j++) ` `            ``result[i][j] = (i == j); `   `    ``while` `(N>0) ` `    ``{ ` `        ``if` `(N%2 == 1) ` `        ``{ ` `            ``multiply(A, result, temp); ` `            ``for` `(``int` `i=0; i 3 ` `    ``// | | ` `    ``// 0 <-1-> 1 ` `    ``int` `V = 4; ``// Number of vertices in graph ` `    ``int` `E = 5; ``// Number of edges in graph ` `    ``int` `graph[][MAX] = { ` `                        ``{0, 1, 1, 1}, ` `                        ``{1, 0, 1, 1}, ` `                        ``{1, 1, 0, 1}, ` `                        ``{1, 1, 1, 0} ` `                    ``}; `   `    ``cout << numOfSpanningTree(graph, V); `   `    ``return` `0; ` `}`

## Java

 `// This Java program finds the number of spanning trees in a` `// graph using Matrix Chain Multiplication.`   `import` `java.util.*;`   `public` `class` `Main {` `    ``static` `final` `int` `MAX = ``100``;` `    ``static` `final` `int` `MOD = ``1000000007``;`   `    ``// Matrix Multiplication` `    ``static` `void` `multiply(``int` `A[][], ``int` `B[][], ``int` `C[][])` `    ``{` `        ``for` `(``int` `i = ``0``; i < MAX; i++)` `            ``for` `(``int` `j = ``0``; j < MAX; j++)` `                ``for` `(``int` `k = ``0``; k < MAX; k++)` `                    ``C[i][j] = (``int``)((C[i][j]` `                                     ``+ (A[i][k] * B[k][j])` `                                           ``% MOD)` `                                    ``% MOD);` `    ``}`   `    ``// Function to find Nth power of A` `    ``static` `void` `power(``int` `A[][], ``int` `N, ``int` `result[][])` `    ``{` `        ``int` `temp[][] = ``new` `int``[MAX][MAX];` `        ``for` `(``int` `i = ``0``; i < MAX; i++)` `            ``for` `(``int` `j = ``0``; j < MAX; j++)` `                ``result[i][j] = (i == j) ? ``1` `: ``0``;` `        ``while` `(N > ``0``) {` `            ``if` `(N % ``2` `== ``1``) {` `                ``multiply(A, result, temp);` `                ``for` `(``int` `i = ``0``; i < MAX; i++)` `                    ``for` `(``int` `j = ``0``; j < MAX; j++)` `                        ``result[i][j] = temp[i][j];` `            ``}` `            ``N = N / ``2``;` `            ``multiply(A, A, temp);` `            ``for` `(``int` `i = ``0``; i < MAX; i++)` `                ``for` `(``int` `j = ``0``; j < MAX; j++)` `                    ``A[i][j] = temp[i][j];` `        ``}` `    ``}`   `    ``// Function to find number of Spanning Trees in a Graph` `    ``// using Matrix Chain Multiplication.` `    ``static` `int` `numOfSpanningTree(``int` `graph[][], ``int` `V)` `    ``{` `        ``int` `result[][] = ``new` `int``[MAX][MAX];` `        ``int` `temp[][] = ``new` `int``[MAX][MAX];` `        ``// Create a copy of graph as the Adjacency matrix` `        ``// will be changed during the process` `        ``for` `(``int` `i = ``0``; i < V; i++)` `            ``for` `(``int` `j = ``0``; j < V; j++)` `                ``temp[i][j] = graph[i][j];` `        ``// Find (V-2)th power of Adjacency matrix` `        ``power(temp, V - ``2``, result);` `        ``int` `ans = ``0``;` `        ``// Find sum of all elements in (V-2)th power` `        ``for` `(``int` `i = ``0``; i < V; i++)` `            ``for` `(``int` `j = ``0``; j < V; j++)` `                ``ans = (``int``)((ans + result[i][j]) % MOD);` `        ``return` `ans;` `    ``}`   `    ``// Driver program` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``// Let us create the following graph` `        ``// 2 <-> 3` `        ``// |   |` `        ``// 0 <-1-> 1` `        ``int` `V = ``4``; ``// Number of vertices in graph` `        ``int` `E = ``5``; ``// Number of edges in graph` `        ``int` `graph[][] = { { ``0``, ``1``, ``1``, ``1` `},` `                          ``{ ``1``, ``0``, ``1``, ``1` `},` `                          ``{ ``1``, ``1``, ``0``, ``1` `},` `                          ``{ ``1``, ``1``, ``1``, ``0` `} };` `        ``System.out.println(numOfSpanningTree(graph, V));` `    ``}` `}`

## C#

 `// C# program to find number of spanning ` `// trees in a graph using Matrix ` `// Chain Multiplication ` `using` `System; `   `class` `GFG ` `{ ` `    ``static` `int` `MAX = 100; ` `    ``static` `int` `MOD = 1000000007; `   `    ``// Matrix Multiplication ` `    ``static` `void` `multiply(``int``[,] A, ``int``[,] B, ``int``[,] C) ` `    ``{ ` `        ``for` `(``int` `i = 0; i < MAX; i++) ` `            ``for` `(``int` `j = 0; j < MAX; j++) ` `                ``for` `(``int` `k = 0; k < MAX; k++) ` `                    ``C[i, j] = (C[i, j] + (A[i, k] * B[k, j]) % MOD) % MOD; ` `    ``} `   `    ``// Function to find Nth power of A ` `    ``static` `void` `power(``int``[,] A, ``int` `N, ``int``[,] result) ` `    ``{ ` `        ``int``[,] temp = ``new` `int``[MAX, MAX]; ` `        ``for` `(``int` `i = 0; i < MAX; i++) ` `            ``for` `(``int` `j = 0; j < MAX; j++) ` `                ``result[i, j] = (i == j) ? 1 : 0; `   `        ``while` `(N > 0) ` `        ``{ ` `            ``if` `(N % 2 == 1) ` `            ``{ ` `                ``multiply(A, result, temp); ` `                ``for` `(``int` `i = 0; i < MAX; i++) ` `                    ``for` `(``int` `j = 0; j < MAX; j++) ` `                        ``result[i, j] = temp[i, j]; ` `            ``} `   `            ``N = N / 2; ` `            ``multiply(A, A, temp); ` `            ``for` `(``int` `i = 0; i < MAX; i++) ` `                ``for` `(``int` `j = 0; j < MAX; j++) ` `                    ``A[i, j] = temp[i, j]; ` `        ``} ` `    ``} `   `    ``// Function to find number of Spanning ` `    ``// Trees in a Graph using Matrix Chain ` `    ``// Multiplication. ` `    ``static` `int` `numOfSpanningTree(``int``[,] graph, ``int` `V) ` `    ``{ ` `        ``int``[,] result = ``new` `int``[MAX, MAX]; ` `        ``int``[,] temp = ``new` `int``[MAX, MAX]; `   `        ``// Create a copy of graph as the ` `        ``// Adjacency matrix will be changed ` `        ``// during the process ` `        ``for` `(``int` `i = 0; i < V; i++) ` `            ``for` `(``int` `j = 0; j < V; j++) ` `                ``temp[i, j] = graph[i, j]; `   `        ``// Find (V-2)th power of Adjacency ` `        ``// matrix ` `        ``power(temp, V - 2, result); `   `        ``int` `ans = 0; `   `        ``// Find sum of all elements in (V-2)th ` `        ``// power ` `        ``for` `(``int` `i = 0; i < V; i++) ` `            ``for` `(``int` `j = 0; j < V; j++) ` `                ``ans = (ans + result[i, j]) % MOD; `   `        ``return` `ans; ` `    ``} `   `    ``// Driver program ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``// Let us create the following graph ` `        ``// 2 <-> 3 ` `        ``// | | ` `        ``// 0 <-1-> 1 ` `        ``int` `V = 4; ``// Number of vertices in graph ` `        ``int` `E = 5; ``// Number of edges in graph ` `        ``int``[,] graph = { ` `                        ``{0, 1, 1, 1}, ` `                        ``{1, 0, 1, 1}, ` `                        ``{1, 1, 0, 1}, ` `                        ``{1, 1, 1, 0} ` `                    ``}; `   `        ``Console.Write(numOfSpanningTree(graph, V)); ` `    ``} ` `}`     `// This code is contributed by NarasingaNikhil`

## Python3

 `# This Python program finds the number of spanning trees in a` `# graph using Matrix Chain Multiplication.`   `MAX` `=` `100` `MOD ``=` `1000000007`   `# Matrix Multiplication` `def` `multiply(A, B, C):` `    ``for` `i ``in` `range``(``MAX``):` `        ``for` `j ``in` `range``(``MAX``):` `            ``C[i][j] ``=` `0` `            ``for` `k ``in` `range``(``MAX``):` `                ``C[i][j] ``=` `(C[i][j] ``+` `(A[i][k] ``*` `B[k][j]) ``%` `MOD) ``%` `MOD`   `# Function to find Nth power of A` `def` `power(A, N, result):` `    ``temp ``=` `[[``0``] ``*` `MAX` `for` `i ``in` `range``(``MAX``)]` `    ``for` `i ``in` `range``(``MAX``):` `        ``for` `j ``in` `range``(``MAX``):` `            ``result[i][j] ``=` `1` `if` `i ``=``=` `j ``else` `0` `    ``while` `N > ``0``:` `        ``if` `N ``%` `2` `=``=` `1``:` `            ``multiply(A, result, temp)` `            ``for` `i ``in` `range``(``MAX``):` `                ``for` `j ``in` `range``(``MAX``):` `                    ``result[i][j] ``=` `temp[i][j]` `        ``N ``=` `N ``/``/` `2` `        ``multiply(A, A, temp)` `        ``for` `i ``in` `range``(``MAX``):` `            ``for` `j ``in` `range``(``MAX``):` `                ``A[i][j] ``=` `temp[i][j]`   `# Function to find number of Spanning Trees in a Graph` `# using Matrix Chain Multiplication.` `def` `numOfSpanningTree(graph, V):` `    ``result ``=` `[[``0``] ``*` `MAX` `for` `i ``in` `range``(``MAX``)]` `    ``temp ``=` `[[``0``] ``*` `MAX` `for` `i ``in` `range``(``MAX``)]` `    ``# Create a copy of graph as the Adjacency matrix` `    ``# will be changed during the process` `    ``for` `i ``in` `range``(V):` `        ``for` `j ``in` `range``(V):` `            ``temp[i][j] ``=` `graph[i][j]` `    ``# Find (V-2)th power of Adjacency matrix` `    ``power(temp, V ``-` `2``, result)` `    ``ans ``=` `0` `    ``# Find sum of all elements in (V-2)th power` `    ``for` `i ``in` `range``(V):` `        ``for` `j ``in` `range``(V):` `            ``ans ``=` `(ans ``+` `result[i][j]) ``%` `MOD` `    ``return` `ans`   `# Driver program` `if` `__name__ ``=``=` `'__main__'``:` `    ``# Let us create the following graph` `    ``# 2 <-> 3` `    ``# |   |` `    ``# 0 <-1-> 1` `    ``V ``=` `4` `# Number of vertices in graph` `    ``E ``=` `5` `# Number of edges in graph` `    ``graph ``=` `[[``0``, ``1``, ``1``, ``1``],` `             ``[``1``, ``0``, ``1``, ``1``],` `             ``[``1``, ``1``, ``0``, ``1``],` `             ``[``1``, ``1``, ``1``, ``0``]]` `    ``print``(numOfSpanningTree(graph, V))`

## Javascript

 `const MAX = 100;` `const MOD = 1000000007;`   `// Matrix Multiplication` `function` `multiply(A, B, C) {` `  ``for` `(let i = 0; i < MAX; i++) {` `    ``for` `(let j = 0; j < MAX; j++) {` `      ``C[i][j] = 0;` `      ``for` `(let k = 0; k < MAX; k++) {` `        ``C[i][j] = (C[i][j] + (A[i][k] * B[k][j]) % MOD) % MOD;` `      ``}` `    ``}` `  ``}` `}`   `// Function to find Nth power of A` `function` `power(A, N, result) {` `  ``let temp = ``new` `Array(MAX).fill().map(() => ``new` `Array(MAX).fill(0));` `  ``for` `(let i = 0; i < MAX; i++) {` `    ``result[i][i] = 1;` `  ``}` `  ``while` `(N > 0) {` `    ``if` `(N % 2 == 1) {` `      ``multiply(A, result, temp);` `      ``for` `(let i = 0; i < MAX; i++) {` `        ``for` `(let j = 0; j < MAX; j++) {` `          ``result[i][j] = temp[i][j];` `        ``}` `      ``}` `    ``}` `    ``N = Math.floor(N / 2);` `    ``multiply(A, A, temp);` `    ``for` `(let i = 0; i < MAX; i++) {` `      ``for` `(let j = 0; j < MAX; j++) {` `        ``A[i][j] = temp[i][j];` `      ``}` `    ``}` `  ``}` `}`   `// Function to find number of Spanning Trees in a Graph` `// using Matrix Chain Multiplication.` `function` `numOfSpanningTree(graph, V) {` `  ``let result = ``new` `Array(MAX).fill().map(() => ``new` `Array(MAX).fill(0));` `  ``let temp = ``new` `Array(MAX).fill().map(() => ``new` `Array(MAX).fill(0));` `  ``// Create a copy of graph as the Adjacency matrix` `  ``// will be changed during the process` `  ``for` `(let i = 0; i < V; i++) {` `    ``for` `(let j = 0; j < V; j++) {` `      ``temp[i][j] = graph[i][j];` `    ``}` `  ``}` `  ``// Find (V-2)th power of Adjacency matrix` `  ``power(temp, V - 2, result);` `  ``let ans = 0;` `  ``// Find sum of all elements in (V-2)th power` `  ``for` `(let i = 0; i < V; i++) {` `    ``for` `(let j = 0; j < V; j++) {` `      ``ans = (ans + result[i][j]) % MOD;` `    ``}` `  ``} ans = ans+ans;` `  ``return` `ans;` `}`   `// Driver program` `// Let us create the following graph` `// 2 <-> 3` `// |   |` `// 0 <-1-> 1` `const V = 4; ``// Number of vertices in graph` `const E = 5; ``// Number of edges in graph` `const graph = [[0, 1, 1, 1], [1, 0, 1, 1], [1, 1, 0, 1], [1, 1, 1, 0]];` `console.log(numOfSpanningTree(graph, V));`

Output

`72`

NOTE:

Cayley’s formula  is a special case of Kirchhoff’s theorem because, in a complete graph of n nodes, the determinant is equal to nn-2

This article is contributed by Kapil Khandelwal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

My Personal Notes arrow_drop_up
Related Articles