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

Generating Permutations. Ranking and Unranking Permutations. The Pigeonhole Principle. The Inclusion and Exclusion Principle

The document discusses operations with permutations such as ranking and unranking permutations. It describes how to order permutations lexicographically so they can be discussed in terms of first, second, etc. It also describes how to generate the kth permutation directly and find the rank of a given permutation directly. The next part of the lecture will cover generating the next permutation in lexicographic order.

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)
76 views

Generating Permutations. Ranking and Unranking Permutations. The Pigeonhole Principle. The Inclusion and Exclusion Principle

The document discusses operations with permutations such as ranking and unranking permutations. It describes how to order permutations lexicographically so they can be discussed in terms of first, second, etc. It also describes how to generate the kth permutation directly and find the rank of a given permutation directly. The next part of the lecture will cover generating the next permutation in lexicographic order.

Uploaded by

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

Generating Permutations.

Ranking and Unranking Permutations.


The Pigeonhole Principle.
The Inclusion and Exclusion Principle

Isabela Drămnesc UVT

Computer Science Department,


West University of Timişoara,
Romania

08 October 2018

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 1 / 35


Remember from Lecture 1 that ...
If A is a finite set with n elements then

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


Remember from Lecture 1 that ...
If A is a finite set with n elements then
B An arrangement of r out of n (or r -permutation) of A is an
ordered sequence of elements from A.

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


Remember from Lecture 1 that ...
If A is a finite set with n elements then
B An arrangement of r out of n (or r -permutation) of A is an
ordered sequence of elements from A.
B A permutation of A is an ordered sequence of all elements of A.

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


Remember from Lecture 1 that ...
If A is a finite set with n elements then
B An arrangement of r out of n (or r -permutation) of A is an
ordered sequence of elements from A.
B A permutation of A is an ordered sequence of all elements of A.
B A combination of r out of n from A is a subset with r
elements from A.

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


Remember from Lecture 1 that ...
If A is a finite set with n elements then
B An arrangement of r out of n (or r -permutation) of A is an
ordered sequence of elements from A.
B A permutation of A is an ordered sequence of all elements of A.
B A combination of r out of n from A is a subset with r
elements from A.

Example (A = {a1 , a2 , a3 })
B The 2-permutations of A are (order is important!):

B The 2-combinations of A are (order is irrelevant!):

B The permutations of A are:

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


Remember from Lecture 1 that ...
If A is a finite set with n elements then
B An arrangement of r out of n (or r -permutation) of A is an
ordered sequence of elements from A.
B A permutation of A is an ordered sequence of all elements of A.
B A combination of r out of n from A is a subset with r
elements from A.

Example (A = {a1 , a2 , a3 })
B The 2-permutations of A are (order is important!):
ha1 , a2 i, ha2 , a1 i, ha1 , a3 i, ha3 , a1 i, ha2 , a3 i, ha3 , a1 i
B The 2-combinations of A are (order is irrelevant!):

B The permutations of A are:

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


Remember from Lecture 1 that ...
If A is a finite set with n elements then
B An arrangement of r out of n (or r -permutation) of A is an
ordered sequence of elements from A.
B A permutation of A is an ordered sequence of all elements of A.
B A combination of r out of n from A is a subset with r
elements from A.

Example (A = {a1 , a2 , a3 })
B The 2-permutations of A are (order is important!):
ha1 , a2 i, ha2 , a1 i, ha1 , a3 i, ha3 , a1 i, ha2 , a3 i, ha3 , a1 i
B The 2-combinations of A are (order is irrelevant!):
{a1 , a2 }, {a1 , a3 }, {a2 , a3 }
B The permutations of A are:

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


Remember from Lecture 1 that ...
If A is a finite set with n elements then
B An arrangement of r out of n (or r -permutation) of A is an
ordered sequence of elements from A.
B A permutation of A is an ordered sequence of all elements of A.
B A combination of r out of n from A is a subset with r
elements from A.

Example (A = {a1 , a2 , a3 })
B The 2-permutations of A are (order is important!):
ha1 , a2 i, ha2 , a1 i, ha1 , a3 i, ha3 , a1 i, ha2 , a3 i, ha3 , a1 i
B The 2-combinations of A are (order is irrelevant!):
{a1 , a2 }, {a1 , a3 }, {a2 , a3 }
B The permutations of A are:
ha1 , a2 , a3 i, ha1 , a3 , a2 i, ha2 , a1 , a3 i, ha2 , a3 , a1 i,
ha3 , a1 , a2 i, ha3 , a2 , a1 i

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


Operations with permutations

In the first part of this lecture we will learn


How to order permutations, such that we can talk about:
B the first permutation, the second permutation, a.s.o.
How to generate directly the k-th permutation
How to find directly the rank of a given permutation.

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


Relations of order for r -permutations

Assume A is a finite set with n elements.


1 First, we order the elements of set A

⇒ A = {a1 , a2 , . . . , an } where
a1 = first element
...
an = the n-th element.
⇒ A becomes an ordered set (an alphabet) in which
a1 < a2 < . . . < an .
2 the r -permutations are “words” hb1 , ..., br i of length r which
we order like the words in a dictionary, for example:
ha1 , a2 i < ha1 , a3 i < ha2 , a1 i < . . .
This way of ordering r -permutations is called lexicographic
ordering:
hb1 , . . . , br i < hc1 , . . . , cr i if there is a position k such that
bi = ci for 1 ≤ i < k, and bk < ck .

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 4 / 35


Relations of order for r -permutations
Preliminaries

Let A = {a1 , . . . , an } be an ordered set with a1 < . . . < an


