Connectivity
Searching a graph, and what the search reveals: the pieces it falls into, the edges and vertices it hangs on, an order for a directed graph without cycles, and whether two colours are enough. When the answer is no, the reason comes with it.
Two ways to search
const g = graph("1-2 1-3 2-4 3-4 4-5 6-7");
bfs(g, 1).order; // [1, 2, 3, 4, 5]: nearest first
bfs(g, 1).depth.get(5); // 3 edges away
dfs(g, 1).order; // [1, 2, 4, 3, 5]: as deep as possible first
components(g); // [[1, 2, 3, 4, 5], [6, 7]]
isConnected(g); // false
Breadth-first search keeps a queue: it visits the start, then everything one edge away, then everything two edges away, and so on, which is why the depth it records is the distance. Depth-first search keeps a stack: it follows edges as far as it can and backs up only when stuck. Each visits every vertex it can reach once and looks at every edge at most twice, so both take time proportional to . Both are written without recursion, so a path of a million vertices does not overflow the stack.
Weak points: bridges and cut vertices
const g = graph("a-b b-c c-a c-d d-e e-f f-d"); // two triangles joined by c-d
bridges(g).map((e) => e.from + "-" + e.to); // ["c-d"]
cutVertices(g); // ["c", "d"]
blocks(g); // [["d", "e", "f"], ["c", "d"], ["a", "b", "c"]]
A bridge is an edge whose removal disconnects its component, and a cut vertex is a vertex whose removal does. An edge is a bridge exactly when it lies on no cycle. One depth-first search finds them all (Hopcroft and Tarjan): number the vertices in the order they are discovered, and let be the smallest number reachable from 's subtree using at most one edge that is not in the search tree. A tree edge is a bridge when , and is a cut vertex when for some child (the root is a cut vertex when it has two or more children). The blocks are the pieces between cut vertices; every edge lies in exactly one.
Directed graphs: strong components and topological order
const d = digraph("1>2 2>1 2>3 3>4 4>3 4>5");
stronglyConnectedComponents(d); // [[5], [3, 4], [1, 2]]
condensation(d).graph.toString(); // "1>2 2>3": the components, in order
const tasks = digraph("wake>shower wake>coffee coffee>leave shower>dress dress>leave");
topologicalOrder(tasks); // { acyclic: true, order: ["wake", "shower", "coffee", "dress", "leave"] }
topologicalOrder(digraph("a>b b>c c>a")); // { acyclic: false, cycle: ["a", "b", "c", "a"] }
Two vertices are strongly connected when each can reach the other. That is an equivalence relation, and its classes are the strong components; shrinking each to a point gives the condensation, which never has a cycle. A topological order lists the vertices so that every edge points forward. It exists exactly when there is no directed cycle, and Kahn's method finds it by repeatedly taking a vertex with nothing left pointing into it. When it gets stuck, every remaining vertex has an edge coming in, so following edges backwards must eventually repeat a vertex: that cycle is returned as the proof that no order exists.
Two colours: bipartite graphs
bipartition(grid(2, 3)); // { bipartite: true, parts: [[…], […]] }
bipartition(cycleGraph(5)); // { bipartite: false, oddCycle: [3, 2, 1, 5, 4, 3] }
A graph is bipartite when its vertices split into two sides with every edge crossing between them. König's theorem says this happens exactly when there is no cycle of odd length. One direction is easy: going round a cycle switches sides at every step, so a cycle must have even length. The search proves the other: colour by breadth-first search, alternating with depth; if an edge ever joins two vertices of the same colour, the two search paths back to where they meet, together with that edge, form an odd cycle, which is returned as the certificate.
Distances
const P = petersen();
distance(P, "1,2", "3,4"); // 1: disjoint pairs are joined
diameter(P); // 2
girth(P); // { girth: 5, cycle: [ … ] }: no triangles, no squares
center(pathGraph(5)); // [3]
The distance between two vertices is the fewest edges on a path between them (weights are for shortest paths). The eccentricity of a vertex is its distance to the farthest vertex; the diameter and radius are the largest and smallest eccentricities, and the centre is where the radius is attained. The girth is the length of a shortest cycle, found by a breadth-first search from every vertex; a shortest cycle through the start appears as the first non-tree edge that closes two branches meeting only at the start.
@ratmath/discrete and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.