<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.0 20120330//EN" "JATS-archivearticle1.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink">
  <front>
    <journal-meta />
    <article-meta>
      <title-group>
        <article-title>MergeShu e: a very fast, parallel random permutation algorithm</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Axel Bacher</string-name>
          <email>bacher@lipn.univ-paris13.fr</email>
          <xref ref-type="aff" rid="aff1">1</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Olivier Bodini</string-name>
          <email>bodini@lipn.univ-paris13.fr</email>
          <xref ref-type="aff" rid="aff1">1</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Alexandros Hollender</string-name>
          <xref ref-type="aff" rid="aff1">1</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Jeremie Lumbroso</string-name>
          <email>lumbroso@cs.princeton.edu</email>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>Princeton University</institution>
        </aff>
        <aff id="aff1">
          <label>1</label>
          <institution>Universite Paris 13</institution>
        </aff>
      </contrib-group>
      <fpage>43</fpage>
      <lpage>52</lpage>
      <abstract>
        <p>This article introduces an algorithm, MergeShuffle, which is an extremely e cient algorithm to generate random permutations (or to randomly permute an existing array). It is easy to implement, runs in n log2 n + O(1) time, is in-place, uses n log2 n + (n) random bits, and can be parallelized across any number of processes, in a shared-memory PRAM model. Finally, our preliminary simulations using OpenMP1 suggest it is more e cient than the Rao-Sandelius algorithm, one of the fastest existing random permutation algorithms. We also show how it is possible to further reduce the number of random bits consumed, by introducing a second algorithm BalancedShuffle, a variant of the Rao-Sandelius algorithm which is more conservative in the way it recursively partitions arrays to be shu ed. While this algorithm is of lesser practical interest, we believe it may be of theoretical value.</p>
      </abstract>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>Introduction</title>
      <p>Random permutations are a basic combinatorial object, which are useful in their own right for a lot of applications,
but also are usually the starting point in the generation of other combinatorial objects, notably through bijections.</p>
      <p>The well-known Fisher-Yates shu e [FY48, Dur64] iterates through a sequence from the end to the beginning
(or the other way) and for each location i, it swaps the value at i with the value at a random target location j at
or before i. This algorithm requires very few steps|indeed a random integer and a swap at each iteration|and
so its e ciency and simplicity have until now stood the test of time.</p>
      <p>There have been two trends in trying to improve this algorithm. First, the algotithm initially assumed some
source of randomness that allows for discrete uniform variables, but there has been a shift towards measuring
randomness better with the random bit model. Second, with the avent of large core clusters and GPUs, there is
an interest in making parallel versions of this algorithm.</p>
      <p>Copyright c by the paper's authors. Copying permitted for private and academic purposes.
Algorithm 1 The classical Fisher-Yates shu e [FY48] to generate random permutations, as per
Durstenfeld [Dur64].</p>
      <p>1: procedure FisherYatesShuffle(T )
2: for i = n 1 to 0 do
3: j random integer from f0; : : : ; ig
4: Swap(T , i, j)
5: end for</p>
      <sec id="sec-1-1">
        <title>6: end procedure</title>
      </sec>
      <sec id="sec-1-2">
        <title>The random-bit model.</title>
        <p>Much research has gone into simulating probability distributions, with most algorithms designed using in nitely
precise continuous uniform random variables (see [Dev86, II.3.7]). But because (pseudo-)randomness on
computers is typically provided as 32-bit integers|and even bypassing issues of true randomness and bias|this model
is questionable. Indeed as these integers have a xed precision, two questions arise: when are they not precise
enough? when are they too precise? These are questions which are usually ignored in typical xed-precision
implementations of the aforementioned algorithms. And it suggests the usefulness of a model where the unit of
randomness is not the uniform random variable, but the random bit.</p>
        <p>This random bit model was rst suggested by Von Neumann [Neu51], who humorously objected to the use
