# Lexicographic Unranking of Combinations Revisited

^{*}

## Abstract

**:**

## 1. Introduction

`sym`) are all closed source so we cannot take a look at their implementation to understand these differences. However, a detailed study of the different algorithms from the literature and some practical considerations presented in Section 4 provide some insights on this question. Sagemath is a special case to us. Since version 9.1, our algorithm has been implemented (in Python 3) and is used as the native algorithm.

#### Combinatorial Context

**Definition**

**1.**

**Definition**

**2.**

- In the lexicographic order, we say that A is smaller than B if and only if both combinations have the same (possibly empty) prefix such that $({a}_{0},\dots ,{a}_{p-1})=({b}_{0},\dots ,{b}_{p-1})$ and if in addition ${a}_{p}<{b}_{p}$.
- In the co-lexicographic order, we say that A is smaller than B if and only if the finite sequence $({a}_{k-1},\dots ,{a}_{0})$ is smaller than $({b}_{k-1},\dots ,{b}_{0})$ for the lexicographic order.
- If A is smaller than B for a given order, then, for the reverse order, B is smaller that A.

**Definition**

**3.**

## 2. Unranking through Factoradics: A New Strategy

#### 2.1. Link between the Factorial Number System and Permutations

**Definition**

**4.**

**Definition**

**5.**

**Fact**

**1.**

Algorithm 1 Unranking a permutation. | ||

1: function Extract(F,n,k) | ||

1: function UnrankingPermutation(n,u) | 2: P ← [0,1,…,n − 1] | |

2: F ← factoradic(u) | 3: L ← [0,…,0] | ▷ k components |

3. while $\mathtt{length}\left(F\right)<n$ do | 4. for i from 0 to $k-1$ do | |

4: $\mathtt{append}(F,0)$ | 5: $L\left[i\right]\leftarrow P\left[F\right[n-1-i\left]\right]$ | |

5: return $\mathrm{E}\mathrm{XTRACT}(F,n,n)$ | 6: $\mathtt{remove}(P,F[n-1-i\left]\right)$ | |

7: return L | ||

factoradic$\left(u\right)$: computes the factoradic of u; | ||

length$\left(F\right)$: computes the number of components in F; | ||

append$(F,i)$: appends the element i at the end of F; | ||

remove$(F,i)$: removes from F the element at index i. |

`swap`operation giving an in-place shuffle to unrank permutations in the lexicographic order; put differently: Durstenfeld’s algorithm [21] cannot be easily adapted for the lexicographic order.

#### 2.2. Combinations Unranking through Factoradics

- We define a bijection between the combinations of k elements among n and a subset of the permutations of n elements.
- We transform the combination rank u into the rank ${u}^{\prime}$ of the appropriate permutation.
- We build (the prefix of) the permutation of rank ${u}^{\prime}$ by using Algorithm 1.

**Definition**

**6.**

**Proposition**

**1.**

**Fact**

**2.**

**Lemma**

**1.**

**Proof.**

**Proposition**

**2.**

**Proof.**

- The values of ${f}_{n-1-j}$ for all $0\le j<i$ have been computed and stored in F.
- The value of ${f}_{n-i-1}$ (which has not been determined yet, as we enter the loop) is at least m.
- The variable ${u}^{\prime}$ holds the rank of the sequence ${\left({f}_{j}\right)}_{0\le j<n-i}$ (note that $j<i$) among all sequences satisfying the condition of Fact 2 with $k=k-i$ and $n=n-i$.

Algorithm 2 Unranking a combination. | ||

1: function RankConversion(n,k,u) | ||

2: F ← [0,…,0] ▷ n components in F | ||

3: i ← 0 | ||

4: m ← 0 | ||

5: while i < k do | ||

1: function UnrankingCombination(n,k,u)2: u′ ← RankConversion(n,k,u) 3: p ← UnrankingPermutation(n,u′) 4: return the first k elements of p | 6: b ← binomial(n − 1 − m − i,k − 1 − i) | |

7: if b > u then | ||

8: F[n − 1 − i] ← m | ||

9: i ← i + 1 | ||

10: else | ||

11: u ← u − b | ||

12: m ← m + 1 | ||

