Graph theory

Colouring and cliques

Colour the vertices so that neighbours differ. The fewest colours that work is the chromatic number χ(G)\chi(G); the number of ways to do it with kk colours is a polynomial in kk.

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 kk needs kk colours, an odd cycle needs three, and otherwise the record that an exhaustive DSATUR search with k−1k-1 colours failed. The wheel with five spokes has no K4K_4, 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 ee, colourings of G−eG - e either give the ends different colours (colourings of GG) or the same (colourings of G/eG/e, with ee contracted). So

P(G,k)=P(G−e,k)−P(G/e,k), P(G, k) = P(G - e, k) - P(G / e, k),

down to edgeless graphs with P=knP = k^n. For a cycle this gives P(Cn,k)=(k−1)n+(−1)n(k−1)P(C_n, k) = (k-1)^n + (-1)^n (k-1). 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)

ω(G)≤χ(G)\omega(G) \le \chi(G), and χ(G)≥n/α(G)\chi(G) \ge n/\alpha(G) since each colour class is independent. The Petersen graph has α=4\alpha = 4, so it needs at least ⌈10/4⌉=3\lceil 10/4 \rceil = 3 colours.

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