Guest
Sign in to keep your graphs and progress.

Practice book
Graph Theory Through Problems300 Exercises with Hints, Worked Algorithms and Complete Solutions
By Mohammed Islam Hadjoudj
Learn graph theory by solving problems: 300 exercises, from routine checks to challenging proofs, each with a complete solution.
Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on AmazonSelect an algorithm and generate steps to begin visualization
One determinant counts every spanning tree
Counts the spanning trees of a graph as a determinant: build the Laplacian, remove one row and column, and eliminate exactly
Select an algorithm and generate steps to begin visualization
A spanning tree of a connected graph is a set of edges that connects all the vertices without forming a cycle. A graph usually has many of them, often an astronomical number, and Kirchhoff's matrix-tree theorem counts them exactly with linear algebra. Build the Laplacian matrix L = D − A, with each vertex's degree on the diagonal and −1 for every edge between two vertices off it. Remove any one row and the matching column. The determinant of what is left is the number of spanning trees. Gustav Kirchhoff found this in 1847 while studying electrical networks, and it is one of the first results of algebraic graph theory.
The visualizer fills the Laplacian in two steps: first the degrees on the diagonal, then −1 for each edge, so every row adds up to zero. That is why the full Laplacian has determinant 0 and the theorem removes a row and a column first, here those of the first vertex. The determinant of the remaining (n − 1) × (n − 1) matrix is computed by Bareiss elimination, a fraction-free variant of Gaussian elimination: each new entry is (a[i][j] · pivot − a[i][p] · a[p][j]) divided by the previous pivot, and the division is always exact, so the numbers stay integers and the count is exact. A zero pivot means swapping rows, which flips the sign, and if no row can be swapped in, the determinant is 0 and the graph is disconnected. The last pivot is the determinant, the number of spanning trees. When there are few enough, the visualizer then lists them one by one on the canvas to check the count. The whole computation takes O(n³) arithmetic operations.
Counting spanning trees measures how robustly a network is connected: a network with more spanning trees has more ways to stay connected when links fail, so the count appears in network reliability and in the design of communication networks. In electrical engineering, Kirchhoff's original setting, the cofactors of the Laplacian give effective resistances between nodes. Random spanning trees, sampled with the same matrix tools, are used in graph sparsification and in physics models. The Laplacian itself is the central object of spectral graph theory, behind spectral clustering, graph neural networks and diffusion on graphs. For complete graphs the theorem gives Cayley's formula, n^(n − 2) trees on n labelled vertices.
Build the Laplacian, delete one row and column, and compute the determinant exactly with integer arithmetic.
CountSpanningTrees(G):
L = n × n zero matrix
for each vertex v: L[v][v] = degree(v)
for each edge u–v: L[u][v] -= 1, L[v][u] -= 1
M = L without the row and column of one vertex
prev = 1
for p = 1 .. n - 2: // Bareiss elimination
if M[p][p] == 0: swap in a row below (or return 0)
for i, j > p:
M[i][j] = (M[i][j] × M[p][p] - M[i][p] × M[p][j]) / prev
prev = M[p][p]
return M[n-1][n-1] // with the sign of the swapsBareiss' division is always exact, so every intermediate entry is an integer (a minor of the matrix). Ordinary Gaussian elimination with fractions or floating point would also work, but rounding errors make large counts unreliable.
Count the spanning trees of the house loaded here.
Example graph: Five vertices: a square A, B, D, C (edges A-B, B-D, D-C, C-A) with a roof E joined to A and B. Six edges, so each spanning tree keeps four of them.
11 spanning trees, from the determinant and from the list. The count can be checked by hand: A and B are joined by three paths of lengths 1 (the edge), 2 (through E) and 3 (round the square), and such a graph has 1·2 + 2·3 + 3·1 = 11 spanning trees.
Time: O(n³) arithmetic operations · Space: O(n²)
Building the Laplacian is O(n² + m). Elimination on the (n − 1) × (n − 1) matrix updates O(n²) entries for each of n − 2 pivots, O(n³) operations. With Bareiss every intermediate number is a minor, so its size stays polynomial in n and in the bit length of the entries. Listing the trees is a different matter: there can be n^(n − 2) of them, which is why the visualizer lists them only when there are few.
Counting spanning trees, finding one, and finding the cheapest one are different problems.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Enumerate all subsets of n − 1 edges | Very small graphs, or when the trees themselves are needed. | C(m, n − 1) checks |
| Kruskal or Prim | One spanning tree of minimum total weight, not the count. | O(m log n) |
| Eigenvalues of the Laplacian | The same count as the product of the non-zero eigenvalues divided by n. Useful in theory and for structured graphs, not for exact integers. | O(n³), floating point |
Read the full article: Minimum Spanning Trees (MST)
Read the full article: Spectral Graph Theory in Machine Learning
Related algorithms: Kruskal's MST Algorithm, Prim's MST Algorithm, Prüfer Code, Borůvka's Algorithm, Connected Components