VOOZH about

URL: https://www.geeksforgeeks.org/dsa/total-number-spanning-trees-graph/

⇱ Number of Spanning Trees in a Graph - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Number of Spanning Trees in a Graph

Last Updated : 9 May, 2026

Given a connected undirected graph with v vertices and its adjacency matrix representation, determine the total number of spanning trees that can be formed from the graph.

A spanning tree is a subgraph that connects all vertices of a graph with exactly v-1 edges and contains no cycles.

Example:

Input: v = 4 adj[][] = [ [0,1,0,0], [1,0,1,0], [0,1,0,1], [0,0,1,0] ]

👁 frame_3205

Output: 1
Explanation: The graph is connected and has exactly v-1 edges, so it is already a tree, hence only one spanning tree exists.

Input: v = 4 adj[][] = [ [0,1,1,0], [1,0,1,0], [1,1,0,1], [0,0,1,0] ]

👁 frame_3204

Output: 3
Explanation: The graph has one cycle among nodes 0, 1, and 2, so removing one edge forms a spanning tree. Since the cycle has 3 edges, removing any one gives 3 possible spanning trees.

👁 frame_3203-

Using Kirchhoff's Theorem

Kirchhoff’s Theorem works by converting the graph into a matrix form that captures how nodes are connected. This matrix (called the Laplacian matrix) represents the structure of the graph, and its determinant (cofactor) gives the total number of spanning trees.

Consider the following graph,

👁 frame_3202

All possible spanning trees are as follows,

👁 frame_3206

Here are the steps showing how the approach works to find the number of spanning trees in the given graph using Kirchhoff’s Theorem.

  • Create the adjacency Matrix for the given graph:
👁 matrix_8
  • Convert the adjacency matrix into the Laplacian matrix which is achieved using the following steps.

A Laplacian matrix L, where

  • 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, i,e., L[i][i] = degree of node i
  • Replace all non-diagonal 1's with -1.  L[i][j] = -1 if there is an edge between i and j, L[i][j] remains 0 otherwise
👁 Image
  • Remove any one row and one column from the Laplacian matrix. For example, remove the first row and first column. The cofactor value is the same for any row and column removed, so you can choose any.
  • Find the determinant (cofactor) of the resulting matrix. This value gives the total number of spanning trees "3". The determinant remains the same regardless of which row and column are removed.
👁 Image

Cayley’s formula for Complete Graph : It is a special case of Kirchhoff’s theorem because, in a complete graph of n nodes, the determinant is equal to n^(n-2).

👁 Screenshot_20230311_014727

It works here because the Laplacian matrix represents how all nodes in the graph are connected. The determinant expands into all possible edge combinations, and due to its mathematical properties, invalid ones (cycles or disconnected) cancel out. As a result, only valid spanning trees remain, so the determinant directly gives their total count.


Output
16

Time Complexity: O(n^3) due to Gaussian elimination for determinant.
Auxiliary Space : O(n^2) for storing matrices.

Comment
Article Tags:
Article Tags: