Count it, list it, pick one at random, or draw the graph
The numbers of combinatorics as exact integers of any size; every standard family of objects in a fixed order, with the millionth one found without listing the rest; a random generator that gives the same stream from the same seed on every machine; and graphs with exact weights, where every "no" comes with its reason.
What is here
Counting, enumeration and the generator (phase 1); graphs: structure, searching, paths, tours and trees (phase 2); generating functions and recurrences (phase 3); matchings, flows, colouring, planarity, isomorphism and spectra (phase 4); exact linear programming with matrix and bimatrix games (phase 5); combinatorial and cooperative games and voting (phase 6); relations, orders and Pólya counting (phase 7); and error-correcting codes and designs (phase 8). Graph drawing is still planned and listed at the bottom of this page.
A first program
import { binomial, partitionCount, permutations, setPartitions, rng } from "@ratmath/discrete";
binomial(100, 50); // 100891344545564193334812497256n: exact, however large
partitionCount(200); // 3972999029388n
const p = permutations(20); // all 2432902008176640000 of them, none listed yet
p.unrank(999999); // [1, 2, …, 10, 13, 18, 19, 14, 20, 12, 16, 15, 17, 11]
p.rank([20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]); // 2432902008176639999n
const g = rng("my experiment");
setPartitions(["ann", "bo", "cy", "di"]).random(g); // a uniformly random way to split four people into groups
g.shuffle([1, 2, 3, 4, 5]); // the same shuffle every time this seed is used
import { graph, shortestPath, bipartition, petersen, hamiltonCycle } from "@ratmath/discrete";
const roads = graph("a-b:0.1 b-c:0.2 c-d:0.3 a-d:0.6");
shortestPath(roads, "a", "d").distance; // exactly 3/5: both routes tie (in floating point they differ)
bipartition(graph("1-2 2-3 3-1")); // { bipartite: false, oddCycle: [2, 1, 3, 2] }: the reason
hamiltonCycle(petersen()).exists; // false, proved by exhaustive search
import { linearProgram, matrixGame } from "@ratmath/discrete";
linearProgram({ maximize: [30, 50], constraints: [[[2, 4], "<=", 100], [[3, 2], "<=", 90]] }).dual;
// [45/4, 5/2]: the prices that prove 1350 is the best
String(matrixGame([[3, -1], [-2, 1]])); // "value 1/7; row plays r1 3/7, r2 4/7; column plays c1 2/7, c2 5/7"
Three ideas
Counts are exact. Every counting function returns a BigInt. has 30 digits and all of them are right; nothing is rounded, so identities can be checked by ===.
A family is a numbered list you never have to write out. permutations(20) is an object that knows its size, the object at any place (unrank) and the place of any object (rank). That turns "a uniformly random permutation" into "a uniformly random number below ", and it is how a search or a simulation can be split up or resumed. See Enumeration.
A "no" comes with its reason. A graph that is not bipartite comes back with an odd cycle, a directed graph with no topological order with a directed cycle, a shortest-path question spoiled by a negative cycle with that cycle, a graph with no Euler tour with its odd vertices. See Connectivity. The same goes for optimisation: an optimum comes with the dual prices that prove it, an impossible linear program with a Farkas combination of its constraints. See Linear programming.
Random means reproducible. The generator is integer arithmetic from end to end. A seed fixes the stream on every platform, whole numbers of any size are exactly uniform, and probabilities are exact fractions. See The seeded generator.
What is planned
From the library roadmap. Sizes are rough estimates in lines of source.
| Phase | Contents | Estimate |
|---|---|---|
| 1 | Counting, enumeration with ranking, the seeded generator | built |
| 2 | Graphs: structure, traversal, paths, trees, families | built |
| 3 | Generating functions and recurrences | built |
| 4 | Matchings, flows, colouring, planarity, isomorphism, spectra | built |
| 5 | Exact linear programming; matrix and bimatrix games | built |
| 6 | Combinatorial, extensive-form and cooperative games; voting | built |
| 7 | Sets, relations, partial orders; Pólya counting | built |
| 8 | Codes and designs | built |
| 9 | Graph drawing | 900 in graphics |
The package depends on Core, on @ratmath/algebraic for exact roots (closed forms of recurrences, spectra) and permutation groups (automorphisms, Pólya counting), and on @ratmath/logic, whose Boolean functions (truth tables, normal forms, minimisation) are re-exported here and as logic. Linear programming is its own exact simplex method, and characteristic polynomials are computed over the integers directly. The statistics library draws its random data from the generator here.
@ratmath/discrete and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.