Home › Permutations vs Combinations › Lesson 10

Ranking and Unranking Permutations and Combinations

Lesson 10 of 13

Key idea of this section

Lexicographic rank of σ ∈ Sₙ (from 0):  r(σ) = ∑ᵢ Lᵢ · (n − i)!   (factorial number system).
Colex rank of {c₁ < c₂ < ⋯ < ck} ⊂ {0, 1, 2, …}:  N = C(c₁, 1) + C(c₂, 2) + ⋯ + C(ck, k).
Ranking turns combinatorial objects into integers and back, enabling uniform sampling and indexing.

The problem

A ranking of a finite set 𝒳 of combinatorial objects with respect to a total order is the bijection rank : 𝒳 → {0, 1, …, |𝒳| − 1} giving each object its position; unranking is the inverse map. Efficient ranking and unranking allow one to store an object as an integer, to index arrays by objects, to sample uniformly at random (choose a uniform integer and unrank it), and to split exhaustive searches into independent ranges for parallel computation.

Lexicographic ranking of permutations

Order Sₙ lexicographically by one-line words. For n = 3 the order is 123, 132, 213, 231, 312, 321.

Theorem 10.1. The lexicographic rank (counting from 0) of σ ∈ Sₙ is r(σ) = ∑ᵢ₌₁ⁿ Lᵢ (n − i)!, where (L₁, …, Lₙ) is the Lehmer code.

Proof. A permutation τ precedes σ if and only if, for some i, τ agrees with σ in positions 1, …, i − 1 and τ(i) < σ(i). For a fixed i, the possible values of τ(i) are the values smaller than σ(i) not used in positions 1, …, i − 1 — that is, the values smaller than σ(i) that appear in σ to the right of position i. There are Lᵢ of them, and for each the remaining n − i positions can be filled in (n − i)! ways. Summing over i gives the result. ∎

Since 0 ≤ Lᵢ ≤ n − i, Theorem 10.1 is the statement that every integer 0 ≤ r < n! has a unique representation r = ∑ₘ₌₀n−1 dₘ m! with digits 0 ≤ dₘ ≤ m, the factorial number system (factoradic). Unranking is a mixed-radix conversion followed by the decoding of Theorem 9.2.

Example 10.2. For σ = 3 1 4 2, L = (2, 0, 1, 0) and r(σ) = 2 · 3! + 0 · 2! + 1 · 1! + 0 · 0! = 13, so σ is the fourteenth permutation of [4] in lexicographic order. Conversely, to unrank 13 in S₄, write 13 = 2 · 6 + 0 · 2 + 1 · 1 + 0, so L = (2, 0, 1, 0). From {1, 2, 3, 4} take the element of index 2 (counting from 0), namely 3; from {1, 2, 4} take index 0, namely 1; from {2, 4} take index 1, namely 4; the last element is 2. This recovers 3 1 4 2.

Implemented naively each conversion costs O(n²) operations; with a Fenwick tree (binary indexed tree) for the order statistics it costs O(n log n). Myrvold and Ruskey (2001) gave linear-time ranking and unranking algorithms, at the price of using an order on Sₙ that is not lexicographic.

The successor algorithm

To step through Sₙ in lexicographic order without ranking, use the following procedure, already described by Nārāyaṇa Paṇḍita in the fourteenth century. Given σ, find the largest i with σ(i) < σ(i + 1) (if none exists, σ is the last permutation); find the largest j > i with σ(j) > σ(i); swap σ(i) and σ(j); and reverse the suffix σ(i + 1)⋯σ(n). For example, from 1 4 3 2 we find i = 1, j = 4, swap to get 2 4 3 1, and reverse the suffix to obtain 2 1 3 4. The correctness argument is that the suffix after position i is decreasing, so it is the last arrangement of its entries, and the successor must increase position i by the least possible amount and then begin the smallest arrangement of the suffix.

Colexicographic ranking of combinations

Represent a k-subset of ℕ = {0, 1, 2, …} as A = {c₁ < c₂ < ⋯ < ck}. The colexicographic order compares two k-subsets by their largest elements first: A precedes B if the largest element of the symmetric difference A △ B belongs to B. For 2-subsets of {0, 1, 2, 3} the order is 01, 02, 12, 03, 13, 23.

Theorem 10.3 (Combinatorial number system). The colex rank of A = {c₁ < ⋯ < ck} among all k-subsets of ℕ is N(A) = ∑ᵢ₌₁ᵏ C(cᵢ, i). Consequently every integer N ≥ 0 has a unique representation N = C(ck, k) + ⋯ + C(c₁, 1) with ck > ⋯ > c₁ ≥ 0.

Proof. A k-subset B precedes A exactly when the largest element of A △ B lies in A; call it cᵢ. Then B agrees with A above cᵢ, so B contains ci+1, …, ck, omits cᵢ, and has its remaining i elements below cᵢ, chosen arbitrarily from {0, …, cᵢ − 1}. There are C(cᵢ, i) such B for each i, and summing gives the rank. Since every k-subset has only finitely many predecessors and there are infinitely many k-subsets, the colex order is a well-order of type ω, so the rank map is a bijection onto ℕ; this is the uniqueness and existence of the representation. ∎

The rank does not depend on any ambient n, which makes colex the natural order for combinations. Unranking is greedy: choose ck as the largest c with C(c, k) ≤ N, subtract, and repeat with k − 1. For example, with k = 3, the subset {1, 3, 4} has rank C(1, 1) + C(3, 2) + C(4, 3) = 1 + 3 + 4 = 8; conversely for N = 8, the largest c with C(c, 3) ≤ 8 is 4 (C(4, 3) = 4, C(5, 3) = 10), leaving 4; the largest c with C(c, 2) ≤ 4 is 3, leaving 1; and C(1, 1) = 1 gives c₁ = 1.

Lexicographic order on k-subsets of [n] is obtained from colex by a reflection: if φ(A) = {n − a : a ∈ A} ⊆ {0, …, n − 1}, then the lexicographic rank of A equals C(n, k) − 1 − N(φ(A)). For example, among the 3-subsets of [6], A = {2, 4, 5} has φ(A) = {1, 2, 4}, with N = 1 + 1 + 4 = 6, so its lexicographic rank is 20 − 1 − 6 = 13.

Exercise

  • (a)
    Find the lexicographic rank (from 0) of 2 4 1 3 in S₄.
  • (b)
    Unrank 100 in S₅ (lexicographic order, counting from 0).
  • (c)
    Find the 3-subset of ℕ with colex rank 50.
Show solution

Answers: (a) L = (1, 2, 0, 0), so r = 1 · 6 + 2 · 2 = 10. (b) 100 = 4 · 24 + 0 · 6 + 2 · 2 + 0 · 1, so L = (4, 0, 2, 0, 0); decoding from {1, 2, 3, 4, 5} gives 5, then 1, then 4, then 2, then 3: the permutation 5 1 4 2 3. (c) C(7, 3) = 35 ≤ 50 < C(8, 3) = 56, leaving 15 = C(6, 2), leaving 0 = C(0, 1): the subset {0, 6, 7}.

V.3.1