CS301 handouts.doc - 3 · CS301 – Data Structures Lecture No. 04 _____ Page 43 of 505 In the end,...

2
CS301 – Data Structures Lecture No. 04 ___________________________________________________________________ Page 42 of 505 Josephus Problem Now we will see an example where circular link list is very useful. This is Josephus Problem. Consider there are 10 persons. They would like to choose a leader. The way they decide is that all 10 sit in a circle. They start a count with person 1 and go in clockwise direction and skip 3. Person 4 reached is eliminated. The count starts with the fifth and the next person to go is the fourth in count. Eventually, a single person remains. You might ask why someone has to choose a leader in this way. There are some historical stories attached to it. This problem is also studied in mathematics. Let’s see its pictorial view. We have ten numbers representing the ten persons who are in a circle. The value of M shows the count. As the value of M is three, the count will be three. N represents the number of persons. Now we start counting clockwise. After counting up to three, we have the number four. The number four is eliminated and put in the eliminated column. After eliminating the number four, we will start our counting from number five. Counting up to three, we have number eight which is eliminated and so on. N=10, M=3 4 5 6 7 8 9 10 1 2 3 N=10, M=3 4 5 6 7 8 9 10 1 2 3 Eliminated

Transcript of CS301 handouts.doc - 3 · CS301 – Data Structures Lecture No. 04 _____ Page 43 of 505 In the end,...

Page 1: CS301 handouts.doc - 3 · CS301 – Data Structures Lecture No. 04 _____ Page 43 of 505 In the end, only number five will remain intact. If we have ten persons (N = 10) in a circle

CS301 – Data Structures Lecture No. 04 ___________________________________________________________________

Page 42 of 505

Josephus Problem Now we will see an example where circular link list is very useful. This is Josephus Problem. Consider there are 10 persons. They would like to choose a leader. The way they decide is that all 10 sit in a circle. They start a count with person 1 and go in clockwise direction and skip 3. Person 4 reached is eliminated. The count starts with the fifth and the next person to go is the fourth in count. Eventually, a single person remains. You might ask why someone has to choose a leader in this way. There are some historical stories attached to it. This problem is also studied in mathematics. Let’s see its pictorial view.

We have ten numbers representing the ten persons who are in a circle. The value of M shows the count. As the value of M is three, the count will be three. N represents the number of persons. Now we start counting clockwise. After counting up to three, we have the number four. The number four is eliminated and put in the eliminated column.

After eliminating the number four, we will start our counting from number five. Counting up to three, we have number eight which is eliminated and so on.

N=10, M=3 4

5

6

7

8910

1

2

3

N=10, M=3 4

5

6

7

8910

1

2

3

Eliminated

Page 2: CS301 handouts.doc - 3 · CS301 – Data Structures Lecture No. 04 _____ Page 43 of 505 In the end, only number five will remain intact. If we have ten persons (N = 10) in a circle

CS301 – Data Structures Lecture No. 04 ___________________________________________________________________

Page 43 of 505

In the end, only number five will remain intact.

If we have ten persons (N = 10) in a circle and eliminate after counting up to three (M = 3). If we start our count from one, who will be the leader? We have studied this earlier and know that the person who is sitting at the fifth position will become the leader. Suppose if the value of N is 300 or 400 and the value of M is 5 or 10. Now who will be the leader? This is a mathematical problem where we can change the values of N and M. There is a formula where the values of N, M are allotted. You can calculate who should become the leader. Here we will not solve it mathematically. Rather, it will be tackled as a computer problem. If you analyze the pictures shown above, it gets clear that this can be solved with the circular link list. We arrange these numbers in a circularly-linked list, point the head pointer at the starting number and after calling the next method for three times, we will reach the node which is to be removed. We will use the remove method to remove the node. Then the next method is called thrice from there and the node is removed. We will continue this till we have

N=10, M=3 4

5

6

7

8

9

10

1

2

3

Eliminated

N=10, M=3 4

5

6

7

8

910

1

2

3

Eliminated