Enumerative graph theory · formal theorem

Cayley’s Formula for Labeled Trees

For every n ≥ 1, the number of labeled unrooted trees on the vertex set Fin n is exactly n^(n − 2).

Scope: For every natural number n with 1 ≤ n, the number of labeled unrooted trees on Fin n is n^(n − 2).

Lean checkedBuild passed
Unfinished proof stepsNone
PublicationAccepted formal theorem
Cayley formula statement mapLabeled trees on n vertices correspond to Prüfer codes of length n minus two, giving exactly n to that power.
Exact scope: For every natural number n with 1 ≤ n, the number of labeled unrooted trees on Fin n is n^(n − 2).

Exact formal proposition

Hypotheses and conclusion

theorem cayleyTreeFormula : CayleyTreeFormulaStatement

Proof route

Why Prüfer codes count labeled trees

6 proof stages

The checked proof turns labeled trees into Prüfer codes by repeatedly deleting one canonical leaf, builds an exact decoder with a final-pair step, proves both state machines are inverse, and transfers the code-space cardinality back to trees.

These stages follow the checked source and explain the mathematical route. They summarize the argument; they are not a visualization of Lean's internal proof term.

  1. Scientific figure illustrating: For n ≥ 2, counting trees reduces to counting length-n−2 codes over the same labels.
    For n ≥ 2, counting trees reduces to counting length-n−2 codes over the same labels.
    01

    The tree count becomes a code count

    LabeledTree (n + 2) ≃ PruferCode (n + 2)

    The proof separates the one-vertex boundary case and reduces the main count to an equivalence between trees and functions from n−2 positions into the labels.

    Lean lemmas for this step
    • PruferCode
    • pruferCode_natCard
    • labeledTree_one_natCard
    • pruferEquiv
  2. A before-and-after tree diagram shows blue leaf ℓ joined directly to gold vertex 4 before deletion and only vertex 4 remaining afterward; a gold guide carries 4 into a single code tile.
    Deleting leaf ℓ removes only its edge to vertex 4; the encoder emits 4 and preserves the reduced tree.
    02

    The encoder emits the deleted leaf’s neighbor

    chosenStep emits the neighbor label and erases the chosen leaf

    The left tree has eight vertices and seven edges. The blue leaf ℓ has degree one and is directly incident to gold vertex 4. In the right tree, ℓ and that edge are absent; the same seven other vertices and six edges remain connected and acyclic, and 4 becomes a leaf. A gold guide from vertex 4 enters the single code tile labeled 4.

    Lean lemmas for this step
    • chosenLeaf
    • PruferState.chosenStep
    • PruferState.chosenStep_labelFinset_eq_erase_chosenLeaf
  3. Scientific figure illustrating: Three canonical deletions shrink the remaining-label set and fill the Prüfer code in a fixed order.
    Three canonical deletions shrink the remaining-label set and fill the Prüfer code in a fixed order.
    03

    Repeated pruning produces the ordered code

    pruferEncodeState records an ordered head and recursive tail

    Successive encoder states erase STAR, SQUARE, and DIAMOND. Their neighbors become the ordered code prefix while original labels persist.

    Lean lemmas for this step
    • PruferState.labelFinset
    • PruferState.liftedGraph
    • pruferEncodeState_succ_zero
    • pruferEncodeState_succ_tail
  4. Scientific figure illustrating: Each code token determines one decoder edge, and the last two labels supply the seventh edge.
    Each code token determines one decoder edge, and the last two labels supply the seventh edge.
    04

    The decoder rebuilds every edge

    pruferDecodeTree : PruferCode (n + 2) → LabeledTree (n + 2)

    The decoder joins the least remaining label absent from the tail to the current head. Six recursive edges and the final pair produce a tree.

    Lean lemmas for this step
    • decodeLeaf
    • PruferDecodeState.step
    • PruferDecodeState.finish
    • pruferDecodeGraph_isTree
    • pruferDecodeTree
  5. Scientific figure illustrating: Decode after encode recovers the tree, and encode after decode recovers the code.
    Decode after encode recovers the tree, and encode after decode recovers the code.
    05

    Encoder and decoder close both inverse loops

    decode (encode T) = T and encode (decode c) = c

    State invariants align the chosen leaf with the least missing label at every step, proving both inverse laws.

    Lean lemmas for this step
    • PruferState.decodeLeaf_encode_eq_chosenLeaf_label
    • pruferDecodeState_encode_graph_sup_final_liftedGraph_eq
    • pruferEncode_decodeTree_succ
    • pruferEquiv
  6. Scientific figure illustrating: With n choices at each of n−2 positions, the code space has n^(n−2) elements.
    With n choices at each of n−2 positions, the code space has n^(n−2) elements.
    06

    The code space gives Cayley’s count

    Nat.card (LabeledTree n) = n ^ (n − 2)

    The equivalence transfers the code-space cardinality to labeled trees; the one- and two-vertex cases are handled separately in the source.

    Lean lemmas for this step
    • pruferCode_natCard
    • pruferEquiv
    • labeledTree_one_natCard
    • cayleyTreeFormula

