Josephus problem

From testwiki
Jump to navigation Jump to search

Template:Short description

Claude Gaspar Bachet de Méziriac's interpretation of the Josephus problem with 41 soldiers and a step size of 3, showing that places 16 and 31 are last to be killed – time progresses inwards along the spiral, green dots denoting live soldiers, grey dead soldiers, and crosses killings

In computer science and mathematics, the Josephus problem (or Josephus permutation) is a theoretical problem related to a certain counting-out game. Such games are used to pick out a person from a group, e.g. eeny, meeny, miny, moe.

Error creating thumbnail:
A drawing for the Josephus problem sequence for 500 people and skipping value of 6. The horizontal axis is the number of the person. The vertical axis (top to bottom) is time (the number of cycle). A live person is drawn as green, a dead one is drawn as black.[1]

In the particular counting-out game that gives rise to the Josephus problem, a number of people are standing in a circle waiting to be executed. Counting begins at a specified point in the circle and proceeds around the circle in a specified direction. After a specified number of people are skipped, the next person is executed. The procedure is repeated with the remaining people, starting with the next person, going in the same direction and skipping the same number of people, until only one person remains, and is freed.

The problem—given the number of people, starting point, direction, and number to be skipped—is to choose the position in the initial circle to avoid execution.

History

The problem is named after Flavius Josephus, a Jewish historian and leader who lived in the 1st century. According to Josephus's firsthand account of the siege of Yodfat, he and his 40 soldiers were trapped in a cave by Roman soldiers. They chose suicide over capture, and settled on a serial method of committing suicide by drawing lots. Josephus states that by luck or possibly by the hand of God, he and another man remained until the end and surrendered to the Romans rather than killing themselves. This is the story given in Book 3, Chapter 8, part 7 of Josephus's The Jewish War (writing of himself in the third person):

Template:Blockquote

The details of the mechanism used in this feat are rather vague. According to James Dowdy and Michael Mays,Template:Sfn in 1612 Claude Gaspard Bachet de Méziriac suggested the specific mechanism of arranging the men in a circle and counting by threes to determine the order of elimination.Template:Sfn This story has been often repeated and the specific details vary considerably from source to source. For instance, Israel Nathan Herstein and Irving Kaplansky (1974) have Josephus and 39 comrades stand in a circle with every seventh man eliminated.Template:Sfn A history of the problem can be found in S. L. Zabell's Letter to the editor of the Fibonacci Quarterly.Template:Sfn

As to intentionality, Josephus asked: “shall we put it down to divine providence or just to luck?”[2] But the surviving Slavonic manuscript of Josephus tells a different story: that he “counted the numbers cunningly and so managed to deceive all the others”.[2][3] Josephus had an accomplice; the problem was then to find the places of the two last remaining survivors (whose conspiracy would ensure their survival). It is alleged that he placed himself and the other man in the 31st and 16th place respectively (for Template:Mvar = 3 below).Template:Sfn

Variants and generalizations

File:Josephus problem 30 9.svg
Variant of the Josephus problem with 30 people and a step size of 9 – time progresses inwards along the spiral, green dots denoting live soldiers, grey dead soldiers, and crosses killings

A medieval version of the Josephus problem involves 15 Turks and 15 Christians aboard a ship in a storm which will sink unless half the passengers are thrown overboard. All 30 stand in a circle and every ninth person is to be tossed into the sea. The Christians need to determine where to stand to ensure that only the Turks are tossed.Template:Sfn In other versions the roles of Turks and Christians are interchanged.

Template:Harvnb describe and study a "standard" variant: Determine where the last survivor stands if there are Template:Mvar people to start and every second person (Template:Mvar = 2 below) is eliminated.

A generalization of this problem is as follows. It is supposed that every Template:Mvarth person will be executed from a group of size Template:Mvar, in which the Template:Mvarth person is the survivor. If there is an addition of Template:Mvar people to the circle, then the survivor is in the Template:Math-th position if this is less than or equal to Template:Math. If Template:Mvar is the smallest value for which Template:Math, then the survivor is in position Template:Math.Template:Sfn

Solution

File:Josephus problem table.svg
Penultimate (pink) and ultimate (ultramarine) places in the Josephus problem for various group size, n and step size, k. In [ the SVG file,] hover over the values to show the full order of killing.

In the following, n denotes the number of people in the initial circle, and k denotes the count for each step, that is, k1 people are skipped and the k-th is executed. The people in the circle are numbered from 1 to n, the starting position being 1 and the counting being inclusive.

k = 2

The problem is explicitly solved when every second person will be killed (every person kills the person on their left or right), i.e. k=2. (For the more general case k2, a solution is outlined below.) The solution is expressed recursively. Let f(n) denote the position of the survivor when there are initially Template:Mvar 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 is as though there were no first time around the circle.

If the initial number of people were even, then the person in position Template:Mvar during the second time around the circle was originally in position 2x1 (for every choice of Template:Mvar). Let n=2j. The person at f(j) who will now survive was originally in position 2f(j)1. This yields the recurrence

f(2j)=2f(j)1.

If the initial number of people were odd, then person 1 can be thought of 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 Template:Mvar was originally in position 2x+1. This yields the recurrence

f(2j+1)=2f(j)+1.

When the values are tabulated of n and f(n) a pattern emerges (Template:OEIS2C, also the leftmost column of blue numbers in the figure above):

Template:Mvar 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 m and Template:Mvar are chosen so that n=2m+l and 0l<2m, then f(n)=2l+1. It is clear that values in the table satisfy this equation. Or it can be thought that after Template:Mvar people are dead there are only 2m people and it goes to the 2l+1st person. This person must be the survivor. So f(n)=2l+1. Below, a proof is given by induction.

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

