2. Hamiltonian Cycles
Cycle that visits every node exactly once. NP-Complete ☠️.
Tip: some problems that seems to need a Hamiltonian solution can be reframed so that an Eulerian solution works instead.
Prerequisite: Inclusion Exclusion Principle
Example:
Example:
From left to right: union; removing all double overlaps; add triple overlap back once
Held–Karp Algorithm
DP algorithm that cuts down checking for the existence of a Hamiltonian cycle from to .
We fix a as starting vertex (a cycle must contain all nodes).
State: a Hamiltonian path exists for the subset , typically represented as bitmask, ending on
Base case:
Recurrence: for some ()
Extraction: for some with
Runtime:
- for iterating over the dp table
- for iterating over all possible endings, where checking is because we use an adjacency matrix Total: Space complexity:
Inclusion-Exclusion Hamiltonian Cycle Algorithm
walks of length in with start- and end nodes that don’t visit any nodes in
is easy to compute: We first raise the adjacency matrix to the -th power to get the number of -length walks between any two nodes (See [[11. Matrices and Graphs#11. Matrices and Graphs#Number of Walks|A&D/11. Matrices and Graphs]]). We then read off the entry .
However, they aren’t all
- subtract those that are
Special Graph Types
Grid Graphs
Hamiltonian cycle # nodes even
If uneven: We can color in the graph with a chess board grid. A Hamiltonian cycle would have to alternatively visit light and dark shaded nodes. To form a cycle, it would need to visit equally many dark and light shaded nodes. However, if it is uneven, we have an uneven number of dark shaded and light shaded nodes.
If even: We orient the even edge vertically. We can then go from left to right multiple times while always returning to the starting edge:
n-Dimensional Hypercubes
Always have Hamiltonian cycles.
Dirac’s Theorem
Theorem
If is a graph with vertices in which every vertex has at least neighbors, then is Hamiltonian.