and N = {1, 2, . . . , n}.
1 The r -permutations of A are “words” of the form hai1 , . . . , air i
with i1 , . . . , ir ∈ N.
2 hai1 , . . . , air i is an r -permutation of A if and only if (i1 , . . . , ir )
is an r -permutation of N.
3 hai1 , . . . , air i < haj1 , . . . , ajr i if and only if
hi1 , . . . , ir i < hj1 , . . . , jr i.
⇒ it is sufficient to know how to order and to enumerate the
r -permutations of numbers from the set N.

From now on we will consider only the r -permutations of the


ordered set A = {1, . . . , n}.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 5 / 35


Rank of an r -permutation

The rank of an r -permutation is the position where the


r -permutation occurs in lexicographic order, starting from position
0.
Example (A = {1, 2, 3})
2-permutation rank permutation rank
h1, 2i 0 h1, 2, 3i 0
h1, 3i 1 h1, 3, 2i 1
h2, 1i 2 h2, 1, 3i 2
h2, 3i 3 h2, 3, 1i 3
h3, 1i 4 h3, 1, 2i 4
h3, 2i 5 h3, 2, 1i 5

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 6 / 35


Generating the next permutation in lexicographic order

Problem
How can we compute directly (and reasonably fast) the
permutation of N = {1, . . . , n} that is after the permutation
hp1 , . . . , pn i in lexicographic order?

Example (N = {1, 2, 3, 4, 5})


permutation next permutation
h5, 1, 3, 2, 4i
h5, 2, 4, 3, 1i
h5, 4, 3, 2, 1i

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 7 / 35


Generating the next permutation in lexicographic order

Problem
How can we compute directly (and reasonably fast) the
permutation of N = {1, . . . , n} that is after the permutation
hp1 , . . . , pn i in lexicographic order?

Example (N = {1, 2, 3, 4, 5})


permutation next permutation
h5, 1, 3, 2, 4i h5, 1, 3, 4, 2i
h5, 2, 4, 3, 1i
h5, 4, 3, 2, 1i

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 7 / 35


Generating the next permutation in lexicographic order

Problem
How can we compute directly (and reasonably fast) the
permutation of N = {1, . . . , n} that is after the permutation
hp1 , . . . , pn i in lexicographic order?

Example (N = {1, 2, 3, 4, 5})


permutation next permutation
h5, 1, 3, 2, 4i h5, 1, 3, 4, 2i
h5, 2, 4, 3, 1i h5, 3, 1, 2, 4i
h5, 4, 3, 2, 1i

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 7 / 35


Generating the next permutation in lexicographic order

Problem
How can we compute directly (and reasonably fast) the
permutation of N = {1, . . . , n} that is after the permutation
hp1 , . . . , pn i in lexicographic order?

Example (N = {1, 2, 3, 4, 5})


permutation next permutation
h5, 1, 3, 2, 4i h5, 1, 3, 4, 2i
h5, 2, 4, 3, 1i h5, 3, 1, 2, 4i
h5, 4, 3, 2, 1i none

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 7 / 35


Operations with permutations
Enumerating the permutations in lexicographic order

The permutation after hp1 , . . . , pn i in lexicographic order can be


computed as follows:

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 8 / 35


Operations with permutations
Enumerating the permutations in lexicographic order

The permutation after hp1 , . . . , pn i in lexicographic order can be


computed as follows:
1 Find i such that pi > . . . > pn is the longest decreasing suffix
of hp1 , . . . , pn i

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 8 / 35


Operations with permutations
Enumerating the permutations in lexicographic order

The permutation after hp1 , . . . , pn i in lexicographic order can be


computed as follows:
1 Find i such that pi > . . . > pn is the longest decreasing suffix
of hp1 , . . . , pn i
2 Find j ≥ i such that pj is the smallest number greater than
pi−1 .

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 8 / 35


Operations with permutations
Enumerating the permutations in lexicographic order

The permutation after hp1 , . . . , pn i in lexicographic order can be


computed as follows:
1 Find i such that pi > . . . > pn is the longest decreasing suffix
of hp1 , . . . , pn i
2 Find j ≥ i such that pj is the smallest number greater than
pi−1 .
3 Permute pj with pi−1 , and then reverse the suffix pi , . . . , pn .

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 8 / 35


Operations with permutations
Enumerating the permutations in lexicographic order

The permutation after hp1 , . . . , pn i in lexicographic order can be


computed as follows:
1 Find i such that pi > . . . > pn is the longest decreasing suffix
of hp1 , . . . , pn i
2 Find j ≥ i such that pj is the smallest number greater than
pi−1 .
3 Permute pj with pi−1 , and then reverse the suffix pi , . . . , pn .

Example
hp1 , p2 , p3 , p4 , p5 i = h5, 2, 4, 3, 1i
h5, 2, 4, 3, 1i

i =3

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 8 / 35


Operations with permutations
Enumerating the permutations in lexicographic order

The permutation after hp1 , . . . , pn i in lexicographic order can be


computed as follows:
1 Find i such that pi > . . . > pn is the longest decreasing suffix
of hp1 , . . . , pn i
2 Find j ≥ i such that pj is the smallest number greater than
pi−1 .
3 Permute pj with pi−1 , and then reverse the suffix pi , . . . , pn .

Example
hp1 , p2 , p3 , p4 , p5 i = h5, 2, 4, 3, 1i
h5, 2, 4, 3, 1i
swap values of pi−1 = 2 and pj = 3
i =3 j =4

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 8 / 35


Operations with permutations
Enumerating the permutations in lexicographic order

The permutation after hp1 , . . . , pn i in lexicographic order can be


computed as follows:
1 Find i such that pi > . . . > pn is the longest decreasing suffix
of hp1 , . . . , pn i
2 Find j ≥ i such that pj is the smallest number greater than
pi−1 .
3 Permute pj with pi−1 , and then reverse the suffix pi , . . . , pn .

Example
hp1 , p2 , p3 , p4 , p5 i = h5, 2, 4, 3, 1i
h5, 3, 4, 2, 1i invert hpi , . . . , pn i = h4, 2, 1i