Proof: The strong induction is used on Template:Mvar. The base case n=1 is true. The cases are considered separately when Template:Mvar is even and when Template:Mvar is odd.

If Template:Mvar is even, then choose l1 and m1 such that n/2=2m1+l1 and 0l1<2m1. Note that l1=l/2. f(n)=2f(n/2)1=2((2l1)+1)1=2l+1 is had where the second equality follows from the induction hypothesis.

If Template:Mvar is odd, then choose l1 and m1 such that (n1)/2=2m1+l1 and 0l1<2m1. Note that l1=(l1)/2. f(n)=2f((n1)/2)+1=2((2l1)+1)+1=2l+1 is had where the second equality follows from the induction hypothesis. This completes the proof.

Template:Mvar can be solved to get an explicit expression for f(n):

f(n)=2(n2log2(n))+1

The most elegant form of the answer involves the binary representation of size Template:Mvar: f(n) can be obtained by a one-bit left cyclic shift of Template:Mvar itself. If Template:Mvar is represented in binary as n=1b1b2b3bm, then the solution is given by f(n)=b1b2b3bm1. The proof of this follows from the representation of Template:Mvar as 2m+l or from the above expression for f(n).

Implementation: If Template:Mvar denotes the number of people, the safe position is given by the function f(n)=2l+1, where n=2m+l and 0l<2m.

Now if the number is represented in binary format, the first bit denotes 2m and remaining bits will denote Template:Mvar. For example, when Template:Tmath, its binary representation is:

n    = 1   0   1   0   0   1
2m   = 1   0   0   0   0   0
l    =     0   1   0   0   1
/**
 * @param n the number of people standing in the circle
 * @return the safe position who will survive the execution 
 *   f(N) = 2L + 1 where N =2^M + L and 0 <= L < 2^M
 */
public int getSafePosition(int n) {
	// find value of L for the equation
	int valueOfL = n - Integer.highestOneBit(n);
	return 2 * valueOfL  + 1;
}

Bitwise

The easiest way to find the safe position is by using bitwise operators. In this approach, shifting the most-significant set bit of Template:Mvar to the least significant bit will return the safe position.[4] Input must be a positive integer.

n    = 1   0   1   0   0   1
n    = 0   1   0   0   1   1
/**
 * @param n (41) the number of people standing in the circle
 * @return the safe position who will survive the execution 
 */
public int getSafePosition(int n) {
    return ~Integer.highestOneBit(n*2) & ((n<<1) | 1);
    //     ---------------------- ---  | ------------
    //     Get the first set bit   |   | Left Shift n and flipping the last bit
    //    and take its complement  |   |
    //                             |   |
    //                Multiply n by 2  |
    //                         Bitwise And to copy bits exists in both operands.
}

k = 3

In 1997, Lorenz Halbeisen and Norbert Hungerbühler discovered a closed-form for the case k=3. They showed that there is a certain constant

α0.8111...

that can be computed to arbitrary precision. Given this constant, choose Template:Mvar to be the greatest integer such that round(α(3/2)m)n (this will be either m=round(log3/2n/α) or m1). Then, the final survivor is

f(n)=3(nround(α(3/2)m))+(2 if is rounded up else 1)

for all n5.

As an example computation, Halbeisen and Hungerbühler give n=41,k=3 (which is actually the original formulation of Josephus' problem). They compute:

mround(log3/241/0.8111)round(9.68)=10
round(α(3/2)m)round(0.8111(3/2)10)=47 and therefore m=9
round(0.8111(3/2)9)round(31.18)=31 (note that this has been rounded down)
f(n)=3(4131)+1=31

This can be verified by looking at each successive pass on the numbers Template:Val through Template:Val:

Template:Nowrap
Template:Nowrap
Template:Nowrap
Template:Nowrap
Template:Nowrap
Template:Nowrap
Template:Nowrap
Template:Val

The general case

Dynamic programming is used to solve this problem in the general case by performing the first step and then using the solution of the remaining problem. When the index starts from one, then the person at s shifts from the first person is in position ((s1)modn)+1, where Template:Mvar is the total number of people. Let f(n,k) denote the position of the survivor. After the k-th person is killed, a circle of n1 remains, and the next count is started with the person whose number in the original problem was (kmodn)+1. The position of the survivor in the remaining circle would be f(n1,k) if counting is started at 1; shifting this to account for the fact that the starting point is (kmodn)+1 yields the recurrenceTemplate:Sfn

f(n,k)=((f(n1,k)+k1)modn)+1, with f(1,k)=1,

which takes the simpler form

g(n,k)=(g(n1,k)+k)modn, with g(1,k)=0

if the positions are numbered from 0 to n1 instead.

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, ..., (n/kk)-th people as one step, then changing the numbering.Template:Citation needed

This improved approach takes the form

g(n,k)={0if n=1(g(n1,k)+k)modnif 1<n<k{g(nnk,k)nmodk+nif g(nnk,k)<nmodkk(g(nnk,k)nmodk)k1if g(nnk,k)nmodk}if kn

See also

References

Citations

Template:Reflist

Sources

Template:Refbegin

Template:Refend

Further reading

Template:Refbegin

Template:Refend

  1. Cite error: Invalid <ref> tag; no text was provided for refs named formulae.org
  2. 2.0 2.1 Cohen, Richard. Making History: The Storytellers Who Shaped the Past, p. 54 (Simon & Schuster 2022).
  3. Template:Cite book
  4. Cite error: Invalid <ref> tag; no text was provided for refs named github.com