Euler and Hamilton

Euler and Hamilton

Two questions that sound alike: can you walk every edge exactly once, or visit every vertex exactly once? The first has a simple test and a fast answer. The second has neither, and is answered here by exhaustive search on small graphs.

Every edge once: Euler

const k = graph("A-B A-B A-C A-C A-D B-D C-D", { multi: true });   // the bridges of Königsberg
eulerTour(k);
// { exists: false, reason: "a tour needs 0 or 2 vertices of odd degree; this graph has 4", odd: ["A", "B", "C", "D"] }

eulerTour(complete(5));     // every degree is 4: a closed tour
// { exists: true, closed: true, vertices: [1, 2, 3, 1, 4, 2, 5, 3, 4, 5, 1], edges: [ … ] }
eulerTour(graph("1-2 2-3 3-1 3-4"));   // two odd vertices: a tour from one to the other

Euler's theorem (1736): a connected graph has a closed walk using every edge exactly once if and only if every vertex has even degree, and an open one if and only if exactly two vertices have odd degree, where it must start and end. Necessity is the counting argument: every time the walk passes through a vertex it uses two edges there. In Königsberg all four land masses had odd degree, so no walk could cross each of the seven bridges once.

Sufficiency is constructive, and the construction is Hierholzer's method: walk from the start along unused edges until you are stuck, which with even degrees can only happen back at the start. If unused edges remain, some vertex on the circuit has one; walk a second circuit from there and splice it in. Done with a stack, each edge is handled once, so the whole tour takes time proportional to ∣E∣|E|. In a directed graph the condition is that every vertex has as many edges in as out (or one vertex has one extra out and another one extra in).

Every vertex once: Hamilton

hamiltonCycle(platonic("dodecahedron"));   // Hamilton's "icosian game" (1857): { exists: true, cycle: [ … ] }
hamiltonCycle(petersen());
// { exists: false, reason: "no cycle through every vertex: an exhaustive search over all 1024 vertex sets found none" }
hamiltonCycle(star(4));     // { exists: false, reason: "1 has fewer than two neighbours", vertex: 1 }
hamiltonPath(grid(3, 3));   // a path exists, though no cycle does

No test as simple as Euler's is known for Hamilton cycles, and deciding whether one exists is NP-complete. So the answer is found by search, limited to 20 vertices. The search is the Held–Karp dynamic programme: for each set SS of vertices containing a fixed start, record which vertices can end a path from the start that visits exactly SS. Each set is extended by one vertex at a time, so the work is about 2n2^n sets times nn, not n!n! orders. When no Hamilton cycle exists, that is proved by the search having covered every set.

Before searching, some quick reasons are tried, since they are easier to read than "the search found none": a disconnected graph, a vertex with fewer than two neighbours, or a cut vertex (a cycle through every vertex has no cut vertex). Some graphs need the full search: the Petersen graph has none of these defects and still has no Hamilton cycle.

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