of xed-precision pseudo-random uniform variates in conjunction with transcendant functions approximated by
truncated series. His remarks and algorithms spurred a fruitful line of theoretical research seeking to determine
which probabilities can be simulated using only random bits (unbiased or biased? with known or unknown bias?),
with which complexity (expected number of bits used?), and which guarantees ( nite or in nite algorithms?
exponential or heavy-tailed time distribution?). Within the context of this article, we will focus on designing
practical algorithms using unbiased random bits.</p>
        <p>In 1976, Knuth and Yao [KY76] provided a rigorous theoretical framework, which described generic optimal
algorithms able to simulate any distribution. These algorithms were generally not practically usable: their
description was made as an in nite tree|in nite not only in the sense that the algorithm terminates with
probability 1 (an unavoidable fact for any probability that does not have a nite binary expansion), but also in
the sense that the description of the tree is in nite and requires an in nite precision arithmetic to calculate the
binary expansion of the probabilities.</p>
        <p>In 1997, Han and Hoshi [HH97] provided the interval algorithm, which can be seen as both a generalization and
implementation of Knuth and Yao's model. Using a random bit stream, this algorithm amounts to simulating a
probability p by doing a binary search in the unit interval: splitting the main interval into two equal subintervals
and recurse into the subinterval which contains p. This approach naturally extends to splitting the interval
in more than two subintervals, not necessarily equal. Unlike Knuth and Yao's model, the interval algorithm
is a concrete algorithm which can be readily programmed... as long as you have access to arbitrary precision
arithmetic (since the interval can be split to arbitrarily small sizes). This work has recently been extended and
generalized by Devroye and Gravel [DG15].</p>
        <p>We were introduced to this problematic through the work of Flajolet, Pelletier and Soria [FPS11] on Bu on
machines, which are a framework of probabilistic algorithms allowing to simulate a wide range of probabilities
using only a source of random bits.</p>
        <p>One easy optimization of the Fisher-Yates algorithm (which we use in our simulations) is to use a recently
discovered optimal way of drawing discrete uniform variables [Lum13].</p>
      </sec>
      <sec id="sec-1-3">
        <title>Prior Work in Parallelization.</title>
        <p>There has been a great deal of interest in nding e cient parallel algorithms to randomly generate permutations,
in various many contexts of parallelization, some theoretical and some practical [Gus03, Gus08, San98, Hag91,
AS96, CB05, CKKL98, And90].</p>
        <p>Most recently, Shun et al.[SGBFG15] wrote an enlightening article, in which they looked at the intrinsic
parallelism inherent in classical sequential algorithms, and these can be broken down into independent parts which
may be executed separately. One of the algorithms they studied is the Fisher-Yates shu e. They considered
the insertion of each element of the algorithm as a separate part, and showed that the dependency graph, which
provides the order in which the parts must be executed, is a random binary search tree, and as such, is well
known to have on average a logarithmic height [Dev86]. This allowed them to show that the algorithm could be
distributed on n= log n processors, by using linear auxiliary space to track the dependencies.</p>
        <p>Our contribution takes a di erent direction to provide another algorithm with similar guarantees. Our
algorithm is completely in-place, and can be parallelized to give a log n speedup given enough processors. It runs,
in practice, extremely fast even when run sequentially (presumably due to better cache performance compared
with Fisher-Yates). We hope, in future work, to paralellize it further to approach the performance of Shun et
al.'s algorithm.</p>
        <p>Relatively recently, Flajolet et al. [FPS11] formulated an elegant random permutation algorithm which uses only
random bits, using the trie data structure, which models a splitting process: associate to each element of a set
x 2 S an in nite random binary word wx, and then insert the key-value pairs (wx; x) into the trie; the ordering
provided by the leaves is then a random permutation.</p>
        <p>This general concept is elegant, and it is optimized in two ways:
the binary words thus do not need to be in nite, but only long enough to completely distinguish the elements;
the binary words do not need to be drawn a priori, but may be drawn one bit (at each level of the trie) at
a time, until each element is in a leaf of its own.</p>
        <p>This algorithm turns out to have been already exposed in some form in the early 60's, independently by
