Generating Permutations. Ranking and Unranking Permutations. The Pigeonhole Principle. The Inclusion and Exclusion Principle
Generating Permutations. Ranking and Unranking Permutations. The Pigeonhole Principle. The Inclusion and Exclusion Principle
08 October 2018
Example (A = {a1 , a2 , a3 })
B The 2-permutations of A are (order is important!):
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!):
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:
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
⇒ 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 .
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?
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?
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?
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
hp1 , p2 , p3 , p4 , p5 i = h5, 2, 4, 3, 1i
h5, 2, 4, 3, 1i
i =3
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
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
Example
hp1 , p2 , p3 , p4 , p5 i = h5, 2, 4, 3, 1i
h5, 3, 1, 2, 4i = next permutation
Problems
1 How to compute directly 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}.
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.
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
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.
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).
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
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
h10, 9, 7, 4i
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)
r1 = 0 ·α− 0
ri = (i − 1) ·α− b(i − 1)αc if 2 ≤ i ≤ Q + 1
rQ+2 = 0 ·α− (−1)
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).
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
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
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.
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
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
N0 + r − 1
√
where r is the number of prime numbers ≤ n.