Colouring and cliques
Colour the vertices so that neighbours differ. The fewest colours that work is the chromatic number ; the number of ways to do it with colours is a polynomial in .
The chromatic number
import { petersen, wheel, chromaticNumber, greedyColouring, isProperColouring } from "@ratmath/discrete";
const c = chromaticNumber(petersen());
c.number; // 3
c.lowerBound; // { kind: "odd-cycle", cycle: [...], note: "an odd cycle cannot be coloured with two colours" }
chromaticNumber(wheel(5)).lowerBound; // { kind: "search", note: "no 3-colouring exists: …" }
isProperColouring(petersen(), greedyColouring(petersen())); // { ok: true, clash: null }
An upper bound is a colouring, which anyone can check. A lower bound needs an argument: a clique of size needs colours, an odd cycle needs three, and otherwise the record that an exhaustive DSATUR search with colours failed. The wheel with five spokes has no , yet needs four colours: the rim is an odd cycle and the hub sees all of it.
The chromatic polynomial
import { chromaticPolynomial, cycleGraph } from "@ratmath/discrete";
chromaticPolynomial(cycleGraph(5)).toString(); // "k^5 − 5k^4 + 10k^3 − 10k^2 + 4k"
const P = chromaticPolynomial(petersen());
P.at(3); // 120n
P.at(4); // 12960n
For an edge , colourings of either give the ends different colours (colourings of ) or the same (colourings of , with contracted). So
down to edgeless graphs with . For a cycle this gives . The recursion is memoised on graph shape, which keeps graphs like Petersen quick.
Cliques and independent sets
import { maximumClique, maximalCliques, maximumIndependentSet, graph } from "@ratmath/discrete";
maximumClique(petersen()); // ["1,2", "3,4"]: Petersen has no triangle
maximumIndependentSet(petersen()); // 4 vertices
maximalCliques(graph("a-b b-c c-a c-d")); // [["a","b","c"], ["d","c"]] (Bron–Kerbosch)
, and since each colour class is independent. The Petersen graph has , so it needs at least colours.
@ratmath/discrete and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.