A Comparative Study on the Algorithms for a Generalized Josephus Problem

Similar documents
Lecture 5. Counting Sort / Radix Sort

Sorting in Linear Time. Data Structures and Algorithms Andrei Bulatov

6.854J / J Advanced Algorithms Fall 2008

CIS 121 Data Structures and Algorithms with Java Spring Stacks, Queues, and Heaps Monday, February 18 / Tuesday, February 19

Chapter 24. Sorting. Objectives. 1. To study and analyze time efficiency of various sorting algorithms

Ones Assignment Method for Solving Traveling Salesman Problem

. Written in factored form it is easy to see that the roots are 2, 2, i,

Sorting 9/15/2009. Sorting Problem. Insertion Sort: Soundness. Insertion Sort. Insertion Sort: Running Time. Insertion Sort: Soundness

why study sorting? Sorting is a classic subject in computer science. There are three reasons for studying sorting algorithms.

Heaps. Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015

CIS 121 Data Structures and Algorithms with Java Fall Big-Oh Notation Tuesday, September 5 (Make-up Friday, September 8)

Combination Labelings Of Graphs

Hash Tables. Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015.

CIS 121 Data Structures and Algorithms with Java Spring Stacks and Queues Monday, February 12 / Tuesday, February 13

Pseudocode ( 1.1) Analysis of Algorithms. Primitive Operations. Pseudocode Details. Running Time ( 1.1) Estimating performance

Lower Bounds for Sorting

How do we evaluate algorithms?

Counting the Number of Minimum Roman Dominating Functions of a Graph

Sum-connectivity indices of trees and unicyclic graphs of fixed maximum degree

Thompson s Group F (p + 1) is not Minimally Almost Convex

Lecture Notes 6 Introduction to algorithm analysis CSS 501 Data Structures and Object-Oriented Programming

A Note on Least-norm Solution of Global WireWarping

condition w i B i S maximum u i

Outline and Reading. Analysis of Algorithms. Running Time. Experimental Studies. Limitations of Experiments. Theoretical Analysis

A New Morphological 3D Shape Decomposition: Grayscale Interframe Interpolation Method

Morgan Kaufmann Publishers 26 February, COMPUTER ORGANIZATION AND DESIGN The Hardware/Software Interface. Chapter 5

Examples and Applications of Binary Search

On (K t e)-saturated Graphs

Analysis of Algorithms

3D Model Retrieval Method Based on Sample Prediction

Improved Random Graph Isomorphism

DATA STRUCTURES. amortized analysis binomial heaps Fibonacci heaps union-find. Data structures. Appetizer. Appetizer

Elementary Educational Computer

Higher-order iterative methods free from second derivative for solving nonlinear equations

Algorithm. Counting Sort Analysis of Algorithms

Running Time ( 3.1) Analysis of Algorithms. Experimental Studies. Limitations of Experiments

Analysis of Algorithms

CIS 121. Introduction to Trees

An Efficient Algorithm for Graph Bisection of Triangularizations

Lecture 1: Introduction and Strassen s Algorithm

Running Time. Analysis of Algorithms. Experimental Studies. Limitations of Experiments

Load balanced Parallel Prime Number Generator with Sieve of Eratosthenes on Cluster Computers *

CSC 220: Computer Organization Unit 11 Basic Computer Organization and Design

n n B. How many subsets of C are there of cardinality n. We are selecting elements for such a

Lecture 2: Spectra of Graphs

Protected points in ordered trees

Chapter 1. Introduction to Computers and C++ Programming. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Data Structures Week #9. Sorting

Massachusetts Institute of Technology Lecture : Theory of Parallel Systems Feb. 25, Lecture 6: List contraction, tree contraction, and

An Efficient Algorithm for Graph Bisection of Triangularizations

Lecture 28: Data Link Layer

Graphs. Minimum Spanning Trees. Slides by Rose Hoberman (CMU)

Lecture 6. Lecturer: Ronitt Rubinfeld Scribes: Chen Ziv, Eliav Buchnik, Ophir Arie, Jonathan Gradstein

INTERSECTION CORDIAL LABELING OF GRAPHS

Cubic Polynomial Curves with a Shape Parameter

Data Structures and Algorithms. Analysis of Algorithms

Homework 1 Solutions MA 522 Fall 2017

CHAPTER IV: GRAPH THEORY. Section 1: Introduction to Graphs

The isoperimetric problem on the hypercube

New Results on Energy of Graphs of Small Order

