A Simple In-Place Algorithm for In-Shuffle
The paper presents a simple, linear time, in-place algorithm for performing a 2-way in-shuffle which can be used with little modification for certain other k-way shuffles.
Authors: Peiyush Jain
A Simple In-Place Algorithm for In-Sh uffle. P eiyush Jain, Microsoft Corp oration. July 2004 1 In tro duction An in-shuffle of a deck of cards is done by cutting the deck in to t wo equal halves and interlea ving them per fectly , with the first c a rd of the second half b eing the first card of the sh uffled deck. In case of 2 n cards, this kind o f a s h uffle gives rise to a p ermutation (called the in-shuffle p ermutation of or der 2 n ) given by: i → 2 i mo d (2 n + 1) , i ∈ { 1 , 2 , . . . , 2 n } In [EM00] it has b een shown that the pro blem of merg ing lists in-place ca n be reduced to p erforming in-shuffles in-place. In the s ame paper , the authors provide a linear time, in-place algorithm for p erforming the in- sh uffle, when the list is represented as an arr a y , indexed 1 to 2 n . In this pap e r we provide a simpler algorithm which per mits an easy implementation. 2 Preliminarie s An y p ermutation is a set o f disjoint cycles. F or e.g. the in-shuffle of 6 elements is comp osed of t wo c y cles: (1 , 2 , 4) and (3 , 6 , 5). It is easy to move the elements of a single cyc le using just an extra lo cation, b y a “cycle leader” algorithm, [FMP95]. This algor ithm pr o ceeds by repea tedly making a space in the list, computing the index o f elemen t tha t b elongs to that space and moving that element , so creating a new space. F or any permutation, we can pro ceed b y applying the cycle leader algor ithm to each cycle in turn in order to re alise the whole per m utation. If, for a p ermu- tation, we can compute th e lo cation of an element o f the ’next’ cycle in constan t time and minimal space, we could r ealise the whole p ermutation using a linea r time and a n in-place alg o rithm. W e show that, w hen 2 n is of the for m 3 k − 1, we can easily determine the cycles of the in-s h uffle p ermutation of or der 2 n . W e will need the following theorem from num b er theor y: Theorem 1 If p is an o dd prime and g is a primitive r o ot of p 2 , then g is a primitive r o ot of p k for any k ≥ 1 . 1 A pro of of this theorem can be found in [Nar0 0, p 20-21 ]. It can b e easily se e n that 2 is a primitive ro ot o f 9. F rom the a bov e theor em it follo ws that 2 is also a primitive ro ot of 3 k for an y k ≥ 1 . This implies that the group ( Z / 3 k ) ∗ is cyclic with 2 b eing its gener ator. Now let us ana lyse the cycles o f an in-shuffle p erm utation when 2 n = 3 k − 1. The cycle containing 1 is nothing but the gro up ( Z/ 3 k ) ∗ , whic h c o nsists of all n umbers relatively prime to 3 k and less than it. Let 1 ≤ s < k . Consider the cycle containing 3 s . Every num ber in this c ycle is o f the form 3 s 2 t ( modulo 3 k ) for 1 ≤ t ≤ ϕ (3 k ) (where ϕ is the Euler- totien t function). Since 2 is a generator of ( Z/ 3 k ) ∗ , this cycle contains exactly the n umbers les s than 3 k which are divis ible b y 3 s but not by any higher pow er of 3. This means that in an in-sh uffle permutation of order 3 k − 1, we ha ve exactly k cycles with 1 , 3 , 3 2 , . . . , 3 k − 1 each belo nging to a differen t cycle. Thus for these per m utations, it becomes easy to pick the ’next’ cycle in o rder to apply the cycle leader algorithm. No te that the length of the cycle containing 3 s is ϕ (3 k ) / 3 s , which helps us implement the cycle leader algorithm mo re efficiently . W e pr esen t the algorithm for the genera l ca se of an in-sh uffle per m utation of o rder 2 n in the next section. 3 Algorithm W e star t with an o bserv ation. Supp ose for each 2 n we ca n ea s ily find a 2 m (= Ω( n ) and ≤ 2 n ) for which we have a linear time, in-pla ce, in-shuffle algorithm, we then ha ve a linear time, in-place, in-s huffle algorithm for 2 n . This is b ecause we can sh uffle the arr a y [ a 1 , a 2 , . . . , a m , a m +1 , . . . , a n , a n +1 , . . . , a n + m , a n + m +1 , . . . , a 2 n ] to lo ok like [ a 1 , a 2 , . . . , a m , a n +1 , . . . , a n + m , a m +1 . . . , a n , a n + m +1 . . . , a 2 n ] in linear time and in-place b y doing a right c y clic shif t of the elements a m +1 , . . . , a n + m b y a distance m . W e can achiev e this b y reversing the sub-array [ m + 1 , . . . , n + m ] follow ed b y a reversal of the sub-ar ra ys [ m + 1 , . . . , n ] and [ n + 1 , . . . , n + m ]. W e can now p erform a n in-shuffle of the first 2 m elements and r ecursiv ely p erform an in-sh uffle of the remaining elements. Thu s we have the following In-shuffle Algorithm: Input: An ar ra y A [1 , . . . , 2 n ] Step 1. Find a 2 m = 3 k − 1 suc h that 3 k ≤ 2 n < 3 k +1 Step 2. Do a rig h t cyclic shift o f A [ m + 1 , . . . , n + m ] by a distance m Step 3. F or eac h i ∈ { 0 , 1 , . . . , k − 1 } , starting at 3 i , do the cycle lea der a lgo- rithm for the in-shuffl e p erm utation of order 2 m Step 4. Recurs iv ely do the in-shuffle alg orithm on A [2 m + 1 , . . . , 2 n ]. 2 4 Space and Time Complexit y Let T ( n ) b e the time taken on an input array o f size n . Let us lo ok at the time and s pace b ounds for each of the s teps. Step 1 ca n b e do ne in-place in O (log n ) time. Step 2 ca n b e do ne in-place in O ( n ) time. Step 3 ca n b e do ne in-place in O ( m ) time. Step 4 is a tail r e c ur siv e call a nd hence can b e implemented in constant space. The time taken by this call is T (2( n − m )). Since m = Ω( n ), it follows that the total time taken by this algorithm is O ( n ). Thu s w e have a linear time, in-place algor ithm for the in-shuffle of a list of 2 n element s. 5 Conclusion W e have describ ed a linear time, in-place, in-sh uffle algor ithm which I b elieve is interesting, simple and easy to implemen t. It is not obvious whether the approach used ( using a p such that k is a primitive r o ot of p 2 ) in this pap er can be generalised for k -way shuffl es, but it is probable t hat w e ca n use this approach for fixed squarefree k , [HEA86]. F or small k , this approach can definitely be used. Ac kno wledgeme n t I would lik e to thank Barukh Ziv for sugg esting that I write up this pap er. I would also lik e to thank K arthik Mahesh for pointing this problem to me and Anand Ganesh for the interesting discussions we had. References [EM00] J. Ellis and M. Markov. In- situ, s ta ble merging by wa y of per fect s h uffle. The Co mputer Journal. 43(1):40-5 3, (20 00). [FMP95] F aith E . Fic h, J. Ian Munr o , and P atricio V. P oblete. Perm uting in place. SIAM Journal on Computing , 24 (2):266-278, 1995 . [HEA86] D. R. Heath-Brown. Artin’s conjecture for primitive ro ots. Quart. J. Math. O xfor d S er. (2) 37 (198 6), no. 145, 27-38 . [Nar00] W. Narkiewicz. The Development of Pri me Numb er The ory: F r om Eu- clid to Har dy and Littlewo o d. Spr inger Mono graphs in Mathematics, New Y or k . 2000 . 3
Original Paper
Loading high-quality paper...
Comments & Academic Discussion
Loading comments...
Leave a Comment