13: ▷F is the factoradic decomposition | ||

14: return composition(F) | ||

binomial(n,k) computes the value of $\left(\underset{k}{n}\right)$; | ||

composition(F): computes the integer whose factoradic is F. |

`binomial`function is called (see, e.g., the book [7] (p. 66) or the papers [22,23]). During the conversion from the rank of the combination to the one of the associated permutation, the coefficients are obtained via trials (in the factoradic notation) for ${f}_{n-1}$ to ${f}_{n-k}$, remarking that through our bijection ${\mathcal{P}}_{n,k}$ the latter sequence is weakly increasing. Thus, the worst cases are obtained when the value ${f}_{n-k}$ is as large as possible, that is $n-k$. In these cases, the number of calls to

`binomial`is n.

`binomial`, when u describes the whole range from 0 to $\left(\genfrac{}{}{0pt}{}{n}{k}\right)-1$, we introduce the following cumulative sequence.

**Lemma**

**2.**

`binomial`while unranking all possible combinations u from 0 to $\left(\genfrac{}{}{0pt}{}{n}{k}\right)-1$. The sequence satisfies: ${u}_{n,0}=0$ and ${u}_{n,n+i}=0$ for all n and $i>0$, and otherwise:

`OEIS A127717`(throughout this paper, a reference

`OEIS A⋯`points to Sloane’s Online Encyclopedia of Integer Sequences www.oeis.org (accessed on 15 March 2021)). The bijection between both structures is direct, and thus we provide new information about this sequence in the following.

**Proof.**

**Theorem**

**3.**

**Proof.**

**Corollary**

**1.**

`binomial`. For n being large and k being of the form $\alpha n$ for $0<\alpha <1$, this average number of calls is

## 3. Classical Unranking Algorithms

#### 3.1. Unranking through the Recursive Method

Algorithm 3 Recursive Unranking. | |

1: function UnrankingRecursive(n,k,u)2: L ← RecGeneration(n,k,u) 3: L′ ← [0,…,0] ▷ k components 4: for i from 0 to k − 1 do5: L′[i] ← n − 1 − L[k − 1 − i] 6: return L′ | 1: function RecGeneration(n,k,u)2: if k = 0 then3: return []4: if n = k then5: return [0,1,2,…,k − 1]6: b ← binomial(n − 1,k − 1)7: if u < b then8: R ← RecGeneration(n − 1,k − 1,u) 9: append(R,n − 1)10: return R11: else12: return RecGeneration(n−1,k,u−b) |

**Remark**

**1.**

**Proposition**

**3.**

**Corollary**

**2.**

**Proof.**

`binomial`function, when u describes the whole range of integers from 0 to $\left(\genfrac{}{}{0pt}{}{n}{k}\right)-1$. Ruskey justified such a measure by supposing the table of all binomial coefficients pre-computed, thus each call is equivalent. In Section 4, we discuss this measure. We introduce the sequence (to simplify the notations, we use several times the notations ${u}_{n,k}$ and $U\left(z\right)$ for distinct sequences and series) ${u}_{n,k}$ equal to the cumulative number of calls to

`binomial`for the whole range of possible values for u.

**Lemma**

**3.**

`binomial`for the recursive unranking while unranking all possible u from 0 to $\left(\genfrac{}{}{0pt}{}{n}{k}\right)-1$. The sequence satisfies: ${u}_{n,0}=0$ and ${u}_{n,n+i}=0$ for all n and $i\ge 0$, and otherwise

**Proof.**

`binomial`and a recursive call. The cumulative cost of the first call to binomial is $\left(\genfrac{}{}{0pt}{}{n}{k}\right)$, the cumulative cost of the recursive calls for $u<\left(\genfrac{}{}{0pt}{}{n-1}{k-1}\right)$ is ${u}_{n-1,k-1}$ and the cumulative costs of the recursive calls for $u\ge \left(\genfrac{}{}{0pt}{}{n-1}{k-1}\right)$ is ${u}_{n-1,k}$. □

**Theorem**

**4.**

**Proof.**

`OEIS A059797`. We thus can complete its properties in the

`OEIS`using our results.

**Corollary**

**3.**

`binomial`. For n being large and k being of the form$\alpha \xb7n$for$0<\alpha <1$, we get:

