0% found this document useful (0 votes)
13 views11 pages

Josephus Algorithm

The document describes the Josephus problem, where a group of people are killed every kth person in a circle until one remains. It provides an algorithm using a circular queue to solve the problem for any given number of people and killing interval. The algorithm dequeues people from the queue until one person remains, with examples provided for 40 people with an interval of 3.

Uploaded by

bryaltyumi28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views11 pages

Josephus Algorithm

The document describes the Josephus problem, where a group of people are killed every kth person in a circle until one remains. It provides an algorithm using a circular queue to solve the problem for any given number of people and killing interval. The algorithm dequeues people from the queue until one person remains, with examples provided for 40 people with an interval of 3.

Uploaded by

bryaltyumi28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Josephus

Algorithm
The problem
According to the legend, during the Great Jewish Revolt against the
Romans, a group of 40 Jewish soldiers found themselves trapped in
a cave.

Instead of surrendering to the Romans, they decided to


commit suicide, forming a circle and killing each other every
third man until all were dead.
The mathematical
problem :

If there are n people numbered from 1 to n in a group, and


every k-th person is eliminated in a circle,

what person will be the last one remaining alive?


Let Qc a circular Queue

A Let len(Qc) = n

l
Let r the distance to choose a player that will die
(Note: In common or popular J.A , r=1)

g from i=1 to i<= len (Qc) ; i++

o
enqueue (Qc,i)
Let x=s where s is the start index Pi starts J.A

r P = x - 1

i
while len(Qc)>1 :
u = P + r % len(Qc)

t if u==P then

h u = u+1
P = u % len (Qc)
m Dequeue (Qc)
P = u % len(Qc)
Sea Qc una cola circular.

A Sea len(Qc) = n.

l
Sea r la distancia para elegir un jugador que morirá.

(Nota: En el común o popular J.A, r=1).


g desde i=1 hasta i<= len(Qc); i++

o encolar(Qc, i)

Sea x=s donde s es el índice de inicio donde Pi comienza el J.A


r P = x - 1

i mientras len(Qc) > 1:

t
u = P + r % len(Qc)

si u == P entonces

m u = u + 1

o P = u % len(Qc)

desencolar(Qc)

P = u % len(Qc)
1.
Let r = 3
P1 02 03
start P1
0 1
5 P2
P= 0
P6 u= 0+3%6 = 3
2
3==0 ? No

4
P3 P= 3%6 = 3

P5 Dequeue
3
P4 P= 3%5 = 3
2.

P1 02 03
P= 3
0
1 u= 3+3%5 =

P2 6%5 = 1

P6 1==3 ? No

Dequeue
4
2 P= 1%5 = 1

P3
P5 3 P= 1%4 = 1
3.

P1 02 03
P= 1
0
1 u= 1+3%4 = 4%4 = 0

4==1 ? No

P3 P= 0%4=0

Dequeue
P6 P= 0%3 = 0

3 2
P5
4.

P3 02 P= 0 03
u= 0%3 = 3%3 = 0
0 0==0 ? sí
1 u=u+1 u=1

P5 P= 1%3=1

Dequeue
P6 P= 1%2 = 1
2
5.

0 02 P= 1 03
u= 1+3%2 = 4%2 = 0

0==1 ? no

P6 P3 P= 0%2=0

Dequeue

P= 0%1 =0

1
s(6) =P6
r=3
Practice Exercise

s(7) =P1
r=2

You might also like