Graph theory

Matchings and flows

A matching pairs vertices along edges; a flow sends material through a network. In both, the best answer comes with a short proof that nothing better exists: a Tutte–Berge set, a König cover, dual prices, or a cut.

Maximum matching in any graph

import { graph, star, maximumMatching } from "@ratmath/discrete";

const g = graph("a-b b-c c-a c-d d-e e-f f-d");   // two triangles joined by an edge
const m = maximumMatching(g);
m.edges;          // [["a","b"], ["c","d"], ["e","f"]]: perfect
maximumMatching(star(5)).certificate;
// { set: [0], oddComponents: [[1],[2],[3],[4],[5]], bound: 1 }

Edmonds' blossom algorithm grows alternating trees and shrinks odd cycles. The certificate is the Tutte–Berge formula: for any set UU of vertices, with odd⁡(G−U)\operatorname{odd}(G-U) the number of odd components left when UU is removed,

ν(G)  =  min⁡U⊆V12(∣V∣+∣U∣−odd⁡(G−U)). \nu(G) \;=\; \min_{U \subseteq V} \tfrac12\bigl(|V| + |U| - \operatorname{odd}(G - U)\bigr).

Each odd component must send a vertex to UU to be matched completely, and UU has only ∣U∣|U| vertices. The algorithm returns a UU where the bound equals the matching size (from the Gallai–Edmonds decomposition), and check() recomputes both sides from the graph alone.

Bipartite graphs: König and Hall

import { bipartiteMatching } from "@ratmath/discrete";
bipartiteMatching(graph("a-x a-y b-x c-x"), { left: ["a", "b", "c"] });
// size 2, cover ["a", "x"], hall { set: ["c", "b"], neighbours: ["x"] }

In a bipartite graph the largest matching equals the smallest vertex cover (König), and a side can be matched completely exactly when every set SS of its vertices has at least ∣S∣|S| neighbours (Hall). When it cannot, the answer names a set SS with ∣N(S)∣<∣S∣|N(S)| < |S|.

Assignment and stable matching

import { assignment, stableMatching } from "@ratmath/discrete";
const a = assignment([[4, 1, 3], [2, 0, 5], [3, 2, 2]]);
a.assignment;   // [1, 0, 2]: row 0 takes column 1, …
a.cost;         // 5, with rowPrices [2, 1, 2] and columnPrices [1, −1, 0]
stableMatching({ a: ["x", "y", "z"], b: ["y", "x", "z"], c: ["x", "y", "z"] },
               { x: ["b", "a", "c"], y: ["a", "b", "c"], z: ["a", "b", "c"] }).matching;
// { a: "x", b: "y", c: "z" }, with no blocking pair

The assignment problem is a linear program whose optimum is always a permutation. Its dual prices ui+vj≤ciju_i + v_j \le c_{ij}, with equality on the chosen cells, prove the cost is least: any assignment costs at least ∑ui+∑vj\sum u_i + \sum v_j. Gale–Shapley's proposal algorithm gives the stable matching best for the proposers.

Maximum flow and minimum cut

import { digraph, maxFlow } from "@ratmath/discrete";
const f = maxFlow(digraph("s>a:3 s>b:2 a>b:1 a>t:2 b>t:3"), "s", "t");
f.value;        // 5
f.cut.source;   // ["s"]: the cut {s→a, s→b} has capacity 5 too
maxFlow(graph("s-a:1/2 a-t:1/3 s-t:1"), "s", "t").value;   // 4/3, exactly

Edmonds–Karp augments along shortest paths in the residual network. When no augmenting path is left, the vertices still reachable from the source form a cut whose capacity equals the flow, which is the max-flow min-cut theorem: no flow exceeds any cut, so both are optimal. Capacities are exact fractions, so the answer is too.

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