
Efficient Algorithms to Rank and Unrank Permutations in Lexicographic Order Blai Bonet Departamento de Computaci´on Universidad Sim´on Bol´ıvar Caracas, Venezuela [email protected] Abstract The main contribution of this paper is the development of novel uniform algorithms for ranking and unranking of We present uniform and non-uniform algorithms to rank and permutations in lexicographic order that perform O(n log n) unrank permutations in lexicographic order. The uniform arithmetic operations and utilize O(n) space. The algo- O n n algorithms run in ( log ) time and outperform Knuth’s rithms are very simple and easy to implement. We also per- ranking algorithm in all the experiments, and also the linear- time non-lexicographic algorithm of Myrvold-Ruskey for form an empirical comparison against the linear-time algo- permutations up to size 128. The non-uniform algorithms rithm of Myrvold and Ruskey, and the algorithm of Knuth in generalize Korf-Schultze’s linear time algorithm yet require order to account for the hidden constant factors. As it will much less space. be seen, the new algorithms dominate Knuth’s algorithm, are faster than Myrvold and Ruskey’s for small n, and fall a bit short of the latter for n up to size 1,024. Introduction We also present a generalization of Korf-Schultze’s non- In an increasing number of different applications, most of uniform linear-time ranking algorithm that permits to reduce them related to heuristic search and combinatorial optimiza- the size of the advice. tion (Korf & Schultze 2005; Ruskey, Jiang, & Weston 1995; The paper is organized as follows. The next two sections Critani, Dall’Aglio, & Biase 1997), there is the need to are devoted to the uniform and non-uniform algorithms. rank a given permutation over n elements into an integer Then, we present some empirical results, and conclude. between 0 and n! 1, and also to unrank such integer into a permutation.− In others cases, one is interested in Uniform Algorithms a particular subset of elements, and given a permutation Permutations of size n are assumed to be over inte- over n elements, rank/unrank the positions of these ele- gers 0, . , n 1 , and denoted by π = π0 . πn 1. ments (Culberson & Schaeffer 1998; Korf & Felner 2002; For example,{ π− =} 25714603 denotes the permutation− Felner, Korf, & Hanan 2004). A ranking function is called 0 1 2 3 4 5 6 7 in which 0 is mapped into lexicographic if it maps a permutation and its lexicographi- 2 5 7 1 4 6 0 3 cally next permutation into consecutive integers. 2, 1 is mapped into 5, and so on. The lexicographic ranking There are well-known algorithms for these tasks that per- of π is defined as form O(n) arithmetic operations but not in lexicographic r(π) = d0 (n 1)!+d1 (n 2)!+ +dn 2 1!+dn 1 0! order (Myrvold & Ruskey 2001). For lexicographic rank- · − · − ··· − · − · ing/unranking, there are O(n log n) algorithms that rely on (1) modular arithmetic based on inversion tables (Knuth 1973, where di is the relative position of element i with respect to Ex. 6, p. 19), and also unrank algorithms based on binary the elements j < i. For π = 25714603, d0 = 2 since 0 search. Myrvold and Ruskey (Myrvold & Ruskey 2001) is goes into position 2, d1 = 4 since 1 goes into position mention that using a data structure of Dietz (1989), the num- 4 once 0 is fixed at position 2, and so on. In general, di equals minus the number of elements that are to the ber of operations can be reduce to O(n log n/ log log n) but πi j > i left of element i. Each d ranges over 0, . , n 1 i the algorithm is rather complicated and difficult to imple- i { − − } independently of the others. The vector (d0, . , dn 1) is ment. All these algorithms are uniform in the sense that they − work for permutations of any size without the need to pre- known as the factorial representation of r(π) as they are the process or store additional information (advice). digits in a factorial-base system and also as the inversion table for (Knuth 1973). Non-uniform linear-time algorithms for ranking and un- π Another equivalent expression for is ranking in lexicographic order were presented in (Korf & r(π) Schultze 2005), but these need to pre-calculate and store an r(π) = ( (d0 (n 1)+d1) (n 2)+ +dn 2) 1+dn 1 . advice of exponential size. Applications that need to per- ··· · − · − ··· − · −(2) form a large number of rank/unrank operations of small size, Both expressions, (1) and (2), are closely related to the way e.g. n = 16, can amortize the time to calculate the advice. tuples over Cartesian products are ranked into unique inte- 18 1 2 3 4 1 0 1 1 1 2 2 2 0 1 0 0 0 1 1 0 0 1 1 1 1 1 1 1 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 1 0 0 1 0 1 0 1 1 0 0 1 0 1 0 1 2 3 4 5 6 7 0 1 3 4 5 6 7 0 1 3 4 6 7 0 1 3 4 6 (a) π0 = 2, d0 = 2 (b) π1 = 5, d1 = 4 (c) π2 = 7, d2 = 5 (d) π3 = 1, d3 = 1 5 6 7 8 2 3 2 4 3 4 4 4 1 1 2 1 1 1 2 2 2 1 2 2 2 2 2 2 0 1 1 0 1 1 0 1 0 1 1 0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 3 4 6 0 3 6 0 3 3 (e) π4 = 4, d4 = 2 (f) π5 = 6, d5 = 2 (g) π6 = 0, d6 = 0 (h) π7 = 3, d7 = 0 Figure 1: the ranking algorithm applied to permutation π = 25714603. Panels (a)–(h) show the paths transversed, the final configuration of the tree after transversing each path, and the calculated value for each di. n gers. The pair (x, y) X Y is ranked into x Y + y as Input: permutation π and array T of size 21+⌈log ⌉ − 1 for each x there are Y∈ possibilities× for y. For| the| triplet Output: rank of π (x, y, z) X Y |Z,| we can either think of it as the pair begin (x, (y, z))∈and× rank× it into x Y Z + (y Z + z), or think of =k : ⌈log n⌉ it as the pair ((x, y), z) and| rank|| it| into|(x|Y + y) Z + z. rank := 0 | | | | for i to 1+k do T i Both expression are equivalent. As each factorial digit di = 1 2 − 1 [ ] := 0 ranges over n i possible values, the vector of factorial dig- for i = 1 to n do − =ctr : π[i] its is ranked into (1) (or equivalently (2)). Once the vector k of factorial digits is known, the rank of the permutation is node := 2 + π[i] j k obtained in linear time. Therefore, the difficult task is to for = 1 to do if node is odd then ctr := ctr−T [(node ≫ 1) ≪ 1] compute the digits efficiently. T [node] := T [node] + 1 node := node ≫ 1 Ranking T [node] := T [node] + 1 The naive approach to compute the factorial digits is to scan rank := rank · (n + 1 − i) + ctr the permutation from left to right counting the number of return rank elements to the left of i that are less than i, and then setting end di to πi minus such count. This method requires linear time per digit which results in an O(n2) algorithm. Figure 2: O(n log n) uniform lexicographic ranking. However, if at the time of computing di, πi > n/2 and the number of elements less than i in positions 0⌈ to ⌉n/2 is stored in a counter, then only positions between n/⌈2 +⌉ value stored at the left child of the parent, and 2) increase ⌈ ⌉ the value stored at the current node. At the end of the jour- 1 and πi 1 must be scanned. If we apply this principle recursively,− dividing by half successively the interval [0, n ney, the value of the counter is the value of di. − 1], the time to compute di is reduced to O(log n). This is the For example, for π = 25714603, the path transversed for underlying principle in the algorithm that is explained next. d0 starts at the leaf associated with π0 = 2 (the third leaf Let us assume for the moment that n = 2k isapowerof2. from the left). Since all initial stored values equal zero then In order to store the required counts, we make use of a com- d0 = 2. The resulting tree and the path transversed for d0 plete binary tree of height k whose nodes store the counts, are shown in Fig. 1(a). For d1, the counter is initialized to and where the n leaves are associated with the elements of π1 = 5 and the path starts at the leaf associated to 5. This the permutation. Initially, all stored values equal zero. leaf is a right child but its left sibling has a stored value of To calculate di, the algorithm transverses the tree in a 0 so the counter does not change. The next node is a left bottom-up manner. Starting at the leaf associated with πi child and so the counter is not decremented.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages6 Page
-
File Size-