Description: There are people standing in a circle waiting to be executed. The counting out begins at some point in the circle and proceeds around the circle in a fixed direction. In each step, a certain number of people are skipped and the next person is executed. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed people are removed), until only the last person remains, who is given freedom.
I Googled this 'Josephus problem' and the Wikipedia hit gives me a dynamic-programming solution: f(n,k)=((f(n-1,k)+k-1) mod n)+1, with f(1,k)=1
, but this only yields the last survivor. How can I get the sequence of the people executed? Say, p(5, 3) = {3,1,5,2,4}.
Also, is there a O(nlogn)
solution instead of a O(nk)
one?
To stimulate the program you can use a struct which contain the name of the player and a tag which keep the track if the player is active or not. Every time in a new round you skip a particular number of players, so use a loop and and a conditional statement so that all the players which are out of the game are ignored and only those in the game are counted. And of course add printf statements to print the current status.
To answer this question of outputting the sequence of execution, a simulation is required. This means O(nk) complexity. It is impossible to get sequence of execution [which is O(n)] while seeking O(nlogn) time complexity at the same time. Because you must output every person to be executed, which is O(n).
kkonrad's reference to Range Trees yield a nice O(nlogn) solution. As others have pointed out, a circular linked list is an efficient data structure for this problem. I find 200_success' Java solution from Code Review to be very elegant and readable.
There are some more details on Wikipedia for this Josephus problem (k = 2).
http://en.wikipedia.org/wiki/Josephus_problem
To get sequence of executed persons and last survivor you just need to simulate whole process from the beginning. Given description of procedure this would be quite easy task. Formula that you present is only shortcut to check who will survive and to obtain answer quickly.
Description on how to do this in O(n log n) using Range Trees is here: http://pl.scribd.com/doc/3567390/Rank-Trees
More detailed analysis can be found here: http://www.imt.ro/romjist/Volum12/Number12_1/pdf/02-MCosulschi.pdf
The people would be stored in array of size n. If the person at index
i
was executed now, the next would be given by(i+k)%m
where m is the number of people remaining. After every iteration, the array size would decrease by 1 and the remaining elements will be accordingly shifted.Input: People[0..n-1], n, k, i (= index of first person executed)
The pseudo code would be something like:
The most natural data structure to represent the people is a circular buffer. My solution creates a linked list, ties the tail of the list back to the head, then repeatedly counts around the buffer to the next person to be executed, removes that person from the buffer, and continues until the tail of the buffer points to itself.
For example:
You can read a fuller explanation at my blog, which gives three different solutions. Or you can run the program at http://programmingpraxis.codepad.org/RMwrace2.