On Monday, we considered some basic puzzles involving finite number of hats. We continue these investigations here with a slightly more involved puzzle. This puzzle is due to Todd Ebert and appeared in his 1998 PhD thesis. It attracted much attention, largely due to its connections with coding theory, and even inspired a New York Times article.

## Puzzle 3: Hats and Coding

### The Puzzle

There’s a finite group of prisoners. The prisoners are led into a room, where each is randomly (and independently) adorned with either a red or a blue hat (each has a 50% probability). Each prisoner can see the hat colors of all of the other prisoners, but no prisoner can see the color of their own hat. Each prisoner has a device with three buttons: ‘Red,’ ‘Blue,’ and ‘Pass.’ At a fixed time, the prisoners simultaneously press a button, with each prisoner either trying to guess the color of their hat or passing. If at least one prisoner guesses a color and *everyone *who guesses a color guesses correctly, then the prisoners are released. If everyone passes or at least one person guesses incorrectly, then the prisoners are executed. The prisoners are not allowed to communicate once they are given their hats; their guesses or passes must be based solely on the colors of the hats they can see. However, the prisoners can confer beforehand and devise a strategy. How can the prisoners maximize their probability of being released?

### Solution for 3 prisoners

A naive strategy would be to choose one prisoner to be the ‘guesser.’ That prisoner will always guess ‘red,’ and everybody else will pass. This strategy will have a success rate of . It turns out, though, that we can do better. This might initially be surprising to you. Since anybody guessing the color of their own hat has no useful information about the color of their hat, the success rate of *any *individual guess will be . However, if we cleverly choose the circumstances in which a prisoner will guess and the color they will guess, we can concentrate the wrong guesses in a small number of ‘bad’ configurations of hats while spreading out the right guesses to a larger number of ‘good’ configurations.

To see how this might work, let’s suppose there are three prisoners, and let’s suppose we use the following strategy, which we call Strategy A: if a prisoner sees that the other two prisoners have the same color hat, they will guess the other color. If a prisoner sees that the other two prisoners have different colors, then they will pass.

How will Strategy A fare? If all three of the prisoners have the same color hat, then all three prisoners will guess the other color, and all three will be wrong. However, if two prisoners are wearing one color and one prisoner is wearing the other, then only the prisoner wearing the color appearing just once will guess, and they will be right! This strategy fails, therefore, only if all of the prisoners have the same color. What are the chances of this happening? For each color, the chances of all hats being that color are , so the chances of it happening for either color is . Strategy A therefore has a success rate of .

This is certainly better than the success rate of the naive strategy. Is it the best we can do? To make further analysis less cumbersome, let’s replace ‘red’ and ‘blue’ with ‘0’ and ‘1,’ i.e., suppose that prisoners with red hats are labeled ‘0’ and prisoners with blue hats are labeled ‘1,’ and that the prisoners guess ‘0’ or ‘1’ rather than ‘red’ or ‘blue.’ Let’s also suppose that, as part of their strategizing, the prisoners number themselves, from 1 to 3 (or 1 to if there are prisoners). A configuration of hats can therefore be represented as a sequence of length 3 (or, generally, ) of 0s and 1s. Note that, for 3 prisoners, there are 8 possible configurations of hats (in general, the number is ):

000 001 010 011 100 101 110 111

When Strategy A is deployed, 12 total guesses are made throughout all configurations: 6 incorrect guesses are made in the configurations 000 and 111, and 6 correct guesses are made in the other configurations. As mentioned, because the prisoners have no real information about their own hat colors, this equality of correct and incorrect guesses is necessary. Suppose, then, that we had a strategy better than Strategy A. Since Strategy A succeeds in 6 of the 8 configurations, this new strategy would have to succeed in at least 7. It would therefore have to have at least 7 correct guesses across all of the configurations, which means it would also have to have at least 7 incorrect guesses. However, these 7 incorrect guesses would have to be restricted to the single configuration on which the strategy fails. Each configuration only has room for 3 guesses, so this is impossible. Therefore, Strategy A is optimal.

### Generalizing the solution

With an eye to generalizing Strategy A to work with more prisoners, let’s look at it a little more deeply. Given two configurations of hats, let us say that the two configurations are *close *if they are same configuration or if one can be turned into the other by changing just one hat. So, using our ‘0’ and ‘1’ notation, 110 and 100 are close, whereas 101 and 010 are not close. Strategy A identifies two special configurations (000 and 111), which we will call *bad configurations*, which satisfy the following conditions:

