Gray code
Problem Setup
Let’s consider the following scenario:
In front of you are
switches. Each switch can either be in the ON or OFF state, and they all start in the OFF position. These switches are the key to a safe, which can only be unlocked when the switches are set in a specific combination.
However, you’ve forgotten the correct combination.
Your goal is to try different switch combinations to unlock the safe, but you want to find the most efficient way to do so.
The simplest solution is to perform a brute force search, testing all
For example, when
Code | Binary | Switch State |
---|---|---|
OFF-OFF-OFF | ||
OFF-OFF-ON | ||
OFF-ON-OFF | ||
OFF-ON-ON | ||
ON-OFF-OFF | ||
ON-OFF-ON | ||
ON-ON-OFF | ||
ON-ON-ON |
This method works, but the number of switch toggles required can be quite high.
For instance, transitioning from
In total, the number of switch presses is 11.
Since that seems a bit tiring, we now ask, “Is there a way to reduce the number of toggle actions?”
Case for
If we fix the first
While it’s difficult to check all possible sequences for minimizing the number of switch toggles, for
(1) 0,1,2,3
Code | Binary | Switch State | Number of Switch Changes from Previous Step |
---|---|---|---|
OFF-OFF | - | ||
OFF-ON | |||
ON-OFF | |||
ON-ON |
The total number of switch changes is 4.
(2) 0,1,3,2
Code | Binary | Switch State | Number of Switch Changes from Previous Step |
---|---|---|---|
OFF-OFF | - | ||
OFF-ON | |||
ON-ON | |||
ON-OFF |
The total number of switch changes is 3.
(3) 0,2,1,3
Code | Binary | Switch State | Number of Switch Changes from Previous Step |
---|---|---|---|
OFF-OFF | - | ||
ON-OFF | |||
OFF-ON | |||
ON-ON |
The total number of switch changes is 4.
(4) 0,2,3,1
Code | Binary | Switch State | Number of Switch Changes from Previous Step |
---|---|---|---|
OFF-OFF | - | ||
ON-OFF | |||
ON-ON | |||
OFF-ON |
The total number of switch changes is 3.
(5) 0,3,1,2
Code | Binary | Switch State | Number of Switch Changes from Previous Step |
---|---|---|---|
OFF-OFF | - | ||
ON-ON | |||
OFF-ON | |||
ON-OFF |
The total number of switch changes is 5.
(6) 0,3,2,1
Code | Binary | Switch State | Number of Switch Changes from Previous Step |
---|---|---|---|
OFF-OFF | - | ||
ON-ON | |||
ON-OFF | |||
OFF-ON |
The total number of switch changes is 5.
Conclusion
For
Case for
Next, let’s consider the case when
So, we aim from the start to minimize the number of switch changes to 7.
By considering each number from
To find the optimal switch sequence, we need to trace a path through this graph that visits each vertex exactly once, which will give us the desired sequence.
Fundamentally distinct sequences of bit changes correspond to the three paths shown in the right-side figure.
In general, such paths are called Hamiltonian paths.
Taking into account symmetry, we find that even when restricted to paths starting at
General Case for
Using the method described above for
For
One possible approach to constructing such a sequence is to use the Hamiltonian path for the
After some thought, we can devise the following algorithm:
First, apply the known sequence to the lower
bits while keeping the most significant bit at . Once we reach the end of the sequence, flip only the most significant bit to
. Next, apply the reverse of the
-bit sequence to the lower bits.
For example, starting with the sequence for
For
This algorithm allows us to generate an infinite sequence of numbers.
This sequence is commonly referred to as the Gray code.
The Number of Gray Codes
While “Gray code” often refers specifically to the sequence generated by the algorithm described above, the term can also refer more generally to any sequence in which adjacent terms differ by exactly one bit.
As we have seen, this is equivalent to finding a Hamiltonian path on the
We’ve already determined that for
- For
, there are 5,712 Gray codes. - For
, there are 5,859,364,320.
The number grows rapidly.
This sequence is listed in OEIS A003043.