i =3

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 8 / 35


Operations with permutations
Enumerating the permutations in lexicographic order

The permutation after hp1 , . . . , pn i in lexicographic order can be


computed as follows:
1 Find i such that pi > . . . > pn is the longest decreasing suffix
of hp1 , . . . , pn i
2 Find j ≥ i such that pj is the smallest number greater than
pi−1 .
3 Permute pj with pi−1 , and then reverse the suffix pi , . . . , pn .

Example
hp1 , p2 , p3 , p4 , p5 i = h5, 2, 4, 3, 1i
h5, 3, 1, 2, 4i = next permutation

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 8 / 35


Enumeration of permutations in lexicographic order
Pseudocode

NextPermutation(p: int[0 .. n − 1])


i := n − 2;
while (p[i] > p[i + 1])
i--;
j := n − 1;
while (p[j] < p[i])
j--;
// swap p[i] with p[j]
tmp := p[i];
p[i] := p[j];
p[j] := tmp;
// revert (p[i+1], ..., p[n-1])
for (k := 0; k < b(n − i − 1)/2c; k++)
// swap p[i + 1 + k] with p[n − 1 − k]
tmp := p[i + 1 + k];
p[i + 1 + k] := p[n − 1 − k];
p[n − 1 − k] := tmp;
return p;
Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 9 / 35
Operations with permutations

Problems
1 How to compute directly the rank of a permutation

hp1 , . . . , pn i of N = {1, . . . , n} in lexicographic order?


2 How to compute directly the permutation hp1 , . . . , pn i of
N = {1, . . . , n} with rank k?
Note that the rank is a number between 0 and n! − 1.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 10 / 35


Computing the rank of a permutation

Let r be the rank of a permutation hp1 , . . . , pn i.


B If p1 = 1 then 0 ≤ r < (n − 1)!
B If p1 = 2 then (n − 1)! ≤ r < 2 · (n − 1)!
...
B If p1 = k then (k − 1) · (n − 1)! ≤ r < k · (n − 1)!
...
B If p1 = n then (n − 1) · (n − 1)! ≤ r < n · (n − 1)! = n!

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 11 / 35


Computing the rank of a permutation

Let r be the rank of a permutation hp1 , . . . , pn i.


B If p1 = 1 then 0 ≤ r < (n − 1)!
B If p1 = 2 then (n − 1)! ≤ r < 2 · (n − 1)!
...
B If p1 = k then (k − 1) · (n − 1)! ≤ r < k · (n − 1)!
...
B If p1 = n then (n − 1) · (n − 1)! ≤ r < n · (n − 1)! = n!
⇒ in general, (p1 − 1) · (n − 1)! ≤ r < p1 · (n − 1)!

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 11 / 35


Computing the rank of a permutation

Let r be the rank of a permutation hp1 , . . . , pn i.


B If p1 = 1 then 0 ≤ r < (n − 1)!
B If p1 = 2 then (n − 1)! ≤ r < 2 · (n − 1)!
...
B If p1 = k then (k − 1) · (n − 1)! ≤ r < k · (n − 1)!
...
B If p1 = n then (n − 1) · (n − 1)! ≤ r < n · (n − 1)! = n!
⇒ in general, (p1 − 1) · (n − 1)! ≤ r < p1 · (n − 1)!
⇒ rank of hp1 , . . . , pn i = (p1 − 1) · (n − 1)! +
rank of hp2 , . . . , pn i in the
lexicographic enumeration of
the permutations of N − {p1 }

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 11 / 35


Computing the rank of a permutation

Let r be the rank of a permutation hp1 , . . . , pn i.


B If p1 = 1 then 0 ≤ r < (n − 1)!
B If p1 = 2 then (n − 1)! ≤ r < 2 · (n − 1)!
...
B If p1 = k then (k − 1) · (n − 1)! ≤ r < k · (n − 1)!
...
B If p1 = n then (n − 1) · (n − 1)! ≤ r < n · (n − 1)! = n!
⇒ in general, (p1 − 1) · (n − 1)! ≤ r < p1 · (n − 1)!
⇒ rank of hp1 , . . . , pn i = (p1 − 1) · (n − 1)! +
rank of hp2 , . . . , pn i in the
lexicographic enumeration of
the permutations of N − {p1 }
⇒ r can be computed recursively.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 11 / 35


Computing the rank of a permutation

Example
The permutation hp1 , p2 , p3 , p4 , p5 i = h2, 3, 1, 5, 4i has rank
r = (2 − 1) · (5 − 1)!+ rank of h3, 1, 5, 4i in the lex. order of
the permutations of {1, 3, 4, 5}.

rank of h3, 1, 5, 4i in the lex. order of the permutations of


{1, 3, 4, 5} coincides with
rank of h2, 1, 4, 3i in the lex. order of the permutations of
{1, 2, 3, 4}
(the values of all elements p1 = 2 were decreased by 1)

By recursion, we find out that the rank of h2, 1, 4, 3i is 7.


⇒ rank of h2, 3, 1, 5, 4i is 24 + 7 = 31.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 12 / 35


Operations with permutations
Pseudocode

Rank(p : int[0 .. n − 1])


if n == 1
return 0
else
q : int[0 .. n − 2];
// adjust p[1..n-1] to become a permutation of {1, ..., n − 1}
// memorized in the array q[0 .. n − 2]
for(i := 1; i ≤ n − 1; i++)
if(p[i] < p[0])
q[i − 1] = p[i];
else
q[i − 1] = p[i] − 1;
return Rank[q] + (p[0] − 1) · (n − 1)!

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 13 / 35


Computing the permutation with a given rank

We look for an algorithm to compute directly the permutation


hp1 , . . . , pn i with rank r when 0 ≤ r < n!.
We already noticed that if the permutation hp1 , . . . , pn i has
rank r , then (p1 − 1) · (n − 1)! ≤ r < p1 · (n − 1)!
 