- Every configuration is close to one of the bad configurations.
- No configuration is close to two different bad configurations.

Once these bad configurations have been identified, Strategy A can be formulated as follows. Each prisoner looks at the other hats and figures out whether what they see is consistent with one of the bad configurations. If it is, then they guess that their hat is the color that is *different *from the color of their hat in this bad configuration. So, for example, if Prisoner 1 sees two blue hats, they know that this is consistent with one of the bad configurations, namely 111, so Prisoner 1 guesses 0, or ‘red.’ If the prisoner sees something that is inconsistent with all of the bad configurations, they pass. So, if Prisoner 1 sees that 2 is wearing blue and 3 is wearing red, then they know the configuration could not possibly be any of the bad configurations, so they pass.

Suppose now that the number of prisoners is an arbitrary natural number, . There are now possible configurations of hats, each represented by a string of 0s and 1s of length . Let denote the set of all strings of 0s and 1s of length . In analogy with our analysis of Strategy A, let us make the following definition.

**Definition: **Suppose (so is a set of strings of length of 0s and 1s). We say is an *n-**Hamming set *if it satisfies the following two conditions:

- Every element of is close to a member of .
- There is no element of that is close to more than one member of .

Suppose for a moment that is an -Hamming set. What can we say about ? For each , let be the set of such that is close to . For each , there are elements that are close to it ( is close to itself, and there are ways to change one position of ). Therefore, each set has size . Moreover, condition 1 in the definition of Hamming set implies that the union of all of the s is all of , which has size , and condition 2 in the definition of Hamming set implies that, if and are different members of , then . Putting this all together, we obtain:

, or

Since is an integer, it must be the case that evenly divides , so, in order for there to be an -Hamming set, must be a power of 2. Thus, we may assume there is a natural number such that , in which case .

It turns out that, if is a power of 2, then there is always an -Hamming set. We will not prove this fact; it can be done with a modest amount of linear algebra.

**Theorem: **Suppose . Then there is a -Hamming set.

This allows us to demonstrate an optimal strategy for the puzzle in cases in which the number of prisoners is one less than a power of 2. To see this, suppose is a natural number and . Let be an -Hamming set. Now suppose that the prisoners have been given their hats and, based on what a prisoner sees, the configuration could be one of the elements of . Then the prisoner will guess the color they are *not *wearing in that element of . In all other cases, the prisoner will pass. For example, if , 0110011 is in , and Prisoner 1 sees ?110011, then they will guess ‘1.’ If the actual configuration is 1110011, all other prisoners will pass, and the strategy will succeed. If the actual configuration is 0110011, then all of the prisoners will guess (and all will be incorrect). Condition 1 of the definition of Hamming set implies that at least one prisoner will guess in each configuration. Condition 2 implies that the strategy is well-defined. Therefore, the strategy will only fail if the configuration appears in . Since , the success rate of the strategy is . The more prisoners there are, the more likely they are to succeed. Also, an easy calculation similar to that done for shows that this is the best we can do.

If the number of prisoners is not one less than a power of two, things don’t work out quite so nicely. Note that, if , then a strategy for prisoners can be employed by prisoners as follows: prisoners are chosen to pretend like they are the only prisoners and enact the strategy for prisoners. The remaining prisoners are sent off to twiddle their thumbs (and, of course ‘pass’ when the time comes to guess). This strategy will have the same success rate as the corresponding strategy for prisoners. Thus, if there are prisoners and is the largest natural number such that , then there is a strategy for players with a success rate of . For some values of (such as , where ), this is still optimal. For others, it might not be. The question of the optimal strategy for prisoners when is not necessarily one less than a power of 2 turns out to be a very difficult question, and we won’t say more about it here.

### Coding theory

We end by remarking that Hamming sets can be used to develop error-correcting codes. Suppose you are sending some information, but the channel you are using to send it is a little noisy, and a small but non-negligible amount of error is introduced. You would like to be able to both identify and correct these errors. Here’s one way to do that. Suppose that your message can be sent using an alphabet of 16 symbols. Let be a 7-Hamming set. By the calculations above, , so we can assign each letter of our alphabet to a member of . We can then send the message, using the members of to stand for the corresponding letters. Now, when the message is received, if a 7-bit string is in , then it can be decoded as the corresponding letter. If it is not in , then there is a unique element of that is close to the received string, so the string can be changed to that element of and then decoded. As long as there is no 7-bit string in which two errors occur (and, if the error rate is sufficiently small, this is a reasonable assumption), then the message is transmitted perfectly.

## One thought on “Hats and Hamming Codes”