Traversing the Hypercube
Ossarion-429 is a multi-dimensional being travelling across hypercubes of various dimensions. Bored and having nothing else to do, he wonders to himself if it’s possible to travel along the edges of a hypercube, visiting each vertex exactly once before returning to the start. Such a path is called a Hamiltonian cycle.
Excited with the challenge, he takes a look at a 2D hypercube, an object that you would call a “square”. Starting at a vertex, he easily finds that he can traverse the following path, visiting each vertex exactly once.
Up next is a 3D hypercube, or a “cube”. With more options at each vertex to navigate, it’s harder to find a route that traverses each vertex only once. Eventually, though, Ossarion-429 finds such a path. Can you find it too?
So far so good; we’ve managed to find a Hamiltonian cycle for 2D and 3D hypercubes. But how about a 4-dimensional hypercube? Or 5? As the number of vertices increases exponentially with the number of dimensions, would there ever be a hypercube that doesn’t have a Hamiltonian cycle? If yes, what’s the smallest n such that an n-dimensional hypercube has no such path? If not, prove it.
Solution
All n-dimensional hypercubes have a Hamiltonian cycle, so long as n≥2, which I’ll explain why at the end.
Let’s prove this by considering the Hamiltonian cycle on a square.
To construct a cube, we can place 2 squares side by side and connect their corresponding vertices.
Let’s draw a Hamiltonian cycle for each square, leaving out the last edge that brings us back to the start. We’ll also reverse the path on one of the squares.
Note how we can connect the two paths together like this, forming a Hamiltonian cycle for a cube.
We can perform a similar process to construct a Hamiltonian cycle for a 4D hypercube.
This works for higher-dimensional hypercubes, proving that there will always be a Hamiltonian cycle.
Extra Credit
There’s also another way to solve this. Let’s consider a cube with unit length, placing one corner at the origin of a 3D cartesian space. Its vertices will be at the coordinates (0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), and (1, 1, 1). We then convert each vertex directly into binary by concatenating its coordinates. For instance, the vertex at (1, 0, 1) will be 101. If you know what Gray codes are, you’ll notice that the Gray code sequence of length 3 will form our Hamiltonian cycle.
000, 001, 011, 010, 110, 111, 101, 100.
This works because of 3 facts:
- Every number in the Gray code sequence is unique, ensuring that we will never visit the same vertex twice.
- 2 vertices are connected by an edge if and only if their coordinates differ by 1 value. For instance, (0, 0, 0) is adjacent to (1, 0, 0), (0, 1, 0), and (0, 0, 1). Since each consecutive number in the Gray code differs by at most one value, the sequence forms a valid path on the cube.
- The last number in the Gray code sequence is only 1 bit off from the first number, allowing us to form a closed cycle.
Let’s take a look at our earlier examples with the binary labels.
But why is this not true for n=1? Well, that’s because a 1D hypercube is just a line.
The path that traverses each vertex once will start at one vertex, go to the other, then back, crossing the same edge twice. In graph theory, cycles cannot repeat a vertex or an edge, so we cannot consider this a Hamiltonian cycle based on this technicality.
Try These Next
Jewel Thieves and a Stolen Gem
The Moonstone has been stolen. Police have three suspects under surveillance—three of the city’s most notorious jewel thieves—but no evidence to arrest any of...
Stack of Coins 1B
You and your friend stumble on another pirate’s treasure: 99 copper coins and 1 gold coin. As before, these old coins are no longer legal tender, so the copper...
Vanishing Square
The diagrams below show two arrangements of the same four pieces. Both form a 13 by 5 right triangle. Yet the first arrangement contains a 1-square-unit gap,...