Analysis Metrics. Intro to Algorithm Analysis. Slides. 12. Alg Analysis. 12. Alg Analysis

What are we going to learn? CSC Data Structures Analysis of Algorithms. Overview. Algorithm, and Inputs

Computers and Scientific Thinking

Computational Geometry

BOOLEAN MATHEMATICS: GENERAL THEORY

9.1. Sequences and Series. Sequences. What you should learn. Why you should learn it. Definition of Sequence

A Study on the Performance of Cholesky-Factorization using MPI

Chapter 9. Pointers and Dynamic Arrays. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

An Improved Shuffled Frog-Leaping Algorithm for Knapsack Problem

Chapter 3 Classification of FFT Processor Algorithms

Fast Fourier Transform (FFT) Algorithms

1.2 Binomial Coefficients and Subsets

Image Segmentation EEE 508

The Magma Database file formats

quality/quantity peak time/ratio

15-859E: Advanced Algorithms CMU, Spring 2015 Lecture #2: Randomized MST and MST Verification January 14, 2015

Pattern Recognition Systems Lab 1 Least Mean Squares

Python Programming: An Introduction to Computer Science

Exact Minimum Lower Bound Algorithm for Traveling Salesman Problem

CSE 417: Algorithms and Computational Complexity

On Infinite Groups that are Isomorphic to its Proper Infinite Subgroup. Jaymar Talledo Balihon. Abstract

Accuracy Improvement in Camera Calibration

BASED ON ITERATIVE ERROR-CORRECTION

Chapter 10. Defining Classes. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Basic allocator mechanisms The course that gives CMU its Zip! Memory Management II: Dynamic Storage Allocation Mar 6, 2000.

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures

Counting Regions in the Plane and More 1

COSC 1P03. Ch 7 Recursion. Introduction to Data Structures 8.1

Optimal Mapped Mesh on the Circle

A Note on Chromatic Transversal Weak Domination in Graphs

The Counterchanged Crossed Cube Interconnection Network and Its Topology Properties

Chapter 11. Friends, Overloaded Operators, and Arrays in Classes. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Improvement of the Orthogonal Code Convolution Capabilities Using FPGA Implementation

Relationship between augmented eccentric connectivity index and some other graph invariants

Algorithms for Disk Covering Problems with the Most Points

Perhaps the method will give that for every e > U f() > p - 3/+e There is o o-trivial upper boud for f() ad ot eve f() < Z - e. seems to be kow, where

A graphical view of big-o notation. c*g(n) f(n) f(n) = O(g(n))

Exercise 6 (Week 42) For the foreign students only.

A Generalized Set Theoretic Approach for Time and Space Complexity Analysis of Algorithms and Functions

CS200: Hash Tables. Prichard Ch CS200 - Hash Tables 1

Transcription:

Appl. Math. If. Sci. 7, No. 4, 1451-1457 (2013) 1451 Applied Mathematics & Iformatio Scieces A Iteratioal Joural http://dx.i.org/10.12785/amis/070425 A Comparative Study o the Algorithms for a Geeralized Josephus Problem Lei Wag 1 ad Xiaog Wag 2,3, 1 Microsoft AdCeter, Bellevue, WA 98004, USA 2 Faculty of Mathematics ad Computer Sciece, Fuzhou Uiversity, 350002 Fuzhou, Chia 3 Faculty of Computer Sciece, Quazhou Normal Uiversity, 362000 Quazhou, Chia Received: 3 Dec. 2012, Revised: 14 Ja. 2013, Accepted: 12 Mar. 2013 Published olie: 1 Jul. 2013 Abstract: The classic Josephus problem ca be described as follows: There are objects, cosecutively umbered from 1 through, arraged i a circle. We are give a positive iteger k. Begiig with a desigated first object, we proceed aroud the circle, removig every kth object. After each object is removed, coutig cotiues aroud the circle that remais. This process cotiues util all objects have bee removed. I a geeralized Josephus problem, a umber of lives l is itroduced for the problem. Each object has l lives. The object is removed oly whe it has bee selected l times. I this paper we preset a fast algorithm for geeratig the Josephus permutatio for the geeralized Josephus problem. Our ew algorithm ca also be applied to a more geeral case of the geeralized Josephus problem where the lives for all objects ca be differet. The time ad space complexities of ew algorithms are O(llogk) ad O() respectively. The computatioal experimets demostrate that the achieved results are ot oly of theoretical iterest, but also that the techiques developed may actually lead to cosiderably faster algorithms. Keywords: Josephus permutatio, hit sequece, full biary tree 1. Itroductio I this paper we study a ew variat of the Josephus problem [1, 2, 3]. The classic Josephus problem is defied as follows. Suppose that objects, cosecutively umbered from 1 through, are arraged i a circle ad that we are give a positive iteger k. Begiig with a desigated first object, we proceed aroud the circle, removig every kth object. After each object is removed, coutig cotiues aroud the circle that remais. This process cotiues util all objects have bee removed. The order i which the objects are removed from the circle defies a particular permutatio of the itegers 1, 2,,. This permutatio is usually called the (, k) Josephus permutatio. For example, the (6,4) Josephus permutatio is (4 2 1 3 6 5). There are also various geeralizatios o the classic Josephus problem i the literature [4, 6]. I a recet research [5], the classic Josephus problem was geeralized as follows. A uiform umber of lives l is itroduced for the problem. Each object has l lives. The object is removed oly whe it has bee selected for l times. This ew variat of Josephus problem was called Felie Josephus problem. I this geeralized Josephus problem, correspodig to the order i which the objects are removed from the circle, there is also a particular permutatio of the itegers 1,2,,. This permutatio is called the (, k, l) Josephus permutatio. For example, the (6,4,2) Josephus permutatio is (4 2 1 6 5 3). The classic Josephus problem is a special case of the geeralized Josephus problem whe l = 1. We are iterested i algorithms which, give itegers, k ad l, geerate the correspodig (,k,l) Josephus permutatio. A O( 2 ) time algorithm usig O( 2 ) space to compute the last removed object of a give geeralized Josephus problem with objects is sketched i [5]. The time ad space complexities of the algorithm are idepedet of the problem parameters k ad l. The algorithm is coceptual feasible. Due to the O( 2 ) time ad space costs of the algorithm, the algorithm is oly of theoretical iterest. It is ot practical. I this paper we preset a practical algorithm for geeratig the geeral (, k, l) Josephus permutatio. Our Correspodig author e-mail: wagxiaog@qztc.edu.c Natural Scieces Publishig Cor.

1452 L. Wag ad X. Wag: Algorithms for a Geeralized... ew algorithm ca also be applied to a more geeral case of the geeralized Josephus problem where the lives for all objects ca be differet. The time ad space complexities of the ew algorithm is O(llogk) ad O() respectively. Throughout the paper, we assume that a real RAM with a uiform cost criterio is the computatioal model. I particular, this meas that each arithmetic operatio requires costat time. The orgaizatio of the paper is as follows. I the followig 4 sectios we describe the algorithms ad our computatioal experiece with the algorithms for geeratig the geeral (, k, l) Josephus permutatio. I sectio 2 we describe a ew variat tree based algorithm for geeratig the hit sequeces of the geeralized Josephus problem ad their correctess ad complexities. Based o the algorithm i sectio 2, a improved algorithm for geeratig the (, k, l) Josephus permutatios is proposed i sectio 3. I sectio 4 we give a computatioal study of the preseted algorithms which demostrates that the achieved results are ot oly of theoretical iterest, but also that the techiques developed may actually lead to practical algorithms. Some cocludig remarks are i sectio 5. 2. A Fast Algorithm I this sectio we describe a fast algorithm for geeratig the (, k, l) Josephus permutatio. The algorithm is a variat of a tree based algorithm for classic Josephus problem proposed by Lloyd [3]. The ew variat of the algorithm uses a heap like data structure ad modified to solve the geeralized Josephus problem. 2.1. The Data Structures of the Algorithm The parameters to the geeralized Josephus problem are the umber of objects, the skip factor k, ad the umber of lives l. We make o assumptios about these parameters. I particular, k or l or both, could be larger tha. Whe the parameters are give, the followig otatios are itroduced for the geeralized Josephus problem i [5]. Let h i be the ith selected object. The sequece h 1,h 2,,h l is called the hit sequece. Let x i be the ith removed object. The sequece x 1,x 2,,x is called the kill sequece. It is obvious that the kill sequece forms the (, k, l) Josephus permutatio of (1, 2,, ). I the followig sectios, a object is called active before it is removed, ad dead after it is removed. For the give geeralized Josephus problem, there are objects ad the skip factor is k. I the followig sectios, we cosider a more geeral Josephus problem, where the umbers of lives for each object are ot ecessary cosistet. Assume at the begiig, the umber of lives for the ith object be l i,1 i. We use a array l to store the umber of lives for each object. Whe the ith object is selected, the correspodig value of l[i] is decreased by 1. If the value of l[i] becomes 0, the ith object is removed. Let m = 2 logk ad b = /m. The objects {1,,} are divided evely ito b groups as follows. {1,,m};{m+1,,2m}; ;{(b 1)m+1,,} Each group has m objects, except the last oe. For each group j,1 j b, we use a array a[ j 1] of size 2m 1 to store a full biary tree T j, with itegers i group j stored at its leaves a[ j 1][m 1],a[ j 1][m],,a[ j 1][2m 2]. All of the b full biary trees T j,1 j b costitute a two dimesioal array a. The ith active object i T j is the ith active iteger ecoutered i a left-to-right traversal of the leaves of T j. Also, a sequece of objects y 1,,y p is i leaf order with respect to the tree T j, if for 1 i p, y i is the ith active iteger i tree T j. Whe the object i a leaf is removed, the correspodig leaf is dead, otherwise it is a active leaf. The full biary tree T j is represeted as a array idexed biary tree a[ j 1]. For each ode a[ j 1][i],0 i m 2, its left child ode is a[ j 1][2 i + 1] ad its right child ode is a[ j 1][2 i+2]. For each ode a[ j 1][i], its paret ode is a[ j 1][ (i 1)/2 ]. Every ode of each tree a[ j 1] cotais a cout of the umber of active leaves i the subtree rooted at that ode. This is the size of the ode. Whe a object is removed, the size of each subtree cotaiig the object is decreased sice that object is ow dead. Notice that the meaigs of the values stored i the leaf odes ad the o-leaf odes of each tree T j are differet. It is obvious that the umber of active leaves i the subtree rooted at a leaf is 1 if the leaf is active, ad 0 if the leaf is dead. I order to distiguish the ode type of each tree T j, we chage the sig of iteger e stored i a leaf to e. Wheever we read a egative iteger e, we kow that it is a leaf ode represetig the object e. I additio, the trees are ubly liked together i a circular fashio, with tree T j liked after the tree T j 1 ad before the tree T j+1. Two arrays prev ad ext are used to represet the backward ad the forward liks respectively. 2.2. A Algorithm for Geeratig Hit Sequeces The algorithm cosists of two parts: the iitializatio procedure ad the mai program. I the iitializatio Natural Scieces Publishig Cor.

