0% found this document useful (0 votes)
63 views66 pages

Permutations: Analytic Combinatorics Part One

The document discusses permutations and analytic combinatorics. It defines a permutation as an ordering of the numbers 1 through N. Permutations can be represented as sequences of labeled atoms or as mappings from 1 to N to itself. The inverse of a permutation is the inverse of this mapping. Permutations can be represented using a lattice with marked rows and columns, where the inverse is the transpose. Analytic combinatorics uses exponential generating functions to study properties of permutations and other combinatorial structures.

Uploaded by

Hoàng Thi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views66 pages

Permutations: Analytic Combinatorics Part One

The document discusses permutations and analytic combinatorics. It defines a permutation as an ordering of the numbers 1 through N. Permutations can be represented as sequences of labeled atoms or as mappings from 1 to N to itself. The inverse of a permutation is the inverse of this mapping. Permutations can be represented using a lattice with marked rows and columns, where the inverse is the transpose. Analytic combinatorics uses exponential generating functions to study properties of permutations and other combinatorial structures.

Uploaded by

Hoàng Thi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 66

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
Orientation
Second half of class
• Surveys fundamental combinatorial classes.
• Considers techniques from analytic combinatorics to study them .
AN INTRODUCTION

ANALYSIS
TO THE

ALGORITHMS
OF

• Includes applications to the analysis of algorithms.


S E C O N D E D I T I O N

ROBERT SEDGEWICK
PHILIPPE FLAJOLET

chapter combinatorial classes type of class type of GF

6 Trees unlabeled OGFs

7 Permutations labeled EGFs

8 Strings and Tries unlabeled OGFs

9 Words and Mappings labeled EGFs

Note: Many more examples in book than in lectures.


2
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

7a.Perms.Basics
Basics

Definition. A permutation is an ordering or the numbers 1 through N.

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

Def. A permutation is a sequence of labelled atoms.

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

2 1 3 1 3 2 4 1 3 4 2 counting sequence EGF

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

Alternate def. A permutation is a mapping of the numbers 1 through N to itself.

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

Def. The inverse of a permutation is the inverse of that mapping.

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

public static int[] inverse(int[] a)


inverse
{
1
int N = a.length;
int[] b = new int[N]; 2 1
for (int i = 0; i < N; i++)
2 3 1
b[a[i]-1] = i+1;
return b; 2 3 4 1
}
2 3 5 4 1

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

Implication. Representation of inverse is transpose of representation of permutation.


9
Review: A combinatorial bijection

Alternate def. A permutation is a set of cycles.

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

Set of cycles representation


12
15 2 8
7 16
6 4 5
9 1 14
10 13 3
11
10
Review: The symbolic method for labelled classes (transfer theorem)

Theorem. Let A and B be combinatorial classes of labelled objects with EGFs A(z) and B(z). Then

construction notation semantics EGF

disjoint union A +B disjoint copies of objects from A and B ( )+ ( )


ordered pairs of copies of objects,
labelled product A★B ( ) ( )
one from A and one from B

SEQk ( A ) k- sequences of objects from A ( )


sequence
SEQ ( A ) sequences of objects from A
( )
SETk ( A ) k-sets of objects from A ( ) / !
set
SET ( A ) sets of objects from A ( )

CYCk ( A ) k-cycles of objects from A ( )/


cycle
CYC ( A ) cycles of objects from A ln
( )

11
Review: symbolic method to count permutations

How many permutations of length N ?

Class P, the class of all permutations Atom type class size GF

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);
}
}

Q. Model for input?

A. Random permutation.

Q. Realistic?

Q. Absolutely, if we put entries in random order before the sort! Chapter 2


13
Application: Randomly permuting an array/generate a random permutation

input (maybe not in random order)


Algorithm (Knuth)
T S R P O N M L I
• Move from left to right.
• Exch each entry with a random entry to its right. N S R P O T M L I

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

use 1 2 3 4 5 6 7 8 9 as input to get a random permutation 6 8 1 3 7 5 9 4 2


14
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

7b.Perms.Cycles
Review: Permutations and derangements

How many sets of cycles of length N ?

∗ “A permutation is a set of cycles"


Construction =( ( ))

! "
EGF equation ( ) = exp ln =
− −
∗ ∗
Counting sequence = ![ ] ( )= !

How many derangements of length N ?

“Derangements are permutations


Construction = ( > ( )) with no singleton cycles"

! " −
/ + / + / +...
EGF equation ( )= = exp ln − =
− −
( )
Expansion [ ] ( ) =
! !

16
Review: generalized derangements

How many permutations of length N have no cycles of length ≤ M ?

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

Alternate def. A permutation is a mapping of the numbers 1 through N to itself.

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

Def. The inverse of a permutation is the inverse of that mapping.

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

Q. What is the inverse of an involution?


19
Inverse of an involution

An involution is a mapping of the numbers 1 through N to itself with all 1- or 2-cycles

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

Def. The inverse of an involution is the inverse of that mapping.

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

Q. What is the inverse of an involution? A. ITSELF!


20
Lattice representation of an involution
marked
column
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 9
2 12
3 11
4 4
5 5
6 15
7 7
8 13 inverse
9 1
10 10
11 3
12 2
13 8
14 16
15 6
16 14
marked
9 12 11 4 5 15 7 13 1 10 3 2 8 16 6 14
row
involution

Representation of involution is symmetric about the main diagonal.


21
Application: Reciprocal cipher

An involution is a permutation that is its own inverse.

Implication: Can encrypt and decrypt with the same machine.

Enigma (WW II)

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

How many perms are comprised entirely of 2-cycles?

Example: ROT-13 (world's weakest cryptosystem)

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

How many involutions of size N ?


2 6
1 7 5
5 3

“Involutions are permutations with


Construction = ( ( )) ! ( ( )) all cycles of length 1 or 2"

OGF equation ( )= + /

+ / !
Coefficients ![ ] =
! ( )!
Laplace method

/
Asymptotics
Complex asymptotics
(stay tuned for Part 2)

25
Generalized involutions

How many permutations of length N have no cycles of length > M ?

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.

Prisoner A, a mathematician, bemoans their fate, claiming


the probability of success is on the order of 2−100 ≈ 8⋅10−31.

Prisoner B, who knows analytic combinatorics, claims to know


a strategy that gives them better than 30% chance of success.

What is Prisoner B’s strategy?


28
100 prisoners solution

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.

Prisoner B’s strategy: Each prisoner "follows the cycle"


• Opens the drawer corresponding to his ID.
• Uses the number in that drawer to decide which drawer to open next.
• Continues until finding the drawer containing his ID.

Q. When does Prisoner's B strategy succeed?

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

Review: Cumulated cost approach for parameters


• Define GF for counting sequence and CGF.
• Identify construction to give CGF equation.
• Solve to get explicit formula for CGF.
• Extract coefficients from GF to get counting seq.
• Extract coefficients from CGF to get cumulated cost.
• Divide to compute expected value

Small trick for permutations:


! | | !
• Use exponential CGF. ( )= ( ) =
| |! ≥
!
∈P
• Treat as OGF to extract expected value directly.
Why does it work? ![ ] ( )
cumulated cost
= [ ] ( )=
• N ! is the normalizing factor for ECGF. counting sequence ! !
• N ! is also the counting sequence.
31
Application: Selection sort

public static void sort(Comparable[] a)


{
S O R T I N G E X A M P L E
int N = a.length;
for (int i = 0; i < N; i++)
{ min
int min = i; min
(1st update) min
for (int j = i+1; j < N; j++) (2nd update) min min
(3rd update)
if (less(a[j], a[min])) min = j; (4th update) min
exch(a, i, min); (5th update)
}
}

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.

Q. How many left-right minima in a random 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.

Q. How many lrm in a random permutation of size N ? 1 1 2 3 4 1 2 4 3 1


2 2 1 3 4 2 1 4 3 2
2 3 1 2 4 3 1 4 2 2
2 4 1 2 3 4 1 3 2 2
1 2 3 1 1 1
1 3 2 4 1 3 4 2
2 1 3 2 2 2 3 1 4 2 3 4 1 2
1 2 3 1 2 2 3 3 2 1 4 3 2 4 1 3
1
2 1 1 3 2 1 3 4 2 1 3 4 2 3 1 3
P1 = 1 2 3 1
P2 = 2 2 1 1 4 2 3 1 4 3 2 1
B1 = 1
B2 = 1 + 2 = 3 3 2 1 3 2 4 1 3 2 4 3 1
B1/P1 = 1 2 2
B2/P2 = 3/2 = 1.5 P3 = 6 2 3 4 1 2 3 4 2 1 3
B3 = 2・1 + 3・2 + 1・3 = 11 3 4 3 1 2 4 3 2 1 4
B3/P3 = 11/6 ≐ 1.833 P4 = 24
B4 = 6・1 + 11・2 + 6・3 + 1・4 = 50
B4/P4 = 50/24 ≐ 2.083 33
Construction for left-right minima

Create |p|+1 perms from a perm p by star product construction.

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

Original perm has lrm(p) left-right minima.


Q. How many left-right minima in the set of constructed perms?
A. (|p| + 1) lrm(p) +1

|p| + 1 copies of the only the one ending


original perm in 1 adds a lrm
34
Average number of left-right minima in a random permutation

! | | !
CGF. ( )= ( ) =
| |! ≥
!
∈P
| |+
Apply construction. = (| | + ) ( )+
(| | + )!
P

| |+ | |+
Simplify. = ( ) +
(| |)! (| | + )!
P P

! +
Substitute. = ( )+ = ( ) + ln
( + ) −

Solve. ( )= ln OGF for the Harmonic numbers

cumulated cost
=

= + = .
Expand. [ ] ( )= =
! average # lrm in a = + +
.
= . ✓
random permutation .
= + + + = .
35
Cycles

Q. How many cycles in a random permutation of size N ?


all 2

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

B3 = 2・1 + 3・2 + 1・3 = 11


B3/P3 = 11/6 ≐ 1.833 B4 = 6・1 + 11・2 + 6・3 + 1・4 = 50
B4/P4 = 50/24 ≐ 2.083 36
Construction for cycles

Create |p|+1 perms from a perm p by inserting |p|+1 into 4 3


every position in every cycle (including the null cycle) 5 1
2
6 7

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

A. (|p| + 1) cycles(p) +1 same as for lrm (!) 4


3
7 5 2
6
|p| + 1 copies of the from the 1
original perm null cycle
37
Average number of cycles in a random permutation (same derivation as for lrm)

! | | !
CGF. ( )= ( ) =
| |! !
∈P ≥

| |+
Decompose. = (| | + ) ( )+
(| | + )!
P

| |+ | |+
Simplify. = ( ) +
(| |)! (| | + )!
P P

! +
Substitute. = ( )+ = ( ) + ln
( + ) −

Solve. ( )= ln OGF for the Harmonic numbers

cumulated cost
=

= + = .
Expand. [ ] ( )= =
! average # cycles in a = + +
.
= . ✓
random permutation .
= + + + = .
38
Left-right minima and cycles

Q. Is there a 1:1 correspondence ?

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

Q. How many 1-cycles in a random permutation of size N ?

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

B3 = 2・0 + 3・1 + 1・3 = 6


B3/P3 = 1 all 1
B4 = 9・0 + 8・1 + 6・2 + 1・4 = 24
B4/P4 = 24/24 = 1
41
Construction for 1-cycles

Create |p|+1 perms from a perm p by inserting |p|+1 into 4 3


every position in every cycle (including the null cycle) 5 1
2
6 7

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

A. (|p| + 1) cyc1(p) + 1 − cyc1(p) = |p| cyc1(p) + 1 4


3
7 5 2
6
|p| + 1 copies of the from the 1-cycles changed 1
original perm null cycle to 2-cycles
42
Average number of 1-cycles in a random permutation

! | | !
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

Q. How many 2-cycles in a random permutation of size N ?

A. 1/2

Q. How many r-cycles in a random permutation of size N ?

A. 1/r

45
Inversions

Def. An inversion in a permutation is the number of pairs ii jj with i > j.


Equivalent: Sum number of entries larger and to the left of each entry.
0 1 2 3 4 1 2 4 3 1
Q. How many inversions in a random permutation of size N ? 1 2 1 3 4 2 1 4 3 2
2 3 1 2 4 3 1 4 2 3
2 4 1 2 3 4 1 3 2 4
1 2 3 0 1 2
1 3 2 4 1 3 4 2
2 1 3 1 2 2 3 1 4 2 3 4 1 4
1 2 0 3 1 2 2 3 3 2 1 4 3 2 4 1 4
1 0
2 1 1 1 3 2 1 3 4 2 1 3 4 2 3 1 4
P1 = 1 2 3 1
P2 = 2 2 2 1 4 2 3 1 4 3 2 3
B1 = 0
B2 = 0 + 1 = 1 3 2 1 3 2 4 1 3 2 4 3 1
B1/P1 = 0 3 4
B2/P2 = 1/2 = 0.5 P3 = 6 4 3 4 1 2 3 4 2 1 5
B3 = 2・1 + 2・2 + 1・3 = 9 5 4 3 1 2 4 3 2 1 6
B3/P3 = 9/6 = 1.5 P4 = 24
B4 = 3・1 + 7・2 + 5・3 + 6・4 + 2・5 + 1・6 = 72
B4/P4 = 72/24 = 3 46
Application: Insertion sort
i = 10
sorted before i untouched after i

public static void sort(Comparable[] a) A E G I N O R S T X M P L E D Q Z


{
int N = a.length; A E G I Nexchange
O R M S with
T entry
M X toPits LleftEif larger
D Q Z
for (int i = 1; i < N; i++)
{ A E G I N O R S M T X P L E D Q Z
for (int j = i; j > 0; j--)
if (less(a[j], a[j-1])) A E G I N O R M S T X P L E D Q Z
exch(a, j, j-1);
else break; A E G I N O M R S T X P L E D Q Z
}
} A E G I N M O R S T X P L E D Q Z

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

A. The number of inversions in the permutation.

Q. How many inversions in a random permutation?

Section 2.1
47
Construction for inversions

Create |p|+1 perms from a perm p by "largest" construction.

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

Original perm has inv(p) inversions.


Q. How many inversions in the set of constructed perms?
A. (|p| + 1) inv(p) + (|p| + 1) |p| / 2

|p| + 1 copies of the all the inversions


original perm caused by |p| + 1
48
Average number of inversions in a random permutation

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

Definition. A combinatorial class is a set of combinatorial objects and an


associated size function that may have an associated parameter.

Definition. The bivariate generating function (BGF ) associated with a class


is the formal power series
| |
| | (( ))
( , )= (unlabelled)
(labelled)
| |!

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

Define ANk to be the number of elements of size N with parameter value k.


| |
( )
Fundamental (elementary) identity ( , )= =
| |! !

Q. How many objects of size N with value k ?


A. ![ ][ ] ( , ) =
( , )=
!

Q. Average value of a parameter of a permutation ?


( , ) ( , ) =
=
!
A. [ ] ( , ) ( , ) =
[ ] ( , )= ( , ) =
=
!
53
Review: Average number of cycles in a random permutation with CGFs

! | | !
CGF. ( )= ( ) =
| |! !
∈P ≥

| |+
Decompose. = (| | + ) ( )+
(| | + )!
P

| |+ | |+
Simplify. = ( ) +
(| |)! (| | + )!
P P

! +
Substitute. = ( )+ = ( ) + ln
( + ) −

Solve. ( )= ln OGF for the Harmonic numbers

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 for Bz(z, u). ( , )= ( , )


Solve ODE. ( , )=
( − )

Average number of cycles. ( , )= ln


− −
[ ] ( , )= ✓
55
Average number of cycles in a random permutation with BGFs and the symbolic method

Combinatorial class. P, the class of all permutations

Construction. = ( ( ))

! " immediate from


BGF equation ( , ) = exp ln =
− ( − ) transfer theorem.

Average number of cycles. ( , )= ln


− −

[ ] ( , )= ✓

Bottom line: BGFs are the method of choice in analyzing parameters


56
Average number of cycles of a given size in a random permutation

Combinatorial class. P, the class of all permutations

Construction. = ( != + ( ))

ln +
BGF equation ( , )= immediate from
transfer theorem.

Average number of cycles. ( , )=

[ ] ( , )= ≥ ✓ 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 ( , )= = =
| |! ! ( − )

Distribution ( , )= ( + )...( + ) (Taylor's theorem)


!

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

6 120 274 225 85 15 1


59
Stirling numbers of the first kind (cycle numbers) distribution

.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

Cycle length distribution.

.
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

1. Read pages 345-413 in text.

AN INTRODUCTION

ANALYSIS
TO THE

ALGORITHMS
OF

S E C O N D E D I T I O N

2. Run experiments to validate mathematical results.


ROBERT SEDGEWICK
PHILIPPE FLAJOLET

Experiment 1. Generate 1000 random permutations for


N = 100, 1000, and 10,000 and compare the average
number of cycles and 1-cycles with the values predicted
by analysis.

Experiment 2. Extra credit. Validate the results of Exercise 7.61


for N = 1000 and k = 10 by generating 10,000 random
permutations and plotting the histogram of occurences of cycles
of length 10.

3. Write up solutions to Exercises 7.29, 7.45, and 7.61.

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

You might also like