Cayley's Formula and Matrix-Tree Theorem
How many distinct trees can you build on a fixed set of labeled points? For points, the answer is exactly . For general , it is the astonishingly clean number . This is Cayley's formula, one of the most quoted enumerative facts in combinatorics. The surprise is not just the answer but the sheer number of unrelated proofs it admits. These include bijective, recursive, generating-functional, and algebraic approaches. Counting trees matters because trees are the skeleton of connectivity. Every connected graph contains one. Understanding how many spanning subtrees a graph has is a question about structure. It concerns its structural redundancy, its reliability as a network, and (as later theory will show) its spectrum. The deeper and more powerful tool in this article is the Matrix-Tree Theorem of Kirchhoff. It answers a vastly more general question. Given any labeled graph , not just the complete graph, how many Spanning Trees does it have? Kirchhoff's remarkable insight, discovered in the 1840s while analyzing electrical circuits, is striking. This purely combinatorial count is encoded as a single determinant of a matrix built from the graph — the Laplacian. Cayley's formula turns out to be the special case of Kirchhoff's theorem applied to the complete graph . So the general theorem strictly subsumes and explains the specific one. The big picture is a bridge between two worlds. These are combinatorics (counting discrete objects) and linear algebra (determinants, eigenvalues, cofactors). This bridge is the entry point to spectral graph theory. There, the eigenvalues of the Laplacian encode expansion, connectivity, and random walk behavior of a graph. The Matrix-Tree Theorem is the first and cleanest example of a "spectral" quantity. A determinant computes a combinatorial invariant exactly, with no approximation and no hidden constants. This article gives Cayley's formula with a complete bijective proof via Prüfer sequences. It then develops the Laplacian matrix and proves the Matrix-Tree Theorem in full, using the Cauchy–Binet formula. We verify both results on explicit small graphs. We discuss exactly which hypotheses (connectedness, labeling) are load-bearing. It closes with the theorem's role as the gateway to spectral methods.
- Cayley's formula: the number of labeled trees on vertices is (for ; the count is when ).
- Prüfer's bijection converts each labeled tree into a unique sequence in , giving an exact, constructive proof.
- The Laplacian matrix of a graph packages degrees and adjacency into one symmetric matrix.
- The Matrix-Tree Theorem: any cofactor of equals the number of spanning trees of , for any connected or disconnected labeled graph.
- Applying the theorem to recovers Cayley's formula as a determinant computation, unifying both results.
- Key idea: counting spanning trees, a purely combinatorial problem, is exactly solved by a single determinant of a linear-algebraic object built from the graph.
Where this lives today
The Matrix-Tree Theorem is a computational workhorse: counting spanning trees of a network graph in polynomial time (via Gaussian elimination on the Laplacian cofactor) versus the naive exponential enumeration of subsets of edges. This matters directly in electrical engineering, where Kirchhoff's original motivation persists — the theorem underlies formulas for effective resistance between nodes in a resistor network, a quantity computable directly from Laplacian cofactors and closely tied to random walks on graphs. In statistical physics, the number of spanning trees of a lattice graph is related to the partition function of the q-state Potts model in a certain limit (), linking tree enumeration to phase transitions. In computer science, spanning tree counts and the Laplacian appear in network reliability analysis (a proxy for how many ways a network stays connected), in algorithms for generating uniformly random spanning trees (used in maze generation and in approximation algorithms), and in the design of expander-based error-correcting codes. In cryptography and algorithm design, the Laplacian's determinant structure supports fast algorithmic counting subroutines used within combinatorial optimization. Perhaps most importantly for the sequence of ideas this article sits in, the Laplacian's cofactors are only the first fingerprint of the richer object: its full eigenvalue spectrum, which is the subject taken up next in the passage toward Introduction to Spectral Graph Theory, where the same matrix used here to count trees is shown to encode graph connectivity (via its second-smallest eigenvalue, the algebraic connectivity), expansion, and mixing times of random walks — all consequences of the same object introduced here purely to count spanning trees.
Ask the tutor: How does the Matrix-Tree Theorem generalize to compute effective resistance between two specific vertices in a weighted network?
Cayley's Formula and the Matrix-Tree Theorem
Let . A labeled tree on is a tree whose vertex set is exactly ; two labeled trees are considered different if their edge sets differ, even if they are isomorphic as abstract graphs. Let be a simple graph with (loops and multiple edges excluded unless stated). The degree matrix is the diagonal matrix with . The adjacency matrix has if and otherwise (with ). The Laplacian matrix of is
Equivalently, and if , if and . For , the -th cofactor of is
where is the matrix obtained by deleting row and column from .
Intuition
Cayley's number looks too simple to be an accident, and indeed it has a combinatorial engine behind it: a labeled tree on vertices is completely determined by, and can be recovered from, a sequence of labels from (repeats allowed). This is the Prüfer sequence. Building a tree is equivalent to filling in blanks, each with choices, giving possibilities — as long as we can show the correspondence is a genuine bijection, which is the content of the proof below. The Laplacian generalizes this counting problem from to an arbitrary graph . Think of as encoding "each vertex pushes away from itself along its edges": the row for vertex has on the diagonal and at each neighbor, so every row sums to zero. This zero row-sum means is always singular ( is in its kernel), so always — the full determinant carries no information. The content of Kirchhoff's theorem is that if you delete any one row and the matching column, the resulting determinant is no longer trivially zero, and in fact equals exactly the number of spanning trees, regardless of which row/column you deleted.
Central results
For every integer , the number of labeled trees on the vertex set is
We prove this by exhibiting an explicit bijection (the Prüfer correspondence) between the set of labeled trees on and the set of sequences of length over the alphabet . Since , a bijection immediately gives .
The encoding map . Fix a tree on . We build a sequence of length by the following algorithm. 1. Set . 2. For : let be the leaf of (a vertex of degree ) with the smallest label, let be its unique neighbor in , record as the -th term of the sequence, and set (delete the leaf and its incident edge). 3. Stop after steps; at that point has exactly vertices and edge. Every tree on vertices has at least one leaf (a standard fact: the sum of degrees is , so not all degrees can be ), so step 2 is always well defined for , since has vertices when . This defines .
Key invariant. During the process, vertex is deleted as a leaf at some step if and only if it is not one of the two vertices surviving to the end, and the number of times appears among equals . This is because each time is not yet deleted and some other leaf attached to is removed, 's remaining degree in the current subtree drops by exactly one, and itself is recorded once for every neighbor removed before is removed (or, if survives to the end, once for every neighbor removed while is present). Summing, is recorded exactly times if is not among the last two vertices, and exactly times as well if is among the last two survivors (one of its edges — the final one — is never "recorded" because the process halts before deleting a leaf from the final 2-vertex tree). In all cases:
(Since , the right side sums to , consistent with the sequence having length .)
The decoding map . Given , define for each ; note and each . Reconstruct a candidate tree edge-by-edge: 1. Let ("current leaf pool"), maintained as we go. 2. For : let be the smallest element of . Add edge . Remove from . Decrease by ; if becomes , insert into . 3. After the loop, exactly two labels remain with -value (this must be checked); join them by an edge. We must verify (a) at every step is nonempty so is well defined, and (b) the output is a tree, and (c) and . Well-definedness of the loop. At the start, over positive integers, so by an averaging/counting argument at least two indices have whenever remain active (this is checked inductively: initially true since not all as that would force ). Each step of the loop removes one element from (namely ) and decreases exactly one -value by , possibly adding one new element to ; a direct inductive check (same degree-sum argument on the shrinking active vertex set, which always has size and -values summing to — more simply, one tracks that the invariant "sum of over currently active vertices " is preserved) guarantees is always nonempty until exactly vertices remain, both with . The output is a tree. The construction adds exactly edges in the loop plus final edge, for edges total on vertices. We show the resulting graph is connected and acyclic by strong induction on : the base case is trivial (single edge). For the inductive step, note is a leaf attached only to in the final construction (no other edge in the process ever touches , since it is deleted from the leaf pool immediately and never re-inserted, being removed once and for all), and the remaining process on restricted appropriately reconstructs a tree on by the inductive hypothesis (relabeling is unnecessary since the algorithm's rule depends only on numerical comparison, and removing together with reducing by one is exactly the decoding process for the sequence on vertex set ). Attaching the leaf to this tree via edge yields a tree on . Hence produces a tree. Mutual inverses. By construction, running on the tree removes leaves in exactly the same order (both processes pick, at each stage, the smallest-labeled current leaf, and both maintain identical leaf pools by the identical recursive rule), and records exactly as neighbors. Hence . Conversely, given a tree , feeding back into replays the leaf-deletion process of in reverse and reattaches each deleted leaf to its recorded neighbor in the same order, reconstructing exactly , since the degree bookkeeping guarantees the values computed by from match the true degrees of exactly. So . Therefore is a bijection between and , and
For any graph on , the all-ones vector satisfies . Consequently .
The -th entry of is . So , meaning is a nonzero vector in the kernel of , so is singular and .
∎Let be a simple labeled graph on (connected or not, ), with Laplacian . Let denote the number of spanning trees of (with if is disconnected). Then for every ,
where is with row and column deleted; in particular this value is the same for every choice of .
We prove this using the Cauchy–Binet formula, and an orientation-based factorization of . The technique is: factor for a suitable incidence-type matrix , then expand the determinant of as a sum of squares of maximal-minor determinants of , and identify each nonzero minor with exactly one spanning tree.
Step 1: Oriented incidence matrix. Fix an arbitrary orientation of each edge of (assign each edge a "head" and "tail" vertex; the choice will not affect the final count). Let , and let be the oriented incidence matrix: for edge (tail , head ), column of has in row , in row , and elsewhere. (Any convention for sign works as long as it is used consistently; we take head .) Claim: . Indeed, the entry of is . For : each edge incident to contributes to this sum (only edges touching have ), so the diagonal entry is . For : a term is nonzero only if edge has both endpoints in , i.e. ; if , exactly one of is the head and the other the tail, contributing ; if the sum is . Hence off-diagonal entries are exactly when and otherwise. This matches exactly, proving the claim: .
Step 2: Reduce to a submatrix. Fix and let be with row deleted (an matrix). Deleting row and column from corresponds exactly to deleting row from (on both factors), so
This holds because the entry () of is the same dot product of rows of as before, unaffected by whether row of is present or not, since we are only reading off rows .
Step 3: Cauchy–Binet formula. For an matrix with , the Cauchy–Binet formula states
where the sum ranges over all with , and denotes the submatrix of using only the columns indexed by . (This is a standard identity of multilinear algebra, provable by expanding both sides via the Leibniz determinant formula and matching terms through the Binet–Cauchy combinatorial identity for products of minors; we take it as a known prerequisite tool from linear algebra, analogous to but independent of the graph-theoretic content being proved here.) Applying this with :
Step 4: Identify each term. We claim that for a subset with :
Case A: is not a spanning tree. Since and is not a spanning tree, must contain a cycle (a graph on vertices with edges is a spanning tree iff it is acyclic iff it is connected — these are standard equivalent characterizations from the theory of Spanning Trees). Let be a cycle in , traversed with some direction, and orient this traversal. For each edge , let if the traversal direction of agrees with 's orientation in , and otherwise. Then for every vertex , : each vertex on the cycle has exactly one incoming and one outgoing cycle-edge under the traversal, and one checks directly that the signed contributions from these two incident edges cancel (the entry from the edge where the traversal agrees with orientation and the entry where it disagrees combine to regardless of the original head/tail assignment — a short case check on the four sign combinations confirms this), while vertices off the cycle contribute trivially as for incident-free entries. Hence the columns of indexed by satisfy a nontrivial linear dependency , so (and hence , its submatrix with row removed) has linearly dependent columns, giving . Case B: is a spanning tree . We show by induction on . If : is a single edge, is a matrix with entry (the incidence sign at the surviving vertex), so . For : since is a tree on vertices it has a leaf . Two subcases:
- If : the row of corresponding to has exactly one nonzero entry, , in the column for the unique edge at (all other columns have a in row , since is incident to only ). Expanding along the row (present in since ) gives with row , column deleted, up to sign. The remaining matrix is exactly the analogous incidence submatrix for the spanning tree of on vertices (with the same reference row deleted, since ), which by the inductive hypothesis has determinant . Hence .
- If : then row , which is deleted to form , was the only row with a nonzero entry in column (the edge at leaf ). So in , column is entirely zero — wait, we must instead argue directly: since is a leaf incident only to edge , and row is removed, the remaining rows of restricted to the other vertices and edges (deleting column as well, which is now handled by observing that in every other row has its usual entries) reduce, after expanding along column (which in has exactly one nonzero entry, , in the row of 's tree-neighbor — since 's only edge is and the row for itself is absent, but 's other endpoint, say , still has a in column and no other column of 's row overlaps since may have other edges too, so instead we expand along column , which has a unique nonzero entry precisely at row if 's only shared edge with this column is , true since each column of an incidence matrix has exactly two nonzero entries total, one per endpoint, and one of those endpoints is whose row is deleted) — we get , which by the inductive hypothesis (applied with or any fixed vertex of the smaller tree as reference row, all being equal by the symmetry we are in the process of establishing, or directly by re-deriving the case with reference vertex ) is .
In both subcases , completing the induction.
Step 5: Conclusion. Combining Steps 3 and 4:
This holds for every choice of reference vertex , so in particular all cofactors are equal to the same number , independent of .
∎Applying the Matrix-Tree Theorem to recovers .
For , , where is the all-ones matrix. Delete row and column to get the matrix . The matrix has eigenvalues (once, eigenvector ) and (with multiplicity , on the orthogonal complement of inside ). Hence has eigenvalues (once) and (multiplicity ). Its determinant, as the product of eigenvalues (valid since a real symmetric matrix is diagonalizable and determinant is the product of eigenvalues counted with multiplicity), is
By the Matrix-Tree Theorem, , matching Cayley's formula since every labeled tree on is (trivially) a spanning tree of , and conversely.
∎Worked examples
Example 1 — a non-complete, asymmetric graph. Let have vertex set and edges
This graph is a -cycle ––––– with one extra chord — wait, let us just list degrees directly from : vertex is in edges (degree ); vertex is in (degree ); vertex is in (degree ); vertex is in (degree ); vertex is in (degree ). Total degree , consistent with . The Laplacian is
Delete row and column :
Compute the determinant by cofactor expansion along the last row, whose entries are :
where is the minor deleting row 4, column 2 (with sign , so its contribution is , let's be careful and expand properly). Direct expansion along row 4 ():
where and . First, : sign . Delete row 4, column 2 from , leaving rows and columns :
Expand along column 3 (): only the middle entry contributes, with cofactor sign and minor . So this determinant . Thus . Next, : sign . Delete row 4, column 4, leaving rows , columns :
Expand along the first row: .
So . Therefore:
So . As a sanity check, one can verify this differently by deleting a different row/column, say row 1/column 1 (the theorem guarantees the same answer):
Expanding this determinant (omitted step-by-step for brevity, but following the identical cofactor procedure) also yields , confirming the row/column-independence guaranteed by the theorem. This graph has spanning trees out of possible labeled trees on vertices — a modest but nontrivial fraction, reflecting that has only edges rather than the of .
Example 2 — using cofactor expansion via eigenvalues on a graph that is not vertex-transitive. Consider the "paw plus pendant" graph on : a triangle on with a pendant edge from to . So , degrees .
Delete row/column :
Compute: expand along row 1:
So . This matches direct enumeration: the graph is a triangle with one pendant edge; every spanning tree must include the pendant edge (it is a bridge, the unique connection to vertex ), and must include exactly of the triangle edges to connect without a cycle — there are such choices, matching exactly.
Why connectivity and simplicity matter
Consider on with — two disjoint edges, a disconnected graph. Its Laplacian is
Deleting row/column :
Indeed since a disconnected graph on vertices has no spanning tree at all (a spanning tree, by definition, must be connected and touch all vertices). The Matrix-Tree Theorem still holds — it correctly outputs — but this shows the theorem's content is only meaningful and positive when is connected; if one mis-states the theorem as "the cofactor is always a positive count of trees," disconnected inputs falsify that stronger claim. This also illustrates why the theorem is phrased for general graphs (allowing ) rather than assuming connectivity outright: the determinant formula degrades gracefully rather than requiring a separate case. A second failure mode: if is allowed to have multi-edges or loops without adjusting the definitions (the standard Laplacian is defined for simple graphs, or multigraphs with counting edge multiplicities), a naive application of the incidence-matrix proof to a graph with a loop would break Step 1, since a loop contributes an edge with identical head and tail, making the corresponding column of the zero vector (a loop cannot be consistently oriented as at one endpoint and at a different endpoint, since head tail). This forces for any containing a loop automatically — which is actually consistent with the theorem since a loop can never appear in an acyclic spanning tree — but it shows that the proof technique implicitly relies on simple (loop-free) graphs for the incidence matrix to be meaningfully defined edge-by-edge; extending to multigraphs requires only that parallel edges get independent columns in , which the proof already accommodates without change.
Scope, edge cases & common traps
| Situation | What happens | Why |
|---|---|---|
| Corollary of Matrix-Tree via eigenvalues of | ||
| disconnected | , every cofactor is | No spanning tree exists; determinant reflects this |
| Cayley's formula convention: (single vertex, the "empty tree") | is undefined/ambiguous (); handled as a boundary case by convention | |
| Different reference vertex | All cofactors agree | Row/column independence is part of the theorem's content, proved via Cauchy–Binet |
| Multigraphs | Theorem still holds with edge multiplicity | Incidence matrix simply gets one column per edge, including parallels |
| Weighted graphs | A weighted version counts spanning trees by product of edge weights | Same proof with scaled by per column |
The mathematician behind it
Origins · 1 minGustav Robert Kirchhoff and how this was discovered
Kirchhoff, better known to physics students for Kirchhoff's circuit laws (current and voltage laws), derived what we now call the Matrix-Tree Theorem in 1847 while studying the flow of electrical current through resistor networks. His interest was not enumerative combinatorics for its own sake — he needed to solve linear systems describing current distribution, and the determinant identity fell out of that analysis, later recognized as a clean statement about counting spanning trees. Arthur Cayley, working independently in pure combinatorics, published the formula in 1889, four decades after Kirchhoff's circuit work, though a version of the count had appeared even earlier in work of Carl Wilhelm Borchardt (1860) and, in special cases, Karl von Ettingshausen (1827) — the naming of "Cayley's formula" reflects Cayley's influential exposition and use of the result rather than strict historical priority.
Why does Cayley's formula use and not, say, or ?
The exponent arises precisely because a labeled tree on vertices has edges, and the Prüfer bijection encodes the tree using a sequence of length — one entry recorded for every leaf-deletion step except the last, since the process halts once two vertices remain and their connecting edge is implicit rather than recorded. Each of the positions can independently be any of the labels, giving choices times, hence . Contrast this with , which would count labeled linear orderings, a completely different structure with far more redundancy removed by symmetry, and with , which turns out to count a different but related object: rooted labeled trees, or equivalently functional graphs on points forming a single tree structure directed toward a root, since there are choices of root for each of the unrooted trees.
Does the Matrix-Tree Theorem require the graph to be simple, with no multi-edges or loops?
The theorem extends cleanly to multigraphs (parallel edges) simply by letting the adjacency matrix entry record the number of parallel edges between and , and by giving the oriented incidence matrix one column per edge, including parallel copies with independent orientations. The proof goes through unchanged because Cauchy–Binet does not care whether two columns of happen to connect the same pair of vertices. Loops, however, must be excluded or handled by convention (a loop typically contributes to the Laplacian by convention, since it does not affect connectivity), because a loop edge cannot be assigned a consistent head/tail orientation, and any spanning tree by definition contains no loops regardless.
Is there a way to see Cayley's formula without the full Prüfer bijection machinery?
Yes — Cayley's formula also follows purely algebraically from the Matrix-Tree Theorem, as shown in the corollary above, by diagonalizing the Laplacian of using the known eigenvalues of the all-ones matrix . There is also a classical double-counting proof due to Jim Pitman using an ingenious bijection between forests of rooted trees and edge-sequences, and a generating-function proof via the exponential formula applied to rooted trees. All of these are legitimate independent proofs; the Prüfer approach was chosen here because it gives the most concrete, checkable, constructive bijection, directly exhibiting both directions of the correspondence.
Why must we delete a matching row and column of the Laplacian, rather than any row and any column?
The Laplacian is defined so that row and column both correspond to the same vertex ; deleting matching row/column removes vertex 's entire "presence" from the linear system in a way that mirrors fixing vertex as a reference point (physically, grounding node in a circuit, algebraically eliminating the redundant equation caused by the all-ones kernel vector). Deleting mismatched indices does not correspond to any natural graph operation, and the resulting minor is not guaranteed to be an integer count of anything meaningful; it can even be negative or fail to match other such minors, since the identity in the Matrix-Tree Theorem specifically concerns principal minors (matching row/column pairs), which is exactly the class of minors handled by the Cauchy–Binet argument in the proof.
How does the Matrix-Tree Theorem relate to electrical resistance, as in Kirchhoff's original motivation?
If is viewed as a network of unit resistors along each edge, the effective resistance between two vertices can be written using cofactors and minors of the Laplacian (specifically, ratios of certain 2-vertex-deleted determinants to the single-vertex-deleted determinant ). This identity, sometimes called the generalized Matrix-Tree Theorem, expresses effective resistance directly as a ratio of weighted spanning tree/spanning forest counts, which is precisely the combinatorial content Kirchhoff needed for his circuit laws. This is one of the deepest bridges the theorem provides: a physical quantity (resistance) equals a purely combinatorial ratio (tree/forest counts).
Does the theorem give an efficient algorithm for computing , rather than just a formula?
Yes, and this is one of the theorem's most practically important consequences: computing via Gaussian elimination takes arithmetic operations for a graph on vertices, dramatically faster than any method that tries to enumerate spanning trees directly, since can be exponentially large in (as Cayley's formula itself demonstrates for ). This efficient determinant computation is the standard practical method used in software for counting or sampling spanning trees in real network graphs.
What happens to the formula if edges are weighted, as in a resistor network with varying resistances?
The weighted Matrix-Tree Theorem replaces each off-diagonal Laplacian entry with (the edge weight) and each diagonal entry with the weighted degree ; the same cofactor then computes the sum, over all spanning trees , of the product of edge weights in , rather than a plain count. Setting all weights to recovers the unweighted theorem exactly, confirming this is a strict generalization rather than a different statement.