Appl. Math. If. Sci. 7, No. 4, 1451-1457 (2013) / www.aturalspublishig.com/jourals.asp 1453 procedure, the data structure for the whole algorithm is iitialized as follows. Algorithm 2.1: INIT(, k, b, ext, prev) m 2 logk,b /m for i 0 to b 1 s i m,t (i+1) m for j 0 to m 1 u[ j] 0 for j s to mi(t 1, 1) u[ j s] ( j+ 1) BUILD(u, a[i]) t (i+1) mod b ext[i] t, prev[t] i I the algorithm described above, the sub-algorithm BUILD(u, v) is used to build the full biary tree v described i the last subsectio with its leaves give by the array u i the left-to-right order. A recursive fuctio COUNT(v,i) is used to cout the umber of active leaves i the subtree rooted at ode v[i] i a bottom-up fashio. A leaf ode with size 1 is recogized by the sig of its value. Algorithm 2.2: BUILD(u, v) procedure COUNT(v, i) if m<i+2 if v[i]<0 the the retur (1) else retur (0) v[i] COUNT(v,2 i+1) else v[i] v[i]+ COUNT(v,2 i+2) retur (v[i]) mai for i 0 to m 1 v[m+i 1] u[i] COUNT(v, 0) It is clear that the algorithm COUNT(v, i) requires O(m) = O(k) time ad so the algorithm COUNT(v,i) for each biary tree requires O(k) time. Thus, the iitializatio procedure requires O() total time. By the set of b trees represeted by the array a, the mai program to geerate the hit sequeces cosists of the followig: We will show later that the total cost of these operatios to geerate a hit object is O(logk). Whe two cosecutive trees become small, we the combie the two trees ito a ew tree of height logk. Such a combiig operatio is used to guaratee that the movemet betwee trees whe geeratig a hit object is limited to oe or two. By amortizig the cost of the operatios amog the dead objects i the two origial trees, we ca maitai the O(logk) cost for each hit object. Algorithm 2.3: ALGORITHM-L(, b) i 0,r 0,t b 1 while i< TREE(r, t) j LEAF(r,t) HIT(i, j,r,t) I the algorithm ALGORITHM-L(, b), three subalgorithms are called. The sub-algorithm TREE(r, t) is used to locate the tree cotaiig the ext active object to be hit. The sub-algorithm LEAF(r,t) is used to fid the ext active leaf to be hit. The sub-algorithm HIT(i, j,r,t) is used to output the ext hit object ad adjust the data structure. I these three sub-algorithms, the variable r plays a importat role. The variable r records the umber of active objects to the right of curretly hit leaf ode i the full biary tree cotaiig it. This variable is very useful to fid the positio of the ext hit object i our data structures. With the chagig of the hit sequeces, the variable r chages accordigly. The variable t is the idex of the full biary tree cotaiig the active leaf to be hit. Algorithm 2.4: TREE(r, t) if t = ext[t] ad r<k the{ r k+ a[t][0] 1 (k r 1) mod a[t][0] while r<k else t ext[t],r r+ a[t][0] Algorithm 2.5: LEAF(r, t) j 0,xt r k while j+ 1<m rs SIZE(a[t][2 j+ 2]) if r rs<k the j 2 j+ 2 else j 2 j+ 1,r r rs r xt retur ( j) I the algorithms above, the fuctio SIZE(a[t][ j]) returs the umber of active leaves i the subtree rooted at the ode a[t][ j]. Algorithm 2.6: HIT(i, j, r, t) h a[t][ j] output (h) if l[h 1]>1 the l[h 1] l[h 1] 1 else KILL(i, j,r,t) Natural Scieces Publishig Cor.

1454 L. Wag ad X. Wag: Algorithms for a Geeralized... The sub-algorithm KILL(i, j,r,t) is the heart of the whole algorithm which removes the dead leaf from the curret biary tree ad adjusts the curret data structures. Algorithm 2.7: KILL(i, j, r, t) a[t][ j] 0 while j> 0 j ( j 1)/2,a[t][ j] a[t][ j] 1 if t ext[t] if a[t][0]+a[ext[t]][0]=m { r r+ a[ext[t]][0] the COMBINE(t) the else if{ a[t][0]+a[prev[t]][0]=m t prev[t] the COMBINE(t) The algorithm COMBINE(t) will combie the two small trees a[t] ad a[ext[t]] ito a ew biary tree a[t], ad the tree a[ext[t]] will o loger be used. Algorithm 2.8: COMBINE(t) x ext[t],i 0 for j 0 { to m 1 if a[t][m+ j 1]<0 the u[i] a[t][m+ j 1],i i+1 for j 0 { to m 1 if a[x][m+ j 1]<0 the u[i] a[x][m+ j 1],i i+1 BUILD(u, a[t]) ext[t] ext[ext[t]], prev[ext[t]] t Now we cosider the time complexity for the whole algorithm. From the tests performed i the algorithm KILL(i, j, r, t), we kow that at the begiig of each executio of TREE(r, t), either there is exactly oe tree with active leaves, or for each tree a[t] with active leaves we have, a[t][0]+a[ext[t]][0] m k. It follows that there must be two trees with active leaves for the while-loop body of TREE(r, t) to be executed. Therefore, the while-loop body of each call of TREE(r,t) is executed at most twice. I the leaf search algorithm LEAF(r, t), the search follows a path from the root of the tree a[t] to a leaf a[t][ j]. Sice each tree created by the algorithm has a height of logk, the cost per iteratio of the while-loop i LEAF(r, t) is O(log k). Next we cosider the algorithm KILL(i, j, r, t). The time cost of the algorithm cosists of two parts. The first part is a series of ode value chages from the leaf a[t][ j] to the root a[t][0]. Sice the height of the tree is logk, the time of the first part is O(logk). The secod part of the time cost is icurred by the algorithm COMBINE(t). The cost of the algorithm is miated by the executio time of BUILD(u,a[t]) to build the ew full biary tree a[t]. We already kow the time cost for this task is O(k). This part of cost ca amortized to each leaf of the tree as follows. I the algorithm COMBINE(t), trees a[t] ad a[ext[t]] are combied. Whe these trees are created, each has m active leaves. Whe the algorithm COMBINE(t) is performed, these two trees have m active leaves ad m dead leaves. The O(m) cost of the algorithm ca thus be divided evely amog the objects stored at the m dead leaves. Each such object is thus assiged a cost of O(1). Sice each object is charged at most oce, the total cost of the algorithm COMBINE(t) performed i the whole algorithm is O(). Deote L= l i. Sice there are total L iteratios for the whole algorithm ad each iteratio requires O(log k) time. The time complexity of the whole algorithm is thus O(Llogk). The space used by the algorithm is obviously O(). Theorem 1 The algorithm ALGORITHM-L(, b) for solvig the geeralized Josephus problem requires O(L log k) time ad O() space. 3. A Improved Algorithm By explorig the followig property of the hit sequeces for a give geeralized Josephus problem, we ca improved the algorithm described i the last sectio further. A similar property was metioed i [5] with a iformal proof. The fact metioed there seems right, but the proof is ot strict ad icorrect. We will express this useful property by a theorem ad give a strict proof here. We divide the hit sequece ito segmets by the successively removed objects as follows r 1,r 2,,r. The segmet r i is called roud i hit sequece. The last object of r i is the ith removed object. Theorem 2 For each fixed idex i,1 i, the hit sequeces r i ca be uiquely formulated as r i = α s β, where s is a positive iteger, ad both of the sequeces α ad β are sequeces of uique elemets of {1,2,,}. Proof.We cosider the hit sequece r i for fixed idex i. There has bee i 1 objects removed before r i. The curret i + 1 objects remaied ca be writte i decreasig order by p 1, p 2,, p i+1. The positio p d is the startig positio where its predecessor x i 1 was just removed. The the ext object hit will be h 1 = p 1+(d+k 1) mod i+1. I geeral, suppose r i = t, ad for each 1 j t, d j = 1+(d+ jk 1) mod i+1, h j = p d j, the the hit sequece r i ca be writte as h 1,h 2,,h t. Natural Scieces Publishig Cor.

Appl. Math. If. Sci. 7, No. 4, 1451-1457 (2013) / www.aturalspublishig.com/jourals.asp 1455 If all of the objects h 1,h 2,,h t are distict, the we kow the lives of the object h t must be 1, ad it is the ext removed object. This is the case of α = /0 ad β = {h 1,h 2,,h t }. Otherwise, the lives of each object i {h 1,h 2,,h t } must be greater tha 1. I this case, there must be a cycle i {h 1,h 2,,h t }. Suppose the cycle be {h u,h u+1,,h v }, 1 u v t. This meas that all of the objects h 1,h 2,,h v are distict, ad h v+1 = h u is the first object repeatedly hit. We ca show that u = 1, sice otherwise h u 1 = h v has bee hit repeatedly before h u. Therefore, we kow that {h 1,h 2,,h v } forms a cycle of the hit sequece. Suppose that l = mi{l[h j ] 1 j v}, ad j = mi{ j l[h j ] = l,1 j v}. It is readily see that h j will be the ext object to be removed. That is h j = h t. If we deote α = {h 1,h 2,,h v }, β = {h 1,h 2,,h j }, ad s = l 1, the the hit sequece r i ca be writte as r i = α s β. We ca easily see that s > 0, sice l > 1. I the special case of j = v, we have r i = α s+1. From Theorem 2, we kow that the lives of the curret objects ca be easily updated from the iformatio i determiig r i = α s β. First, if p j {h 1,h 2,,h t }, the the lives of p j remais uchaged. From the proof of Theorem 2 we kow that the last object of β is the removed object x i of roud r i. So, if p j β, the l[p j ] = l[p j ] s 1. If p j α ad p j β, the l[p j ]=l[p j ] s. Accordig to the Theorem 2, we ca improve the algorithm for solvig the geeralized Josephus problem to a ew algorithm ALGORITHM-N(,b) as follows. The sub-algorithm HIT(i, j, r, t) of the algorithm ALGORITHM-L(,b) is chaged to follows. Algorithm 3.1: HIT(i, j, r, t) h a[t][ j] q[g] h,g g+1 if g=1{ or l[h 1]<l[q[mi] 1] mi g 1 the p[0] t, p[1] j, p[2] r if l[q[mi] 1]=1 { or g>1 ad h=q[0] CHANGE(t, j,r,g,mi) the KILL(i, j,r,t) I the algorithm described above, we use a array q to record the hit sequece α of roud r i as stated i Theorem 2. A variable mi is used to record the idex of α with miimal lives. Whe a object with oly oe life is foud or a cycle of the hit sequece is foud, we the have determied r i = α s β. With the miimal lives i α recorded by mi, we ca easily update the lives of the curret objects accordig to Theorem 2 by algorithm CHANGE(t, j, r, g, mi) as follows. Algorithm 3.2: CHANGE(t, j, r, g, mi) d l[q[mi] 1] for i 0 to mi l[q[i] 1] l[q[i] 1] d for i mi+1 to g 2 l[q[i] 1] l[q[i] 1] d+ 1 g 0,t p[0], j p[1],r p[2] I the two sub-algorithms above, aother array p is used to record the iformatio t, j,r o the tree cotaiig the ext removed active object. Other parts of the algorithm ALGORITHM-L(, b) remais uchaged. Suppose for each hit sequece r i, the correspodig cyclic hit sequece stated i Theorem 2 be α i, ad the legth of α i be deoted as t i,1 i. Deote T = t i. Sice there are totally T iteratios for the whole modified algorithm ad each iteratio requires O(log k) time. The time complexity of the improved algorithm is thus O(T logk). The space used by the algorithm is obviously O(). Theorem 3 The improved algorithm ALGORITHM-N(, b) for solvig the geeralized Josephus problem requires O(T log k) time ad O() space. The modified algorithm is ideed a improvemet o the algorithm described i the last sectio, sice T = is much smaller tha L = t i l i. For example, if ad k are relatively prime, ad each object has a uiform umber of lives l, the the hit sequece will have the form α l 1 β, where α ad β are permutatios of {1,2,,} ad β is the Josephus permutatio for l = 1 [5]. So, i this case, the legth of α equals to, ad T = L = t i =, l i = l. The origial algorithm requires O(l logk) time, while the improved algorithm requires O(logk) time eve though the umber of lives l may ted to ifiite. 4. Computatioal Experimets I this sectio, we give some computatioal experimets o the performace of the algorithms for the geeralized Josephus problem. Natural Scieces Publishig Cor.

1456 L. Wag ad X. Wag: Algorithms for a Geeralized... Table 1: Comparig algorithms for small k ad l: Ruig times i secods k l Alg-R Alg-L Alg-N 10 2 4 6 0 0 0 10 3 8 9 0.031 0 0 10 4 16 12 1.81 0.109 0.047 10 5 32 15 177.029 1.342 0.421 10 6 64 18 ***** 18.876 4.571 10 7 128 21 ***** 259.974 50.653 Table 2: Comparig algorithms for small l ad icreasig k: Ruig times i secods k l Alg-R Alg-L Alg-N 10 5 10 2 15 175.048 1.685 1.951 10 5 10 3 15 174.969 2.201 2.591 10 5 10 4 15 175.172 2.652 2.824 10 5 10 5 15 174.829 2.793 0.639 10 5 10 6 15 174.096 2.808 0.671 10 5 10 7 15 174.268 2.901 0.641 10 5 10 8 15 174.845 2.917 0.671 10 5 10 9 15 174.564 2.917 0.655 Table 3: Comparig algorithms for ad k fixed ad icreasig l: Ruig times i secods k l Alg-R Alg-L Alg-N 10 5 10 7 10 2 174.721 17.893 0.655 10 5 10 7 10 3 174.252 176.997 0.687 10 5 10 7 10 4 174.252 1767.79 0.641 10 5 10 7 10 5 174.221 ***** 0.655 10 5 10 7 10 6 796.755 ***** 0.655 10 5 10 7 10 7 904.894 ***** 0.641 10 5 10 7 10 8 910.572 ***** 0.761 10 5 10 7 10 9 915.517 ***** 0.686 Table 4: Comparig algorithms for fixed ad icreasig k ad l: Ruig times i secods k l Alg-R Alg-L Alg-N 10 5 3723359 10 2 173.955 19.921 0.655 10 5 41912239 10 3 173.784 198.588 0.671 10 5 126590963 10 4 173.987 1962.31 0.671 10 5 152585351 10 5 173.797 ***** 0.671 10 5 198676927 10 6 174.268 ***** 0.671 10 5 271830677 10 7 176.274 ***** 0.702 10 5 273068023 10 8 200.711 ***** 0.687 10 5 273068023 10 9 432.151 ***** 0.702 Our computatioal experimets were carried out o a persoal computer with Petium(R) Dual Core CPU 2.10 GHz ad 2.0 Gb RAM. The word size of the processor is w=32. We compare the three algorithms, the O( 2 ) time algorithm i [5], deoted by ALGORITHM-R(), the algorithm ALGORITHM-L() described i sectio 2 ad the improved algorithm ALGORITHM-N() described i sectio 3 for 5 differet test data sets. I order to compare the algorithms with ALGORITHM-R(), the data sets are desiged with a uiform umber of lives for each test case. The ruig times i secods of the 3 algorithms for computig the geeralized Josephus permutatios with small parameters k ad l are compared i Table 1. The times reported for each value of idicate the results of ruig repeated 10 times to mitigate the effects of ram fluctuatios i system overhead. I the followig tables, the etries marked with symbol ***** could ot be solved due to isufficiet memory or time. As expected, i our experimets for small k ad l, the improved algorithm performs best with the umber of objects icreasig. For this data set the Algorithm-R exhibits a Ω( 2 ) time boud i the worst case. Table 2 gives the ruig times i secods of the 3 algorithms for computig the geeralized Josephus permutatios with small parameters l ad icreasig k. For this data set, the time costs of the 3 algorithms have oly small chages as the parameter k icreasig. Table 3 gives the ruig times i secods of the 3 algorithms for computig the geeralized Josephus permutatios with parameters ad k fixed ad icreasig l. As expected, i our experimets for icreasig l, the time cost of the algorithm ALGORITHM-L() is icreasig with the parameter l icreasig. While the algorithm ALGORITHM-R() ad the improved algorithm ALGORITHM-N() are ot affected by the icreasig of the parameter l. The improved algorithm ALGORITHM-N() performs best. For this data set the ALGORITHM-R() exhibits a Ω( 2 ) time boud too. Table 4 gives the ruig times i secods of the 3 algorithms for computig the geeralized Josephus permutatios with parameters fixed ad icreasig k ad l. The effect of icreasig k is ot sigificat for the 3 algorithms. Table 5 gives the ruig times i secods of the 3 algorithms for computig the geeralized Josephus permutatios with the 3 parameters,k ad l icreasig. As expected, for this data set, the improved algorithm performs best with the 3 parameters,k ad l icreasig. The algorithm ALGORITHM-L() is affected sigificatly by the parameter l ad ALGORITHM-R() exhibits a Ω( 2 ) time boud i the worst case. Natural Scieces Publishig Cor.

Appl. Math. If. Sci. 7, No. 4, 1451-1457 (2013) / www.aturalspublishig.com/jourals.asp 1457 Table 5: Comparig algorithms for, k ad l icreasig: Ruig times i secods k l Alg-R Alg-L Alg-N 10 2 195 10 2 0 0 0 10 3 1942 10 3 0.046 0.249 0.015 10 4 19420 10 4 2.013 29.702 1.575 10 5 194201 10 5 174.408 3795.01 0.702 10 6 1942007 10 6 ***** ***** 8.253 5. Cocludig Remarks We have proposed a efficiet algorithm for computig the geeralized Josephus permutatios. The time cost of the ew algorithm is O(T logk) ad O() space is used. For some special cases of the geeralized Josephus problem, T = t i = O(). So our ew algorithm requires O( log k) time i these cases. This is a big improvemet o the previous algorithms. For the geeral Josephus problem, whether the formula T = t i = O() is always true is a ope problem. We will ivestigate the problem further. Lei Wag, PhD i Computer Sciece from Georgia Istitute of Techology 2011. Applied researcher at Microsoft. Has experiece i computer sciece with emphasis i algorithm desig. The areas of iterest are approximatio ad ramized algorithms,mechaism desig, market equilibrium computatio. Xiaog Wag, Professor i Computer Sciece Departmet of Quazhou Normal Uiversity ad Fuzhou Uiversity,Chia. Has experiece i computer sciece ad applied mathematics. The areas of iterest are desig ad aalysis of algorithms,expoetial-time algorithms for NP-hard problems,strategy game programmig. Ackowledgemet The authors ackowledge the fiacial support of Sciece ad Techology of Fegze uder Grat No.2009FZ24 ad 2010FZ02 ad the Haixi Project of Fujia uder Grat No.A099. The authors are grateful to the aoymous referee for a careful checkig of the details ad for helpful commets that improved this paper. Refereces [1] W.W. ROUSE BALL AND H.S.M. COXETER. Mathematical Recreatios ad Essays, Dover Publicatios (1987). [2] R.L. GRAHAM, D.E. KNUTH AND O. PATASHNIK. Cocrete Mathematics, Addiso Wesley (1994). [3] E.L. LLOYD. A O(logm) algorithm for the Josephus problem. Joural of Algorithms 4(3), (1983) 262 270. [4] A. M. ODLYZKO AND H. S. WILF. Fuctioal Iteratio ad the Josephus Problem. Glasgow Math. J. 33, (1991) 235 240. [5] F. RUSKEY AND A. WILLIAMS. The Felie Josephus Problem. Theory of Computig Systems, 50, (2012) 20 34. [6] N. THERIAULT. Geeralizatios of the Josephus problem. Utilitas Mathamatica 58, (2000) 161 173. Natural Scieces Publishig Cor.