#### 3.2. Unranking through Combinadics

**Fact**

**5.**

`Sagemath`[28] (the previous unranking algorithm from Sagemath is stored in the Software Heritage Archive swh:1:cnt:c60366bc03936eede6509b23307321faf1035e23;lines=539-605). In the beginning of 2020, we replaced the Sagemath implementation by the algorithm presented in Section 2 (the new unranking algorithm from Sagemath is stored in the Software Heritage Archive swh:1:cnt:b2a68056554dbf90fa55e76820f348d9d55019e3;lines=539-653 (accessed on 15 March 2021)).

Algorithm 4 Unranking a combination. |

1: function UnrankingViaCombinadic($n,k,u$) |

2. $L\leftarrow [0,\dots ,0]$ ▹k components |

3. ${u}^{\prime}\leftarrow \mathtt{binomial}(n,k)-1-u$ |

4. $v\leftarrow n$ |

5. for i from 0 to $k-1$ do |

6. $v\leftarrow v-1$ |

7. $b\leftarrow \mathtt{binomial}(v,k-i)$ |

8. while ${u}^{\prime}<b$ do |

9. $v\leftarrow v-1$ |

10. $b\leftarrow \mathtt{binomial}(v,k-i)$ |

11. ${u}^{\prime}\leftarrow {u}^{\prime}-b$ |

12. $L\left[i\right]\leftarrow n-1-v$ |

13. return L |

`binomial`function. First note that, the values n and k being given, the worst cases are obtained when v gets as small as possible at the end of the loop, thus for all u whose combinadic satisfies ${c}_{1}=0$. Hence, the worst case complexity is $n-1$. Again, we complete this analysis, by computing the average complexity of the algorithm. To reach this goal, we again introduce the sequence ${u}_{n,k}$ computing the cumulative number of calls to

`binomial`when u ranges from 0 to $\left(\genfrac{}{}{0pt}{}{n}{k}\right)-1$.

**Lemma**

**4.**

`binomial`while unranking all possible u from 0 to $\left(\genfrac{}{}{0pt}{}{n}{k}\right)-1$. The sequence satisfies: ${u}_{n,0}=1$ and ${u}_{n,n+i}=0$ for all n and $i>0$, and otherwise

`OEIS A264751`. Both combinatorial objects can be put in bijection, and thus some conjectures stated there are solved in the following.

**Proof.**

`binomial`are necessary to determine ${c}_{k}$, then ${c}_{k}-{c}_{k-1}$ calls to determine ${c}_{k-1}$, …, and finally ${c}_{2}-{c}_{1}$ to determine ${c}_{1}$. Hence, the total number of binomial coefficients evaluations necessary to compute the combinadic of u is $n-{c}_{1}$ (and thus only depends on ${c}_{1}$). Besides, for a given $j\ge 0$, the number of finite sequences ${c}_{1}=j<{c}_{2}<{c}_{3}<\cdots <{c}_{k}<n$ is equal to the number of sequences $0\le {c}_{2}^{\prime}<{c}_{3}^{\prime}<\cdots <{c}_{k}<n-j-1$ by the change of variable ${c}_{i}^{\prime}\leftarrow {c}_{i}-j-1$. Hence, this number is equal to $\left(\genfrac{}{}{0pt}{}{n-1-j}{k-1}\right)$. In addition, there is a first call to

`binomial`at the beginning of the algorithm to reverse the rank, regardless of the value of u. We thus obtain:

**Theorem**

**6.**

**Corollary**

**4.**

`binomial`in Algorithm 4 for n being large and k being of the form $\alpha n$ for $0<\alpha <1$ is

Algorithm 5 Unranking a combination (alternative algorithm). |

1: function UnrankingViaCombinadic2($n,k,u$) |

2: $L\leftarrow [0,\dots ,0]$ ▹k components |

3. $r\leftarrow 0$ |

4. for i from 0 to $k-2$ do |

5. if $i\ne 0$ then $L\left[i\right]\leftarrow L[i-1]$ |

6: else $L\left[i\right]\leftarrow -1$ |

7: while true do |

8: $L\left[i\right]\leftarrow L\left[i\right]+1$ |

