The Matrix-Tree Theorem: Counting Spanning Trees with a Determinant
Table of Contents
- • A known case, for scale
- • The Laplacian matrix
- • The theorem
- • Building the proof: incidence matrices
- • Cauchy-Binet does the heavy lifting
- • The lemma: trees give ±1, cycles give 0
- • Worked example
- • Why this beats enumeration
- • Electrical networks: where the same matrix reappears
- • The broader pattern
Take a connected graph and ask a simple question: how many different spanning trees does it have? A spanning tree is a subset of edges that connects every vertex with no cycles, the minimum skeleton that holds the graph together. For a small graph you can just draw them all out. For anything larger, enumeration is hopeless, the count grows combinatorially and there’s no obvious shortcut.
Except there is one, and it’s a genuinely strange one. Build a specific matrix out of the graph, delete any row and the matching column, and take the determinant. That number is exactly the count of spanning trees. No enumeration, no search, just linear algebra. This is Kirchhoff’s theorem, usually called the Matrix-Tree theorem, and it’s worth understanding both because the result is surprising and because the proof is a clean piece of reasoning that ties together incidence matrices, the Cauchy-Binet formula, and a simple induction on leaves.
A known case, for scale
Before building the general machinery, it helps to have a reference point. For the complete graph on vertices, , Cayley’s formula gives the count directly:
For that’s spanning trees. Cayley’s formula only works for complete graphs, though, and most graphs aren’t complete. What we want is a method that works for any connected graph, and ideally one that’s fast: enumerating spanning trees directly takes exponential time, but as we’ll see, computing a determinant does not.
The Laplacian matrix
Given a graph with vertices, define the Laplacian matrix as
where is the diagonal degree matrix (the degree of vertex on the diagonal) and is the adjacency matrix. Concretely, for vertices , if there’s an edge between and and otherwise, and equals the degree of vertex . Every row sums to zero by construction, which already tells you is singular: the all-ones vector is in its kernel.
This matrix shows up constantly in graph theory (spectral clustering, random walks, the graph Fourier transform) but the property we care about here is the one Kirchhoff noticed in 1847, in the context of electrical circuits rather than graph theory as we’d now call it.
The theorem
Kirchhoff’s theorem. Let be the Laplacian of a connected graph on vertices. Delete any row and column from to get an matrix . Then
the number of spanning trees of , and this value doesn’t depend on which row/column you deleted.
That last part, the independence from choice of , is a nice corollary of the proof rather than something you need to assume. All cofactors of are equal.
Building the proof: incidence matrices
The proof runs through a second matrix, the oriented incidence matrix . Pick an arbitrary orientation for each edge of (which endpoint is the “head” and which is the “tail” is irrelevant to the final count, it’s just bookkeeping). is , one row per vertex and one column per edge, where for edge from vertex to vertex :
A short computation shows , regardless of which orientation you picked (flipping an edge’s direction flips the sign of an entire column, which doesn’t change ). This is the bridge between the combinatorial structure of the graph and the determinant we want to compute.
Now delete row from to get , an matrix. Deleting a row of before multiplying by its transpose has the same effect as deleting the matching row and column from afterward, so
where is exactly the reduced Laplacian from the theorem statement, with row and column removed.
Cauchy-Binet does the heavy lifting
The Cauchy-Binet formula generalises the familiar to non-square matrices. For a matrix with ,
where the sum runs over all subsets of columns, and is the submatrix formed by keeping only those columns.
Apply this to , which is :
The sum is over every possible way of choosing edges out of the graph’s edges. This is already suggestive: we’re summing a square over all -edge subsets, and spanning trees are exactly the -edge subsets that hold the graph together. The remaining work is to show that each term in the sum is if the edges form a spanning tree and otherwise.
The lemma: trees give ±1, cycles give 0
Claim. Let be a set of edges. Then if is a spanning tree of , and otherwise.
First, notice that with exactly edges on vertices, there are only two possibilities. Either is a forest, in which case a forest with vertices and edges can only have one component (a forest with components has edges, and forces ), meaning is a spanning tree. Or isn’t a forest, in which case it contains a cycle. There’s no third option.
Case 1: contains a cycle. Orient the cycle consistently in one direction. Walking around it and summing the incidence columns with a or depending on whether each edge’s stored orientation agrees with the walking direction produces the zero vector: every vertex on the cycle gets one contribution and one contribution from its two cycle edges, which cancel. This is a nontrivial linear dependence among the columns of restricted to the cycle’s edges, so those columns, and therefore all of , are linearly dependent. The determinant is .
Case 2: is a spanning tree. This goes by induction on . The base case is a single vertex with no edges, an empty matrix, whose determinant is by convention, matching the single (trivial) spanning tree of a one-vertex graph.
For the inductive step, recall that any tree with two or more vertices has at least two leaves (vertices of degree 1). Since row was already deleted from , pick a leaf (at least one of the tree’s leaves must differ from , since a tree has at least two). Vertex touches exactly one edge of , call it . In the matrix , row therefore has exactly one nonzero entry, , in the column for , and zeros everywhere else.
Expanding the determinant along row gives , where is the matrix left after deleting row and column . But that’s exactly the incidence-derived matrix you’d build for the smaller graph (remove the leaf and its edge), using the spanning tree , with row still excluded. By the inductive hypothesis, . So .
That closes the lemma. Substituting back into the Cauchy-Binet sum:
Every spanning tree contributes exactly , everything else contributes , and the sum is just a count. That’s the theorem. The argument never used the fact that we deleted row specifically, relabelling vertices shows the same holds for any deleted row/column, which is why all cofactors agree.
Worked example
Take the graph on vertices with edges , which is with the edge between and removed (sometimes called the diamond graph). Degrees are .
Delete row 4 and column 4:
So the theorem predicts 8 spanning trees. That’s checkable independently: has 16 spanning trees total (Cayley’s formula), and by the symmetry of every one of its 6 edges appears in the same number of them. Each tree has 3 edges, so summing edge-appearances across all 16 trees gives , spread evenly over 6 edges, meaning each edge appears in trees. The trees that survive removing edge are exactly the ones that never used it: . Both routes agree.
Why this beats enumeration
The determinant of an matrix can be computed via Gaussian elimination in time. Directly enumerating spanning trees, by contrast, is exponential in the worst case, the number of trees itself grows exponentially (a dense graph on 20 vertices can easily have trillions of spanning trees), so no algorithm that lists them one at a time can keep up. The Matrix-Tree theorem sidesteps the listing entirely: it gets the count out of a computation whose cost barely notices how large that count is.
Electrical networks: where the same matrix reappears
Kirchhoff didn’t arrive at this theorem through graph theory. He was analysing resistor networks, and the Laplacian shows up there too, under a different name, as the conductance matrix. If every edge is a 1-ohm resistor, injecting current at one node and extracting it at another produces node voltages governed by exactly the linear system (with one node grounded as a reference, since itself is singular).
Solving that system with Cramer’s rule brings back the same cofactors that count spanning trees, and the result is a second classical formula, also due to Kirchhoff: the effective resistance between nodes and is
where is the total spanning tree count as before, and counts spanning 2-forests that separate and : subsets of edges that form exactly two trees covering all vertices between them, with in one and in the other. The derivation follows the same Cauchy-Binet pattern as the main proof, just applied to the numerator and denominator of Cramer’s rule instead of to a single determinant, so it won’t be repeated in full here, but the machinery is identical.
Checking this against the diamond graph: we already know . For , count the 2-edge forests that put vertices 3 and 4 in different components. There are ways to choose 2 edges from the graph’s 5 edges, and since no two edges share both endpoints, every pair is automatically a forest. Going through all 10 pairs, 8 of them separate vertex 3 from vertex 4, and only the two pairs that route both edges through a single shared vertex adjacent to both ( and ) keep 3 and 4 in the same component. So , giving
Solving the circuit directly, by writing out Kirchhoff’s current law at each node with 1 amp injected at vertex 3 and extracted at vertex 4, and grounding vertex 2, confirms the same answer: . The graph-counting formula and the direct circuit solution land on the same number, because they’re computing the same determinant from two different starting points.
The broader pattern
What makes this theorem satisfying isn’t just that the formula works, it’s that a single matrix, built from purely local information (which vertices are adjacent to which), encodes a global combinatorial count that has no obvious reason to be computable so cheaply. The same object, reduced the same way, also happens to answer a completely different physical question about current flow. That’s not a coincidence so much as a reflection of the fact that spanning trees and current flow are, underneath, the same linear algebra: both are asking how many independent ways there are to route a single unit of flow through a graph without creating a cycle.