324 points · by bladecatcher · 8y ago · 121 comments
Is this problem useful for anything? I've seen multiple variations of these prisoner problems now, and I can never remember any being useful in other contexts.
> If the number of team members and the fraction of boxes which are opened is fixed, the winning probability stays strictly larger than zero when more empty boxes are added
Replace boxes with nodes and now you have some math to determine graph traversals. Maybe a bit more math and you can expand it for multiple paths. I don't know though, I'm not a mathematician and I don't normally have to do more than DFS and BFS in coding interviews, but sure I can see some way that this might be useful.
[0] For a sequence like ATGC, the primer is the opposite nucleotide, therefore the 'matching number' would be not ATGC as well, but TACG.
[1] Look, bio is weird, like, jumping genes are actually a thing. This set-up, though strange, is not as unreasonable as a lot of stuff that goes on.
> Because the prisoner starts on the box of their own number they are, by definition, on the chain that contains their ticket (there is only one ticket that points to that box).
> The prison director's assignment of prisoner numbers to drawers can mathematically be described as a permutation of the numbers 1 to 100.
> Every permutation can be decomposed into disjoint cycles, that is, cycles which have no common elements.
> In the initial problem, the 100 prisoners are successful if the longest cycle of the permutation has a length of at most 50. Their survival probability is therefore equal to the probability that a random permutation of the numbers 1 to 100 contains no cycle of length greater than 50.
What did you think was missing from the Wikipedia article?
Oddly enough as stated the problem fails. It essentially assumes an evenly distributed random placement of numbers. However if the warden simply adds the same random number to every single box it ends up as a very long cycle and they all fail.
Worse, they have to communicate which order to count from. Is box 1 the top left or bottom left box?
PS: The problem shows up in many such puzzles because 'the problem' was based on the math rather than the math being chosen to solve the problem.
The director _randomly_ puts one prisoner's number in each closed drawer.
And it's easy to break if you can swap two. Say the warden adds one. If you swap 50 and 100, you have two cycles of 50.
> The director randomly puts one prisoner's number in each closed drawer.
For a nice exposition of this, see Curtin and Warshauer's article "The Locker Puzzle."
---
[0] More specifically, a game where all lockers are left open, so every strategy has the same probability of winning.
I was thinking, the solution presented seemed not only invalid but ill-defined, as you could hit a cycle and still have picks left. With this, it totally makes sense why it works: if there are disjoint cycles, this strategy guarantees that each prisoner is on its own cycle. And I guess the math works out that in unidirected graphs of 100 nodes, 30% of them contain only disjoint cycles of 50 or fewer nodes.
So, by starting in your own locker, you will eventually end up finding your own number. The only constraint is that everyone has to do it in 50 tries or less, i.e. there's no cycle of length >50.
You can assume whatever you wish, but the calculated proof is right there in your statement.
"Before the first prisoner enters the room, the prisoners may discuss strategy—but may not communicate once the first prisoner enters to look in the drawers."
And, in the example given, "That prisoners 5 to 8 will also find their numbers can also be derived from the information gained by the first three prisoners."
There's never any explanation of what happens when a prisoner opens a drawer and finds it empty. In the first example given, what happens when prisoner 5 goes in and there's nothing in drawer 5? The algorithm doesn't seem to account for this.
It seems like there's some sort of assumption that the later prisoners are gathering information from the earlier prisoners, but the problem set-up seems to preclude this? They're going into a room so they can't watch each other, the drawers are closed afterwards so they can't derive information from which drawers are open/closed, and they're not allowed to communicate.
Am I not following something?
1) You don't take your number when you find it. I guess my brain assumed "find" meant "find and take with you to prove you found it." Perhaps a better term might be "encounter."
2) Here's a much better explanation (thanks micaeked): http://datagenetics.com/blog/december42014/index.html. Essentially, the algorithm works because following numbers to drawers will eventually create a loop (e.g., drawer 2 points to 4 points to 6 points to 2). By starting with the drawer with your number, you're guaranteed to be in a loop with your number. The only question is whether your loop is less than 50 drawers long...
Furthermore, this confirms that there IS no transfer of information from previous prisoners in any way.
So then the question is: what proportion of the permutations of 100 numbers contain a cycle greater than 50 vertices long? Is it 30%? The claim made in the Wikipedia article is that the prisoners have around 30% chance of surviving.
Edit: looks like that is the case. You can even take the limit of the number of boxes (hence prisoners) to infinity and their probability of survival never drops below 30%! This is an amazing result.
1) You will be in a loop (by a property of how permutations and hashing work).
2) Using the algorithm in the solution, you always go to the box indicated by the number in the drawer you open.
So if you start with "your" number's drawer, that it is "pointed to" by the drawer with your number in it. So your number is part of that loop.
If you take i_0, then i_1 = s(i_0), then i_2 = s(s(i_0))... and so forth, at some point you will encounter a value you have already seen (because you can only visit n different values at most), and from that point onward you will loop. The trick is that the first value you will see twice MUST be i_0 (your starting point). If not, that is if s(i_k) = i_m where m > 0 (and i_k is the last value before you loop back) then s(i_k) = s(i_{m-1}). This means that i_k = i_{m-1} which is in contradiction with the fact that i_k is the last value before looping (i_k was already visited at step m-1)
Edit to answer TFQ: if your start with your own number as i_0, the proof above shows that at some point you will loop back to it. Then it’s a question of whether you loop back in 50 moves or less.
If at first you try box 3 and it doesn't contain your number 3, then your number 3 is out there and its box points to the one you tried. Thus its box is in your chain and has your number.
Also, when the prisoners find their number they don't remove it. No drawers are ever empty. They just have to open a drawer that contains their number.
From what I can see, no prisoner needs any information beyond knowing their own number. Once they know that, they're just searching the cycle that their number is in to see if it's less than 50-long.
If the hacker needs to break the longest permutation in half (consider the simple permutation x->(x+1), they could ... swap box 0 with box 50, so box 0 points to 51 (->52 .. -> 99 -> 0) and box 50 points to box 1. That makes 2 cycles of size 50, but requires inspecting 50+1 boxes
The prisoners are risking death...
The rules are clear... only 50 boxes per prisoner...
Why would somebody take the risk of opening 100 boxes?
I suppose it's an interesting thought experiment...
It's asked differently: given the same setup, except you have one extra person who's the leader, and he's allowed to go in and look at all the boxes, then make one change.
[edit] I reread the explanation and get it now. Only 1 prisoner (40) will actually get to open box 40. I was trying to think of this from the point of view of implementing it in code. I guess the cycle stops when a prisoner finds their own number or reaches 50 attempts.
I'm assuming the bigger tree is still incomplete. More levels can be inserted, reflecting the reasoning of the a priori knowledge. I assume the supposed advantage would thus prove to be imaginary. Or in simpler terms:
Normally the tree would have the sequence in the following order: box distribution, choose a box, reveal one empty box, switch choice or don't.
However, if you are predetermined to switch, then the order would be changed to switch before the reveal. Thereby, the reveal is irrelevant to the result of switching and the probabilities are equal again.
So now, you're presented with your second decision. Do you stick or switch? Well, if you stick, that's going to lead to success if and only if you were right on your first guess. And if you switch, that's going to lead to success if and only if you were wrong on your first guess.
We all agree that you were more likely to be wrong on your first guess. So you should bet on the fact that you were initially wrong. I.e. switch.
Before the reveal, you don't know which box to switch to. Monty knows which boxes are empty, and he gives you partial information about that by revealing one of them.
What are some practical uses for this? Distributed hash table traversal using one's own address [1]?
Exercise sheets for discrete mathematics courses.
But that's absurdly wrong. They're out by a factor of 10^14 !!! So, are there any important problems in computer science / software engineering contexts that have this form? If so, they may have speed-ups of several orders of magnitude that mathematically and algorithmically-minded people might completely miss?
People might find it interesting to play around with implementing the solution in code.
Is it hopeless now?
I suspect the chances are still negligible for 100 prisoners though.
I think from a probabilistic standpoint, they're equivalent as n increases. Think of it as the inverse; what's the probability each box is checked by its owner?
Pb = Nb / N, or number of people that checked that box divided by number of people total.
If there is perfect, premeditated overlap, then Nb / N is exactly 50/100, so we get the "(1/2) ^ 100" that was already cited in the article.
If prisoners choose randomly though, the expected value of Nb is still 50 though. You've maximized Nb in practice, but not really in theory.
Real world context implied that prisoners would also take the tickets, but that is not specifically stated.
You allow each player to look into half of the boxes. Each player can choose which boxes to look into. The player "wins" if they find a paper with their number written on it. But they can't tell anyone if they won or lost, what they saw in the boxes, or which boxes they picked.
The problem then, is the following: Which boxes should each player look into, such that the chance of all of them "winning" at the same time is as high as possible.
For example, one way to put the 10 numbers in the 10 boxes is:
Box 1: 10
Box 2: 9
Box 3: 8
Box 4: 4
Box 5: 6
Box 6: 5
Box 7: 7
Box 8: 3
Box 9: 2
Box 10: 1
The ordering of the numbers in the boxes is a permutation of the 10 numbers. We know that there are 10! possible ways to place the numbers in the boxes.
Now, consider the following strategy that player X can use to open his 5 boxes. He first opens the box numbered X. If he sees his number in it, then he wins. Otherwise, he saw a different number Y. He then opens the box numbered Y. If he finds his number, he wins. Otherwise, he continues until he has opened the 5 boxes he is allowed to open.
If the player was allowed to open as many boxes as he likes, they he would always win. That is because he would either find his number in a box, or continue opening boxes he hasn't opened before. Why is this true ? Well, let's say it isn't, and that he opens boxes 3 -> 2 -> 5 -> 7 -> 5. But that cannot happen: The first time he opened box 5 was because he saw the number 5 in box 2. And if he had to see box 5 again, this means that box 7 also had the number 5 in it. In fact, the only way player X could go back to a box he has seen, is when he finds his number (X) that will lead him back to the first box he opened.
Now, we know that a player "wins" if he finds his number before opening more than 5 boxes. Or, put it differently, if the loop he follows has length at most 5. In the example above, player 2 would open box 2, which would lead him to box 9, which contains number 2, forming a loop of length 2.
So, when do all players "win" at the same time ? When there does not exist a loop that has length 6 or more. Or more specifically, there is no loop of length 6, or 7, or 8, or 9, or 10. So: P_win = 1 - P_loop(6) - P_loop(7) - P_loop(8) - P_loop(9) - P_loop(10).
We can show [1] that P_loop(k) = 1/k, when k > 5. We need the loop to be larger than 5 because in this case, there can only be one such loop on the permutation.
But then P_win would be 1 - 1/6 - 1/7 - 1/8 - 1/9 - 1/10 or ~35.4%. In general, if we have N players and N boxes then P_win would be: P_win = 1 - Sum[k=n/2+1 .. n]P_loop(k) or P_win = 1 - ( Sum[k=1 .. N]P_loop(k) - Sum[k=1 .. N/2]P_loop(k) ) or P_win ~= 1 - ( ln(N) - ln(N/2) ) > ~30%
(each sum is a harmonic series sum.)
[1] Why is P_loop(k) = 1/k when k > N/2 though ?
Let's say you have numbers 1, 2, .., k. All permutations of them are k!. How many of those k! permutations form a loop ? Well, at the first position, you can place any number other than '1', so you have (k-1) choices. Say you placed number Z at position 1. Then at position Z, you can't place '1' and you can't place 'Z', so you are left with (k-2) choices. In the last position, you have to place number '1' to close the loop, so you have no choice. So, you have (k-1)(k-2)(k-3)...1 ways to form a loop, or (k-1)!.
Therefore, with a little bit of squinting, we can see that the probability that a random permutation has a loop on it of length exactly k is (k-1)!/k!, or 1/k.
We can formalize this argument as follows: There are (N choose k) ways to pick the k places on the permutation where there exists a cycle. There are (k-1)! valid ways to form a cycle on these k places. There are (N-k)! ways to arrange the remaining elements on the permutation. There are N! permutations, Therefore, P_loop(k, N) = (N choose k)(N-k)!(k-1)! / N! = N! / k! / (N-k)! * (N-k)! * (k-1)! / N! = 1/k