MergeShuffle: a very fast, parallel random permutation algorithm

2015 
This article introduces an algorithm, MERGESHUFFLE, which is an extremely efficient algorithm to generate random permutations (or to randomly permute an existing array). It is easy to implement, runs in nlog2n + O(1) time, is in-place, uses nlog2n + Θ(n) random bits, and can be parallelized accross any number of processes, in a shared-memory PRAM model. Finally, our preliminary simulations using OpenMP1 suggest it is more efficient 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 shuffled. While this algorithm is of lesser practical interest, we believe it may be of theoretical value. 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. The well-known Fisher-Yates shuffle [11, 10] 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 efficiency and simplicity have until now stood the test of time. But there have been two trends in trying to improve this algorithm: first, initially the algorithm assumes some source of randomness that allows for discrete uniform variables, but this 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. The random-bit model. Much research has gone into simulating probability distributions, with most algorithms designed using infinitely precise continuous uniform random variables (see [8, 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 fixed ∗Electronic address: axel.bacher@lipn.univ-paris13.fr †Electronic address: olivier.bodini@lipn.univ-paris13.fr ‡Electronic address: halexandros@web.de §Electronic address: lumbroso@cs.princeton.edu; corresponding author Full code available at: https://github.com/axel-bacher/mergeshuffle 1 ar X iv :1 50 8. 03 16 7v 1 [ cs .D S] 1 3 A ug 2 01 5 Algorithm 1 The classical Fisher-Yates shuffle [11] to generate random permutations, as per Durstenfeld [10]. 1: procedure FISHERYATESSHUFFLE(T ) 2: for i = n− 1 to 0 do 3: j ← random integer from {0, . . . , i} 4: SWAP(T , i, j) 5: end for 6: end procedure precision, two questions arise: when are they not precise enough? when are they too precise? These are questions which are usually ignored in typical fixed-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. This random bit model was first suggested by Von Neumann [26], who humorously objected to the use of fixed-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 (finite or infinite algorithms? exponential or heavy-tailed time distribution?). Within the context of this article, we will focus on designing practical algorithms using unbiased random bits. In 1976, Knuth and Yao [18] 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 infinite tree—infinite not only in the sense that the algorithm terminates with probability 1 (an unavoidable fact for any probability that does not have a finite binary expansion), but also in the sense that the description of the tree is infinite and requires an infinite precision arithmetic to calculate the binary expansion of the probabilities. In 1997, Han and Hoshi [17] 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 [9]. We were introduced to this problematic through the work of Flajolet, Pelletier and Soria [12] on Buffon machines, which are a framework of probabilistic algorithms allowing to simulate a wide range of probabilities using only a source of random bits. One easy optimization of the Fisher-Yates algorithm (which we use in our simulations) is to use an recently discovered optimal way of drawing discrete uniform variables [19]. Prior Work in Parallelization. There has been in particular a great deal of interest in finding efficient parallel algorithms to randomly generate permutations, in various many contexts of parallelization, some theoretical and some practical [14, 15, 23, 16, 1, 5, 6, 2]. Most recently, Shun et al. [24] wrote an enlightening article, in which they looked at the intrinsic pa-
    • Correction
    • Source
    • Cite
    • Save
    • Machine Reading By IdeaReader
    27
    References
    5
    Citations
    NaN
    KQI
    []