r
⇒ p1 = +1
(n − 1)!
⇒ If (q1 , . . . , qn−1 ) is the permutation with rank
r − (p1 − 1) · (n − 1)! then

qi if qi < p1 ,
pi+1 =
qi + 1 if qi ≥ p1 .

for all 1 ≤ i < n.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 14 / 35


Minimum change permutations

There are many other orders to generate all permutations,


different from the lexicographic order.
Often, we want the fast generation of all permutations:
B This means to generate very fast the next permutation from
the previous one.
B In 1963, Heap discovered an algorithm that generates the next
permutation by exchanging the values of only two elements.
Heap’s algorithm is the fastest known algorithm to
generate all permutations.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 15 / 35


Algorithms for the fast generation of all permutations
Heap’s algorithm: pseudocode

for(i := 1; i ≤ n; i++)
p[i] := i
for(c := 1; c ≤ n; c++)
1. generate all permutations hp[1], . . . , p[n − 1]i without modifying p[n];
(at the end of step 1, p contains the last generated permutation)
2. swap the value of p[n] with that of p[f (n,  c)]
1 if n is odd,
where f (n, c) =
c if n is even.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 16 / 35


Algorithms for the fast generation of all permutations
Heap’s algorithm: pseudocode

for(i := 1; i ≤ n; i++)
p[i] := i
for(c := 1; c ≤ n; c++)
1. generate all permutations hp[1], . . . , p[n − 1]i without modifying p[n];
(at the end of step 1, p contains the last generated permutation)
2. swap the value of p[n] with that of p[f (n,  c)]
1 if n is odd,
where f (n, c) =
c if n is even.

Remarks

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 16 / 35


Algorithms for the fast generation of all permutations
Heap’s algorithm: pseudocode

for(i := 1; i ≤ n; i++)
p[i] := i
for(c := 1; c ≤ n; c++)
1. generate all permutations hp[1], . . . , p[n − 1]i without modifying p[n];
(at the end of step 1, p contains the last generated permutation)
2. swap the value of p[n] with that of p[f (n,  c)]
1 if n is odd,
where f (n, c) =
c if n is even.

Remarks
B Heap’s algorithm generates all permutations of {1, . . . , n} in an order different
from the lexicographic order.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 16 / 35


Algorithms for the fast generation of all permutations
Heap’s algorithm: pseudocode

for(i := 1; i ≤ n; i++)
p[i] := i
for(c := 1; c ≤ n; c++)
1. generate all permutations hp[1], . . . , p[n − 1]i without modifying p[n];
(at the end of step 1, p contains the last generated permutation)
2. swap the value of p[n] with that of p[f (n,  c)]
1 if n is odd,
where f (n, c) =
c if n is even.

Remarks
B Heap’s algorithm generates all permutations of {1, . . . , n} in an order different
from the lexicographic order.
B Every permutation differs from the previous one by a transposition (that is, a
swap of the values of 2 elements).

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 16 / 35


Algorithms for the fast generation of all permutations
Heap’s algorithm: pseudocode

for(i := 1; i ≤ n; i++)
p[i] := i
for(c := 1; c ≤ n; c++)
1. generate all permutations hp[1], . . . , p[n − 1]i without modifying p[n];
(at the end of step 1, p contains the last generated permutation)
2. swap the value of p[n] with that of p[f (n,  c)]
1 if n is odd,
where f (n, c) =
c if n is even.

Remarks
B Heap’s algorithm generates all permutations of {1, . . . , n} in an order different
from the lexicographic order.
B Every permutation differs from the previous one by a transposition (that is, a
swap of the values of 2 elements).

Example
Heap’s algorithm enumerates the permutations of {1, 2, 3} in the following order:

h1, 2, 3i, h2, 1, 3i, h3, 1, 2i, h1, 3, 2i, h2, 3, 1i, h3, 2, 1i

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 16 / 35


Exercises (part 1)

1 Write a program which reads a sequence of n numbers, and


then it displays:
”a permutation” if the sequence is a permutation of {1, . . . , n}
”not a permutation” otherwise.
2 Write a program which reads numbers n and
r ∈ {0, 1, . . . , n! − 1}, and then it displays the permutation
{1, . . . , n} with rank r .
3 Write a program which reads a permutation of {1, . . . , n} and
it displays the rank of that permutation.
4 Write a program which reads a permutation ha1 , . . . , an i and
computes its inverse, that is, the permutation hb1 , . . . , bn i
such that bai = abi = i for all 1 ≤ i ≤ n.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 17 / 35


Exercises (part 2)

1 Write a program which reads a permutation and computes the


next permutation in lexicographic order.
2 Write a program which reads a permutation and computes the
previous permutation in lexicographic order.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 18 / 35


The Pigeonhole Principle

Suppose that a flock of 13 pigeons flies into a set of 12 pigeonholes.


The number of holes is smaller than the number of pigeons ⇒ at
least one pigeonhole must have at least 2 pigeons in it.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 19 / 35


The Pigeonhole Principle

Suppose that a flock of 13 pigeons flies into a set of 12 pigeonholes.


The number of holes is smaller than the number of pigeons ⇒ at
least one pigeonhole must have at least 2 pigeons in it.

The Pigeonhole Principle (or Dirichlet’s Principle)


Let n be a positive integer. If more than n objects are distributed among
n containers, then some container must contain more than one object.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 19 / 35


The pigeonhole principle
Applications in combinatorial reasoning

Establish the existence of a particular configuration or combination


in many situations.
1 Suppose 367 freshmen are enrolled in the lecture on

combinatorics. Then two of them must have the same