Rao [Rao61] and by Sandelius [San62]. Their generalization extends to the case where we split the set into
R subsets (and where we would then draw random integers instead of random bits), but in practice the case
R = 2 is the most e cient. The interest of this algorithm is that it is, as far as we know, the rst example of a
random permutation algorithm which was written to be parallelized.
2</p>
      </sec>
    </sec>
    <sec id="sec-2">
      <title>The MergeShu e algorithm</title>
      <p>The new algorithm which is the central focus of this paper was designed by progressively optimizing a
splittingtype idea for generating random permutation which we discovered in Flajolet et al.[FPS11]. The resulting
algorithm closely mimics the structure and behavior of the beloved MergeSort algorithm. It gets the same
guarantees as this sorting algorithm, in particular with respect to running time and being in-place.</p>
      <p>To optimize the execution of this algorithm, we also set a cut-o threshold, a size below which permutations
are shu ed using the Fisher-Yates shu e instead of increasingly smaller recursive calls. This is an optimization
similar in spirit to that of MergeSort, in which an auxiliary sorting algorithm is used on small instances.
2.1</p>
      <sec id="sec-2-1">
        <title>In-Place Shu ed Merging</title>
        <p>The following algorithm is the linchpin of the MergeShu e algorithm. It is a procedure that takes two arrays (or
rather, two adjacent ranges of an array T ), both of which are assumed to be randomly shu ed, and produces a
shu ed union.</p>
        <p>Importantly, this algorithm uses very few bits. Assuming a two equal-sized sub-arrays of size k each, the
algorithm requires 2k + (pk log k) random bits, and is extremely e cient in time because it requires no auxiliary
space.</p>
        <p>Lemma 2.1. Let A and B be two randomly shu ed arrays, respectively of sizes n1 and n2. Then the procedure