9: $b\leftarrow \mathtt{binomial}(n-L[i]-1,k-i-1)$ |

10: $r\leftarrow r+b$ |

11: if $r>u$ then exit the loop |

12: $r\leftarrow r-b$ |

13: $L[k-1]\leftarrow L[k-2]+u-r+1$ |

14: return L |

`Matlab`[29] whose code was also presented by Ruskey ([7], p. 65). The latter approach also does trials to find the last coefficient of the combination instead of computing it directly on Line 13.

**Lemma**

**5.**

`binomial`while unranking all possible u from 0 to $\left(\genfrac{}{}{0pt}{}{n}{k}\right)-1$. For all n, the sequence satisfies ${u}_{n,k}=0$ when $k=1,2$ or $k>n$, and otherwise

**Theorem**

**7.**

**Corollary**

**5.**

`binomial`in Algorithm 5 for n being large and k being of the form $\alpha n$ for $0<\alpha <1$ is

**Remark**

**2.**

## 4. Improving Efficiency and Realistic Complexity Analysis

`GMP`

`C`library is the best way to carry out fast operations, but the distinct behaviors observed in the table suggests that deeper differences exists between the various implementations than just the choice of the library.

`binomial`function is equivalent to n (when k grows linearly). A first question to investigate is about this complexity measure: Is it reasonable? An obvious approach consists in comparing these results with the actual run time of the algorithms.

#### 4.1. First Experiments to Visualize the Time Complexity in a Real Context

`C`using the classical

`GMP`library for big integers arithmetic (the implementation and the exhaustive material used for repeating the experiments are available at http://github.com/Kerl13/combination_unranking (accessed on 15 March 2021)). The experiments were run on a standard laptop PC with an I7-8665U CPU, 32Gb RAM running Ubuntu Linux 2020. Moreover, except for Table 1, which required running different computer algebra systems and thus the X server, the experiments were run in the console while the X server and other time-consuming services (wifi, sound, etc.) were off. On the leftmost plot, we present the average time for the unranking of a combination for n ranging from 250 to 10,000 by steps of 250 too. Note that all the curves are merged: all algorithms seem to need almost exactly the same time to unrank a combination. In the rightmost plot, we present a version with memoization (memoization is an optimization technique consisting in storing the results of expensive function calls and returning the cached values when the function is called again with the same arguments; this technique is sometimes called “tabling”) for the algorithms: we first run a pre-computation step storing all binomial coefficients that will be used. The second step consists in unranking the combinations by using the pre-calculated values for the binomial coefficients. In our plot, we present only the time used for the second step. We remark that the recursive Algorithm 3 is not as efficient as the others. Below, we show that this is due to its recursive nature and that this can be easily avoided. As explained above, Algorithm 5 should be less efficient than Algorithm 4, which is indeed apparent in the plots. Our Algorithms 2 and 4 are the most efficient when used in this setup.

#### 4.2. Improving the Implementations of the Algorithms

Algorithm 6 Unranking a combination with optimization. |

1: function OptimizedUnrankingCombination($n,k,u$) |

2: $L\leftarrow [0,\dots ,0]$ ▹k components |

3: $b\leftarrow \mathtt{binomial}(n-1,k-1)\xb7n$ |

4: $m\leftarrow 0;\phantom{\rule{1.em}{0ex}}i\leftarrow 0$; |

5: while $i<k-1$ do ▹ Invariant: $b=\left(\genfrac{}{}{0pt}{}{n-m-i-1}{k-1-i}\right)\xb7(n-m-i)$ |

6: $b\leftarrow b/(n-m-i)$ |

7: if $b>u$ then |

8: $L\left[i\right]\leftarrow m+i$ |

9: $b\leftarrow b\xb7(k-i-1)$ |

10: $i\leftarrow i+1$ |

11: else |

12: $u\leftarrow u-b$ |

13: $b\leftarrow b\xb7(n-m-k)$ |

14: $m\leftarrow m+1$ |

15: if $k>0$ then $L[k-1]\leftarrow n+u-b$ |

16: return L |

Algorithm 7 Recursive method with optimizations. | |

1: function OptimizedUnranking-Recursive(n,k,u) 2: L ← [0,…,0] ▷ k components 3: b ← binomial(n,k)4: UnrankTR(L,0,0,n,k,u,b) 5: return L | 1: function UnrankTR(L,i,m,n,k,u,b)2: if k = 0 then do nothing3: else if k = n then4: for j from 0 to k − 1 do5: L[i + j] ← m + j 6: else7: b ← b/n 8: if u < b then9: L[i] ← m 10: b ← (k − 1) · b 11: UnrankTR(L,i + 1,m + 1,n − 1,k − 1,u,b) 12: else13: u ← u − b 14: b ← (n − k) · b 15: UnrankTR(L,i,m + 1,n − 1,k,u,b) |

`binomial`are done), this only impacts the second order of their complexity.

#### 4.3. Realistic Complexity Analysis

**Theorem**

**8.**

## 5. Extensions of the Algorithmic Context

#### 5.1. Objects Counted by Multinomial Coefficients

**Proposition**

**4.**

Algorithm 8 Unranking a combination with repetitions. |

1: function UnrankingCombinationWithRepetitions($n,K=({k}_{1},\dots ,{k}_{m}),u$) |

2: $F\leftarrow [0,\dots ,0]$ ▷n components in M |

3. ${u}^{\prime}\leftarrow u$ |

4: ${n}^{\prime}\leftarrow {k}_{m}$ |

5: for i from $m-1$ downto 1 do |

6: ${n}^{\prime}\leftarrow {n}^{\prime}+{k}_{i}$ |

7: $b\leftarrow \mathtt{binomial}({n}^{\prime},{k}_{i})$ |

8: $({u}^{\prime},{u}^{\prime \prime})\leftarrow \mathtt{division}({u}^{\prime},b)$ |

9: ${F}^{\prime}\leftarrow \mathtt{factoradic}\left(\mathrm{R}\mathrm{ANK}\mathrm{C}\mathrm{ONVERSION}({n}^{\prime},{k}_{i},{u}^{\u2033})\right)$ |

10: for j from 0 to ${k}_{i}-1$ do |

11: $F[{n}^{\prime}-{k}_{i}+j]\leftarrow {F}^{\prime}[{n}^{\prime}-{k}_{i}+j]$ |

12: $r\leftarrow \mathtt{composition}\left(F\right)$ |

13: return $\mathrm{U}\mathrm{NRANKING}\mathrm{P}\mathrm{ERMUTATION},r)$ |

division$(s,t)$: returns the pair $(q,r)$ corresponding respectively to the quotient and theremainder of |

the integer division of s by t. |

**Proposition**

**5.**

**Proof.**

- The values of ${f}_{j}$ for all $0\le j<{k}_{m}+\cdots +{k}_{i+1}$ have been computed and stored in F.
- The values of ${f}_{{k}_{m}+\cdots +{k}_{i+1}},\cdots ,{f}_{{k}_{m}+{k}_{i+1}+{k}_{i}-1}$ (which has not been determined yet, as we enter the loop) are equal to the factoradics of the rank ${u}^{\prime}\mathrm{mod}\left(\genfrac{}{}{0pt}{}{{k}_{m}+\cdots +{k}_{i}}{{k}_{i}}\right)$ in the combinations of ${k}_{i}$ elements among ${k}_{m}+\cdots +{k}_{i}$ possible elements.
- The variable ${u}^{\prime}$ holds the rank of the runs that must be still unranked.

#### 5.2. Objects Counted by k-Permutations

**Proposition**

**6.**

**Proof.**

## 6. Conclusions

## Author Contributions

## Funding

## Data Availability Statement

## Acknowledgments

## Conflicts of Interest

## References

- Shablya, Y.; Kruchinin, D.; Kruchinin, V. Method for Developing Combinatorial Generation Algorithms Based on AND/OR Trees and Its Application. Mathematics
**2020**, 8, 962. [Google Scholar] [CrossRef] - Grebennik, I.; Lytvynenko, O. Developing software for solving some combinatorial generation and optimization problems. In Proceedings of the 7th International Conference on Application of Information and Communication Technology and Statistics in Economy and Education, Sofia, Bulgaria, 3–4 November 2017; pp. 135–143. [Google Scholar]
- Tamada, Y.; Imoto, S.; Miyano, S. Parallel Algorithm for Learning Optimal Bayesian Network Structure. J. Mach. Learn. Res.
**2011**, 12, 2437–2459. [Google Scholar] - Myers, A.F. k-out-of-n:G System Reliability With Imperfect Fault Coverage. IEEE Trans. Reliab.
**2007**, 56, 464–473. [Google Scholar] [CrossRef] - Bodini, O.; Genitrini, A.; Naima, M. Ranked Schröder Trees. In Proceedings of the Sixteenth Workshop on Analytic Algorithmics and Combinatorics, ANALCO 2019, San Diego, CA, USA, 6 January 2019; pp. 13–26. [Google Scholar] [CrossRef] [Green Version]
- Ali, N.; Shamoon, A.; Yadav, N.; Sharma, T. Peptide Combination Generator: A Tool for Generating Peptide Combinations. ACS Omega
**2020**, 5, 5781–5783. [Google Scholar] [CrossRef] [PubMed] - Ruskey, F. Combinatorial Generation; 2003; (unpublished); Available online: https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.93.5967 (accessed on 15 March 2021).
- Knuth, D.E. The Art of Computer Programming, Volume 4A, Combinatorial Algorithms; Addison-Wesley Professional: Boston, MA, USA, 2011. [Google Scholar]
- Skiena, S. The Algorithm Design Manual, Third Edition; Texts in Computer Science; Springer: Berlin/Heidelberg, Germany, 2020. [Google Scholar] [CrossRef]
- Ryabko, B.Y. Fast enumeration of combinatorial objects. Discret. Math. Appl.
**1998**, 8, 163–182. [Google Scholar] [CrossRef] [Green Version] - Nijenhuis, A.; Wilf, H.S. Combinatorial Algorithms; Computer Science and Applied Mathematics; Academic Press: New York, NY, USA, 1975. [Google Scholar]
- Flajolet, P.; Zimmermann, P.; Van Cutsem, B. A calculus for the random generation of labelled combinatorial structures. Theor. Comput. Sci.
**1994**, 132, 1–35. [Google Scholar] [CrossRef] - Martínez, C.; Molinero, X. A generic approach for the unranking of labeled combinatorial classes. Random Struct. Algorithms
**2001**, 19, 472–497. [Google Scholar] [CrossRef] [Green Version] - Kokosinski, Z. Algorithms for Unranking Combinations and their Applications. In Proceedings of the Seventh IASTED/ISMM International Conference on Parallel and Distributed Computing and Systems, Washington, DC, USA, 19–21 October 1995; Hamza, M.H., Ed.; IASTED/ACTA Press: Calgary, AB, Canada, 1995; pp. 216–224. [Google Scholar]
- Pascal, E. Sopra una formula numerica. G. Di Mat.
**1887**, 25, 45–49. [Google Scholar] - Beckenbach, E.F.; Pólya, G. Applied Combinatorial Mathematics; R.E. Krieger Publishing Company: Malabar, FL, USA, 1981. [Google Scholar]
- Laisant, C.A. Sur la numération factorielle, application aux permutations. Bull. Soc. Math. Fr.
**1888**, 16, 176–183. [Google Scholar] [CrossRef] [Green Version] - Fisher, R.A.; Yates, F. Statistical Tables for Biological, Agricultural and Medical Research; Oliver & Boyd: London, UK, 1948. [Google Scholar]
- Bonet, B. Efficient algorithms to rank and unrank permutations in lexicographic order. In AAAI Workshop on Search in AI and Robotics—Technical Report; The AAAI Press: Menlo Park, CA, USA, 2008; pp. 142–151. [Google Scholar]
- Bodini, O.; Genitrini, A.; Peschanski, F. A Quantitative Study of Pure Parallel Processes. Electron. J. Comb.
**2016**, 23, 39. [Google Scholar] [CrossRef] - Durstenfeld, R. Algorithm 235: Random Permutation. Commun. ACM
**1964**, 7, 420. [Google Scholar] [CrossRef] - Buckles, B.P.; Lybanon, M. Algorithm 515: Generation of a Vector from the Lexicographical Index [G6]. ACM Trans. Math. Softw.
**1977**, 3, 180–182. [Google Scholar] [CrossRef] - Er, M.C. Lexicographic ordering, ranking and unranking of combinations. Int. J. Comput. Math.
**1985**, 17, 277–283. [Google Scholar] [CrossRef] - Flajolet, P.; Sedgewick, R. Analytic Combinatorics; Cambridge University Press: Cambridge, UK, 2009. [Google Scholar]
- Flajolet, P.; Sedgewick, R. An Introduction to the Analysis of Algorithms, 2nd ed.; Addison Wesley: Boston, MA, USA, 2013. [Google Scholar]
- Kreher, D.L.; Stinson, D.R. Combinatorial Algorithms: Generation, Enumeration, and Search; CRC Press: Boca Raton, FL, USA, 1999. [Google Scholar]
- McCaffrey, J. Generating the mth Lexicographical Element of a Mathematical Combination. MSDN. 2004. Available online: http://docs.microsoft.com/en-us/previous-versions/visualstudio/aa289166(v=vs.70) (accessed on 15 March 2021).
- The Sage Developers. SageMath, the Sage Mathematics Software System (Version 9.2). 2020. Available online: https://www.sagemath.org/ (accessed on 15 March 2021).
- Butler, B. Function kSubsetLexUnrank, MATLAB Central File Exchange. 2015. Available online: http://fr.mathworks.com/matlabcentral/fileexchange/53976-combinatorial-numbering-rank-and-unrank (accessed on 15 March 2021).
- Bostan, A.; Chyzak, F.; Giusti, M.; Lebreton, R.; Lecerf, G.; Salvy, B.; Schost, E. Algorithmes Efficaces en Calcul Formel, 10th ed.; Printed by CreateSpace: Palaiseau, France, 2017; 686p. [Google Scholar]