birthday.
Proof. There are more freshmen than calendaristic days. By
pigeonhole principle, at least 2 freshmen were born in same
calendaristic day.
2 n boxers did compete in a round-robin tournament. We know
that no contestant was undefeated. Then two boxers must
have the same record in the tournament.
Proof. There are n boxers, and every boxer has between 0
and n − 2 wins. (Note that no boxer has n − 1 wins, because
we know that no boxer was undefeated.)
By pigeonhole principle, at least 2 boxers must have the same
winning record.
Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 20 / 35
The pigeonhole principle
Generalization: Let m and n be positive integers. If more than m · n
objects are distributed among n containers, then at least one container
must contain at least m + 1 objects.
Proof: by contradiction. If we place at most m objects in all
containers, then the total number of objects would be at most m · n.

Theorem
a1 + a2 + . . . + an
If a1 , a2 , . . . , an ∈ R and µ = , then there exist integers
n
i and j with 1 ≤ i, j ≤ n such that ai ≤ µ and aj ≥ µ.

Proof: by contradiction.
If every element is strictly greater than µ then µ = (a1 + a2 +
n·µ
. . . +an )/n > = µ, contradiction ⇒ ∃ai ≤ µ.
n
If every element is strictly smaller than µ then µ = (a1 + a2 +
n·µ
. . . +an )/n < = µ, contradiction ⇒ ∃aj ≥ µ.
n
Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 21 / 35
The pigeonhole principle
Application 1: Monotonic subsequences

Definition (Monotonic sequence)


A sequence a1 , a2 , . . . , an is
increasing if a1 ≤ a2 ≤ . . . ≤ an
strictly increasing if a1 < a2 < . . . < an
decreasing if a1 ≥ a2 ≥ . . . ≥ an
strictly decreasing if a1 > a2 > . . . > an

Consider the sequence 3, 5, 8, 10, 6, 1, 9, 2, 7, 4.


What are the increasing subsequences of maximal length?

h3, 5, 8, 10i, h3, 5, 8, 9i, h3, 5, 6, 7i, h3, 5, 6, 9i

What are the decreasing subsequences of maximal length?

h10, 9, 7, 4i

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 22 / 35


The pigeonhole principle
Application 1: Monotonic subsequences (continued)

Theorem
Suppose m, n ∈ N − {0}. A sequence of more than m · n real numbers
must contain either an increasing subsequence of length at least m + 1,
or a strictly decreasing subsequence of length at least n + 1.

Proof.
r1 , r2 , . . . , rm·n+1
For every 1 ≤ i ≤ m · n + 1, let
ai :=length of longest increasing subseq. starting with ri
di :=length of longest strictly decreasing subseq. starting with ri
For example, if the sequence is 3, 5, 8, 10, 6, 1, 9, 2, 7, 4 then
a2 = 3 (for the subsequence 5, 8, 10 or 5, 8, 9)
d2 = 2 (for the subsequence 5, 1 or 5, 2)

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 23 / 35


The pigeonhole principle
Application 1: Monotonic subsequences (Proof continued)

We assume the theorem is false ⇒ 1 ≤ ai ≤ m and 1 ≤ di ≤ n


⇒ the pair (ai , di ) has m · n possible values.
There are m · n + 1 such pairs ⇒ ∃i < j with (ai , di ) = (aj , dj ).
If i < j and (ai , di ) = (aj , dj ) then
1 The maximum length of increasing subsequences starting from
ri and from rj is ai .
2 The maximum length of strictly decreasing subsequences

starting from ri and from rj is di .