Merge produces a randomly shu ed union C of these arrays, of size n = n1 + n2.
Proof. Since A and B are assumed to be initially randomly shu ed, we may assume that they consist of identical
symbols, say a's and b's respectively. It su ces to prove that, after the execution of the procedure, all words
with n1 occurrences of a and n2 of b appear with the same probability. We may reinterpret the procedure as rst
randomly drawing a's and b's by ipping coins, stopping when we would write the n1 + 1-st a or the n2 + 1-st
b, and then writing down the missing b's or a's and using the Fisher-Yates shu e to swap them into random
locations.</p>
        <p>The correction of the procedure is based on the following fact: after the execution of the rst loop (lines
5{14), the elements of T from 0 to i 1 are randomly shu ed. Indeed, these elements consist either of n1 a's
and n2 + i n b's (if the array A was depleted rst) and n1 + i n a's and n2 b's (if B was depleted rst), which
can appear in every permutation with equal probability.</p>
        <p>The rest of the proof then mimics that of the Fisher-Yates shu e, based on the loop invariant: after every
iteration of the second loop (lines 15{19), the rst i elements of T are randomly shu ed. This shows that the
whole array is randomly shu ed after the procedure.</p>
        <p>Lemma 2.2. Assuming that jn1 n2j = O(pn), the procedure Merge produces a shu ed array C of size n
using n + (pn log n) random bits on average.</p>
        <p>In practice, it is always possible to set up the arrays so that jn1
conditions of this result.
n2j
1, which more that satis es the
Proof. There are two places where the procedure consumes randomness: in the rst loop at line 6 (one bit per
element) and in the second loop at line 16 (on average, (log n) bits per element since we need to draw a random
integer). The number of iterations of the second loop is equal to the number of elements remaining after one of
the arrays A or B is depleted. If n1 and n2 are not too far apart (at most on the order of pn), this number will
have an expected value of (pn). This gives the result.
2.2</p>
      </sec>
      <sec id="sec-2-2">
        <title>Average number of random bits of MergeShu e</title>
        <p>We now give an estimate of the average number of random bits used by our algorithm to sample a random
permutation of size n. Let cost(k) denote the average number of random bits used by a merge operation with
an output of size k. For the sake of simplicity, assume that we sample a random permutation of size n = 2m.
The average number of random bits used is then
i=1
We have seen that cost(k) = k + (pk log k). Thus, the average number of random bits used to sample a random
permutation of size n = 2m is
which nally yields</p>
        <p>Algorithm 1: d
3</p>
        <p>BalancAeldgSohruith me 2: d
For theoretical value, we also present a second algorithm, which introduces an optimization which we believe has
some worth. Algorithm 3: d</p>
        <p>Algorithm 4: The BalancedShuffle algorithm.</p>
        <p>Input: an array T
Result: T is randomly shuffled
Main Function BalancedShuffle(T)
n = length(T)
if n &gt; 1 then</p>
        <p>BalancedShuffle(T[0: n2 ])
BalancedShuffle(T[ n2 :n])</p>
        <p>BalancedMerge(T)
end
Procedure BalancedMerge(T)
n = length(T)
w = uniformly sampled random balanced word of size n
i = 0
j = n/2
for k = 0 to n 1 do
if w[k] = 1 then
swap elements at positions i and j in T
j = j + 1
end
i = i + 1
end
3.1</p>
      </sec>
      <sec id="sec-2-3">
        <title>Balanced Word</title>
        <p>Inspired by Remy [Rem85]'s now classical and e cient algorithm to generate a random binary tree of exact size
from the repeated drawing of random integers, Bacher et al. [BBJ14] produced a more e cient version that
uses, on average 2k + ((log k)2). Binary trees, which are enumerated by the Catalan numbers [Sta15], are in
bijection with Dyck words, which are balanced words containing as many 0's as 1's. So Bacher et al.'s random
tree generation algorithm can be used to produce a balanced word of size 2k using very few extra bits.</p>
      </sec>
      <sec id="sec-2-4">
        <title>Rationale.</title>
        <p>The idea behind using a balanced word is that it is more e cient, in average number of bits.</p>
        <p>Indeed, splitting processes (repeatedly randomly partition n elements until each is in its own partition), are
well known to require n log2 n + O(n) bits on average|this is the path length of a random trie [FS09]. The
linear term comes from the fact that when processes are partitioned in two subsets, these subsets are not of equal
size (which would be the optimal case), but can be very unbalanced; furthermore, with small probability, it is
possible that all elements remain in the same set, especially in the lower levels.</p>
        <p>On the other hand, if we are able to partition the elements into two equal-sized subsets, we should be able to
circumvent this issue. This idea is useful here, and we believe, would be useful in other contexts as well.</p>
      </sec>
      <sec id="sec-2-5">
        <title>Disadvantages. 3.2</title>
      </sec>
      <sec id="sec-2-6">
        <title>Correctness</title>
        <p>The advantage is that using balanced words allows to for a more e cient and sparing use of random bits (and
since random bits cost time to generate, this eventually translates to savings in running time). However this
requires a linear amount of auxiliary space; for this reason, our BalancedShu ed algorithm is generally slower
than the other, in-place algorithms.</p>
        <p>Denote by Sn the symmetric group containing all permutations of size n. Let C(n; k) be the set of all words of
length n on the alphabet f0; 1g containing k 0's and n k 1's. We have jSnj = n! and C(n; k) = nk .</p>
        <p>The algorithm works at follows: to shu e a list of length m + n, we rst shu e recursively the rst m and the
last n elements (we sample an element of Sm and one of Sn independently), then we use Bacher et al.'s algorithm
to sample an element of C(m + n; m). We combine those three elements to produce an element of Sm+n. Since
this combination is bijective, the correctness follows by induction.
3.3</p>
      </sec>
      <sec id="sec-2-7">
        <title>Average number of random bits</title>
        <p>We now give an estimate of the average number of random bits used by our algorithm to sample a random
permutation of size n. Let cost(2k) denote the average number of random bits used to sample a random
balanced word of length 2k (an element of C(2k; k)). For the sake of simplicity, assume that we sample a random
permutation of size n = 2m. The average number of random bits used is then</p>
        <p>For the algorithm we have cost(2k) = 2k + (log2 k) [BBJ14]. Thus, the average number of random bits used
to sample a random permutation of size n = 2m is
The simulations were run on a computing cluster with 40 cores. The algorithms were implemented in C, and their
parallel versions were implemented using the OpenMP library, and delegating the distribution of the threading
entirely to it.
100  
80  
60  
40  
20  
0  
MergeShuffle  SEQ  
MergeShuffle  PAR  
R.-­‐S.  SEQ  
R.-­‐S.  PAR  
Fisher-­‐Yates  
n
Fisher-Yates
MergeShuffle
Rao-Sandelius
BalancedShuffle
1 631 434
1 636 560
1 631 519
1 889 034
19 550 941
19 686 051
19 550 449
22 046 574
2 628 248 831
2 650 387 993
2 628 251 036</p>
      </sec>
      <sec id="sec-2-8">
        <title>Acknowledgements</title>
        <p>This research was partially supported by the ANR MetACOnc project ANR-15-CE40-0014.
[AS96] L. Alonso and R. Schott. A parallel algorithm for the generation of a permutation and applications.</p>
        <p>Theoretical Computer Science, 159(1):15{28, 1996.
[And90] R. Anderson. Parallel algorithms for generating random permutations on a shared memory machine.</p>
        <p>In: Proceedings of the Second Annual ACM Symposium on Parallel Algorithms and Architectures, SPAA
'90, pp. 95{102, New York, NY, USA, 1990. ACM.
[BBJ14] A. Bacher, O. Bodini and A. Jacquot. E cient random sampling of binary and unary-binary trees via
holonomic equations. At https://arxiv.org/abs/1401.1140, 2014.
[Cha06] R. Chandra. Parallel programming in OpenMP. Morgan Kaufmann, 2001.
[CB05] G. Cong and D. A Bader. An empirical analysis of parallel random permutation algorithms on SMPs. In:
Proc. 18th ISCA International Conference on Parallel and Distributed Computing Systems (PDCS 2005),
Las Vegas, NV, 2005.
[CKKL98] A. Czumaj, P. Kanarek, M. Kutylowski and K. Lorys. Fast generation of random permutations via
networks simulation. Algorithmica, 21(1):2{20, 1998.
[DE98] L. Dagum and R. Enon. Openmp: an industry standard api for shared-memory programming. IEEE</p>
        <p>Computational Science and Engineering, 5(1):46{55, 1998.
[Dev86] L. Devroye. Non-Uniform Random Variate Generation. Springer-Verlag, New York, 1986.
[DG15] L. Devroye and C. Gravel. Sampling with arbitrary precision. At https://arxiv.org/abs/1502.02539,
2015.
[Dur64] R. Durstenfeld. Algorithm 235: Random permutation. Communications of the ACM, 7(7):420, 1964.
[FY48] R. A. Fisher and F. Yates. Statistical Tables for Biological, Agricultural and Medical Research. 3rd</p>
        <p>Edition. London : Oliver and Boyd, 1948.
[FPS11] P. Flajolet, M. Pelletier and M. Soria. On Bu on Machines and Numbers. In: D. Randall (Ed.):
Proceedings of the Twenty-Second Annual ACM-SIAM Symposium on Discrete Algorithms, SODA 2011,
pp. 172{183. SIAM, 2011.
[FS09] P. Flajolet and R. Sedgewick. Analytic Combinatorics. Cambridge University Press, 2009.
[Gus03] J. Gustedt. Randomized permutations in a coarse grained parallel environment. In: Proceedings of the
Fifteenth Annual ACM Symposium on Parallel Algorithms and Architectures, SPAA '03, pp. 248{249, New
York, NY, USA, 2003. ACM.
[Gus08] J. Gustedt. Engineering parallel in-place random generation of integer permutations. In: Proceedings
of the 7th International Conference on Experimental Algorithms, WEA'08, pp. 129{141, Berlin, Heidelberg,
2008. Springer-Verlag.
[Hag91] T. Hagerup. Fast parallel generation of random permutations. In: Proceedings of the 18th International
Colloquium on Automata, Languages and Programming, pp. 405{416, New York, NY, USA, 1991.
SpringerVerlag New York, Inc.
[HH97] T. S. Han and M. Hoshi. Interval Algorithm for Random Number Generation. IEEE Transactions on</p>
        <p>Information Theory, 43(2):599{611, 1997.
[KY76] D. E. Knuth and A. C. Yao. The complexity of nonuniform random number generation. Algorithms and</p>
        <p>Complexity: New Directions and Recent Results, pp. 357{428, 1976.
[Lum13] J. Lumbroso. Optimal discrete uniform generation from coin
https://arxiv.org/abs/1304.1916, 2013.
ips, and applications.</p>
        <p>At
[Rao61] C. R. Rao. Generation of random permutation of given number of elements using random sampling
numbers. Sankhya A, 23:305{307, 1961.
[Rem85] J.-L. Remy. Un procede iteratif de denombrement d'arbres binaires et son application a leur generation
aleatoire. RAIRO - Theoretical Informatics and Applications, 19(2):179{195, 1985.
[San62] M. Sandelius. A simple randomization procedure. Journal of the Royal Statistical Society. Series B
(Methodological), 24(2):472{481, 1962.
[San98] P. Sanders. Random permutations on distributed, external and hierarchical memory. Information</p>
        <p>Processing Letters, 67(6):305{309, 1998.
[SGBFG15] J. Shun, Y. Gu, G. E. Blelloch, J. T. Fineman and P. B. Gibbons. Sequential random permutation,
list contraction and tree contraction are highly parallel. In: Proceedings of the Twenty-Sixth Annual
ACMSIAM Symposium on Discrete Algorithms, SODA '15, pp. 431{448. SIAM, 2015.
[Sta15] R. P. Stanley. Catalan Numbers. Cambridge University Press, 2015.
[Neu51] J. von Neumann. Various techniques used in connection with random digits. Applied Mathematics
Series, 12:36{38, 1951.</p>
      </sec>
    </sec>
    <sec id="sec-3">
      <title>A Code Listing for the MergeSort algorithm</title>
      <p>We reproduce here the most part of our algorithm, with some OpenMP [DE98, Cha06] hints. The full code can
be obtained at https://github.com/axel-bacher/mergeshuffle
A.1 The merge procedure
// merge together two lists of size m and n-m
void merge ( unsigned int *t, unsigned int m, unsigned int n) {
unsigned int *u = t;
unsigned int *v = t + m;
unsigned int *w = t + n;
// randomly take elements of the first and second list according to flips
while (1) {
if( random_bit ()) {
if(v == w) break ;
swap (u, v ++);
} else</p>
      <p>if(u == v) break ;
u ++;
// now one list is exhausted , use Fisher - Yates to finish merging
while (u &lt; w) {
unsigned int i = random_int (u - t + 1);
swap (t + i, u ++);
}</p>
      <p>}
A.2 The MergeSort algorithm itself
extern unsigned long cutoff ;
void shuffle ( unsigned int *t, unsigned int n) {
// select q = 2^c such that n/q &lt;= cutoff
unsigned int c = 0;
while ((n &gt;&gt; c) &gt; cutoff ) c ++;
unsigned int q = 1 &lt;&lt; c;
unsigned long nn = n;
// divide the input in q chunks , use Fisher - Yates to shuffle them
# pragma omp parallel for
for ( unsigned int i = 0; i &lt; q; i ++) {
unsigned long j = nn * i &gt;&gt; c;
unsigned long k = nn * (i +1) &gt;&gt; c;
fisher_yates (t + j, k - j);</p>
    </sec>
  </body>
  <back>
    <ref-list />
  </back>
</article>