0% found this document useful (0 votes)
98 views

Permutations With Repetition. Combinations. Enumeration, Ranking and Unranking Algorithms

This document discusses algorithms for ranking and unranking permutations with repetition, as well as generating combinations and subsets. It provides an example problem about calculating the number of possible license plates in the UK based on its license plate format. The document outlines the steps to solve this type of problem, including considering each component of the license plate format separately and then combining them.

Uploaded by

Raluca Romanov
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views

Permutations With Repetition. Combinations. Enumeration, Ranking and Unranking Algorithms

This document discusses algorithms for ranking and unranking permutations with repetition, as well as generating combinations and subsets. It provides an example problem about calculating the number of possible license plates in the UK based on its license plate format. The document outlines the steps to solve this type of problem, including considering each component of the license plate format separately and then combining them.

Uploaded by

Raluca Romanov
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 63

Lecture 3

Permutations with repetition. Combinations.


Enumeration, ranking and unranking algorithms

Isabela Drămnesc UVT

Computer Science Department,


West University of Timişoara,
Romania

15 October 2018

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 1 / 33


Outline

Enumeration, ranking and unranking algorithms for


permutations with repetition
Binary represention of subsets
B Ranking and unranking algorithms
Fast generation of all subsets
B Gray codes; properties
Lexicographically ordered combinations (or subsets)
r -combinations: ranking and unranking algorithms

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 2 / 33


Problem: How many licence plates are possible in U.K.?
Motivation

Licence plates – what for?


B Also known as vehicle registration plates, are used for
authority to be able to track vehicles and determine who the
owner of a vehicle is;

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 3 / 33


Problem: How many licence plates are possible in U.K.?
Motivation

Licence plates – what for?


B Also known as vehicle registration plates, are used for
authority to be able to track vehicles and determine who the
owner of a vehicle is;
B Whenever you purchase a car you have to register it and order
plates for your vehicle;

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 3 / 33


Problem: How many licence plates are possible in U.K.?
Motivation

Licence plates – what for?


B Also known as vehicle registration plates, are used for
authority to be able to track vehicles and determine who the
owner of a vehicle is;
B Whenever you purchase a car you have to register it and order
plates for your vehicle;
B And then, whenever the police need to give a person a parking
or speeding ticket, they use the plates number to enter the
ticket into the police system.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 3 / 33


Problem: How many licence plates are possible in U.K.?
Motivation

Licence plates – what for?


B Also known as vehicle registration plates, are used for
authority to be able to track vehicles and determine who the
owner of a vehicle is;
B Whenever you purchase a car you have to register it and order
plates for your vehicle;
B And then, whenever the police need to give a person a parking
or speeding ticket, they use the plates number to enter the
ticket into the police system.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 3 / 33


How many licence plates are possible in U.K.?
Problem description

In U.K. the license plates are made up of


the regional flag followed by
a two-digit local area code,
a two-digit age identifier (corresponding to the year the
vehicle is registered), followed by
a three-digit sequence of letters.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 4 / 33


How many licence plates are possible in U.K.?
Questions! Ideas?

1 It is a Permutation problem or a Combination problem?


Explain;

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 5 / 33


How many licence plates are possible in U.K.?
Questions! Ideas?

1 It is a Permutation problem or a Combination problem?


Explain;
2 It is a Permutation with Repetitions problem or without
repetitions? Explain.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 5 / 33


How many licence plates are possible in U.K.?
Questions! Ideas?

1 It is a Permutation problem or a Combination problem?


Explain;
2 It is a Permutation with Repetitions problem or without
repetitions? Explain.
3 How to compute Permutations with repetitions? Where to
start from?

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 5 / 33


Permutations with repetition

The r -permutations with repetition of an alphabet


A = {a1 , . . . , an } are the ordered sequences of symbols of the form

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

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 6 / 33


How many licence plates are possible in U.K.?
Finding the solution step by step

1 If we consider that the year is set, then we compute how


many of the random three-letter sequences are possible at the
end of the licence plate;

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 7 / 33


How many licence plates are possible in U.K.?
Finding the solution step by step

1 If we consider that the year is set, then we compute how


many of the random three-letter sequences are possible at the
end of the licence plate;
2 Next, compute how many two-digit year identifiers are
possible;

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 7 / 33


How many licence plates are possible in U.K.?
Finding the solution step by step

1 If we consider that the year is set, then we compute how


many of the random three-letter sequences are possible at the
end of the licence plate;
2 Next, compute how many two-digit year identifiers are
possible;
3 Next, compute how many of the two-letter area codes,
followed by two-digit year identifiers, followed by the random
three-letter sequences are possible.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 7 / 33


How many licence plates are possible in U.K.?
Finding the solution step by step

1 If we consider that the year is set, then we compute how


many of the random three-letter sequences are possible at the
end of the licence plate;
2 Next, compute how many two-digit year identifiers are
possible;
3 Next, compute how many of the two-letter area codes,
followed by two-digit year identifiers, followed by the random
three-letter sequences are possible.
4 The result is ???
Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 7 / 33
Permutations with repetition
Ranking and unranking algorithms in lexicographic order

The r -permutations with repetition can be ordered lexicographically:


B hx1 , . . . , xr i < hy1 , . . . , yr i if there exists k ∈ {1, . . . , n} such that
xk < yk and xi = yi for all 1 ≤ i < k.

Example (A = {a1 , a2 } with a1 < a2 , and r = 3)


r -permutation with repetition of A lexicographic rank
ha1 , a1 , a1 i 0
ha1 , a1 , a2 i 1
ha1 , a2 , a1 i 2
ha1 , a2 , a2 i 3
ha2 , a1 , a1 i 4
ha2 , a1 , a2 i 5
ha2 , a2 , a1 i 6
ha2 , a2 , a2 i 7

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 8 / 33


Ranking and unranking of r -permutations with repetition
Remarks

Let A = {a1 , a2 , . . . , an } with a1 < a2 < . . . < an .


If we define index(ai ) := i − 1 for 1 ≤ i ≤ n, and replace ai
with index(ai ) in the lexicographic enumeration of the
r -permutations, we get
r -permutation encoding as number lexicogaphic rank
with repetition in base n
ha1 , . . . , a1 , a1 , a1 i h0, . . . , 0, 0, 0i 0
.. .. ..
. . .
ha1 , . . . , a1 , a1 , an i h0, . . . , 0, 0, n − 1i n−1
ha1 , . . . , a1 , a2 , a1 i h0, . . . , 0, 1, 0i n
.. .. ..
. . .
ha1 , . . . , a1 , a2 , an i h0, . . . , 0, 1, n − 1i 2n − 1
.. .. ..
. . .

Remark: The r -permutation with repetition of the indexes


is the representation in base n of its lexicographic rank.
Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 9 / 33
Ranking and unranking of r -permutations with repetition
Exercises

1 How would you approach the vehicle plates problem differently


if letters and digits could not occur repeatedly? Explain.
2 How many different licence plates are possible in ROMANIA
based on the license plate set up?
3 Define an algorithm which computes the rank of the
r -permutation with repetition hx1 , . . . , xr i of A = {1, . . . , n}
with respect to the lexicographic order.
4 Define an algorithm which computes r -permutation with
repetition hx1 , . . . , xr i with rank k of A = {1, . . . , n} with
respect to the lexicographic order.
5 Define an algorithm which computes the r -permutation with
repetition immediately after the r -permutation with repetition
hx1 , . . . , xr i of A, in lexicographic order.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 10 / 33


The Problem of roses
The story

Alex is a second year bachelor student at Faculty of


Mathematics and Informatics from West University of
Timisoara. He fell in love with a colleague of his own year and
after a few days he decided that it’s time to declare her his
love. So, he went to the flower shop to buy her a bouquet of
7 roses. The flower shop had white, yellow and red roses. And
because he is a computer scientist, he asked himself:

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 11 / 33


The Problem of roses
The story

Alex is a second year bachelor student at Faculty of


