Discrete Applied Mathematics 166 (2014) 164–169
Contents lists available at ScienceDirect
Discrete Applied Mathematics journal homepage: www.elsevier.com/locate/dam
Lexicographic ranking and unranking of derangements in cycle notation Kenji Mikawa a,∗ , Ken Tanaka b a
Center for Academic Information Service, Niigata University, 8050 Ikarashi 2-no-cho, Nishi-ku, Niigata 950-2181, Japan
b
Faculty of Science, Kanagawa University, 2946 Tsuchiya, Hiratsuka, Kanagawa 259-1293, Japan
article
info
Article history: Received 25 June 2012 Received in revised form 4 September 2013 Accepted 1 October 2013 Available online 21 October 2013
abstract We present lexicographic ranking and unranking algorithms for derangements expressed in cycle notation. These algorithms run in O(n log n) time, require O(n) space, and use O(n) arithmetic operations. Similar algorithms that require less than or equal to O(n log n) time with O(n) space complexity have not previously been proposed. © 2013 Elsevier B.V. All rights reserved.
Keywords: Combinatorial problem Algorithm Derangements Ranking Unranking
1. Introduction A derangement over n integers [n] = {0, 1, . . . , n − 1} is defined as a permutation δ = δ0 δ1 · · · δn−1 of the integers with no fixed points, i.e., δi ̸= i for all i ∈ [n]. The exercise of counting all derangements is a typical example of the inclusion–exclusion principle. Ranking and unranking algorithms are often used in performance evaluation of computer systems and algorithms, or test pattern generation. Applications, such as the optimal stopping problem [4], security protocols [7,8], and the network distribution problem [3,12], where each component is not mapped to itself must require derangements. The number of derangements is given by the recurrence relation, 0 1 (n − 1) (dn−1 + dn−2 )
dn =
if n = 1, if n = 2, if n ≥ 3.
(1)
The ranking function for a set of permutations S is a bijection from S to {0, 1, . . . , |S | − 1}; the unranking function is its inverse. There are some well-known algorithms for ranking and unranking permutations over [n]. For lexicographic ranking and unranking, there are O(n log n) time algorithms that rely on modular arithmetic based on inversion tables and binary search with complete binary trees [5,2]. Myrvold and Ruskey were the first to propose linear time and space algorithms for this problem, but without lexicographic order [10]. The stringent condition that derangements have no fixed points complicates the development of ranking and unranking algorithms not only for lexicographic order, but also for some particular order. We are not aware of any algorithms that run in less than or equal to O(n log n) time and requiring only O(n) space for any order of derangements.
∗
Corresponding author. Tel.: +81 252626113; fax: +81 252626113. E-mail addresses:
[email protected] (K. Mikawa),
[email protected] (K. Tanaka).
0166-218X/$ – see front matter © 2013 Elsevier B.V. All rights reserved. http://dx.doi.org/10.1016/j.dam.2013.10.001
K. Mikawa, K. Tanaka / Discrete Applied Mathematics 166 (2014) 164–169
165
Recently, several papers have been published on enumerating all derangements in constant time to go from one derangement to the next [1,6,9]. By applying the data structure proposed in [9] to our algorithms, we expect to realize O(n log n) time with O(n) space algorithms for lexicographic ranking and unranking, or linear time and space algorithms for some particular order. In this paper, we propose O(n log n) time with O(n) space algorithms for lexicographic ranking and unranking of derangements in cycle notation. Traditionally, the time complexity of ranking and unranking is estimated on a computational model that can perform arithmetic operations in constant time. We discuss this problem using the same computational model in line with other published papers. 2. Cycle notation and its inversion The lexicographic ranking for the set of permutations of size n is an order preserving ranking function p for this set, that is, π precedes lexicographically τ , if and only if p(π ) < p(τ ). The ranking function p for π = π0 π1 · · · πn−1 is computed by n−1
p(π ) =
vi · (n − i − 1)!
(2)
i=0
with its inversion vector v = v0 v1 · · · vn−1 , where vi is the number of entries πj such that i < j and πi > πj . Once the inversion vector has been computed, p(π ) is obtained in linear time from Eq. (2). Therefore, many researchers have struggled to reduce the time complexity of computing the inversion vector. Whereas naive implementations require O(n2 ) time to compute an inversion vector, the time complexity can be improved to O(n log n) time using a binary search tree or a merge sort [5,2]. The cycle notation is an intuitive notation for the order of a permutation that gives a mapping from [n] to [n] as a list of disjoint cycles. Stanley introduced the standard representation for this notation and some properties were detailed in [11]. A permutation π is decomposed into one or several disjoint cycles. For example, the following permutation is decomposed into three cycles, (04), (165), and (23).
π=
0 4
1 6
2 3
3 2
4 0
5 1
6 . 5
(3)
The order of the elements in a cycle does not matter as long as the elements rotate their position while maintaining the order of the permutation. For example, the three cycles (165), (651), and (516) are equivalent. The order of the cycles does not matter either, since they are pairwise disjoint. According to the standard representation, but with some differences where our algorithm focuses on the smallest elements, the elements in each cycle of a permutation are arranged so that the smallest element is placed in the last position and write the permutation by listing the cycles in increasing order of their smallest elements. Following the standard representation, even without the parentheses, we can determine the unique cycle structure of a given permutation. The above permutation is written as 4065132. We denote by C([n]) the set of all derangements over [n] represented in cycle notation. The binary relationship ‘a ≺ b’ means that a is a lexicographic predecessor of b. This can be expressed by the recurrence form, a ≺ b if either a0 < b0 or (a0 = b0 ) ∩ (a1 · · · an−1 ≺ b1 · · · bn−1 ). For convenience, we expand the relationship to a prefix isolation, denoted by a ≺|i b, and defined as a0 < b0 for i = 0 and (a0 · · · ai−1 = b0 · · · bi−1 ) ∩ (ai < bi ) for i > 0. The subdivides the set prefix isolation of predecessors of a given derangement into n partitions. Let A = {a | a ≺ b} and Ai = a | a ≺|i b for a, b ∈ C([n]). Obviously, A = A0 ∪ · · · ∪ An−1 and Ai ∩ Aj = ∅ for i ̸= j, i, j ∈ [n]. Given a derangement σ ∈ C([n]), the first element σ0 must always be chosen from [n] \ {0}, since no derangements have any fixed points, i.e., σ0 ̸= 0 and every cycle has at least two elements. The second element σ1 must be in the state of either σ1 = 0 if the first two elements σ0 σ1 form a cycle, or σ1 ∈ [n] \ {0, σ0 } if the first cycle of σ remains open. After the first cycle has been fixed as σ0 σ1 , the remaining elements are used to form new derangements of length n − 2 into the suffix σ2 σ3 · · · σn−1 . The number of these derangements is (n − 1) × dn−2 . On the other hand, when the first cycle remains open, i.e., σ1 ̸= 0, it continues to form new derangements of length n − 1 into the suffix σ1 σ2 · · · σn−1 . The number of these derangements is (n − 1) × dn−1 . Thus, the total number of derangements in cycle notation is easily derived using the same recurrence formula as Eq. (1). We observe that v is the inversion vector of a derangement if and only if v does not have two consecutive 0s. No inversions with respect to σi are found, i.e., vi = 0, if σi is the last element of the cycle. This is obvious behavior, since σi is the smallest element between σi and σn−1 , and is always placed in the last position of a cycle as the cycle terminator. Thus, both vn−1 = 0 and vn−2 = 1 hold. We show in Table 1 the lexicographic list of cycle notations and their corresponding derangements. 3. Ranking We apply the same definition as the inversion vector to cycle notation and define the appropriate ranking function for derangements. The lexicographic rank of σ ∈ C([n]) can be thought of as the number of predecessors of σ on the lexicographic list of all elements belonging to C([n]). By the inductive definition of lexicographic order, r (σ ) can be expressed by the conceptual formula: r (σ ) =
n−1 ′ σ ∈ C([n])|σ ′ ≺|i σ . i=0
(4)
166
K. Mikawa, K. Tanaka / Discrete Applied Mathematics 166 (2014) 164–169 Table 1 List of cycle notations, inversion vectors, and derangements on [5]. Rank
σ
v
δ
Rank
σ
v
δ
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
10342 10432 12043 12340 12430 13042 13240 13420 14032 14230 14320 20341 20431 21043 21340 21430 23041 23140 23410 24031 24130 24310
10110 10210 11010 11110 11210 12010 12110 12210 13010 13110 13210 20110 20210 21010 21110 21210 22010 22110 22210 23010 23110 23210
10342 10423 12043 12340 12403 13402 13420 13042 14320 14302 14023 23041 24013 20143 23140 24103 24301 24310 20341 23410 23401 20413
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
30241 30421 31042 31240 31420 32041 32140 32410 34021 34120 34210 40231 40321 41032 41230 41320 42031 42130 42310 43021 43120 43210
30110 30210 31010 31110 31210 32010 32110 32210 33010 33110 33210 40110 40210 41010 41110 41210 42010 42110 42210 43010 43110 43210
32401 34102 30412 32410 34012 34021 34120 30421 32140 32041 30142 42310 43120 40321 42301 43021 43012 43102 40312 42103 42013 40123
We know of the inversion vector v of σ where vi is defined as the number of the σj such that σi > σj for i < j. This means that vi indicates the number of possible values of the i-th element less than the value of σi . The number of predecessors of σ that have the same prefix σ0 · · · σi−1 is considered in the following cases. The first case is where vi−1 = 0. A new cycle begins with the i-th element, whenever vi−1 = 0. In this case, the cycle structure of suffixes of length n − i is equivalent to that of C([n − i]). This means that the number of such derangements is (n − i − 1)(dn−i−1 + dn−i−2 ). Obviously, the predecessors of σ belong to a subset of those derangements with the number of these being (vi − 1)(dn−i−1 + dn−i−2 ). The case where i = 0 results in similar behavior, since the first cycle also begins with the first element. The second case is where vi−1 ̸= 0. In this case, every cycle containing σi either closes at σi or remains open. When a cycle closes at σi , no inversions with respect to σi are found, i.e., vi = 0. There is no predecessor of σ whose i-th element is smaller than σi , since σi is the smallest of σi , . . . , σn−1 . Alternatively, if σ has a cycle that neither begins nor ends at σi , then σi can be taken in vi different ways. The predecessors of such a σ comprise two subsets, one of which must contain the derangements including a cycle closed at the i-th element, whereas the other contains the remaining ones. The number of the former derangements is dn−i−1 , while the number of the latter ones is (vi − 1)(dn−i−1 + dn−i−2 ). To summarize the above, the lexicographic rank of σ , which is the cycle notation corresponding to a derangement over [n], is defined as
n−1 0 (vi − 1) · (dn−i−1 + dn−i−2 ) r (σ ) = (vi − 1) · (dn−i−1 + dn−i−2 ) + dn−i−1 i=0
if vi = 0, if i = 0 or vi−1 = 0, otherwise
(5)
with its inversion vector v = v0 v1 · · · vn−1 . An example is given where σ = 4065132, r (σ ) is computed with its inversion vector v = 4043010 as r (σ ) = 3 · (d6 + d5 ) + 3 · (d4 + d3 ) + 2 · (d3 + d2 ) + d3 = 968. The rank function above can be performed in linear time with initial boundaries d1 = 0 and d2 = 1 by summing partial ranks at each stage i in descending order from n − 1 to 0, and requires linear space to store a given derangement and its inversion vector. The conversion of a derangement (cycle notation) into its inversion vector can be implemented independently of the conversion of the inversion vector into the lexicographic rank of the derangement. An efficient conversion has a great impact on preventing the total time and space complexity from growing. It is not that hard to implement the ranking function r (σ ) in O(n log n) time with O(n) space algorithms. For example, we introduce the efficient ranking and unranking algorithms described in [2]. Let T be a complete binary tree of height k with 2k ≥ n leaves, associated with the elements of σ , and where the 2k+1 − 1 nodes store the number of used elements. Initially, all the nodes contain zero and the inversion vector vi contains σi . To compute vi , the algorithm traverses a path in a bottom-up manner from the leaf associated with σi to the root, performing two operations at each node: (1) if the current node is the right child of its parent then subtract the value stored at the left child of the parent from vi , and (2) increase the value stored at the current node. At the end of the traversal, vi is determined. We show in Fig. 1 an example of the conversion of σ = 4065132 into v = 4043010. All the nodes in T are initialized to store zero, as illustrated in Fig. 1(a). The value of v0 initialized to 4 (= σ0 ) is never changed throughout the traversal from the leaf associated with σ0 to the root, since the left sibling of the current node at each level stores zero, as illustrated in Fig. 1(b). The value of v1 is also never changed, since the current node goes up the leftmost path. The value of v2 is decreased twice, at the root and at the right child of the root, and is then changed to 6 − 1 − 1 = 4. The algorithm proceeds until the value
K. Mikawa, K. Tanaka / Discrete Applied Mathematics 166 (2014) 164–169
167
(a) Initialized tree.
(b) σ0 = 4, v0 = 4.
(c) σ1 = 0, v1 = 0.
(d) σ2 = 6, v2 = 4.
(e) σ3 = 5, v3 = 3.
(f) σ4 = 1, v4 = 0.
(g) σ5 = 3, v5 = 1.
(h) σ6 = 2, v6 = 0.
Fig. 1. Example of the conversion of σ = 4065132 into v = 4043010. Source: Adapted from a similar figure in [2].
of v6 is fixed. The last leaf is an extra and is not used in this example, since the number of entries in σ is one less than that of leaves on T . All leaves are not always used in our algorithm. The computational complexity of the conversion given above is evaluated in the discussion below. We make use of a compact implementation for complete binary trees, called a binary heap, which requires at most 2⌈log n⌉+1 − 1 space to store the node values in breadth first order. It is obvious that the algorithm runs in O(n log n) time to complete the conversion of a derangement into its inversion vector. Thus, we obtain the following main theorem. Theorem 1. The ranking function r (σ ) runs in O(n log n) time with O(n) space to compute the lexicographic rank of σ ∈ C([n]). 4. Unranking The unranking function r −1 (x) is the inverse of r (σ ) = x, and returns the derangement corresponding to a given rank 0 ≤ x < |C([n])|. The unranking algorithm carries out the reverse process of r (σ ) such that it first converts x into an inversion vector v and then decodes v into the derangement σ . We apply a similar implementation of r (σ ) to the algorithm to ensure O(n log n) performance. The linear time conversion of x into v is discussed below. We denote by σ ∈ C([n]) the derangement in cycle notation corresponding to a given rank x, and by v the inversion vector of σ . As mentioned in the description of Eq. (5), the rank x is obtained from the sum of the partial ranks associated with the prefixes of σ in order of their increasing length varying from 1 to n. Next we introduce the partial rank, xi , defined as xi = x −
i−1 ′ σ ∈ C([n]) | σ ′ ≺|j σ
(6)
j =0
with the initial boundary x0 = x. Similar to the process of computing r (σ ), the number of inversions of σi is considered in several cases with respect to the behavior of cycles. The first case is where vi−1 = 0. Every new cycle must begin with the i-th element under the condition that vi−1 = 0, and the number of such predecessors is (vi − 1)(dn−i−1 + dn−i−2 ). The case with i = 0 also results in similar
behavior. Thus, we obtain the number of inversions of σi expressed as vi =
xi dn−i−1 +dn−i−2
+ 1 if either vi−1 = 0 or i = 0,
using the partial rank xi . The second case is where vi−1 ̸= 0. In this case, every cycle containing σi either closes at σi or remains open. When a cycle closes at σi , i.e., vi = 0, σ exists in the dn−i−1 derangements created from the different suffixes of length n − i. Thus, we have vi = 0 if vi−1 ̸= 0 and xi < dn−i−1 . Alternatively, if σ has a cycle that neither begins nor ends at σi , then σi can be taken in vi different ways. The predecessors of such σ comprise two subsets, one of which contains the dn−i−1 derangements including a cycle closed at the i-th element, whereas the other contains the remaining (vi − 1)(dn−i−1 + dn−i−2 ) derangements. We
168
K. Mikawa, K. Tanaka / Discrete Applied Mathematics 166 (2014) 164–169
(a) Initialized tree.
(b) v0 = 4, σ0 = 4.
(c) v1 = 0, σ1 = 0.
(d) v2 = 4, σ2 = 6.
(e) v3 = 3, σ3 = 5.
(f) v4 = 0, σ4 = 1.
(g) v5 = 1, σ5 = 3.
(h) v6 = 0, σ6 = 2.
Fig. 2. Example of the conversion of v = 4043010 into σ = 4065132. Source: Adapted from a similar figure in [2].
obtain the partial rank xi as xi = (vi − 1)(dn−i−1 + dn−i−2 )+ dn−i−1 , which is easily transformed into vi = To summarize the above, the inversion vector v = v0 v1 · · · vn−1 of σ is defined as
vi =
0
dn−i−1 + dn−i−2
xi −dn−i−1 dn−i−1 +dn−i−2
+ 1.
if vi−1 ̸= 0 and xi < dn−i−1 ,
xi
+1
if i = 0 or vi−1 = 0,
+1
otherwise,
(7)
xi − dn−i−1 dn−i−1 + dn−i−2
with the boundary condition vn−1 = 0. In fact, the value of vn−1 is always zero, as it is certain toclose at the last element. For example, given rank x = 968 for σ ∈ C [7], the inversion vector v is computed as v0 = 265968 + 1 from x0 = 968, v1 = 0 +44
2 + 1 = 4 from x2 = 41, v3 = 82− + 1 = 3 from x3 = 8, v4 = 0 from x4 = 0 < d2 , v5 = 1 from x1 = 41 < d5 , v2 = 941 +2 +1 from x5 = 0, and v6 = 0 from the definition. If the vi are simply computed by Eq. (7), it may take approximately n steps to expand xi directly from Eq. (6). Assuming that each of the di has been computed in advance to reduce the number of steps, Eq. (6) can be computed in constant time by using the last three values of vi−1 , vi−2 , and xi−1 at each stage i.
x
x i −1 xi = xi−1 − (vi−1 − 1)(dn−i + dn−i−1 ) xi−1 − (vi−1 − 1)(dn−i + dn−i−1 ) − dn−i
if i = 0, if vi−1 = 0, if i = 1 and vi−2 = 0, if vi−1 ̸= 0 and vi−2 ̸= 0.
(8)
Note that unlike the ranking algorithm, the unranking algorithm must obtain all the values of the di before the conversion of
v into σ begins, since the process of computing the xi proceeds in order from x0 to xn−1 despite the fact that the computation
of the di proceeds in a bottom-up manner. Next, we explain how to convert an inversion vector v into the derangement σ corresponding to v . A complete binary tree T of height k is defined as well as that of the ranking algorithm with the exception that every node at height i is initialized to store 2i . To restore σ from v , the algorithm traverses a path in a top-down manner from the root to a leaf, and performs two operations at each node: (1) if the value of vi is less than the left child of the current node, then go down to the left child, otherwise subtract the stored value at the left child from vi and go down to the right child, and (2) decrease the value stored at the current node before going down to either child. At the end of the traversal, σi must be the element associated with the leaf where the algorithm terminates. We show in Fig. 2 an example of the restoration of σ = 4065132 from v = 4043010. Every node in T at height i is initialized to store 2i , as illustrated in Fig. 2(a). The value of v0 is subtracted once at the root and is then changed to 4 − 4 = 0, since the value of the left child of the root is not less than the value of v0 . After the subtraction,
K. Mikawa, K. Tanaka / Discrete Applied Mathematics 166 (2014) 164–169
169
the algorithm goes down the leftmost path beginning at the right child of the root, and finally reaches the leaf associated with σ4 , as illustrated in Fig. 2(b). The value of v1 is never changed, since the algorithm goes down the leftmost path. The value of v2 is decreased twice, at the root and at the right child of the root, and is changed to 4 − 3 − 1 = 0 before the algorithm reaches a leaf. The algorithm proceeds until all the elements of σ are fixed. The algorithm requires only O(n log n) time and linear space to complete the conversion of an inversion vector into the corresponding derangement using a binary heap. Thus, we obtain the following main theorem. Theorem 2. The unranking function r −1 (x) runs in O(n log n) time with O(n) space to compute the derangement associated with a lexicographic rank 0 ≤ x < C([n]). 5. Conclusion In this paper, we proposed algorithms with O(n log n) time and O(n) space complexity for lexicographic ranking and unranking of derangements expressed in cycle notation. The ranking and unranking of derangements for the proper lexicographic order was not discussed here and is quite difficult to achieve in an O(n log n) time implementation. However, our approach of using cycle notation may be useful for linear time ranking and unranking of derangements, which is likely to result in a non-lexicographic order. Acknowledgments The authors would like to thank the reviewers for their detailed comments and helpful suggestions. This research was supported in part by a Grant-in-Aid for Scientific Research (C) 24500076 of the Japan Society for the Promotion of Science. References [1] J.-L. Baril, V. Vajnovszki, Gray code for derangements, Discrete Applied Mathematics 140 (1–3) (2004) 207–221. [2] B. Bonet, Efficient algorithms to rank and unrank permutations in lexicographic order, in: AAAI-Workshop on Search in AI and Robotics, 2008. [3] S. Durocher, D.G. Kirkpatrick, L. Narayanan, On routing with guaranteed delivery in three-dimensional ad hoc wireless networks, Wireless Networks 16 (1) (2010) 227–235. [4] T.S. Ferguson, Who solved the secretary problem? Statistical Science 4 (3) (1989) 282–296. [5] D.E. Knuth, The Art of Computer Programming, Vol. 3: Sorting and Searching, second ed., Addison-Wesley, Reading, MA, 2000 (First Published in 1973). [6] J.F. Korsh, P.S. LaFollette, Constant time generation of derangements, Information Processing Letters 90 (4) (2004) 181–186. [7] C.N. Mathur, K.P. Subbalakshmi, A light weight enhancement to RC4 based security for resource constrained wireless devices, International Journal of Network Security 5 (2) (2007) 205–212. [8] S. Mauw, S. Ramdomirović, P. Ryan, Security protocols for Secret Santa, in: Proc. 18th Security Protocols Workshop, 2010. [9] K. Mikawa, I. Semba, Generating derangements by interchanging at most four elements, Systems and Computers in Japan 35 (12) (2004) 25–31. [10] W. Myrvold, F. Ruskey, Ranking and unranking permutations in linear time, Information Processing Letters 79 (6) (2001) 281–284. [11] R.P. Stanley, Enumerative Combinatorics, Vol. 1, second ed., Cambridge University Press, Cambridge, 2012. [12] F. Wang, S.C. Liew, Wireless MIMO switching, in: Proc. IEEE GLOBECOM, 2012, pp. 4374–4379.