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.
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.
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,
All possible spanning trees are as follows,
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:
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
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.
Cayley’s formula for Complete Graph : Itis a special case of Kirchhoff’s theorem because, in a complete graph of n nodes, the determinant is equal to n^(n-2).
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.
C++
#include<iostream>#include<vector>#include<cmath>#include<algorithm>usingnamespacestd;// Determinant using Gaussian eliminationdoubledeterminant(vector<vector<double>>mat,intn){doubledet=1.0;for(inti=0;i<n;i++){// Find pivotintpivot=i;for(intj=i+1;j<n;j++){if(fabs(mat[j][i])>fabs(mat[pivot][i]))pivot=j;}// If pivot is zero → determinant = 0if(fabs(mat[pivot][i])<1e-9)return0;// Swap rowsif(i!=pivot){swap(mat[i],mat[pivot]);det*=-1;}det*=mat[i][i];// Eliminate belowfor(intj=i+1;j<n;j++){doublefactor=mat[j][i]/mat[i][i];for(intk=i;k<n;k++){mat[j][k]-=factor*mat[i][k];}}}returndet;}// Number of spanning treesintnumOfSpanningTree(vector<vector<int>>&graph,intn){// Laplacian matrixvector<vector<double>>L(n,vector<double>(n,0));for(inti=0;i<n;i++){intdegree=0;for(intj=0;j<n;j++){if(graph[i][j]){degree++;if(i!=j)L[i][j]=-1;}}L[i][i]=degree;}// Minor matrixvector<vector<double>>minor(n-1,vector<double>(n-1));for(inti=0;i<n-1;i++){for(intj=0;j<n-1;j++){minor[i][j]=L[i][j];}}// Determinantreturn(int)round(determinant(minor,n-1));}intmain(){intv=4;vector<vector<int>>graph={{0,1,1,1},{1,0,1,1},{1,1,0,1},{1,1,1,0}};cout<<numOfSpanningTree(graph,v);return0;}
Java
importjava.util.*;classGfG{// Determinant using Gaussian eliminationstaticdoubledeterminant(double[][]mat,intn){doubledet=1.0;for(inti=0;i<n;i++){// Find pivotintpivot=i;for(intj=i+1;j<n;j++){if(Math.abs(mat[j][i])>Math.abs(mat[pivot][i]))pivot=j;}// If pivot is zero → determinant = 0if(Math.abs(mat[pivot][i])<1e-9)return0;// Swap rowsif(i!=pivot){double[]tempRow=mat[i];mat[i]=mat[pivot];mat[pivot]=tempRow;det*=-1;}det*=mat[i][i];// Eliminate belowfor(intj=i+1;j<n;j++){doublefactor=mat[j][i]/mat[i][i];for(intk=i;k<n;k++){mat[j][k]-=factor*mat[i][k];}}}returndet;}// Function to compute number of spanning trees // using adjacency matrixstaticintnumOfSpanningTree(int[][]graph,intv){// Laplacian matrixdouble[][]L=newdouble[v][v];for(inti=0;i<v;i++){intdegree=0;for(intj=0;j<v;j++){if(graph[i][j]==1){degree++;if(i!=j)L[i][j]=-1;}}L[i][i]=degree;}// Minor matrixdouble[][]minor=newdouble[v-1][v-1];for(inti=0;i<v-1;i++){for(intj=0;j<v-1;j++){minor[i][j]=L[i][j];}}// Determinantreturn(int)Math.round(determinant(minor,v-1));}publicstaticvoidmain(String[]args){intv=4;int[][]graph={{0,1,1,1},{1,0,1,1},{1,1,0,1},{1,1,1,0}};System.out.println(numOfSpanningTree(graph,v));}}
Python
# Determinant using Gaussian eliminationdefdeterminant(mat,n):det=1.0foriinrange(n):# Find pivotpivot=iforjinrange(i+1,n):ifabs(mat[j][i])>abs(mat[pivot][i]):pivot=j# If pivot is zero → determinant = 0ifabs(mat[pivot][i])<1e-9:return0# Swap rowsifi!=pivot:mat[i],mat[pivot]=mat[pivot],mat[i]det*=-1det*=mat[i][i]# Eliminate belowforjinrange(i+1,n):factor=mat[j][i]/mat[i][i]forkinrange(i,n):mat[j][k]-=factor*mat[i][k]returndet# Function to compute number of spanning trees # using adjacency matrixdefnumOfSpanningTree(graph,v):# Laplacian matrixL=[[0.0]*vfor_inrange(v)]foriinrange(v):degree=0forjinrange(v):ifgraph[i][j]==1:degree+=1ifi!=j:L[i][j]=-1L[i][i]=degree# Minor matrixminor=[[0.0]*(v-1)for_inrange(v-1)]foriinrange(v-1):forjinrange(v-1):minor[i][j]=L[i][j]# Determinantreturnround(determinant(minor,v-1))if__name__=="__main__":v=4graph=[[0,1,1,1],[1,0,1,1],[1,1,0,1],[1,1,1,0]]print(numOfSpanningTree(graph,v))# Output: 16
C#
usingSystem;classGfG{// Determinant using Gaussian eliminationstaticdoubledeterminant(double[,]mat,intn){doubledet=1.0;for(inti=0;i<n;i++){// Find pivotintpivot=i;for(intj=i+1;j<n;j++){if(Math.Abs(mat[j,i])>Math.Abs(mat[pivot,i]))pivot=j;}// If pivot is zero → determinant = 0if(Math.Abs(mat[pivot,i])<1e-9)return0;// Swap rowsif(i!=pivot){for(intk=0;k<n;k++){doubletemp=mat[i,k];mat[i,k]=mat[pivot,k];mat[pivot,k]=temp;}det*=-1;}det*=mat[i,i];// Eliminate belowfor(intj=i+1;j<n;j++){doublefactor=mat[j,i]/mat[i,i];for(intk=i;k<n;k++){mat[j,k]-=factor*mat[i,k];}}}returndet;}// Function to compute number of spanning trees // using adjacency matrixstaticintnumOfSpanningTree(int[,]graph,intv){// Laplacian matrixdouble[,]L=newdouble[v,v];for(inti=0;i<v;i++){intdegree=0;for(intj=0;j<v;j++){if(graph[i,j]==1){degree++;if(i!=j)L[i,j]=-1;}}L[i,i]=degree;}// Minor matrixdouble[,]minor=newdouble[v-1,v-1];for(inti=0;i<v-1;i++){for(intj=0;j<v-1;j++){minor[i,j]=L[i,j];}}// Determinantreturn(int)Math.Round(determinant(minor,v-1));}staticvoidMain(){intv=4;int[,]graph={{0,1,1,1},{1,0,1,1},{1,1,0,1},{1,1,1,0}};Console.WriteLine(numOfSpanningTree(graph,v));}}
JavaScript
// Determinant using Gaussian eliminationfunctiondeterminant(mat,n){letdet=1.0;for(leti=0;i<n;i++){// Find pivotletpivot=i;for(letj=i+1;j<n;j++){if(Math.abs(mat[j][i])>Math.abs(mat[pivot][i]))pivot=j;}// If pivot is zero → determinant = 0if(Math.abs(mat[pivot][i])<1e-9)return0;// Swap rowsif(i!==pivot){[mat[i],mat[pivot]]=[mat[pivot],mat[i]];det*=-1;}det*=mat[i][i];// Eliminate belowfor(letj=i+1;j<n;j++){letfactor=mat[j][i]/mat[i][i];for(letk=i;k<n;k++){mat[j][k]-=factor*mat[i][k];}}}returndet;}// Function to compute number of spanning trees // using adjacency matrixfunctionnumOfSpanningTree(graph,v){// Laplacian matrixletL=Array.from({length:v},()=>Array(v).fill(0));for(leti=0;i<v;i++){letdegree=0;for(letj=0;j<v;j++){if(graph[i][j]===1){degree++;if(i!==j)L[i][j]=-1;}}L[i][i]=degree;}// Minor matrixletminor=Array.from({length:v-1},()=>Array(v-1).fill(0));for(leti=0;i<v-1;i++){for(letj=0;j<v-1;j++){minor[i][j]=L[i][j];}}// DeterminantreturnMath.round(determinant(minor,v-1));}// Driver codeletv=4;letgraph=[[0,1,1,1],[1,0,1,1],[1,1,0,1],[1,1,1,0]];console.log(numOfSpanningTree(graph,v));// Output: 16
Output
16
Time Complexity: O(n^3) due to Gaussian elimination for determinant. Auxiliary Space : O(n^2) for storing matrices.