Permutations With Repetition. Combinations. Enumeration, Ranking and Unranking Algorithms
Permutations With Repetition. Combinations. Enumeration, Ranking and Unranking Algorithms
15 October 2018
hx1 , . . . , xr i
with x1 , . . . , xr ∈ A.
B The same symbol of A can occur many times
B By the rule of product, there are nr r -permutations with
repetition
Example
A = {a1 , a2 , a3 , a4 , a5 } where a1 = a, a2 = b, a3 = c, a4 = d, a5 = e.
BitString(B: subset of A,
A: ordered set {a1 , . . . , an })
int bit string [0 ..n − 1]
for i:=0 to n − 1 do
if ai ∈ B then
bit string [n − i] := 1
else
bit string [n − i] := 0
return bit string
Definition
The canonic rank of a a subset B of an ordered set A with n elements is
n−1
X
CanonicRank(B, A) := b[i] · 2i
i=0
Example (A = {a0 , a1 , a2 })
subset 3-bit string encoding canonic rank
b2 b1 b0
∅ 000 0
{a0 } 001 1
{a1 } 010 2
{a0 , a1 } 011 3
{a2 } 100 4
{a0 , a2 } 101 5
{a1 , a2 } 110 6
{a0 , a1 , a2 } 111 7
B ⊆ ha0 , . . . , an−1 i
BitString(B, A)
n-bits b[0..n − 1]
CanonicRank(B, A)
for the binary encoding of B
direct computation
n−1
X
b[i] · 2i
i=0
0 ≤ r < 2n
Combination(b, A)
B := {ai | b[n − i − 1] = 1}
Example
With Grey’s method, the subsets of {a, b, c} are enumerated in the
following order:
{}, {c}, {b, c}, {b}, {a, b}, {a, b, c}, {a, c}, {a}
Hypothesis:
A = {1, 2, . . . , n} and X = {x1 , x2 , . . . , xk } with x1 < x2 < . . . < xk
is the subset of A with rank m in the lexicographic enumeration of
all k-combinations of A.
[Keep in mind that 0 ≤ m < kn .]
⇒ one can use (1) and (2) to find x1 : kn − n−xk1 +1 ≤ m < kn − n−x
1
k
⇒ one can use (1) and (2) to find x1 : kn − n−xk1 +1 ≤ m < kn − n−x
1
k