Total no. of Spanning tree in a graph
Pattern: Minimum spanning tree
Kirchhoff's matrix-tree theorem: build the Laplacian (each vertex's degree on the diagonal, -1 for each edge), delete one row and the matching column, and take the determinant. That number is how many spanning trees the graph has.
The problem
Given an undirected graph without repeated edges, return the number of its spanning trees. Vertices are numbered from 0 and each edge is given as [u, v].
Think first
- How many spanning trees does a tree have?
- Why is listing them impossible in general?
- Which theorem counts them directly?
Hints
Brute force, and why it fails
Try every set of n - 1 edges and check it connects everything: exponential. The tests use this on small graphs.
The key insight
The count equals a determinant, which elimination computes in O(V³); Bareiss's method keeps every step a whole number.
Dry run
The complete graph on 4 vertices: every degree is 3.
Step 1 of 3
The plan
L = degree matrix - adjacency matrix
return det(L without row 0 and column 0)
Complexity
- Time
O(V³)- Space
O(V²)
Gaussian (Bareiss) elimination.
Edge cases
- A disconnected graph: 0.
- A single vertex: 1.
- A cycle of n vertices: n.
I've built my logic, show the code
Java
class Solution {
public long countSpanningTrees(int n, int[][] edges) {
if (n == 1) return 1;
long[][] lap = new long[n][n]; // the Laplacian: degrees on the diagonal, -1 for each edge
for (int[] e : edges) {
lap[e[0]][e[0]]++; lap[e[1]][e[1]]++;
lap[e[0]][e[1]]--; lap[e[1]][e[0]]--;
}
int m = n - 1; // Kirchhoff: drop one row and column, take the determinant
long[][] a = new long[m][m];
for (int i = 0; i < m; i++) for (int j = 0; j < m; j++) a[i][j] = lap[i + 1][j + 1];
long prev = 1, sign = 1;
for (int k = 0; k < m; k++) { // Bareiss elimination keeps every number a whole number
if (a[k][k] == 0) {
int swap = k + 1;
while (swap < m && a[swap][k] == 0) swap++;
if (swap == m) return 0; // the graph is disconnected
long[] t = a[k]; a[k] = a[swap]; a[swap] = t;
sign = -sign;
}
for (int i = k + 1; i < m; i++)
for (int j = k + 1; j < m; j++) a[i][j] = (a[i][j] * a[k][k] - a[i][k] * a[k][j]) / prev;
prev = a[k][k];
}
return sign * a[m - 1][m - 1];
}
}C++
class Solution {
public:
long long countSpanningTrees(int n, vector<vector<int>>& edges) {
if (n == 1) return 1;
vector<vector<long long>> lap(n, vector<long long>(n, 0)); // degrees on the diagonal, -1 for each edge
for (const vector<int>& e : edges) {
lap[e[0]][e[0]]++; lap[e[1]][e[1]]++;
lap[e[0]][e[1]]--; lap[e[1]][e[0]]--;
}
int m = n - 1; // Kirchhoff: drop one row and column, take the determinant
vector<vector<long long>> a(m, vector<long long>(m));
for (int i = 0; i < m; i++) for (int j = 0; j < m; j++) a[i][j] = lap[i + 1][j + 1];
long long prev = 1, sign = 1;
for (int k = 0; k < m; k++) { // Bareiss elimination keeps every number a whole number
if (a[k][k] == 0) {
int sw = k + 1;
while (sw < m && a[sw][k] == 0) sw++;
if (sw == m) return 0; // the graph is disconnected
swap(a[k], a[sw]);
sign = -sign;
}
for (int i = k + 1; i < m; i++)
for (int j = k + 1; j < m; j++) a[i][j] = (a[i][j] * a[k][k] - a[i][k] * a[k][j]) / prev;
prev = a[k][k];
}
return sign * a[m - 1][m - 1];
}
};Python
class Solution:
def countSpanningTrees(self, n: int, edges: list[list[int]]) -> int:
if n == 1:
return 1
lap = [[0] * n for _ in range(n)] # the Laplacian: degrees on the diagonal, -1 for each edge
for u, v in edges:
lap[u][u] += 1
lap[v][v] += 1
lap[u][v] -= 1
lap[v][u] -= 1
m = n - 1 # Kirchhoff: drop one row and column, take the determinant
a = [row[1:] for row in lap[1:]]
prev, sign = 1, 1
for k in range(m): # Bareiss elimination keeps every number a whole number
if a[k][k] == 0:
sw = next((i for i in range(k + 1, m) if a[i][k]), None)
if sw is None:
return 0 # the graph is disconnected
a[k], a[sw] = a[sw], a[k]
sign = -sign
for i in range(k + 1, m):
for j in range(k + 1, m):
a[i][j] = (a[i][j] * a[k][k] - a[i][k] * a[k][j]) // prev
prev = a[k][k]
return sign * a[m - 1][m - 1]Common mistakes
- Using floating point, which loses exactness.
- Forgetting to delete a row and column.