Graph theory

Isomorphism and spectra

When are two graphs the same up to renaming, and what symmetries does one graph have? Colour refinement and backtracking answer both for small graphs. The eigenvalues of the adjacency matrix, computed exactly, give invariants that count walks and spanning trees.

Isomorphism

import { isomorphism, petersen, kneser, cycleGraph, graph, pathGraph, star } from "@ratmath/discrete";

isomorphism(petersen(), kneser(5, 2)).isomorphic;   // true, with the map
isomorphism(cycleGraph(6), graph("a-b b-c c-a d-e e-f f-d")).reason;
// "colour refinement … splits the vertices differently": C6 against two triangles
isomorphism(pathGraph(4), star(3)).reason;   // "degree sequences differ: (2, 2, 1, 1) against (3, 1, 1, 1)"

Cheap invariants go first: vertex and edge counts, degree sequences. Then colour refinement (1-dimensional Weisfeiler–Leman): colour each vertex by its degree, then by its colour together with the multiset of its neighbours' colours, until nothing splits. An isomorphism must keep colours, so different class sizes prove there is none, and equal ones cut the backtracking search down. A "yes" is checked edge by edge.

Automorphisms

import { automorphisms, hypercube } from "@ratmath/discrete";
const a = automorphisms(petersen());
a.order;           // 120n: the symmetric group S5 acting on 2-subsets
a.orbits.length;   // 1: vertex-transitive
automorphisms(hypercube(3)).order;   // 48n

The group is built as a chain of stabilisers: fix a vertex, find one automorphism to each vertex of its orbit, then fix the next vertex. By the orbit–stabiliser theorem

∣Aut⁡(G)∣=∏i∣orbit of vi in Gv1…vi−1∣, |\operatorname{Aut}(G)| = \prod_i |\text{orbit of } v_i \text{ in } G_{v_1 \dots v_{i-1}}|,

so few generators give the order exactly. Up to order 50,000 the group also comes as a PermGroup from @ratmath/algebraic, whose own order is compared with the product. The Frucht graph is 3-regular yet has only the identity.

Spectra

import { spectrum, walks, spanningTreesFromSpectrum, spanningTreeCount } from "@ratmath/discrete";
spectrum(petersen()).toString();                     // "−2 (×4), 1 (×5), 3"
spectrum(hypercube(3)).toString();                   // "−3, −1 (×3), 1 (×3), 3"
spectrum(cycleGraph(5)).toString();                  // the values 2 cos(2πk/5), with √5
spanningTreesFromSpectrum(petersen());               // 2000n, as spanningTreeCount gives
walks(cycleGraph(5), 5).closed;                      // 10n closed walks of length 5

The characteristic polynomial det⁡(xI−A)\det(xI - A) is computed over the integers by the Faddeev–LeVerrier recursion, and its roots are found exactly with @ratmath/algebraic. A symmetric matrix has only real eigenvalues, which is checked. The number of closed walks of length kk is tr⁡Ak=∑λik\operatorname{tr} A^k = \sum \lambda_i^k. For the Laplacian L=D−AL = D - A with eigenvalues 0=μ1≤μ2≤⋯≤μn0 = \mu_1 \le \mu_2 \le \dots \le \mu_n, the matrix-tree theorem gives the number of spanning trees:

τ(G)=1n μ2μ3⋯μn, \tau(G) = \frac{1}{n}\,\mu_2 \mu_3 \cdots \mu_n,

which is read off the coefficient of xx in the Laplacian's characteristic polynomial, with no root-finding needed.

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