I’m trying to get a simple method (a script or a c++ snippet, perhaps) that generates a hypercube links data, i.e., given an n-dimensional hypercube with vertices numbered as 1, …, 2n, it produces the output:
1 3
1 5
2 3
...
Where each row represents a connection between two vertices. (related question)
But in a somehow different context. I hope someone has already done this. The input should be the hypercube dimensionality. To remind you, links exist
between two nodes if and only if their node i.d.’s differ in exactly one bit position.
My intention was to use XOR operator, and when the result can be expressed as 2k for some k, then the bit representations differ in a single position, and I write a link. However, I’m not sure how to get this implemented (C++ or script).
Here’s a short, self-contained C++ version that prints the connected vertices for an n-dimensional hypercube:
Example output when n is 3 (assuming you’re okay with vertices that start at 0, not 1):