**Table 1.**Average time (in ms) for the unranking of a combination of k elements among n (too long means that the average time is larger than 30,000 ms).

Time in ms | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|

Sample | Sagemath | Maple | Mathematica | Matlab | ||||||

Our Algo. | v. 9.0 | v. 9.2 | v. 2020.0 | v. 12.1.1.0 | v. R2020b | |||||

Implem. | in C | Their Algo. | New Algo. | Their Algo. | Our Algo. | Their Algo. | Our Algo. | Their Algo. | Our Algo. | |

$n=1000$ | 0.05464 | 2.6045 | 2.9672 | 78.2 | 2.12 | 0.44176 | 4.3145 | 3996.6 | 3041.2 | |

$k=100$ | ||||||||||

$n=1000$ | 0.06052 | 8.8903 | 2.4784 | 614 | 2.96 | 0.34608 | 3.9547 | 3520.6 | 3380.0 | |

$k=500$ | ||||||||||

$n=3000$ | 0.17496 | 15.8968 | 8.7929 | 1180 | 13.2 | 5.9131 | 11.823 | 11,846 | 9315.2 | |

$k=300$ | ||||||||||

$n=3000$ | 0.27524 | 96.3589 | 8.0500 | 6130 | 19.2 | 4.9624 | 13.067 | 11,087 | 9879.4 | |

$k=1500$ | ||||||||||

$n=$ 10,000 | 1.2554 | 191.03 | 31.665 | too long | 65.1 | 21.906 | 39.935 | too long | too long | |

$k=1000$ | ||||||||||

$n=$ 10,000 | 2.3849 | 2245.6 | 29.027 | too long | 97.9 | 29.916 | 46.452 | too long | too long | |

$k=5000$ |

k | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |
---|---|---|---|---|---|---|---|---|---|---|

n | ||||||||||

1 | 0 | 1 | ||||||||

2 | 0 | 3 | 2 | |||||||

3 | 0 | 6 | 8 | 3 | ||||||

4 | 0 | 10 | 20 | 15 | 4 | |||||

5 | 0 | 15 | 40 | 45 | 24 | 5 | ||||

6 | 0 | 21 | 70 | 105 | 84 | 35 | 6 | |||

7 | 0 | 28 | 112 | 210 | 224 | 140 | 48 | 7 | ||

8 | 0 | 36 | 168 | 378 | 504 | 420 | 216 | 63 | 8 |

k | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |
---|---|---|---|---|---|---|---|---|---|---|

n | ||||||||||

1 | 0 | 0 | ||||||||

2 | 0 | 2 | 0 | |||||||

3 | 0 | 5 | 5 | 0 | ||||||

4 | 0 | 9 | 16 | 9 | 0 | |||||

5 | 0 | 14 | 35 | 35 | 14 | 0 | ||||

6 | 0 | 20 | 64 | 90 | 64 | 20 | 0 | |||

7 | 0 | 27 | 105 | 189 | 189 | 105 | 27 | 0 | ||

8 | 0 | 35 | 160 | 350 | 448 | 350 | 160 | 35 | 0 |

Rank, u | Reversed Rank, ${\mathit{u}}^{\prime}=\left(\genfrac{}{}{0pt}{}{6}{2}\right)-1-\mathit{u}$ | Combinadic of ${\mathit{u}}^{\prime}$ | Combination of Rank u |
---|---|---|---|

0 | 14 | (4, 5) | (0, 1) |

1 | 13 | (3, 5) | (0, 2) |

2 | 12 | (2, 5) | (0, 3) |

3 | 11 | (1, 5) | (0, 4) |

4 | 10 | (0, 5) | (0, 5) |

5 | 9 | (3, 4) | (1, 2) |

6 | 8 | (2, 4) | (1, 3) |

7 | 7 | (1, 4) | (1, 4) |

8 | 6 | (0, 4) | (1, 5) |

9 | 5 | (2, 3) | (2, 3) |

10 | 4 | (1, 3) | (2, 4) |

11 | 3 | (0, 3) | (2, 5) |

12 | 2 | (1, 2) | (3, 4) |

13 | 1 | (0, 2) | (3, 5) |

14 | 0 | (0, 1) | (4, 5) |

k | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |
---|---|---|---|---|---|---|---|---|---|---|

n | ||||||||||

1 | 1 | 2 | ||||||||

2 | 1 | 5 | 3 | |||||||

3 | 1 | 9 | 11 | 4 | ||||||

4 | 1 | 14 | 26 | 19 | 5 | |||||

5 | 1 | 20 | 50 | 55 | 29 | 6 | ||||

6 | 1 | 27 | 85 | 125 | 99 | 41 | 7 | |||

7 | 1 | 35 | 133 | 245 | 259 | 161 | 55 | 8 | ||

8 | 1 | 44 | 196 | 434 | 574 | 476 | 244 | 71 | 9 |

k | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |
---|---|---|---|---|---|---|---|---|---|---|

n | ||||||||||

1 | 0 | 0 | ||||||||

2 | 0 | 0 | 1 | |||||||

3 | 0 | 0 | 4 | 2 | ||||||

4 | 0 | 0 | 10 | 10 | 3 | |||||

5 | 0 | 0 | 20 | 30 | 18 | 4 | ||||

6 | 0 | 0 | 35 | 70 | 63 | 28 | 5 | |||

7 | 0 | 0 | 56 | 140 | 168 | 112 | 40 | 6 | ||

8 | 0 | 0 | 84 | 252 | 378 | 336 | 180 | 54 | 7 |

Publisher’s Note: MDPI stays neutral with regard to jurisdictional claims in published maps and institutional affiliations. |

© 2021 by the authors. Licensee MDPI, Basel, Switzerland. This article is an open access article distributed under the terms and conditions of the Creative Commons Attribution (CC BY) license (http://creativecommons.org/licenses/by/4.0/).

## Share and Cite

**MDPI and ACS Style**

Genitrini, A.; Pépin, M.
Lexicographic Unranking of Combinations Revisited. *Algorithms* **2021**, *14*, 97.
https://doi.org/10.3390/a14030097

**AMA Style**

Genitrini A, Pépin M.
Lexicographic Unranking of Combinations Revisited. *Algorithms*. 2021; 14(3):97.
https://doi.org/10.3390/a14030097

**Chicago/Turabian Style**

Genitrini, Antoine, and Martin Pépin.
2021. "Lexicographic Unranking of Combinations Revisited" *Algorithms* 14, no. 3: 97.
https://doi.org/10.3390/a14030097