Permutations: Analytic Combinatorics Part One
Permutations: Analytic Combinatorics Part One
PART ONE
7. Permutations
http://aofa.cs.princeton.edu
Orientation
Second half of class
• Surveys fundamental combinatorial classes.
• Considers techniques from analytic combinatorics to study them .
AN INTRODUCTION
ANALYSIS
TO THE
ALGORITHMS
OF
ROBERT SEDGEWICK
PHILIPPE FLAJOLET
PART ONE
7. Permutations
• Basics
• Sets of cycles
• Left-right-minima
OF
• Other parameters
• BGFs and distributions
http://aofa.cs.princeton.edu
7a.Perms.Basics
Basics
Ex. A group of N students who live in single rooms go to a party that leads to a state of inebriation.
When returning, they each end up in a random room.
student 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
room 9 12 11 10 5 15 1 3 7 6 13 8 2 16 4 14
4
Review: permutations
1 2 3 4 1 2 4 3
2 1 3 4 2 1 4 3
3 1 2 4 3 1 4 2
1 2 3 4 1 2 3 4 1 3 2
1 2 3 1 2 2 3 1 4 2 3 4 1
1 = !
2 1 1 3 2 3 2 1 4 3 2 4 1
P1 = 1
P2 = 2 2 3 1 4 2 1 3 4 2 3 1
!
1 4 2 3 1 4 3 2
= =
3 2 1 !
P3 = 6 2 4 1 3 2 4 3 1
3 4 1 2 3 4 2 1
4 3 1 2 4 3 2 1
P4 = 24
5
Inverse
student 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
room 9 12 11 10 5 15 1 3 7 6 13 8 2 16 4 14
student 7 13 8 15 5 10 9 12 1 4 3 2 11 16 6 14
room 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
room 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
student 7 13 8 15 5 10 9 12 1 4 3 2 11 16 6 14
6
Computing the inverse of a permutation
permutation
1 2 3 4 5 6 7 8 9
8 1 3 7 6 2 9 4 5
2 6 3 5 4 1
Java arrays are 0-based
2 6 3 5 4 1 7
2 6 3 8 5 4 1 7
2 6 3 8 9 5 4 1 7
7
Application: Substitution cipher
Algorithm (traditional)
• Generate random permutation of A-Z (stay tuned).
• Apply as a mapping to encrypt.
• Use inverse to decrypt.
Encryption A B C D E F G H I J K L M N O P Q R S T U V W X Y Z -
random permutation W V L Q I X J A B G - U N F K R Y C D P Z E O M H T S
plaintext A T T A C K - A T - D A W N
ciphertext W P P W L - S W P S Q W O F
Decryption A B C D E F G H I J K L M N O P Q R S T U V W X Y Z -
inverse H I R S V N J Y E G O C X M W T D P - Z L B A F Q U K
ciphertext W P P W L - S W P S Q W O F
plaintext A T T A C K - A T - D A W N
Caveat. Not useful in modern applications because of susceptibility to character frequency analysis.
8
Lattice representation of a permutation
marked
column
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 7
2 13
3 8
4 15
5 5
6 10
7 9
8 12 inverse
9 1
10 4
11 3
12 2
13 11
14 16
15 6
16 14
marked
9 12 11 10 5 15 1 3 7 6 13 8 2 16 4 14
row
permutation
Standard representation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
9 12 11 10 5 15 1 3 7 6 13 8 2 16 4 14
Theorem. Let A and B be combinatorial classes of labelled objects with EGFs A(z) and B(z). Then
11
Review: symbolic method to count permutations
labelled atom Z 1 z
Size |p |, the length of p
! | |
OGF ( )= =
| |! !
∈
“a permutation is empty or an
Construction = + ! atom and a permutation”
OGF equation ( )= + ( )
Solution ( )=
−
![ ] ( )= ! ✓
12
Application: Sorting algorithms
[hundreds of algorithms since 1950]
{
public static
private class Merge
int partition(Comparable[] a, int lo, int hi) input (maybe not in random order)
{{
private
public class
static
j =Quick
int partition(Comparable[] a, int lo, int hi)
int
{{
while
i = lo,
(true)
hi+1; T S R P O N M L I
{ int
private
i = lo,
static
j = int
hi+1;partition(Comparable[] a, int lo, int hi)
while
{
while (true)
(less(a[++i], a[lo])) if (i == hi) break;
{
while int i = lo, j =
(less(a[lo], hi+1; if (j == lo) break;
a[--j]))
if (iwhile
while
>= j)(less(a[++i],
(true)
break; a[lo])) if (i == hi) break; random permutation of the input
while
{ i,(less(a[lo],
exch(a, j); a[--j])) if (j == lo) break;
if (iwhile
>= j)(less(a[++i],
break; a[lo])) if (i == hi) break;
} N L T R M O I P S
exch(a,exch(a,
lo,while
j);i,(less(a[lo],
j); a[--j])) if (j == lo) break;
return} j; if (i >= j) break;
} exch(a,exch(a,
lo, j);i, j);
return
} j;
}
private exch(a,
static voidlo, j);
sort(Comparable[] a, int lo, int hi) sorted output
{ return j;
ifprivate
} <=static
(hi void sort(Comparable[] a, int lo, int hi)
lo) return;
{ j = partition(a, lo, hi);
I L M N O P R S T
int
if
sort(a,private
(hi
lo,<= static
lo) return;
j-1); void sort(Comparable[] a, int lo, int hi)
int
sort(a,{ j+1,
j = partition(a,
hi); lo, hi);
} sort(a,
if (hi
lo,<=j-1);
lo) return;
} sort(a,
int jj+1,
= partition(a,
hi); lo, hi);
} sort(a, lo, j-1);
} sort(a, j+1, hi);
}
}
A. Random permutation.
Q. Realistic?
N L R P O T M S I
for (int i = 0; i < N; i++)
{ N L T P O R M S I
int r = i + StdRandom.uniform(N-i);
int t = a[r]; a[r] = a[i]; a[i] = t; N L T R O P M S I
}
N L T R M P O S I
N L T R M O P S I
All permutations are equally likely:
N L T R M O I S P
• 1st entry equally likely to be any of the N entries.
• 2nd equally likely to be any of the N −1 remaining entries. N L T R M O I P S
• 3rd equally likely to be any of the N −2 remaining entries.
random permutation of the input
• ...
N L T R M O I P S
PART ONE
7. Permutations
• Basics
• Sets of cycles
• Left-right-minima
OF
• Other parameters
• BGFs and distributions
http://aofa.cs.princeton.edu
7b.Perms.Cycles
Review: Permutations and derangements
! " −
/ + / + / +...
EGF equation ( )= = exp ln − =
− −
( )
Expansion [ ] ( ) =
! !
16
Review: generalized derangements
Construction = ( > ( ))
+ + ! "
+ +... = exp ln − − / − ... − /
OGF equation ( )= + +
−
− − − −...
=
−
!
Asymptotics [ ] ( )
17
Involutions
are permutations composed of cycles of length 1 or 2.
I1 = 1
I2 = 2
I3 = 4
I4 = 10
18
Review: Inverse
index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
permutation 9 12 11 10 5 15 1 3 7 6 13 8 2 16 4 14
7 13 8 15 5 10 9 12 1 4 3 2 11 16 6 14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
inverse 7 13 8 15 5 10 9 12 1 4 3 2 11 16 6 14
index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
involution 9 12 11 4 5 15 7 13 1 10 3 2 8 16 6 14
9 12 11 4 5 15 7 13 1 10 3 2 8 16 6 14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
inverse 9 12 11 4 5 15 7 13 1 10 3 2 8 16 6 14
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z -
involution D K R A Z F U J N J B L X I O S V C P - G Q Y M W E T
plaintext A T T A C K - A T - D A W N
Encryption
ciphertext D K R A Z F U J N J B L X I
ciphertext D K R A Z F U J N J B L X I
Decryption
plaintext A T T A C K - A T - D A W N
Caveat. Still susceptible to character frequency analysis but can be useful as a component.
22
Application: How many different Enigma settings?
23
Warmup
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
N O P Q R S T U V W X Y Z A B C D E F G H I J K L M
A T T A C K A T D A W N
R2 = 1 Encryption
N G G N P X N G Q N J A
R4 = 3
N G G N P X N G Q N J A
Decryption
A T T A C K A T D A W N
Construction = ( ( )
/
OGF equation ( )= Stirling's approximation
√
!∼( / )
/ ! √ ! " /
Coefficients ≡ ![ ] /
∼
( / )!
24
Involutions
OGF equation ( )= + /
+ / !
Coefficients ![ ] =
! ( )!
Laplace method
/
Asymptotics
Complex asymptotics
(stay tuned for Part 2)
25
Generalized involutions
Construction = ( ( )) ! ( ( )) ! . . . ! ( ( ))
( )= + / + ... + /
OGF equation
+ / +...+ /
Coefficient asymptotics
Complex asymptotics
(stay tuned for Part 2)
26
In-class exercise
Find [ ] + / + / + / + /
ln − / − / − / − / − / − ...
=[ ] −
− / − / − / − / − /
=[ ] ...
−
=[ ] ( − )( − )( − )( − )( − )...
−
! "
=[ ]( + + + ... + ) − − − − −
.
= =
27
100 prisoners
Problem. 100 prisoners, each uniquely identified by a numbered ID card (1 to 100), have been
sentenced to death, but are given a last chance.
• The ID cards are collected and put in the drawers of a cabinet with 100 numbered drawers
(1 to 100) in random order, one card per drawer
• One at a time, the prisoners are allowed to enter the room containing the cabinet and open,
then close again, at most half the drawers.
• If all prisoners find their own number, they will all be spared.
• If one prisoner fails, they will all be executed.
A. When the random permutation has no cycles of length greater than 50.
.
Probability of success: [ ] exp + + ... + = ( )= .
29
A N A LY T I C C O M B I N AT O R I C S
PART ONE
7. Permutations
• Basics
• Sets of cycles
• Left-right-minima
OF
• Other parameters
• BGFs and distributions
http://aofa.cs.princeton.edu
7c.Perms.LRM
General approach for analyzing parameters
Q. How many times is min updated in the first pass (assuming keys distinct)?
Section 2.1
A. The number of left-right minima in the permutation.
Caveat. Cost for whole sort is complicated, but not significant relative to the number of compares.
32
Left-right minima
Def. A left-right minimum (lrm) in a permutation is a smaller than any item to its left.
4 7 3 6 2 5 1
4 7 3 6 1 5 2
4 7 2 6 1 5 3
3 6 2 5 1 4 ★ 1 = 3 7 2 6 1 5 4
3 7 2 6 1 4 5
3 7 2 5 1 4 6
3 6 2 5 1 4 7
! | | !
CGF. ( )= ( ) =
| |! ≥
!
∈P
| |+
Apply construction. = (| | + ) ( )+
(| | + )!
P
| |+ | |+
Simplify. = ( ) +
(| |)! (| | + )!
P P
! +
Substitute. = ( )+ = ( ) + ln
( + ) −
≥
cumulated cost
=
= + = .
Expand. [ ] ( )= =
! average # lrm in a = + +
.
= . ✓
random permutation .
= + + + = .
35
Cycles
4 1
3
2 3
1
2 3
2 1
1 2 3
1
B1 = 1 1
B1/P1 = 1 3
1
B2 = 1 + 2 = 3 1
B2/P2 = 3/2 = 1.5 3
1
3 1
4 3
4 3 2
7 2 5 1 7 6
4 3 5 1 6
2
5 1 6
4 4
2 3 3
5 7 2
5 1 7 6 6
1
4
Original perm has cycles(p) cycles. 5 1 2
3
6
Q. How many cycles in the set of constructed perms? 7
! | | !
CGF. ( )= ( ) =
| |! !
∈P ≥
| |+
Decompose. = (| | + ) ( )+
(| | + )!
P
| |+ | |+
Simplify. = ( ) +
(| |)! (| | + )!
P P
! +
Substitute. = ( )+ = ( ) + ln
( + ) −
≥
cumulated cost
=
= + = .
Expand. [ ] ( )= =
! average # cycles in a = + +
.
= . ✓
random permutation .
= + + + = .
38
Left-right minima and cycles
A. Yes!
12
15 2
7 16 8
To build a permutation from a set of cycles: 6 4 5
9 1 14
• Identify smallest as the leader in each cycle. 10 13 3
11
• Write cycles in decreasing order of leader.
14 16 5 4 10 6 15 2 12 8 3 11 13 1 7 9
14 16 5 4 10 6 15 2 12 8 3 11 13 1 7 9
To build a set of cycles from a permutation:
• Identify left-right minima. 4
2
14 13 12 1
• Build cycles with entries delimited by lrms 5 15 10
(start a new cycle with each lrm). 16 7 9
6 11 8
3
39
A N A LY T I C C O M B I N AT O R I C S
PART ONE
7. Permutations
• Basics
• Sets of cycles
• Left-right-minima
OF
• Other parameters
• BGFs and distributions
http://aofa.cs.princeton.edu
7d.Perms.Others
1-Cycles
4 0 0
3
1 2
0 0 0
1 2
2 0
1 1 2
0
B1 = 1 0
B1/P1 = 1 2
0
B2 = 0 + 2 = 2 0
B2/P2 = 2/2 = 1 2
0
2 0
4 3
4 3 2
7 2 5 1 7 6
4 3 5 1 6
2
5 1 6
4 4
2 3 3
5 7 2
5 1 7 6 6
1
4
Original perm has cyc1(p) 1-cycles. 5 1 2
3
6
Q. How many 1-cycles in the set of constructed perms? 7
! | | !
CGF. ( )= ( ) =
| |! ≥
!
∈P
| |+
Apply construction. = (| | ( )+ )
(| | + )!
P
| | | |
Differentiate. ( )= | | ( ) + = ( )+
| |! | |!
P P
Solve. ( )=
( )
Integrate. ( )=
cumulated cost
Expand. [ ] ( )= =
! average # 1-cycles in a
random permutation
43
Application: Students and rooms revisited
A group of N students who live in single rooms go to a party that leads to a state of inebriation.
When returning, they each end up in a random room.
Q. What is the average number of students who wind up in their own room?
A. One (!)
44
In-class exercises
A. 1/2
A. 1/r
45
Inversions
A E G I M N O R S T X P L E D Q Z
Q. How many exchanges during the sort? exchanges put M in place among elements to its left
Section 2.1
47
Construction for inversions
3 6 2 5 1 4 7
3 6 2 5 1 7 4 adds 1 inversion
3 6 2 5 7 1 4 adds 2 inversions
3 6 2 5 1 4 ✶ 1 = 3 6 2 7 5 1 4 adds 3 inversions
3 6 7 2 5 1 4 adds 4 inversions
3 7 6 2 5 1 4 adds 5 inversions
7 3 6 2 5 1 4 adds 6 inversions
! | | !
CGF. ( )= ( ) =
| |! ≥
!
∈P
| |+
Apply construction. = (| | + ) ( ) + (| | + )| |/
(| | + )!
P
| |+ | |+ !
Simplify. = ( ) + | | = ( )+
(| |)! (| |)!
P P ≥
Substitute. = ( )+
( − )
Solve. ( )=
( − ) ·
/ != =
cumulated cost
·
/ != = .
( − ) ·
✓
Expand. [ ] ( )= = / != = .
!
average # inversions in a
random permutation ·
/ != =
49
Parameters of permutations
all can be handled in a similar manner
50
A N A LY T I C C O M B I N AT O R I C S
PART ONE
7. Permutations
• Basics
• Sets of cycles
• Left-right-minima
OF
• Other parameters
• BGFs and distributions
http://aofa.cs.princeton.edu
7e.Perms.BGFs
Bivariate generating functions
are the method of choice in analyzing combinatorial parameters.
where |a| is the size and cost(a) is the value of the parameter.
Advantages of BGFs:
• Carry full information.
• Easy to compute counting sequence and CGF (see next slide).
• Full distribution often available via analytic combinatorics.
52
Basic BGF calculations
Definition. The bivariate generating function (BGF ) associated with a labelled class
| | z marks size.
is the formal power series ( , )= ( )
u marks the parameter.
| |!
! | | !
CGF. ( )= ( ) =
| |! !
∈P ≥
| |+
Decompose. = (| | + ) ( )+
(| | + )!
P
| |+ | |+
Simplify. = ( ) +
(| |)! (| | + )!
P P
! +
Substitute. = ( )+ = ( ) + ln
( + ) −
≥
cumulated cost
Expand. [ ] ( )= =
! average # cycles in a
random permutation
54
Average number of cycles in a random permutation with BGFs
! | |
( )
BGF. ( , )=
| |!
∈P
| |+
( )+ ( )
Apply construction . = +| |
(| | + )!
P
| | | |
( )+ ( )
Differentiate wrt z. ( , )= + | |
(| |)! (| |)!
P P
Substitute. = ( , )+ ( , )
Solve ODE. ( , )=
( − )
Construction. = ( ( ))
[ ] ( , )= ✓
Construction. = ( != + ( ))
ln +
BGF equation ( , )= immediate from
transfer theorem.
[ ] ( , )= ≥ ✓ Many, many
examples to follow.
Stay tuned for Part 2
BGFs are the method of choice in analyzing parameters.
57
Number of permutations of size N with k cycles
are known as Stirling numbers of the first kind.
all 2
Notation: 4 1
3
2 3
1
2 3
1 1
1
2 3
1 1
=
3
1
= =
1
3
1
3 1
= = =
= = = =
58
Stirling numbers of the first kind (cycle numbers)
| |
( )
Fundamental identity ( , )= = =
| |! ! ( − )
k
N 1 2 3 4 5 6 7
1 1
2 1 1
3 2 3 1
[ ] ( + )( + )( + ) 4 6 11 6 1
5 24 50 35 10 1
.5
.333
.167
0
0 60
A N A LY T I C C O M B I N AT O R I C S
PART ONE
7. Permutations
• Basics
• Sets of cycles
• Left-right-minima
OF
• Other parameters
• BGFs and distributions
http://aofa.cs.princeton.edu • Exercises
7e.Perms.Exs
Exercise 7.29
Arrangements.
.
AN INTRODUCTION
ANALYSIS
TO THE
ALGORITHMS
OF
S E C O N D E D I T I O N
ROBERT SEDGEWICK
PHILIPPE FLAJOLET
62
Exercise 7.45
Inversions in involutions.
.
AN INTRODUCTION
ANALYSIS
TO THE
ALGORITHMS
OF
S E C O N D E D I T I O N
ROBERT SEDGEWICK
PHILIPPE FLAJOLET
63
Exercise 7.61
.
AN INTRODUCTION
ANALYSIS
TO THE
ALGORITHMS
OF
S E C O N D E D I T I O N
ROBERT SEDGEWICK
PHILIPPE FLAJOLET
64
Assignments for next lecture
AN INTRODUCTION
ANALYSIS
TO THE
ALGORITHMS
OF
S E C O N D E D I T I O N
65
A N A LY T I C C O M B I N AT O R I C S
PART ONE
7. Permutations
http://aofa.cs.princeton.edu