Skip to problem
←

Count Spanning Trees

Hard 0 attempts

Count the distinct spanning trees of the given simple, undirected graph.

A spanning tree selects exactly V - 1 existing edges, includes every vertex, and is connected with no cycles. Two spanning trees are different when their selected edge sets differ; choosing a different root does not create a new spanning tree.

adj[u] lists the neighbors of vertex u. Every undirected edge appears in both endpoint lists, and the graph has no self-loops or repeated edges.

Return 0 if the graph is disconnected. A single vertex has one spanning tree, using no edges. Return the exact count; no modulo is required.

Function contract

Implement countSpanningTrees(adj, V).

Output: Return the exact number of distinct spanning trees as a number. Return 0 for a disconnected graph and 1 for a single vertex.

Example 1

Input
adj = [[1,2],[0,2],[0,1]]
V = 3
Output
3

Example 2

Input
adj = [[],[],[]]
V = 3
Output
0

Example 3

Input
adj = [[]]
V = 1
Output
1
Constraints
  • 1 <= V <= 10
  • 0 <= E <= V*(V-1)/2
  • adj has V rows and represents a simple undirected graph on labels 0 through V - 1
  • Every edge appears in both adjacency rows; no self-loops or duplicate edges

Matrix-Tree Theorem with Exact Elimination

Build the Laplacian: each diagonal entry is the vertex degree, and an off-diagonal entry is -1 when the two vertices are adjacent. Delete one row and the matching column. The determinant of the remaining matrix equals the spanning-tree count.

Use fraction-free elimination with BigInt so intermediate arithmetic is exact. Each division is exact by the elimination invariant. A missing pivot means the determinant is zero. The empty determinant for one vertex is 1.

javascript
function countSpanningTrees(adj, V) {
  if (V === 1) return 1;
  const n = V - 1;
  const matrix = Array.from({length: n}, (_, i) =>
    Array.from({length: n}, (_, j) =>
      i === j ? BigInt(adj[i].length) : adj[i].includes(j) ? -1n : 0n
    )
  );
  let previous = 1n, sign = 1n;
  for (let k = 0; k < n - 1; k++) {
    let pivotRow = k;
    while (pivotRow < n && matrix[pivotRow][k] === 0n) pivotRow++;
    if (pivotRow === n) return 0;
    if (pivotRow !== k) {
      [matrix[k], matrix[pivotRow]] = [matrix[pivotRow], matrix[k]];
      sign = -sign;
    }
    const pivot = matrix[k][k];
    for (let i = k + 1; i < n; i++) {
      for (let j = k + 1; j < n; j++) {
        matrix[i][j] = (matrix[i][j] * pivot - matrix[i][k] * matrix[k][j]) / previous;
      }
      matrix[i][k] = 0n;
    }
    previous = pivot;
  }
  return Number(sign * matrix[n - 1][n - 1]);
}

For V ≤ 10, the complete graph has the largest possible count, V^(V - 2), at most 100,000,000. The final conversion to Number is therefore exact.

Time: O(V³) arithmetic operations. Space: O(V²).

Loading discussion…

Saved in this browser only. Private to you.

JavaScript