Monday, January 1, 2007

23 Prisoner problem

Problem:

The warden meets with 23 new prisoners when they arrive. He tells them, "You may meet today and plan a strategy. But after today, you will be in isolated cells and will have no communication with one another.
"In the prison is a switch room, which contains two light switches labeled A and B, each of which can be in either the 'on' or the 'off' position. I am not telling you their present positions. The switches are not connected to anything.
"After today, from time to time whenever I feel so inclined, I will select one prisoner at random and escort him to the switch room. This prisoner will select one of the two switches and reverse its position. He must move one, but only one of the switches. He can't move both but he can't move none either. Then he'll be led back to his cell.
"No one else will enter the switch room until I lead the next prisoner there, and he'll be instructed to do the same thing. I'm going to choose prisoners at random. I may choose the same guy three times in a row, or I may jump around and come back.
"But, given enough time, everyone will eventually visit the switch room as many times as everyone else. At any time anyone of you may declare to me, 'We have all visited the switch room.'
"If it is true, then you will all be set free. If it is false, and somebody has not yet visited the switch room, you will be fed to the alligators."

Solution 1:
In the strategy session, every prisoner is given a number between 1 and 23. Also, the number of days in a month is assumed to be 23. The rule is to set the switches to EVEN state if the day (nth day) and the prisoner number matches. Otherwise, set it to ODD. EVEN states: 00, 10; ODD states: 01, 11. Whenever a prisoner visits the switch room, he has two information to be noticed.
1) The previous day.
2) State of the switch when he enters.

If they are in agreement, then it means that the nth prisoner entered the room the previous day. The prisoner keeps a note of it. Likewise, every other prisoner keeps a note of the previous events.

If the prisoner, upon entering the room, finds that all prisoners have entered before, he can declare it to the warden and all will be freed.

Drawback with Soln 1:
This method is definitely flawed if the so- called "random number" happens to be sequential. If the prisoner 2 follows prisoner 1 and so on, no prisoner will be in a position to track all the other prisoners but the previous prisoner.

Solution 2:
This solution seems logical and is more elegent compared to the previous one.
The strategy here is to designate one of the prisoners as a scorekeeper. It is better to have the first prisoner entering the room as a SCOREKEEPER.

The scorekeeper, on entering the switch room for the first time, ensures that the switch B is OFF. If it is already in the OFF state, he toggles the switch A. The score is initialised to 1.

On subsequent entries, the scorekeeper toggles switch B. If switch B's position is changed from ON to OFF, the score is incremented by 1.

All the other prisoners see the state of switch B. If switch B is in the OFF state, then it can be turned to ON. Otherwise, switch A is toggled, just to pass the time. If a prisoner changes the state of the switch from OFF to ON once, he cannot do that again and simply toggles switch A on subsequent times.

As scorekeeper is the only person who changes the state of the switch to OFF state, he can count the number of people who have entered the switch room before. If the count reaches 23, it means all the prisoners have visited the switch room and can thus be declared to the warden.