Mathematics and Informatics from West University of
Timisoara. He fell in love with a colleague of his own year and
after a few days he decided that it’s time to declare her his
love. So, he went to the flower shop to buy her a bouquet of
7 roses. The flower shop had white, yellow and red roses. And
because he is a computer scientist, he asked himself:
1 How many different flowers bouquets can be obtained for his
future girlfriend?

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 11 / 33


The Problem of roses
The story

Alex is a second year bachelor student at Faculty of


Mathematics and Informatics from West University of
Timisoara. He fell in love with a colleague of his own year and
after a few days he decided that it’s time to declare her his
love. So, he went to the flower shop to buy her a bouquet of
7 roses. The flower shop had white, yellow and red roses. And
because he is a computer scientist, he asked himself:
1 How many different flowers bouquets can be obtained for his
future girlfriend?
2 Which one is the most beautiful?

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 11 / 33


The Problem of roses
The story

Alex is a second year bachelor student at Faculty of


Mathematics and Informatics from West University of
Timisoara. He fell in love with a colleague of his own year and
after a few days he decided that it’s time to declare her his
love. So, he went to the flower shop to buy her a bouquet of
7 roses. The flower shop had white, yellow and red roses. And
because he is a computer scientist, he asked himself:
1 How many different flowers bouquets can be obtained for his
future girlfriend?
2 Which one is the most beautiful?
Alex needs some help:

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 11 / 33


The Problem of roses
The story

Alex is a second year bachelor student at Faculty of


Mathematics and Informatics from West University of
Timisoara. He fell in love with a colleague of his own year and
after a few days he decided that it’s time to declare her his
love. So, he went to the flower shop to buy her a bouquet of
7 roses. The flower shop had white, yellow and red roses. And
because he is a computer scientist, he asked himself:
1 How many different flowers bouquets can be obtained for his
future girlfriend?
2 Which one is the most beautiful?
Alex needs some help:
1 to compute the number of all possible bouquets;

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 11 / 33


The Problem of roses
The story

Alex is a second year bachelor student at Faculty of


Mathematics and Informatics from West University of
Timisoara. He fell in love with a colleague of his own year and
after a few days he decided that it’s time to declare her his
love. So, he went to the flower shop to buy her a bouquet of
7 roses. The flower shop had white, yellow and red roses. And
because he is a computer scientist, he asked himself:
1 How many different flowers bouquets can be obtained for his
future girlfriend?
2 Which one is the most beautiful?
Alex needs some help:
1 to compute the number of all possible bouquets;
2 to write all the bouquet options.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 11 / 33


The Problem of roses

1 It is a Permutation problem or a Combination problem?


Explain;

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 12 / 33


The Problem of roses

1 It is a Permutation problem or a Combination problem?


Explain;
2 It is a Combination with Repetitions problem or without
repetitions? Explain.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 12 / 33


The Problem of roses

1 It is a Permutation problem or a Combination problem?


Explain;
2 It is a Combination with Repetitions problem or without
repetitions? Explain.
3 How to compute Combinations with repetitions?

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 12 / 33


The Problem of roses

1 It is a Permutation problem or a Combination problem?


Explain;
2 It is a Combination with Repetitions problem or without
repetitions? Explain.
3 How to compute Combinations with repetitions?
4 How to generate all the bouquet options?
Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 12 / 33
Combinations
The binary representation of subsets

An r -combination of a set A = {a1 , a2 , . . . , an } is a subset with r


elements of A.
There is a bijective correspondence between the set of n-bit strings and
the set of subsets of A: 
1 if an−i ∈ B
B ⊆ A 7→ bn−1 bn−2 . . . b0 where bi =
0 otherwise.
n-bit string b0 b1 . . . bn−1 7→ subset {an−i | bi = 1} of A

Example
A = {a1 , a2 , a3 , a4 , a5 } where a1 = a, a2 = b, a3 = c, a4 = d, a5 = e.

∅ ↔ 00000 {a, b} ↔ 00011 {c, d, e} ↔ 11100


{a} ↔ 00001 {a, c} ↔ 00101 {b, c, d, e} ↔ 11110
{b} ↔ 00010 {a, d} ↔ 01001 {a, b, d, e} ↔ 11011
{c} ↔ 00100 {a, e} ↔ 10001 {a, c, d, e} ↔ 11101
{d} ↔ 01000 {b, c} ↔ 00110 {a, b, c, d} ↔ 01111
{e} ↔ 10000 ... {a, b, c, d, e} ↔ 11111

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 13 / 33


The n-bit string encoding of a subset

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

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 14 / 33


The subset of an n-bit string encoding

Combination(b[0..n-1]: bit string,


A: ordered set {a1 , . . . , an })
B:=∅
for i:=0 to n − 1 do
if b[i] = 1 then
add an−i to B
return B

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 15 / 33


The ordering of combinations via bit string encodings
There is a bijective correspondence between the n-bit string encodings
and the numbers from 0 to 2n − 1:
n−1
X
B n-bit-string b[0 .. n − 1] 7→ number b[i] · 2i ∈ {0, 1, . . . , 2n − 1}
i=0
n
B number 0 ≤ r < 2 7→ n-bit-string b[0 .. n − 1] where
jc k
i
b[i] := i where ci is the remainder of dividing r with 2i+1 .
2

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

where b[0 .. n − 1] is the n-bit-string encoding of B as subset of A.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 16 / 33


The ordering of combinations via bit string encodings

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

Remark. This way of enumerating the subsets of a set is called


canonic ordering, and the 3-bit string b2 b1 b0 is called canonic (or
binary) code.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 17 / 33


The ordering of combinations via bit string encodings (2)

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

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 18 / 33


The ordering of combinations via bit string encodings (3)

Given an ordered set A = {a0 , a1 , . . . , an−1 }, and 0 ≤ r < 2n


Find the subset B of A with rank r

0 ≤ r < 2n

n-bit string encoding b[0 .. n − 1]


b[i] := bc/2i c where c := r mod 2i+1 Unrank(A, r )
for 0 ≤ i < n

Combination(b, A)

B := {ai | b[n − i − 1] = 1}

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 19 / 33


Enumerating subsets in minimum change order
Grey codes

Frank Grey discovered in 1953 a method to enumerate subsets in an


order so that adjacent subsets differ by the insertion or deletion of
only one element.
His enumeration scheme is called standard reflected Grey code.

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}

The 3-bit-string encodings b0 b1 b2 of these subsets are

000, 100, 110, 010, 011, 111, 101, 001

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 20 / 33


The standard reflected Grey code
Description

We want to enumerate the subsets of A = {a1 , . . . , an } in


minimum change order Gn . (Gn is the list of those subsets)

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 21 / 33


The standard reflected Grey code
Description

We want to enumerate the subsets of A = {a1 , . . . , an } in


minimum change order Gn . (Gn is the list of those subsets)
We proceed recursively:
1 Compute the list Gn−1 of subsets of B = {a2 , . . . , an } in the
minimum change order of Gray.
2 0
Let Gn−1 be the list of subsets obtained by adding a1 to every
element of a reversed copy fo Gn−1 .
3 0
Gn is the concatenation of Gn−1 with Gn−1 .

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 21 / 33


Properties of Grey’s codes reflected

Assume that B is a subset of the ordered set A with n elements.


If
m is the
P rank of iB in the order of the Grey’s enumeration and
m = n−1 i=0 bi · 2
The codification as a n bit string of B is c0 c1 . . . cn−1
then
ci = (bi + bi+1 ) mod 2 for all 0 ≤ i < n, where bn = 0.
On the other hand, one can prove that

bi = (ci + ci+1 + . . . + cn−1 ) mod 2 for all 0 ≤ i < n.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 22 / 33


Grey’s codes

Example (A = {a, b, c} with a < b < c)

subset Grey rank b0 b1 b2 bit string rank


B m such that of B of B
m = 2i=0 b2−i 2i
P
c0 c1 c2
{} 0 000 000 0
{c} 1 100 100 4
{b, c} 2 010 110 6
{b} 3 110 010 2
{a, b} 4 001 011 3
{a, b, c} 5 101 111 7
{a, c} 6 011 101 5
{a} 7 111 001 1

Notice that ci = (bi + bi+1 ) mod 2 for all 0 ≤ i < 3, where


b3 = 0.
Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 23 / 33
Exercises

1 Use the equations in the previous slide to implement the


ordering method RankGrey(B,A) and the enumeration
method UnrankGrey(A,r) for enumerating the subsets based
on Grey’s codes.
2 Define the method NextGreyRankSubset(A,B) which
computes the subset of A which is the immediately next one
after the subset B in the enumeration of subsets based on
Grey’s codes.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 24 / 33


k-combinations
Generate the k-combinations

Given an ordered set A with n elements and 0 ≤ k ≤ n.


Generate all the k-combinations of A.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 25 / 33


k-combinations
Generate the k-combinations

Given an ordered set A with n elements and 0 ≤ k ≤ n.


Generate all the k-combinations of A.
Method 1 (naive and inefficient): generate and test
1 Generate all the 2n subsets of A
2 Eliminate the generated subsets which do not have k
elements.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 25 / 33


k-combinations
Generate the k-combinations

Given an ordered set A with n elements and 0 ≤ k ≤ n.


Generate all the k-combinations of A.
Method 1 (naive and inefficient): generate and test
1 Generate all the 2n subsets of A
2 Eliminate the generated subsets which do not have k
elements.
Method 2 (simple recursion): If A = {a} ∪ B where a 6∈ B is the
smallest element of A then
1 Generate the list L1 of all (k − 1)-combinations of B, and let
L2 be the list of all k-combinations of B.
2 Let L3 be the list obtained by adding a to all the elements of
L1 .
3 Return the result of the concatenation of L2 with L3 .

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 25 / 33


The Lexicographic Ordering of k-combinations
Request. Preliminary remarks (1)

Assume A = {1, 2, . . . , n} and X = {x1 , x2 , . . . , xk } ⊆ A such that


x1 < x2 < . . . < xk .
Q: Which is the rank of X in the lexicographic enumeration of the
k-combinations of A?
The k-combinations which occur before X in lexicographic order are of 2
kinds:
1 The ones which contain an element smaller than x1 .
2 The ones which contain the minimum element x1 , but the rest of
the elements is a (k − 1)-combination smaller than {x2 , x3 , . . . , xk }.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 26 / 33


The Lexicographic Ordering of k-combinations
Request. Preliminary remarks (1)

Assume A = {1, 2, . . . , n} and X = {x1 , x2 , . . . , xk } ⊆ A such that


x1 < x2 < . . . < xk .
Q: Which is the rank of X in the lexicographic enumeration of the
k-combinations of A?
The k-combinations which occur before X in lexicographic order are of 2
kinds:
1 The ones which contain an element smaller than x1 .
2 The ones which contain the minimum element x1 , but the rest of
the elements is a (k − 1)-combination smaller than {x2 , x3 , . . . , xk }.
⇒ the rank of X in the lexicographic enumeration of the k-combinations
of A is N1 + N2 where
B N1 is the number of k-combinations of the first kind
B N2 is the number of the k-combinations of the second kind

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 26 / 33


The lexicographic ordering of k-combinations
Preliminary remarks (2)

Hypothesis: A = {1, 2, . . . , n}.


How can we compute N1 ?

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 27 / 33


The lexicographic ordering of k-combinations
Preliminary remarks (2)

Hypothesis: A = {1, 2, . . . , n}.


How can we compute N1 ?
The number of k-combinations of A which contain i as the smallest
element is

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 27 / 33


The lexicographic ordering of k-combinations
Preliminary remarks (2)

Hypothesis: A = {1, 2, . . . , n}.


How can we compute N1 ?
The number of k-combinations of A which contain i as the smallest
n−i
element is k−1

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 27 / 33


The lexicographic ordering of k-combinations
Preliminary remarks (2)

Hypothesis: A = {1, 2, . . . , n}.


How can we compute N1 ?
The number of k-combinations
Px1 −1of n−i
A which contain i as the smallest
n−i

element is k−1 ⇒ N1 = i=1 k−1 (the sum rule)

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 27 / 33


The lexicographic ordering of k-combinations
Preliminary remarks (2)

Hypothesis: A = {1, 2, . . . , n}.


How can we compute N1 ?
The number of k-combinations
Px1 −1of n−i
A which contain i as the smallest
n−i

element is k−1 ⇒ N1 = i=1 (the sum rule)
n
 n−1
 n−1
k−1
We know that k = k−1 + k (see lecture 1)

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 27 / 33


The lexicographic ordering of k-combinations
Preliminary remarks (2)

Hypothesis: A = {1, 2, . . . , n}.


How can we compute N1 ?
The number of k-combinations
Px1 −1of n−i
A which contain i as the smallest
n−i

element is k−1 ⇒ N1 = i=1 (the sum rule)
n
 n−1
 n−1
k−1
We know that k = k−1 + k (see lecture 1)
Px1 −1  n−i+1 n−i
 n
 n−x1 +1

⇒ N1 = i=1 k − k = k − k

How can we compute N2 ?

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 27 / 33


The lexicographic ordering of k-combinations
Preliminary remarks (2)

Hypothesis: A = {1, 2, . . . , n}.


How can we compute N1 ?
The number of k-combinations
Px1 −1of n−i
A which contain i as the smallest
n−i

element is k−1 ⇒ N1 = i=1 (the sum rule)
n
 n−1
 n−1
k−1
We know that k = k−1 + k (see lecture 1)
Px1 −1  n−i+1 n−i
 n
 n−x1 +1

⇒ N1 = i=1 k − k = k − k

How can we compute N2 ?


N2 is the rank of {x2 , . . . , xk } in the lexicographic enumeration of
the (k − 1)-combinations of {x1 + 1, x1 + 2, . . . , n − 1, n}

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 27 / 33


The lexicographic ordering of k-combinations
Preliminary remarks (2)

Hypothesis: A = {1, 2, . . . , n}.


How can we compute N1 ?
The number of k-combinations
Px1 −1of n−i
A which contain i as the smallest
n−i

element is k−1 ⇒ N1 = i=1 (the sum rule)
n
 n−1
 n−1
k−1
We know that k = k−1 + k (see lecture 1)
Px1 −1  n−i+1 n−i
 n
 n−x1 +1

⇒ N1 = i=1 k − k = k − k

How can we compute N2 ?


N2 is the rank of {x2 , . . . , xk } in the lexicographic enumeration of
the (k − 1)-combinations of {x1 + 1, x1 + 2, . . . , n − 1, n}
⇒ N2 can be computed recursively.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 27 / 33


The lexicographic ordering of the k-combinations

From the previous remarks results the following recursive implementation


for computing the rank:
RankKSubset({x1 , . . . , xk }, {`, . . . , n}) computes the rank in
lexicographic order of the k-combination {x1 , . . . , xk } of the ordered
set {`, ` + 1, . . . , n − 1, n}. Assume that x1 < x2 < . . . < xk .

RankKSubset({x1 , . . . , xk }, {`, ` + 1, . . . , n})


if (n = k or k=0)
return 0,
p := x1 − ` + 1
if (k = 1)
return p − 1
else
return kn − n−p+1
 
k
+ RankKSubset({x2 , . . . , xk }, {x1 + 1, . . . , n})

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 28 / 33


The lexicographic enumeration of k-combinations
Request. Preliminary remarks

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 .]


Q̂: Which are the values x1 , x2 , . . . , xk ?

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 29 / 33


The lexicographic enumeration of k-combinations
Request. Preliminary remarks

1 The total number of k-combinations of A which contain the element < x1 is


1 −1 
xX
n − i  n  n − x1 + 1
= − ≤ m. (1)
i=1
k −1 k k
n−i 
where k−1 is the number of k-combinations in which the smallest element is
i ∈ {1, . . . , x1 − 1}. This number is ≤ m because all these k-combinations are
lexicographic smaller than X , which has the rank m.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 30 / 33


The lexicographic enumeration of k-combinations
Request. Preliminary remarks

1 The total number of k-combinations of A which contain the element < x1 is


1 −1 
xX
n − i  n  n − x1 + 1
= − ≤ m. (1)
i=1
k −1 k k
n−i 
where k−1 is the number of k-combinations in which the smallest element is
i ∈ {1, . . . , x1 − 1}. This number is ≤ m because all these k-combinations are
lexicographic smaller than X , which has the rank m.
2 The total number of k-combinations of A which contain an element ≤ x1 is
x1 
X n−i n  n − x 
1
= − > m. (2)
i=1
k −1 k k
n−i 
where k−1 is the number of k-combinations in which the smallest element is
i ∈ {1, . . . , x1 }. This number is > m because there are m + 1 integers i between
0 and the rank of X (which is m), and all the k-combinations with such a rank i
contain one element ≤ x1 .

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 30 / 33


The lexicographic enumeration of k-combinations
Request. Preliminary remarks

1 The total number of k-combinations of A which contain the element < x1 is


1 −1 
xX
n − i  n  n − x1 + 1
= − ≤ m. (1)
i=1
k −1 k k
n−i 
where k−1 is the number of k-combinations in which the smallest element is
i ∈ {1, . . . , x1 − 1}. This number is ≤ m because all these k-combinations are
lexicographic smaller than X , which has the rank m.
2 The total number of k-combinations of A which contain an element ≤ x1 is
x1 
X n−i n  n − x 
1
= − > m. (2)
i=1
k −1 k k
n−i 
where k−1 is the number of k-combinations in which the smallest element is
i ∈ {1, . . . , x1 }. This number is > m because there are m + 1 integers i between
0 and the rank of X (which is m), and all the k-combinations with such a rank i
contain one element ≤ x1 .

⇒ one can use (1) and (2) to find x1 : kn − n−xk1 +1 ≤ m < kn − n−x
   1

k

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 30 / 33


The lexicographic enumeration of k-combinations
Request. Preliminary remarks

1 The total number of k-combinations of A which contain the element < x1 is


1 −1 
xX
n − i  n  n − x1 + 1
= − ≤ m. (1)
i=1
k −1 k k
n−i 
where k−1 is the number of k-combinations in which the smallest element is
i ∈ {1, . . . , x1 − 1}. This number is ≤ m because all these k-combinations are
lexicographic smaller than X , which has the rank m.
2 The total number of k-combinations of A which contain an element ≤ x1 is
x1 
X n−i n  n − x 
1
= − > m. (2)
i=1
k −1 k k
n−i 
where k−1 is the number of k-combinations in which the smallest element is
i ∈ {1, . . . , x1 }. This number is > m because there are m + 1 integers i between
0 and the rank of X (which is m), and all the k-combinations with such a rank i
contain one element ≤ x1 .

⇒ one can use (1) and (2) to find x1 : kn − n−xk1 +1 ≤ m < kn − n−x
   1

k

The other elements x2 , . . . , xk can be computed recursively.


Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 30 / 33
The lexicographic enumeration of k-combinations

UnrankKSubset(m, k, {a1 , . . . , an }) produces the k-combination


{x1 , . . . , xk } with rank m of {a1 , . . . , an } in lexicographic order.
Assume that x1 < . . . < xk and a1 < . . . < an .
UnrankKSubset(m, k, {a1 , . . . , an })
if (k = 1)
return ak+1
else if (m = 0)
return {a1 , . . . , am }
else
u := kn

i := 1 
while ki < u − m
i++
x1:=n − (i − 1)
n−x1+1
return {an−i+1 } ∪ UnrankKSubset(m − u + k
,k − 1,{an−i+2 , . . . , an })

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 31 / 33


Solution for the Problem of roses
(slide 11)

Now that we have all the necessary informations, we get back to


the Problem of roses.

1 How many different flowers bouquets can be obtained for


Alex’s future girlfriend?
2 Which one is the most beautiful?
Answers ???
Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 32 / 33
References

S. Pemmaraju, S. Skiena. Combinatorics and Graph Theory


with Mathematica. Section 2.3: Combinations. Cambridge
University Press. 2003.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 3 33 / 33

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy