A New Algorithm For The Josephus Problem Using Binary Count Tree
A New Algorithm For The Josephus Problem Using Binary Count Tree
A New Algorithm For The Josephus Problem Using Binary Count Tree
Abstract. This paper presents a new efficient algorithm for the Josephus problem using Binary
Count Tree. Binary count tree is a data structure ameliorated from the BST which additionally records
the node-count of each sub tree. In this algorithm, the whole Joseph circle is put into the binary count
tree. The target node can be quickly found by moving the pointer between the child node and the
parent node, and then the target will be deleted and the related node-counts will be updated. Analysis
and experiments shows that this algorithm can find out an entire Joseph sequence in time O(n*log
n).
Keywords: Josephus problem, BST, Binary count tree, Binary recursion.
1. Introduction
Josephus Problem is a classic mathematics and computer problem. It can be described as follows:
there are n people consecutively numbered from 1 through n and arranged in a circle. They are forced
to count off. And he who counts digit m off is out and the one next to him will count 1 again, next
person counting m likewise. In this way, people in the circle become fewer and fewer and the last
becomes the winner. There are many other versions of the story, and they will not be narrated here.
Via the description above, two questions of different layers can be posed.
Question 1. If the last winner is our only concern, then it can be described as:
Given positive integers n and m, then find out the number of winner - W(n, m).
For example, W (10, 3) = 4.
Question 2. If each person’s position in the killing sequence is wanted, it can be described as:
Given positive integers n and m, determine the entire sequence - List (n, m).
For instance, List (10,3) = {3,6,9,2,7,1,8,5,10,4}.
It can be easily found that Q2 is more complicated, for the reason that the answer of Q1 is included
in Q2. Q1 an be determined in time O(n) using recursion and we’ve already got a mature algorithm[1],
which will not be introduced in detail here. This paper is meant to design a more concise and efficient
method to solve Q2 in which an improved binary tree is used and then verify it.
12 L 12 R ∧ 6
8 15
L 8 R P 3 ∧ 15 R P 2
19
5 10
∧ 5 ∧ P 1 ∧ 5 ∧ P 1 ∧ 19 ∧ P 1
136
Advances in Computer Science Research, volume 86
Due to the binary recursion we used, the size of the left and right subtrees at each node is almost
the same(with difference no more than 1), and the binary tree generated is nearly full. The height of
the tree generated by n nodes is h, so we can figure out its expression:
T 1
2
L R
...
LL LR
RL RR
137
Advances in Computer Science Research, volume 86
If k> Count (R), then move forward a circle (n steps), and let T'= T, k' = k-n; we can proves that
k′ = k - n> - (Count (L) + 1) >= -Count (L). We will fall in "Case 2" at the next time we search.
If k< -Count (L), go backward a circle (n steps), so that T '= T, k' = k + n; we can proves that k
'= k + n < Count (R) + 1 <= Count (R). The next time we will fall in "Case 1".
The code for implementing the task of taking k steps forward is as follows:
TNode* Step (TNode *T, int k)
{ if(k==0) return T; // T just is the target
int Rc, Lc, RLc, LRc; GetCounts (T, &Rc, &Lc, & RLc, &LRc);
if( 0<k && k<=Rc) //Turn to the right subtree (0<k<=Rc)
{ T=T->R; k =k -(RLc+1); }
else if(-Lc<=k && k<0) //Turn to the left subtree (-Lc<=k<0)
{ T=T->L; k = k + (LRc+1); }
else //Turn to the root node
{ if(T->P != NULL) //If it has a parent
{ if(T->P->L==T) //If it’s the left child of the parent
{ T = T->P; k = k - ( Rc + 1); }
else //If it’s the right child of the parent
{ T = T->P; k = k + ( Lc + 1);}
}
else //If it doesn’t have a parent (T is the root)
{ if(k>0) //If it needs to move forward
{ k = k - T->count; } //Move forward a circle
else if(k<0) //If it needs to go backward
{ k = k + T->count; } //Go backward a circle
}
}
return Step( T, k);
}
3.3 Delete the Target Node
It’s necessary to delete the target node when we find it, which corresponds to the element dequeue
in Josephus ring. As for the deletion of a given node (T), it can be divided into the following categories:
Case 1: If T is the leaf, delete it directly.
Case 2: If T only has one child(shown in fig.5-a), delete T and replace its previous position with
the child of T .
Case 3: If T has two children(shown in fig.5-b), find S, the direct of T, replace the data domain of
T with S’s, and then delete S. It definitely apply to Case 1 and 2, due to the fact that S is the leftmost
mode in the right subtree and has only one child at most.
4 4 4 T 5
2 T 2 6 2 6
6 1 6
(2) (3) (1) (2) (2) (3) (2) (2)
1 5 7 5 7 1 S 5 7 1 7
(a) (b)
138
Advances in Computer Science Research, volume 86
(a) (b)
Fig. 6 Statistics of experiment results
For the purpose of further verifying the time complexity of it, we expand the test scale and gather
more statistics, including the height of the binary counting tree and the average number of movements
(shown as Fig.6-b). The following test data shows that the increase of average moves related to the
height of tree is linear, and the height of the tree is approximately equal to log(n). Therefore, we can
figure out that the complexity of whole algorithm is O(n log n).
5. Summary
All the theorems and experiments mentioned above have proven that the algorithm put forward in
this paper for the Josephus problem,which is based on BCT, is correct. And its time complexity is
O(n log n). In the algorithm,the “counting information” additionally recorded in the BCT is made use
of to find target node rapidly. Therefore, the maintenance of counting information is really crucial
139
Advances in Computer Science Research, volume 86
when an element is deleted. And if the equilibrium of BCT is taken into account,we can further
enhance the efficiency of this algorithm. BCT is an improved data structure and can be used to solve
many other algorithm problems except for the Josephus problem.
References
[1]. Information on http://rosettacode.org/wiki/Josephus_problem.
[2]. Bronson, N. G., Casper, J., Chafi, H., & Olukotun, K. (2010). A practical concurrent binary
search tree. (Vol.45, pp.257-268). ACM.
[3]. Ruskey, F., & Williams, A. (2010). The Feline Josephus Problem. International Conference on
Fun with Algorithms (Vol.50, pp.343-354). Springer, Berlin, Heidelberg.
[4]. Wang, Yong Hong. Comparison between Algorithms of Josephus Problem. Modern Computer
(2008).
140