
100 prisoners problem - bladecatcher
https://en.wikipedia.org/wiki/100_prisoners_problem
======
micaeked
Since wikipedia doesn't explain why it works (or maybe I just didn't
understand it), I found this explanation much better:
[http://datagenetics.com/blog/december42014/index.html](http://datagenetics.com/blog/december42014/index.html)

~~~
cbau
This is a great explanation.

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.

~~~
eggpy
Well, first of all it's a puzzle so it's useful as something fun to think
about. Math is kinda like science though, you never know exactly how or if a
particular discovery will be useful. In this case maybe you could find some
graph traversal scenario where this would be applicable. In the Wikipedia
article under variants they mention

> 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.

------
billysielu
Right, so picking a box at random and following the chain would be bad because
you could get back to where you started having not found your number. Starting
with your own numbered box is your defense against this because to go back to
where you started you would have to have found your number.

~~~
daxfohl
This was my aha moment.

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.

~~~
sigotirandolas
Because of the way that the problem is set up (each of the 100 numbers is in
one of the 100 lockers), it can _always_ be written as a list of disjoint
cycles (mathematically: every permutation can be written as a product of
disjoint cycles).

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.

------
dbatten
I'm confused...

"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?

~~~
dbatten
Two things:

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](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.

~~~
justadudeama
Can you explain to me why it is guaranteed you will be in a loop with your
number?

~~~
SilasX
To try a simpler explanation:

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.

~~~
daxfohl
* Thus you will never be in some other loop. Loops are disjoint. Disjoint circles, not train tracks that interconnect. Just loops. Think about that and realize it.

* This strategy guarantees you'll be in _your_ loop. ("Unfortunately" you'll be at the _end_ of that loop: the box that points to your number. But still, in the loop, which is the point).

* Thus if there are multiple loops, all of length 50 or less, you'll get your number before your number is up.

* And math shows that if there are 100 nodes in a randomly unidirected graph, 30% of them contain no loops greater than length 50.

So if everyone follows this strategy, 30% chance they'll win.

------
edanm
My favorite version of this is the one that shows one prisoner to make a
single swap of two drawers' contents in order to improve their chances.
Surprisingly, this allows for a 100% success rate.

~~~
gowld
Doe the "hacker" get to see all the contents? Or only 50 (half?)

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

~~~
pythonaut_16
That's an interesting follow up question. What is the minimum number of boxes
the hacker/spy/secret agent needs to see to guarantee success?

------
martininmelb
What does the prisoner do if she gets to, for example, box 40 and there is a
40 in the box?

[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.

~~~
jakear
Why would they have gone to box 40 in the first place had they not gotten a
number 40 from elsewhere?

------
contoraria
I still doubt the Monty Hall problem (mentioned in the wiki-article). I
understand the reasoning, summing the probability tree, but there are two ways
to build the probability tree, with the step of reveal or without. Since the
player doesn't know that step, why would it be part of the tree? Because we
know the setup a priori, in a way.

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.

~~~
Myrmornis
You make your initial guess. What's the probability you're right on your
initial guess? 1/3\. What's the probability you're wrong? 2/3.

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.

------
mkstowegnv
At some intuitive level this problem reminded me of other very different
puzzles like [1] and prompted me to wonder if as a general rule the best
tactic when one is faced by a riddle that appears impossible, is to try to
think of solutions that involve self reference. It also makes me wonder if the
brain at a basal unconscious level uses something analogous to the linked list
like solution described here to quickly explore permutation space.

1
[https://en.m.wikipedia.org/wiki/Knights_and_Knaves](https://en.m.wikipedia.org/wiki/Knights_and_Knaves)

------
niftich
If I understand this correctly, this basically uses the drawer's perceived
number as an out-of-band signalling mechanism for the algorithm they agreed to
follow. Each actor must adhere to the same scheme to address drawers. This
means that the drawers must either be uniquely named ("labelled" ahead of
time), or have some sort of natural ordering that's algorithmically
deterministic (e.g. lexicographic, array index, 2D-position using a previously
agreed algorithm like right-then-down).

What are some practical uses for this? Distributed hash table traversal using
one's own address [1]?

[1]
[http://www.bittorrent.org/beps/bep_0005.html](http://www.bittorrent.org/beps/bep_0005.html)

~~~
IneffablePigeon
This "look at it as a permutation problem and follow the disjoint cycles" idea
is loosely what rainbow tables are based on (although I think in that case
because of the possibility of hash collisions they aren't guaranteed to be
disjoint cycles, which requires extra work to solve).

------
Myrmornis
Many mathematical-minded people look at this problem and conclude that the
maximum probability of freedom is close to (1/2)^50.

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?

------
CydeWeys
I wrote up a Python framework to play around with a different 100 prisoners
problem here:
[https://github.com/CydeWeys/hundred_prisoners/tree/master/pr...](https://github.com/CydeWeys/hundred_prisoners/tree/master/prisoners)

People might find it interesting to play around with implementing the solution
in code.

------
engnr567
One key thing about the result is that it doesn't mean that the probability of
survival is 30%. The prison director can choose an arrangement such that the
strategy is guaranteed to fail. If the prison director is intelligent and
after your life, you are infinitely more likely to survive by opening boxes
randomly.

~~~
zwkrt
The prisoners in the game can collude before the game begins, so they can
create their own ordering on the fly. This cannot be overcome by the warden.

------
pramodzion
HN post from 2010 :
[https://news.ycombinator.com/item?id=1891212](https://news.ycombinator.com/item?id=1891212)

------
travismay1
Well, this led me on a bit of a rabbit trail. I made a python script that
illustrates the solution to this problem in action.
[https://gist.github.com/travismay1/8d80fe345786b1d0e93fee812...](https://gist.github.com/travismay1/8d80fe345786b1d0e93fee8127df53b5)

------
twentythree
The third variant of the problem (where one prisoner is allowed to make one
change, which can guarantee that every prisoner survives) is the one I heard
first; I like that problem because it seems even more counter-intuitive, but
also makes it easier to understand why it works.

------
jwilk
Let's make the game harder by not letting the prisoners see what's inside the
opened drawers.

Is it hopeless now?

~~~
majewsky
That's just a game of chance. The point of a skill game, even one that
involves randomness, is that information you get can influence your actions.
If you cannot look inside the drawers, you do not gain any information about
the particular setup and thus cannot execute anything that would resemble a
strategy.

~~~
dmurray
They can still do slightly better than chance by ensuring their guesses have
the right amount of overlaps. For example, if there were only two prisoners,
the right strategy would be for one to pick box 1 and the other one to pick
box 2, improving their odds of survival from 25% to 50%.

I suspect the chances are still negligible for 100 prisoners though.

~~~
peeters
> 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.

~~~
dmurray
> If prisoners choose randomly though, the expected value of Nb is still 50
> though.

The expected value of Nb would still be 50 if all the prisoners checked the
same subset of boxes, but the chance of the prisoners surviving would clearly
be 0.

I think it's correct that if the prisoners don't get the see the contents of
the boxes, then any arrangement where each box is opened by 50 prisoners is
equally good and optimal - but I can't quite see a proof.

~~~
peeters
> The expected value of Nb would still be 50 if all the prisoners checked the
> same subset of boxes, but the chance of the prisoners surviving would
> clearly be 0.

Yes I should have expanded. The probability of escape is

∏ (i = 1..N) Ni / N

So if some Ni is 0, the whole thing is 0. My point was that if you're choosing
randomly, then as N goes to infinity, "Ni / N" already converges to 0.5 . So
your strategy is ensuring optimality, but you'd get there if you took it to
the limit anyway.

As a quick test I did 1000 trials. The minimum overlap in those 1000 trials
was 26. 95% of the time it was above 35. For N = 1000, 95% of the time it was
above 440.

------
keketi
Do the prisoners remove their number from the drawer if they find it? It's not
clear from the problem statement.

~~~
moate
If they did, you essentially go back to random choice because of all the
broken chains created. "Find" is meant to imply literally "discovering the
location of data that correlates to their prisoner number".

Real world context implied that prisoners would also take the tickets, but
that is not specifically stated.

------
utopcell
Say you have 10 players in a row. You number each player by its position in
the row: 1, 2, 3, .., 10. You then write the players' names on 10 pieces of
paper, and you randomly place the papers in 10 boxes. You place exactly one
paper in each of the boxes. The boxes are also numbered from 1 to 10.

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

