rank trees - The Rank Tree We introduce a data structure...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: The Rank Tree We introduce a data structure called the rank tree to solve the Josephus problem efficiently. The Josephus Problem n players, numbered 1 to n , are sitting in a circle; starting at player 1, a hot potato is passed; after m passes, the player holding the hop potato is eliminated, the circle closes ranks, and the game continued with the player who was sitting after the eliminated player picking up the hot potato; the last remaining player wins. The Josephus problem arose in the first century A.D in a cave on a mountain in Israel where Jewish zealots were being besieged Roman soldiers. The historian Josephus was among them. To Josephus’s consternation, the zealots voted to enter into a suicide pact rather than surrender to the Romans. He suggested the game that now bears his name. The hot potato was sentence of death to the person next to the one who got the potato. Josephus rigged to get the last lot and convinced the remaining intended victim that the two of them should surrender. That is how we know about this game; in effect, Josephus cheated. Fig. 1 is an example with 5 players and the number of passes being 1. 3 4 5 1 2 3 4 5 1 3 5 1 3 5 3 Figure 1: The Josephus problem: At each step, the darkest circle represents the initial holder and the lightly shaded circle represents the player who received the hot potato (and is eliminated). Passes are made clockwise. What data structure to use? We need a data structure to store the circle of player with following methods: • Pass the potato to the next person. • Delete the person holding the potato. The Simple Solutions Using a Linked List. The required methods suggest that we can represent the players in a linked list, actually in a circular linked list which is even better considering that players are sitting in a circle. Even though Java does not provide one, we can simulate it easily. If you see the following code, // Return the winner in the Josephus problem. // Linked list implementation. public static int josephus(int people, int passes) { List<Integer> list = new LinkedList<Integer>(); for (int i = 1; i <= people; i++) list.add(i); // Play the game; Iterator<Integer> itr = list.iterator(); while (people > 1) { for (int i = 0; i <= passes; i++) { 1. if (!itr.hasNext()) 2. itr = list.iterator(); 1 itr.next();itr....
View Full Document

This note was uploaded on 04/20/2010 for the course CS CS206 taught by Professor Lee during the Fall '09 term at 카이스트, 한국과학기술원.

Page1 / 6

rank trees - The Rank Tree We introduce a data structure...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online