Trees

Trees

Trees are the connected graphs with no cycles, the skeletons of every connected graph. Here: the lightest spanning tree, the exact number of spanning trees, and a numbered list of all labelled trees through their Prüfer codes.

What a tree is

For a graph with nn vertices the following say the same thing: it is connected and has no cycle; it is connected and has n−1n - 1 edges; it has no cycle and n−1n - 1 edges; between any two vertices there is exactly one path. isTree checks the second, isForest checks for a cycle.

isTree(graph("1-2 2-3 2-4"));    // true
isTree(cycleGraph(4));           // false
isForest(graph("1-2 3-4"));      // true: two trees

The lightest spanning tree

const g = graph("a-b:4 a-c:1 b-c:2 b-d:5 c-d:8 d-e:3 c-e:9/2");
const t = minimumSpanningTree(g);
t.weight.toString();                 // "21/2": a-c, b-c, d-e, c-e
t.edges.map((e) => e.from + "-" + e.to);
minimumSpanningTree(g, { method: "prim" }).weight.toString();    // the same
minimumSpanningTree(g, { maximum: true }).weight.toString();     // the heaviest: "43/2"

A spanning tree uses every vertex and some of the edges. Both methods rest on the cut property: for any split of the vertices into two sides, a lightest edge crossing the split belongs to some minimum spanning tree (if a tree avoided it, adding it makes a cycle that crosses the split a second time, and swapping out that other crossing edge cannot make the tree heavier). Kruskal's method takes the edges from lightest up and keeps each one that joins two different pieces, tracked with a union–find structure. Prim's method grows one tree from a vertex, always adding the lightest edge leaving it. With exact weights, ties are real ties, broken by the order the edges were added, so the answer never depends on rounding. A disconnected graph gets a spanning forest, with connected: false.

How many spanning trees

spanningTreeCount(complete(10));          // 100000000n = 10⁸ (Cayley)
spanningTreeCount(platonic("cube"));      // 384n
spanningTreeCount(petersen());            // 2000n
spanningTreeCount(completeBipartite(3, 4)); // 432n = 3³ · 4²

Kirchhoff's matrix-tree theorem: let L=D−AL = D - A be the Laplacian, the diagonal matrix of degrees minus the adjacency matrix. Then the number of spanning trees equals any cofactor of LL, for example the determinant of LL with its first row and column removed. The determinant is computed exactly with integers by Bareiss elimination, in which every division is exact, so the count is right however large it is. Parallel edges count as different trees.

Prüfer codes, and Cayley's formula

const t = graph("1-4 2-4 3-4 4-5 5-6");
prufer(t);                       // [4, 4, 4, 5]
fromPrufer([4, 4, 4, 5]);        // the same tree back
const trees = labelledTrees(6);
trees.count;                     // 1296n = 6⁴
trees.unrank(1000);              // the tree with code number 1000
trees.random(rng("forest"));     // a uniformly random labelled tree

To encode a tree on the labels 1…n1 \ldots n, remove the smallest leaf and write down its neighbour; repeat until two vertices remain. The result is a sequence of n−2n - 2 labels, and the tree can be rebuilt from it: the first removed leaf is the smallest label that does not appear in the code, and so on. So the code is a bijection between labelled trees and sequences of length n−2n - 2 over nn labels, which proves Cayley's formula

#{labelled trees on n vertices}=n n−2. \#\{\text{labelled trees on } n \text{ vertices}\} = n^{\,n-2}.

A vertex's degree is one more than the number of times it appears in the code, so leaves are exactly the labels that never appear. labelledTrees(n) is a family numbered by code, so ranking, unranking and uniform random trees come for free; the tests check that it lists all 64=12966^4 = 1296 trees on six vertices, each once, and that nn−2n^{n-2} agrees with the matrix-tree count for KnK_n.

Try itEdit the code and run it with @ratmath/discrete and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs