Josephus problem

From Wikipedia, the free encyclopedia

The Josephus problem (or Josephus permutation) is a theoretical problem occurring in computer science and mathematics.

There are n people standing in a circle waiting to be executed. After the first man is executed, k − 1 people are skipped and the k-th man is executed. Then again, k − 1 people are skipped and the k-th man is executed. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed people are removed), until only the last man remains, who is given freedom.

The task is to choose the place in the initial circle so that you survive (are the last one remaining), given n and k.

Contents

[edit] History

The problem is named after Flavius Josephus, a Jewish historian living in the 1st century. As the legend goes, he and his 40 comrade soldiers were trapped in a cave, surrounded by Romans. They chose suicide over capture and decided that they will form a circle and start killing themselves using a step of three. As Josephus did not want to die, he was able to find the safe place, and stayed alive with his comrade, later joining the Romans who captured them.

[edit] Solution

We explicitly solve the problem when every 2nd person will be killed, i.e. k = 2. (For the more general case k\neq 2, we outline a solution below.) We express the solution recursively. Let f(n) denote the position of the survivor when there are initially n people (and k = 2). The first time around the circle, all of the even-numbered people die. The second time around the circle, the new 2nd person dies, then the new 4th person, etc; it's as though there were no first time around the circle. If the initial number of people was even, then the person in position x during the second time around the circle was originally in position 2x − 1 (for every choice of x). So the person in position f(n) was originally in position 2f(n) − 1. This gives us the recurrence:

f(2n)=2f(n)-1.\,

If the initial number of people was odd, then we think of person 1 as dying at the end of the first time around the circle. Again, during the second time around the circle, the new 2nd person dies, then the new 4th person, etc. In this case, the person in position x was originally in position 2x + 1. This gives us the recurrence:

f(2n+1)=2f(n)+1.\,

When we tabulate the values of n and f(n) we see a pattern:

n 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
f(n) 1 1 3 1 3 5 7 1 3 5 7 9 11 13 15 1

This suggests that f(n) is an increasing odd sequence that restarts with f(n) = 1 whenever the index n is a power of 2. Therefore, if we choose m and l so that n = 2m + l and 0\leq l<2^m, then f(n)=2 \cdot l+1. It is clear that values in the table satisfy this equation. But mathematics demands exact proof. Below, we give a proof by induction.

Theorem: If n = 2m + l and 0\leq l<2^m, then f(n) = 2l + 1.

Proof: We use strong induction on n. The base case n = 1 is true. We consider separately the cases when n is even and when n is odd.

If n is even, then choose l1 and m1 such that n/2 = 2^{m_1}+l_1 and 0\leq l_1 < 2^{m_1}. Note that l1 = l / 2. We have f(n) = 2f(n / 2) − 1 = 2((2l1) + 1) − 1 = 2l + 1, where the second equality follows from the induction hypothesis.

If n is odd, then choose l1 and m1 such that (n-1)/2 = 2^{m_1}+l_1 and 0\leq l_1 < 2^{m_1}. Note that l1 = (l − 1) / 2. We have f(n) = 2f((n − 1) / 2) + 1 = 2((2l1) + 1) + 1 = 2l + 1, where the second equality follows from the induction hypothesis. This completes the proof.


If we represent n in binary as n=b0b1b2b3\dots bm, then the solution is given by f(n)=b1b2b3\dots bmb0. The proof of this follows from the representation of n as 2m + l.

The easiest way to solve this problem in the general case is to use dynamic programming. This approach gives us the recurrence:

f(n,k) = (f(n − 1,k) + k)mod n, with f(1,k) = 0

which is evident when considering how the survivor number changes when switching from n − 1 to n. This approach has running time O(n), but for small k and large n there is another approach. The second approach also uses dynamic programming but has running time O(klogn). It is based on considering killing k-th, 2k-th, ..., \lfloor n/k \rfloor-th people as one step, then changing the numbering.

[edit] Variants

According to Concrete Mathematics, section 1.3, Josephus was accompanied by an unindicted co-conspirator; the problem was to find the places of the two last remaining survivors (whose conspiracy would ensure their survival).

[edit] References

[edit] External links

[edit] External links

In other languages