But this is impossible, because
1 If ri ≤ rj then there there is
length ai
z }| {
ri ≤ rj ≤ . . .
| {z }
length ai +1
2 If ri > rj then there is
length di
z }| {
ri > rj > . . . .
| {z }
length di +1
Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 24 / 35
The pigeonhole principle
Application 2: Approximating rational numbers

For every real number x ∈ R we define:

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 25 / 35


The pigeonhole principle
Application 2: Approximating rational numbers

For every real number x ∈ R we define:


The floor of x:
bxc := largest integer m satisfying m ≤ x.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 25 / 35


The pigeonhole principle
Application 2: Approximating rational numbers

For every real number x ∈ R we define:


The floor of x:
bxc := largest integer m satisfying m ≤ x.
The ceiling of x:
dxe := smallest integer m satisfying x ≤ m.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 25 / 35


The pigeonhole principle
Application 2: Approximating rational numbers

For every real number x ∈ R we define:


The floor of x:
bxc := largest integer m satisfying m ≤ x.
The ceiling of x:
dxe := smallest integer m satisfying x ≤ m.
The fractional part of x:
{x} := x − bxc

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 25 / 35


The pigeonhole principle
Application 2: Approximating rational numbers

For every real number x ∈ R we define:


The floor of x:
bxc := largest integer m satisfying m ≤ x.
The ceiling of x:
dxe := smallest integer m satisfying x ≤ m.
The fractional part of x:
{x} := x − bxc
An irrational number is a number that can not be obtained by
dividing two integers.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 25 / 35


The pigeonhole principle
Application 2: Approximating rational numbers

For every real number x ∈ R we define:


The floor of x:
bxc := largest integer m satisfying m ≤ x.
The ceiling of x:
dxe := smallest integer m satisfying x ≤ m.
The fractional part of x:
{x} := x − bxc
An irrational number is a number that can not be obtained by
dividing two integers.
Examples: π = 3.14159265 . . ., e = 2.7182818 . . ., etc.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 25 / 35


The pigeonhole principle
Application 2: Approximating rational numbers

For every real number x ∈ R we define:


The floor of x:
bxc := largest integer m satisfying m ≤ x.
The ceiling of x:
dxe := smallest integer m satisfying x ≤ m.
The fractional part of x:
{x} := x − bxc
An irrational number is a number that can not be obtained by
dividing two integers.
Examples: π = 3.14159265 . . ., e = 2.7182818 . . ., etc.
If α is an irrational number and Q ∈ N − {0}, how close can
we approximate α with a rational number qp when
1 ≤ q ≤ Q?

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 25 / 35


The pigeonhole principle
Application 2: Approximating rational numbers

For every real number x ∈ R we define:


The floor of x:
bxc := largest integer m satisfying m ≤ x.
The ceiling of x:
dxe := smallest integer m satisfying x ≤ m.
The fractional part of x:
{x} := x − bxc
An irrational number is a number that can not be obtained by
dividing two integers.
Examples: π = 3.14159265 . . ., e = 2.7182818 . . ., etc.
If α is an irrational number and Q ∈ N − {0}, how close can
we approximate α with a rational number qp when
1 ≤ q ≤ Q?
p
How small can α − become when 1 ≤ q ≤ Q?

q
Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 25 / 35
The pigeonhole principle
Application 2: Approximating rational numbers (2)

Theorem (Dirichlet’s approximation theorem)


If α is an irrational number and Q a positive integer, then there
exists a rational number p/q with 1 ≤ q ≤ Q such that

p
α − ≤ 1
.
q q · (Q + 1)

Proof. Divide [0, 1] into Q + 1 subintervals of equal length:


     
1 1 2 Q
0, , , ,..., ,1
Q +1 Q +1 Q +1 Q +1

and consider the Q + 2 real numbers

r1 = 0, r2 = {α}, {2α}, . . . , rQ+1 = {Qα}, rQ+2 = 1

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 26 / 35


The pigeonhole principle
Application 2: Approximating rational numbers (2)

There are Q + 2 objects in Q + 1 intervals

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 27 / 35


The pigeonhole principle
Application 2: Approximating rational numbers (2)

There are Q + 2 objects in Q + 1 intervals


⇒ there is i < j with ri , rj in same interval

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 27 / 35


The pigeonhole principle
Application 2: Approximating rational numbers (2)

There are Q + 2 objects in Q + 1 intervals


⇒ there is i < j with ri , rj in same interval
1
⇒ |ri − rj | ≤ Q+1 . Note that (i, j) 6= (1, Q + 2)

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 27 / 35


The pigeonhole principle
Application 2: Approximating rational numbers (2)

There are Q + 2 objects in Q + 1 intervals


⇒ there is i < j with ri , rj in same interval
1
⇒ |ri − rj | ≤ Q+1 . Note that (i, j) 6= (1, Q + 2)
We note that

r1 = 0 ·α− 0
ri = (i − 1) ·α− b(i − 1)αc if 2 ≤ i ≤ Q + 1
rQ+2 = 0 ·α− (−1)

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 27 / 35


The pigeonhole principle
Application 2: Approximating rational numbers (2)

There are Q + 2 objects in Q + 1 intervals


⇒ there is i < j with ri , rj in same interval
1
⇒ |ri − rj | ≤ Q+1 . Note that (i, j) 6= (1, Q + 2)
We note that

r1 = 0 ·α− 0
ri = (i − 1) ·α− b(i − 1)αc if 2 ≤ i ≤ Q + 1
rQ+2 = 0 ·α− (−1)
⇒ every ri is ui · α − vi with ui , vi ∈ Z, and
if i < j then ui = uj only if (i, j) = (1, Q + 2).

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 27 / 35


The pigeonhole principle
Application 2: Approximating rational numbers (2)

There are Q + 2 objects in Q + 1 intervals


⇒ there is i < j with ri , rj in same interval
1
⇒ |ri − rj | ≤ Q+1 . Note that (i, j) 6= (1, Q + 2)
We note that

r1 = 0 ·α− 0
ri = (i − 1) ·α− b(i − 1)αc if 2 ≤ i ≤ Q + 1
rQ+2 = 0 ·α− (−1)
⇒ every ri is ui · α − vi with ui , vi ∈ Z, and
if i < j then ui = uj only if (i, j) = (1, Q + 2).
v − vj
⇒ |ri − rj | = |(ui − uj )α − (vi − vj )| = |ui − uj | ·|α − i |≤ 1
Q+1 .
| {z } ui − uj
q∈[1,Q] | {z }
p
q

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 27 / 35


The pigeonhole principle
Application 2: Approximating rational numbers (2)

There are Q + 2 objects in Q + 1 intervals


⇒ there is i < j with ri , rj in same interval
1
⇒ |ri − rj | ≤ Q+1 . Note that (i, j) 6= (1, Q + 2)
We note that

r1 = 0 ·α− 0
ri = (i − 1) ·α− b(i − 1)αc if 2 ≤ i ≤ Q + 1
rQ+2 = 0 ·α− (−1)
⇒ every ri is ui · α − vi with ui , vi ∈ Z, and
if i < j then ui = uj only if (i, j) = (1, Q + 2).
v − vj
⇒ |ri − rj | = |(ui − uj )α − (vi − vj )| = |ui − uj | ·|α − i |≤ 1
Q+1 .
| {z } ui − uj
q∈[1,Q] | {z }
p
q

p 1
Thus α − ≤ .
q q · (Q + 1)
Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 27 / 35
The Principle of Inclusion and Exclusion
Illustrative example

Suppose there are 50 beads in a drawer: 25 are glass, 30 are


red, 20 are spherical, 18 are red glass, 12 are glass spheres, 15
are red spheres, and 8 are red glass spheres. How many beads
are neither red, nor glass, nor spheres?
Answer: use a Venn diagram with 3 overlapping sets: G of
glass beads, R of red beads, and S of spherical beads.

Observation. |G ∪ R ∪ S| =
|G | + |R| + |S| − |G ∩ R| − |G ∩ S| − |R ∩ S| + |G ∩ R ∩ S|.
Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 28 / 35
The Principle of Inclusion and Exclusion

Assumptions:
N: a universal set
a1 , . . . , ar : properties of the elements of set N
N(ai1 ai2 . . . aim ): the number of objects of N which have
properties ai1 , ai2 , . . . , aim simultaneously.
N0 : the number of objects having none of these properties.

Theorem (Principle of Inclusion and Exclusion)

X X X
N0 = N − N(ai ) + N(ai aj ) − N(ai aj ak ) + . . .
i i<j i<j<k
X
m
+ (−1) N(ai1 . . . aim ) + . . . + (−1)r N(a1 a2 . . . ar ).
i1 <···<im

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 29 / 35


The Principle of Inclusion and Exclusion
Application 1: The Euler ϕ function

ϕ(n):= number of integers 1 ≤ m < n with gcd(m, n) = 1.


Example: ϕ(24) = 8 because there are 8 integers between 1
and 23 that have no factor in common with 24:
1,5,7,11,13,17,19,23.
ϕ(n) is very important in number theory.
ϕ(n) can be computed using the principle of inclusion and
exclusion:
Suppose n = p1n1 . . . prnr where p1 , . . . , pr are distinct prime
numbers, and ni > 0 for 1 ≤ i ≤ r .
Let ai be the property ”smaller than n and divisible by pi ”
(1 ≤ i ≤ r )
⇒ ϕ(n)
X = N0 = X
n− N(ai ) + N(ai aj ) + . . . + (−1)r N(a1 . . . ar ).
i i<j
N(ai1 . . . , aim ) is the number of elements < n divisible by
n
pi1 · . . . · pim ⇒ N(ai1 . . . aim ) = .
pi1 · . . . · pim
Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 30 / 35
The Principle of Inclusion and Exclusion
Application 1: The Euler ϕ function

X n X n n
ϕ(n) = n − + + . . . + (−1)n
pi pi pj p1 p2 . . . pr
i i<j
r  
Y 1
=n 1− .
pi
i=1

   
1 1
Example: ϕ(24) = ϕ(23 · 3) = 24 · 1 − · 1− = 8.
2 3

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 31 / 35


The principle of inclusion and exclusion
Application 2: counting prime numbers

How many prime numbers are between 1 and n?

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 32 / 35


The principle of inclusion and exclusion
Application 2: counting prime numbers

How many prime numbers are between 1 and n?


Remark: If n is not prime, then n = a · b with 1 < a ≤ b

⇒ a2 ≤ n, so a ≤ n and n must be divisible by a prime

number p ≤ n.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 32 / 35


The principle of inclusion and exclusion
Application 2: counting prime numbers

How many prime numbers are between 1 and n?


Remark: If n is not prime, then n = a · b with 1 < a ≤ b

⇒ a2 ≤ n, so a ≤ n and n must be divisible by a prime

number p ≤ n.
⇒ Criterion to count the prime numbers < n:

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 32 / 35


The principle of inclusion and exclusion
Application 2: counting prime numbers

How many prime numbers are between 1 and n?


Remark: If n is not prime, then n = a · b with 1 < a ≤ b

⇒ a2 ≤ n, so a ≤ n and n must be divisible by a prime

number p ≤ n.
⇒ Criterion to count the prime numbers < n:
Start with the set of integers N = {1, . . . , n} and count
N0 = the number
√ of elements left when multiples of prime
numbers p ≤ n are excluded from the set.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 32 / 35


The principle of inclusion and exclusion
Application 2: counting prime numbers

How many prime numbers are between 1 and n?


Remark: If n is not prime, then n = a · b with 1 < a ≤ b

⇒ a2 ≤ n, so a ≤ n and n must be divisible by a prime

number p ≤ n.
⇒ Criterion to count the prime numbers < n:
Start with the set of integers N = {1, . . . , n} and count
N0 = the number
√ of elements left when multiples of prime
numbers p ≤ n are excluded from the set.
The number obtained is not exactly what we want because

we did not count the prime numbers ≤ n
we did count 1

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 32 / 35


The principle of inclusion and exclusion
Application 2: counting prime numbers

How many prime numbers are between 1 and n?


Remark: If n is not prime, then n = a · b with 1 < a ≤ b

⇒ a2 ≤ n, so a ≤ n and n must be divisible by a prime

number p ≤ n.
⇒ Criterion to count the prime numbers < n:
Start with the set of integers N = {1, . . . , n} and count
N0 = the number
√ of elements left when multiples of prime
numbers p ≤ n are excluded from the set.
The number obtained is not exactly what we want because

we did not count the prime numbers ≤ n
we did count 1
The number we are looking for is

N0 + r − 1

where r is the number of prime numbers ≤ n.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 32 / 35


The principle of inclusion and exclusion
Application 2: counting prime numbers

How many prime numbers are between 1 and 120?

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


The principle of inclusion and exclusion
Application 2: counting prime numbers

How many prime numbers are between 1 and 120?



The largest prime number ≤ 120 is 7

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


The principle of inclusion and exclusion
Application 2: counting prime numbers

How many prime numbers are between 1 and 120?



The largest prime number ≤ 120 is 7
Start with the universal set N = {n ∈ N | 1 ≤ n ≤ 120} and
remove from N all elements divisible by a prime number ≤ 7.
This means, we remove from N the elements with properties
a1 = ”is divisible by p1 = 2”
a2 = ”is divisible by p2 = 3”
a3 = ”is divisible by p3 = 5”
a4 = ”is divisible by p4 = 7”
and obtain a set M with N0 elements.

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


The principle of inclusion and exclusion
Application 2: counting prime numbers

How many prime numbers are between 1 and 120?



The largest prime number ≤ 120 is 7
Start with the universal set N = {n ∈ N | 1 ≤ n ≤ 120} and
remove from N all elements divisible by a prime number ≤ 7.
This means, we remove from N the elements with properties
a1 = ”is divisible by p1 = 2”
a2 = ”is divisible by p2 = 3”
a3 = ”is divisible by p3 = 5”
a4 = ”is divisible by p4 = 7”
and obtain a set M with N0 elements.
Q: Is N0 the number we want to compute?

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


The principle of inclusion and exclusion
Application 2: counting prime numbers

How many prime numbers are between 1 and 120?



The largest prime number ≤ 120 is 7
Start with the universal set N = {n ∈ N | 1 ≤ n ≤ 120} and
remove from N all elements divisible by a prime number ≤ 7.
This means, we remove from N the elements with properties
a1 = ”is divisible by p1 = 2”
a2 = ”is divisible by p2 = 3”
a3 = ”is divisible by p3 = 5”
a4 = ”is divisible by p4 = 7”
and obtain a set M with N0 elements.
Q: Is N0 the number we want to compute?
A: Almost correct, except that:
M contains all prime numbers between 1 and 120, except
p1 = 2, p2 = 3, p3 = 5, p4 = 7.
M contains 1, which is not prime.

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


The principle of inclusion and exclusion
Application 2: counting prime numbers

How many prime numbers are between 1 and 120?



The largest prime number ≤ 120 is 7
Start with the universal set N = {n ∈ N | 1 ≤ n ≤ 120} and
remove from N all elements divisible by a prime number ≤ 7.
This means, we remove from N the elements with properties
a1 = ”is divisible by p1 = 2”
a2 = ”is divisible by p2 = 3”
a3 = ”is divisible by p3 = 5”
a4 = ”is divisible by p4 = 7”
and obtain a set M with N0 elements.
Q: Is N0 the number we want to compute?
A: Almost correct, except that:
M contains all prime numbers between 1 and 120, except
p1 = 2, p2 = 3, p3 = 5, p4 = 7.
M contains 1, which is not prime.
The number of prime numbers ≤ 120 is N0 + 4 − 1.
Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 33 / 35
The principle of inclusion and exclusion
Application 2: counting prime numbers (continued)

How many prime numbers are between 1 and 120?

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 34 / 35


The principle of inclusion and exclusion
Application 2: counting prime numbers (continued)

How many prime numbers are between 1 and 120?


4
X X X
N0 = 120 − N(ai ) + N(ai aj ) − N(ai aj ak ) + N(a1 a2 a3 a4 )
i=1 i<j i<j<k

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 34 / 35


The principle of inclusion and exclusion
Application 2: counting prime numbers (continued)

How many prime numbers are between 1 and 120?


4
X X X
N0 = 120 − N(ai ) + N(ai aj ) − N(ai aj ak ) + N(a1 a2 a3 a4 )
i=1 i<j i<j<k
j k
120
Note that N(ai1 . . . aim ) = pi1 ·...·pim (why?)
For example:
N(a1 ) = b120/2c = 60, N(a2 ) = b120/3c = 40,
N(a3 ) = b120/5c = 24, N(a4 ) = b120/7c = 17
N(a1 a2 ) = b120/(2 · 3)c = 20, N(a1 a3 ) = b120/(2 · 5)c = 12,
...
N(a1 a2 a3 a4 ) = b120/(2 · 3 · 5 · 7)c = b120/210c = 0

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 34 / 35


The principle of inclusion and exclusion
Application 2: counting prime numbers (continued)

How many prime numbers are between 1 and 120?


4
X X X
N0 = 120 − N(ai ) + N(ai aj ) − N(ai aj ak ) + N(a1 a2 a3 a4 )
i=1 i<j i<j<k
j k
120
Note that N(ai1 . . . aim ) = pi1 ·...·pim (why?)
For example:
N(a1 ) = b120/2c = 60, N(a2 ) = b120/3c = 40,
N(a3 ) = b120/5c = 24, N(a4 ) = b120/7c = 17
N(a1 a2 ) = b120/(2 · 3)c = 20, N(a1 a3 ) = b120/(2 · 5)c = 12,
...
N(a1 a2 a3 a4 ) = b120/(2 · 3 · 5 · 7)c = b120/210c = 0
⇒ N0 = 120 − (60 + 40 + 24 + 17) + (20 + 12 + 8 + 8 + 5 + 3) −
(4 + 2 + 1 + 1) + 0 = 27.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 34 / 35


The principle of inclusion and exclusion
Application 2: counting prime numbers (continued)

How many prime numbers are between 1 and 120?


4
X X X
N0 = 120 − N(ai ) + N(ai aj ) − N(ai aj ak ) + N(a1 a2 a3 a4 )
i=1 i<j i<j<k
j k
120
Note that N(ai1 . . . aim ) = pi1 ·...·pim (why?)
For example:
N(a1 ) = b120/2c = 60, N(a2 ) = b120/3c = 40,
N(a3 ) = b120/5c = 24, N(a4 ) = b120/7c = 17
N(a1 a2 ) = b120/(2 · 3)c = 20, N(a1 a3 ) = b120/(2 · 5)c = 12,
...
N(a1 a2 a3 a4 ) = b120/(2 · 3 · 5 · 7)c = b120/210c = 0
⇒ N0 = 120 − (60 + 40 + 24 + 17) + (20 + 12 + 8 + 8 + 5 + 3) −
(4 + 2 + 1 + 1) + 0 = 27.
The number we are looking for is 27 + 4 − 1 = 30

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 2 34 / 35


Bibliography

1 Chapter 2, Section 2.1: Generating Permutations of


S. Pemmaraju, S. Skiena. Combinatorics and Graph Theory
with Mathematica. Cambridge University Press 2003.
2 Chapter 2, Sections 2.4 and 2.5 of
J. M. Harris, J. L. Hirst, M.J. Mossinghoff. Combinatorics and
Graph Theory. Second Edition. Springer 2008.
3 Chapter 5 of
K. H. Rosen. Discrete Mathematics and Its Applications. Sixth
Edition. McGraw Hill Higher Education. 2007.

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


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

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