The theorem at a glance

Cayley’s Formula at a glance

Editorial theorem poster stating Cayley’s formula, the Prüfer encoder and decoder, and the exact labeled-tree scope.
Cayley’s formula counts labeled trees through an equivalence with length-n−2 Prüfer codes.

Accessible transcript

Nat.card (LabeledTree n) = n ^ (n − 2)

For every n at least one, the number of finite simple trees on the labeled vertex type Fin n is n raised to n minus two. The proof builds an encoder, decoder, and both inverse laws.

Read the complete poster transcript

COMBINATORICS · LABELED TREES

CAYLEY’S FORMULA

HOW MANY TREES ON n LABELED VERTICES?

FOR EVERY n ≥ 1

|LabeledTree(n)| = n^(n − 2)

PRÜFER CODES

A tree on n ≥ 2 vertices corresponds to a sequence of length n − 2 with entries in Fin n.

LEAF → NEIGHBOR

Remove the least leaf and record its unique neighbor.

CODE → TREE

Reconnect the least missing remaining label, then join the final two labels.

WHY THE COUNT IS n^(n − 2)

n choices for each of n − 2 positions.

HOW THE PROOF MOVES

1 · CHOOSE THE LEAST LEAF

2 · ENCODE ITS NEIGHBOR

3 · REBUILD FROM THE CODE

4 · PROVE BOTH INVERSE LAWS

5 · TRANSFER CARDINALITY

EXACT SCOPE

Finite simple trees on the labeled vertex type Fin n. Counts labeled trees, not unlabeled tree shapes.

Theorem schematic

Prüfer encoding and decoding

Scientific figure illustrating: Canonical leaf deletion turns a labeled tree into a Prüfer code, and the decoder reconstructs the same tree.
Canonical leaf deletion turns a labeled tree into a Prüfer code, and the decoder reconstructs the same tree.

Labeled trees on n vertices ↔ Prüfer codes of length n − 2

The least current leaf is removed while its unique neighbor is recorded. Repetition produces an ordered code, and reversing the state changes reconstructs the labeled tree.

Result boundary

What this theorem does—and does not—establish

For every natural number n with 1 ≤ n, the number of labeled unrooted trees on Fin n is n^(n − 2).

About these visual explanations

These AI-generated visuals explain the theorem and proof route; they are not proof evidence. Their publication review was completed separately from review of the formal result. The exact Lean proposition and checked source remain authoritative.

Line counts exclude blank lines; comments and documentation count. The total is the commit-pinned first-party Lean import closure; Mathlib and other third-party dependencies are excluded.

Continue the mathematics

Open questions and extensions

The pinned theorem and its complete local import closure let people and AI agents inspect the proof, compare another route, isolate reusable lemmas, or formulate a stronger exact statement. Lean checks each proposed extension against its own exact statement.

What the source ZIP contains

The ZIP contains the checked first-party Lean import closure, exact statements and boundaries, license, notice, evidence, source-footprint manifest, and continuation data. Mathlib and other third-party dependencies are not bundled.

Publication review

Review record

Publication reviews accepted

Lean checks the exact proof. Accepted review records cover evidence completeness, statement alignment, scope, and public wording; the visual review covers explanation only.

01

Formal evidence

The formal-evidence review accepted the recorded build, exact declarations, unfinished-step scan, and axiom evidence.

02

Statement alignment

The formal declaration was accepted against the named theorem and its exact variant.

03

Result boundary

The accepted boundary keeps nearby stronger or commonly confused claims out of scope.

04

Public wording

The public-wording review accepted the retained theorem explanation and source presentation. Generated media follows a separate review and promotion gate.

05

Canonical source

The first-party source link is pinned to the checked package commit and exact Lean file.

06

Accepted result

A validated accepted-result record binds the four reviews to the checked formalization.

Expanded visual

Open original image