Graphs

Graphs

A graph is a set of vertices and the edges that join them. Here a graph can be written as a line of text, its weights are exact fractions, and the standard families are one call away.

Writing one down

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

const g = graph("a-b b-c c-a c-d");      // a triangle with a tail
g.order;                                  // 4 vertices
g.size;                                   // 4 edges
g.neighbors("c");                         // ["b", "a", "d"]
g.degreeSequence();                       // [3, 2, 2, 1]

const road = graph("home-shop:3/2 shop-work:2 home-work:4");   // weights are exact fractions
road.weight("home", "shop").toString();                       // "3/2"

const d = digraph("1>2 2>3 3>1 3>4");     // a directed graph: write a>b
d.outNeighbors(3);                        // [1, 4]
d.inDegree(1);                            // 1

The text form reads edges u-v (or u>v in a directed graph), chains a-b-c, lone vertices, and a weight after a colon. Names made of digits become numbers. A list of pairs [[1, 2], [2, 3, "1/2"]] or an adjacency object { a: ["b", "c"] } work as well, and String(g) gives the text back.

A graph is simple unless asked otherwise: a second edge between the same two vertices, or an edge from a vertex to itself, is refused with the reason. Pass { multi: true } for parallel edges (the bridges of Königsberg need them) and { loops: true } for loops.

Degrees and the handshake

The degree of a vertex is the number of edge-ends at it, a loop counting twice. Each edge has two ends, so

∑vdeg⁡v=2 ∣E∣, \sum_{v} \deg v = 2\,|E|,

and the number of vertices of odd degree is always even. In a directed graph each edge leaves one vertex and enters one, so the out-degrees and the in-degrees each add up to ∣E∣|E|.

The standard families

complete(5);              // K₅: every pair joined, 10 edges
cycleGraph(6);            // C₆
pathGraph(4);             // P₄: 1-2-3-4
completeBipartite(3, 3);  // K₃,₃
hypercube(3);             // Q₃: vertices 000 … 111
grid(3, 4);               // vertices "1,1" … "3,4"
petersen();               // the Petersen graph, as the Kneser graph K(5, 2)
platonic("dodecahedron"); // the skeleton of a Platonic solid
turan(10, 3);             // the most edges on 10 vertices with no K₄
randomGraph(12, "1/4", rng("seed"));   // G(n, p), p exact

Vertices are numbered from 1 unless a family has natural names: the cube's bit strings, the grid's row and column, the subsets of a Kneser graph. Random graphs come from the seeded generator, so the same seed gives the same graph on every machine; the probability is an exact fraction, so "1/3" means one third and not the double nearest to it.

New graphs from old

const g = cycleGraph(5);
g.complement();                        // the 5-cycle again: C₅ is self-complementary
g.lineGraph();                         // a vertex for each edge
g.subgraph([1, 2, 3]);                 // induced: those vertices and the edges among them
cartesianProduct(pathGraph(3), pathGraph(4));   // the 3 × 4 grid
tensorProduct(complete(3), complete(2));        // a 6-cycle
join(emptyGraph(3), emptyGraph(3));             // K₃,₃

The Cartesian product G□HG \mathbin{\square} H has a vertex for each pair (a,b)(a, b), with (a,b)(a, b) joined to (c,d)(c, d) when a=ca = c and b∼db \sim d, or b=db = d and a∼ca \sim c: grids are products of paths, and the cube QdQ_d is K2K_2 multiplied by itself dd times. The tensor product joins pairs that move in both coordinates at once, the strong product allows either, and the line graph L(G)L(G) turns edges into vertices, joined when the edges share an end, so a vertex of degree dd in GG contributes (d2)\binom d2 edges to L(G)L(G).

The adjacency matrix, in the order of vertices(), is g.adjacencyMatrix(). Its spectrum is planned for a later phase, through linalg.

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