analytic combinatorics, part 2 · analytic combinatorics, part 2 ricardo bittencourt june 16, 2013...

43
Analytic Combinatorics, part 2 Ricardo Bittencourt June 16, 2013 1 Week 6 1.1 Web Exercise VII.1 Use the tree-like schema to develop an asymptotic expression for the number of brack- etings with N leaves. Solution The symbolic construction for bracketings is: S = Z + SEQ >1 (S) The OGF is given by the following functional equation: S(z)= z + 1 1 - S(z) - 1 - S(z) The first terms are 1, 1, 3, 11, 45. The elements for N =2: (ab) The elements for N =3: ((ab)c) (a(bc)) (abc) The elements for N =4: (((ab)c)d) ((a(bc))d) ((abc)d) ((ab)(cd)) ((ab)cd) (a((bc)d)) (a(b(cd))) (a(bcd)) (a(bc)d) (ab(cd)) (abcd) The elements for N =5: ((((ab)c)d)e) (((a(bc))d)e) (((abc)d)e) (((ab)(cd))e) (((ab)cd)e) ((a((bc)d))e) ((a(b(cd)))e) ((a(bcd))e) ((a(bc)d)e) ((ab(cd))e) ((abcd)e) (((ab)c)(de)) ((a(bc))(de)) ((abc)(de)) (((ab)c)de) ((a(bc))de) ((abc)de) ((ab)((cd)e)) ((ab)(c(de))) ((ab)(cde)) ((ab)(cd)e) ((ab)c(de)) ((ab)cde) (a(((bc)d)e)) (a((b(cd))e)) (a((bcd)e)) (a((bc)(de))) (a((bc)de)) (a(b((cd)e))) (a(b(c(de)))) (a(b(cde))) (a(b(cd)e)) (a(bc(de))) (a(bcde)) (a((bc)d)e) (a(b(cd))e) (a(bcd)e) (a(bc)(de)) (a(bc)de) (ab((cd)e)) (ab(c(de))) (ab(cde)) (ab(cd)e) (abc(de)) (abcde) 1

Upload: others

Post on 29-Mar-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Analytic Combinatorics, part 2

Ricardo Bittencourt

June 16, 2013

1 Week 6

1.1 Web Exercise VII.1Use the tree-like schema to develop an asymptotic expression for the number of brack-etings with N leaves.

SolutionThe symbolic construction for bracketings is:

S = Z + SEQ>1(S)

The OGF is given by the following functional equation:

S(z) = z +1

1− S(z)− 1− S(z)

The first terms are 1, 1, 3, 11, 45. The elements for N = 2:

(ab)

The elements for N = 3:

((ab)c) (a(bc)) (abc)

The elements for N = 4:

(((ab)c)d) ((a(bc))d) ((abc)d) ((ab)(cd)) ((ab)cd) (a((bc)d))(a(b(cd))) (a(bcd)) (a(bc)d) (ab(cd)) (abcd)

The elements for N = 5:

((((ab)c)d)e) (((a(bc))d)e) (((abc)d)e) (((ab)(cd))e) (((ab)cd)e) ((a((bc)d))e)((a(b(cd)))e) ((a(bcd))e) ((a(bc)d)e) ((ab(cd))e) ((abcd)e) (((ab)c)(de))((a(bc))(de)) ((abc)(de)) (((ab)c)de) ((a(bc))de) ((abc)de) ((ab)((cd)e))((ab)(c(de))) ((ab)(cde)) ((ab)(cd)e) ((ab)c(de)) ((ab)cde) (a(((bc)d)e))(a((b(cd))e)) (a((bcd)e)) (a((bc)(de))) (a((bc)de)) (a(b((cd)e))) (a(b(c(de))))

(a(b(cde))) (a(b(cd)e)) (a(bc(de))) (a(bcde)) (a((bc)d)e) (a(b(cd))e)(a(bcd)e) (a(bc)(de)) (a(bc)de) (ab((cd)e)) (ab(c(de))) (ab(cde))(ab(cd)e) (abc(de)) (abcde)

1

The OGF can be written in the form F (z) = Φ(z, F (z)) if we take Φ(z, w) =z + 1

1−w − 1 − w. Let’s check if this function is smooth-implicit. It is analytic at 0,and converges for every z and for |w| < 1. Now let’s find its coefficients:

Φ(z, w) = z +1

1− w− 1− w

= z − 1− w + (1 + w + w2 + w3 + · · · )= z + w2 + w3 + · · ·

We have gm,n ≥ 0, g0,0 = 0, g0,1 = 0 6= 1, and g0,n = 1 > 0 for every n > 2.Let’s solve the characteristic system:

{Φ(r, s) = s

Φw(r, s) = 1{r + 1

1−s − 1− s = s

−1 + 1(1−s)2 = 1

There is only one solution inside the radius of convergence, which is r = 3− 2√

2

and s = 2−√2

2 . Therefore, the function is smooth-implicit and we can use the implicittree-like transfer theorem:

[zN ]S(z) ∼

√2rΦz(r, s)

Φww(r, s)

1

2√π

(1

r

)NN−3/2

∼ 0.0694779× 5.82843N ×N−3/2

This asymptotic converges very quickly:

2

1.2 Program VII.1Do r- and θ-plots of the GF for bracketings.

SolutionOn the plots, we can see there is a zero for z = 0 and an essential singularity in the

imaginary line for Im(z) = 3, and in the real line for Re(z) > 0. Here’s the modulusplot:

Here’s the argument plot:

3

2 Week 5

2.1 Web Exercise VI.1Use the standard function scale to directly derive an asymptotic expression for thenumber of strings in the following CFG:

S = ε+ U × Z1 × S +D × Z0 × SU = Z0 + U × U × Z1

D = Z1 +D ×D × Z0

SolutionThis is the context-free grammar for the bitstrings with an equal number of 0 and

1. The OGF can be found by the symbolic transfer. Notice that, after the symbolictransfer, U(z) and D(z) will have the same expression.

U(z) = z + zU(z)2

U(z) =1−√

1− 4z2

2zS(z) = 1 + zU(z)S(z) + zD(z)S(z)

S(z) = 1 + 2zU(z)S(z)

S(z) =1

1− 2zU(z)

S(z) =1√

1− 4z2

The first terms are 1, 0, 2, 0, 6, 0, 20. The elements for N = 2:

01 10

The elements for N = 4:

0011 0101 0110 1001 1010 1100

The elements for N = 6:

000111 001011 001101 001110 010011 010101 010110011001 011010 011100 100011 100101 100110 101001101010 101100 110001 110010 110100 111000

In order to apply the standard function scale, let’s first introduce y = 4z2. Also,since all odd elements are zero, we’re looking for coefficients of z2n.

4

S(y) =1√

1− y

S(y) ∼∑ n−1/2

Γ(12

) yNS(z) ∼

∑ 1√πn

(4z2)N

[z2n]S(z) ∼ 4N√πn

This asymptotic is quite good, even though the next term is not exponentially small:

2.2 Web Exercise VI.2Give an asymptotic expression for the number of rooted ordered trees for which everynode has 0, 2 or 3 children. How many bits are necessary to represent such a tree?

SolutionThe symbolic construction for this class is:

M = Z × SEQ0,2,3(M)

The OGF therefore is:

M(z) = z(1 +M(z)2 +M(z)3)

The first terms are 1, 0, 1, 1, 2, 5, 8, 21. The elements for N = 3:

The elements for N = 4:

5

The elements for N = 5:

The elements for N = 6:

The elements for N = 7:

This is a simple variety of tree with φ(u) = 1 + u2 + u3. We can easily check thatφ(u) is non-linear, has nonnegative coefficients, is analytic at 0, has φ(0) = 1 6= 0 andconverges everywhere. Additionally, the characteristic equation φ(λ) = λφ′(λ) has apositive real root λ = 0.657298 which is inside the radius of convergence. Therefore,this class is invertible and we can use the invertible transfer theorem:

6

[zN ]M(z) ∼ 1√2πφ′′(λ)/φ(λ)

(φ′(λ))NN−3/2

[zN ]M(z) ∼ 0.214358× 2.61072N ×N−3/2

This asymptotic has error under 10% for N > 15:

A lower bound for the number of bits required to represent a tree is given by:

b > lg[zN ]M(z)

b > lg(0.214358× 2.61072N ×N−3/2)

b > −2.2219 + 1.3844N − 1.5 lgN

2.3 Program VI.1Do r- and θ-plots of 1/Γ(z) in the unit square of size 10 centered at the origin.

SolutionThis is the plot for Γ(z), where we can see the poles at each negative integer on the

real line:

7

The plot for 1/Γ(z) has zeros in the places where Γ(z) has poles, as expected:

The argument of 1/Γ(z) has a nice pattern:

3 Week 4

3.1 Web Exercise V.1Give an asymptotic expression for the number of strings that do not contain the pattern0000000001. Do the same for 0101010101.

SolutionThe OGF for the number of bitstrings not containing a given pattern of size P is

given by the formula below, where c(z) is the autocorrelation of the polynomial.

BP (z) =c(z)

zP + (1− 2z)c(z)

Let’s check each case:

3.1.1 Pattern 0000000001

In this case the autocorrelation is just c(z) = 1 and the OGF is:

BP (z) =1

z10 + 1− 2z

8

This is a rational function, so we can use the rational transfer theorem. The realpole with smallest module is 1/β = 0.5004931182865523. Applying the theorem:

[zn]f(z)

g(z)∼ ν (−β)νf(1/β)

g(ν)(1/β)βnnν−1

∼ (−β)f(1/β)

g′(1/β)βn

∼ 1.00986× 1.99803n

3.1.2 Pattern 010101010101

Now the autocorrelation is c(z) = 1 + z2 + z4 + z6 + z8 and the OGF is:

BP (z) =1 + z2 + z4 + z6 + z8

z10 + (1− 2z)(1 + z2 + z4 + z6 + z8)

The real pole with smallest module is 1/β = 0.5003691048351494. Applying therational transfer theorem:

[zn]f(z)

g(z)∼ ν (−β)νf(1/β)

g(ν)(1/β)βnnν−1

∼ (−β)f(1/β)

g′(1/β)βn

∼ 1.0062× 1.99852n

3.2 Web Exercise V.2Give asymptotic expressions for the number of objects of size N and the number ofparts in a random object of size N for the following classes: compositions of 1s, 2s,and 3s, triple surjections, and alignments with no singleton cycles.

SolutionLet’s check each class:

3.2.1 Compositions of 1s, 2s and 3s

The symbolic construction for this class is F = SEQ(Z + Z2 + Z3), whose OGF is:

F (z) =1

1− z − z2 − z3

The first terms are 1, 2, 4, 7. The elements for N = 1:

1 = 1

The elements for N = 2:

9

2 = 1 + 1

2 = 2

The elements for N = 3:

3 = 1 + 1 + 1

3 = 1 + 2

3 = 2 + 1

3 = 3

The elements for N = 4:

4 = 1 + 1 + 1 + 1

4 = 1 + 1 + 2

4 = 1 + 2 + 1

4 = 2 + 1 + 1

4 = 1 + 3

4 = 2 + 2

4 = 3 + 1

Let’s check for supercriticality. We have G(z) = z + z2 + z3 which convergesfor every z, so the radius of convergence is ρ = ∞. Since limρ→∞G(ρ) = ∞ > 1,the sequence is supercritical and we can use the supercritical schema. The real root ofG(λ) = 1 is λ = 0.543689, therefore the asymptotic is:

fN ∼1

G′(λ)λN+1

∼ 0.336228× 1.83929N+1

∼ 0.61842× 1.83929N

This asymptotic converges very fast, as we can see on this graph of relative error.

10

The expected number of components is:

µN ∼N + 1

λG′(λ)+G′′(λ)

G′(λ)2− 1

∼ 0.213301 + 0.61842N

3.2.2 Triple surjections

The symbolic construction for this class is F = SEQ(SET>2(Z)), whose EGF is:

F (z) =1

1−∑k≥3

zk

k!

=1

1− (ez − 1− z − z2

2 )

=1

2 + z + z2

2 − ez

The first terms are 0, 0, 1, 1, 1, 21, 71. The elements for N = 3:

111

The elements for N = 4:

1111

The elements for N = 5:

11111

The elements for N = 6:

11

111111 111222 112122 112212 112221 121122 121212121221 122112 122121 122211 211122 211212 211221212112 212121 212211 221112 221121 221211 222111

The elements for N = 7:

1111111 1111222 1112122 1112212 1112221 1112222 11211221121212 1121221 1121222 1122112 1122121 1122122 11222111122212 1122221 1211122 1211212 1211221 1211222 12121121212121 1212122 1212211 1212212 1212221 1221112 12211211221122 1221211 1221212 1221221 1222111 1222112 12221211222211 2111122 2111212 2111221 2111222 2112112 21121212112122 2112211 2112212 2112221 2121112 2121121 21211222121211 2121212 2121221 2122111 2122112 2122121 21222112211112 2211121 2211122 2211211 2211212 2211221 22121112212112 2212121 2212211 2221111 2221112 2221121 22212112222111

Let’s check for supercriticality. We have G(z) = ez − 1− z− z2

2 which convergesfor every z, so the radius of convergence is ρ = ∞. Since limρ→∞G(ρ) = ∞ > 1,the sequence is supercritical and we can use the supercritical schema. The real root ofG(λ) = 1 is λ = 1.56812, therefore the asymptotic is:

fN ∼1

G′(λ)λN+1

∼ N !× 0.286031× 0.637706N

This asymptotic doesn’t converge as fast as the previous one, but the error is below10% for N > 25:

The expected number of components is:

12

µN ∼N + 1

λG′(λ)+G′′(λ)

G′(λ)2− 1

∼ 0.0500366 + 0.286031N

3.2.3 Alignments with no singleton cycles

The symbolic construction for this class is F = SEQ(CY C>1(Z)), whose EGF is:

F (z) =1

1−(

log 11−z − z

)=

1

1 + z − log 11−z

The first terms are 0, 1, 2, 12, 64. The elements for N = 2:

The elements for N = 3:

The elements for N = 4:

13

Let’s check for supercriticality. We have G(z) = log 11−z − z which has a pole at

z = 1 and an essential singularity for z > 1, so the radius of convergence is ρ = 1.Since limρ→1G(ρ) = ∞ > 1, the sequence is supercritical and we can use the super-critical schema. The real root of G(λ) = 1 with smallest module is λ = 0.841406,therefore the asymptotic is:

fN ∼1

G′(λ)λN+1

∼ N !× 0.224015× 1.18849N

This is also a rapidly converging asymptotic:

14

The expected number of components is:

µN ∼N + 1

λG′(λ)+G′′(λ)

G′(λ)2− 1

∼ 0.636517 + 0.224015N

3.3 Program V.1Plot the GFs for the set of bitstrings having no occurrence of the pattern 0000000001.Do the same for 0101010101.

SolutionThis is the GF for the pattern 0000000001:

15

This is the GF for the pattern 0101010101:

16

In both cases we can see the dominant real pole near the origin, and pairs of conju-gate poles around the origin.

4 Week 3

4.1 Note IV.28Some ”supernecklaces”.

Draw all the supernecklaces of the 3rd type of size N for N = 1, 2, 3, 4.One estimates

[zn] log

(1

1− log 11−z

)∼ 1

n(1− e−1)−n

where the EGF enumerates labelled cycles of cycles [Hint: take derivatives.]SolutionIn the following graphs, blue links represent edges between nodes of a cycle, and

red links represent edges between cycles. The sequence for the number of superneck-laces starts with 1, 2, 7, 35, 228.

The supernecklaces for N = 1 are:

17

The supernecklaces for N = 2 are:

The supernecklaces for N = 3 are:

The supernecklaces for N = 4 are:

18

Now let’s analyze the generating function S(z). We can use this property of deriva-tives:

zS′(z) =∑

nsnzn

[zn]zS′(z) = nsn

sn =1

n[zn]zS′(z)

Substituting our S(z):

19

sn =1

n[zn]z

d

dz

(log

(1

1− log 11−z

))

=1

n[zn]

z

(1− z)(

1− log(

11−z

))

This function has two poles, one at z = 1 and another at z = 1 − e−1 ∼ 0.63,and also an essential singularity for z > 1. The smallest real pole is z = 1 − e−1, sothe function is meromorphic around this pole and we can use the meromorphic transfertheorem:

α = 1− e−1

h−1 =−f(α)

g′(α)= 1− e−1

c =h−1α

= 1

β =1

α= (1− e−1)−1

f(z)

g(z)= cβn = (1− e−1)−n

The desired coefficients, as expected, are:

sn ∼1

n(1− e−1)−n

4.2 Program IV.1Compute the percentage of permutations having no singleton or doubleton cycles andcompare with the asymptotic estimate from analytic combinatorics, for N = 10 andN = 20.

SolutionA simulation with 100000 random samples returns µ10 = 0.22402 and µ20 =

0.22262. The asymptotic estimation is given by µ = e−H2 = 0.22313 which agreeswith the experiment.

4.3 Program IV.2Plot the derivative of the supernecklace GF.

SolutionWe’ve seen that the derivative of the supernecklace GF is:

20

S(z) =1

(1− z)(

1− log(

11−z

))This is a 3d rendering of the function, where the height is the magnitude of the

complex number, and the hue is the argument of the complex.

We can clearly see the two poles, at z = 0.63 and z = 1, and the essential singu-larity caused by argument discontinuity, on the real line for z > 1.

5 Week 2

5.1 Note II.11Balls switching chambers: the Ehrenfest model.

Consider a system of two chambersA andB (also classically called ”urns”). Thereare N distinguishable balls, and, initially, chamber A contains them all. At any instant12 ,

32 , . . ., one ball is allowed to change from one chamber to the other.Let E[`]

n be the number of possible evolutions that lead to chamber A containing `balls at instant n and E[`](z) the corresponding EGF. Then

21

E[`](z) =

(N

`

)(cosh z)`(sinh z)N−`

E[N ](z) = (cosh z)N ≡ 2−N (ez + e−z)N

Hint: the EGF E[N ] enumerates mappings where each pre-image has an even car-dinality. In particular, the probability that urn A is again full at time 2n is

1

2NN2n

N∑k=0

(N

k

)(N − 2k)2n

SolutionSuppose we annotate which ball has moved at instant n. If the balls are b0, b1, . . . , bN ,

then at instant n = 5 we would get a string like b0b3b0b0b5. The balls always movefrom A to B, or from B to A. This means a ball bi will end up at chamber A if itappears an even number of times in this string.

We can model a word as a sequence of sets (where each set correspond to a letterfrom the alphabet, and each element of the set correspond to the position in the wordwhere this letter appears). If we have a word built from an alphabet withN letters, withthe restriction that all letters appear an even number of times, then this is its symbolicconstruction:

E[N ](z) = SEQN

(∑even k

SETk(z)

)Applying the transfer theorems for EGFs:

E[N ](z) =

(∑even k

zk

k!

)N

E[N ](z) =

∑k≥0

zk

k![k is even]

N

E[N ](z) =

∑k≥0

zk

k!

(1 + (−1)k

2

)N

E[N ](z) =

1

2

∑k≥0

zk

k!+

1

2

∑k≥0

(−z)k

k!

N

E[N ](z) =

(1

2

(ez + e−z

))NE[N ](z) = (cosh z)N

22

Using the same reasoning, we can prove that a word built from an alphabet with Nletters, with the restriction that all letters appear an odd number of times, leads to:

E[N ]odd (z) = (sinh z)N

.Now suppose we want to count the words where the first ` letters of the alphabet

appear an even number of times, and the remainingN−` letters appear an odd numberof times. The EGF will be:

E[first `](z) = (cosh z)`(sinh z)N−`

But we don’t want to count just the words where first ` letters of the alphabet appearan even number of times. Instead, we want all combinations of ` letters appearing aneven number of times. The EGF can be built by just summing all

(N`

)combinations of

letters, leading to the final result:

E[`](z) =

(N

`

)(cosh z)`(sinh z)N−`

We can use this result to find the probability that urn A is full again at time 2n. Thetotal number of evolutions is N2n, and the number of evolutions where all balls are inA after 2n steps is:

(2n)![z2n]E[N ](z) = (2n)![z2n]1

2N(ez + e−z)N

= (2n)![z2n]1

2N

∑0≤k≤N

(N

k

)ez(N−k)e−zk

= (2n)![z2n]1

2N

∑0≤k≤N

(N

k

)ez(N−2k)

= (2n)![z2n]1

2N

∑0≤k≤N

(N

k

)∑j≥0

(N − 2k)jzj

j!

= (2n)![z2n]1

2N

∑j≥0

∑0≤k≤N

(N

k

)(N − 2k)jzj

j!

= (2n)!1

2N

∑0≤k≤N

(N

k

)(N − 2k)2n

(2n)!

=1

2N

∑0≤k≤N

(N

k

)(N − 2k)2n

The probability therefore is:

23

µ =1

N2n2N

∑0≤k≤N

(N

k

)(N − 2k)2n

5.2 Note II.31Combinatorics of trigonometrics

Interpret tan z1−z , tan tan z, and tan(ez − 1) as EGFs of combinatorial classes.

SolutionLet’s consider the following symbolic construction, for increasing binary trees

where each node contains an element described by H:

F = H +H�F 2

Using the transfer theorems for EGFs, we have:

F (z) = H(z) +H(z)�F (z)2

F (z)−H(z) = H(z)�F (z)2

F ′(z)−H ′(z) = H ′(z)F (z)2

F ′(z) = H ′(z)(1 + F (z)2)

dF (z)

dz= H ′(z)(1 + F (z)2)

dF (z)

1 + F (z)2= H ′(z)dz∫

dF (z)

1 + F (z)2=

∫H ′(z)dz

arctan(F (z)) = H(z)

tan(arctan(F (z))) = tan(H(z))

F (z) = tan(H(z))

Therefore all the EGFs we’re looking for can be described as ”increasing binarytrees of something”. Let’s check one by one now.

5.2.1 F (z) = tan( z1−z )

We know that z1−z is the EGFs of non-empty permutations, so the interpretation is ”an

increasing binary tree of permutations”. The first few terms are 0, 1, 2, 8, 48.The elements of F1 = 1:

The elements of F2 = 2:

24

The elements of F3 = 8:

The elements of F4 = 48:

25

5.2.2 F (z) = tan(tan(z))

Since tan(z) is the EGFs of zig-zag permutations of odd size, the interpretation is ”anincreasing binary tree of zig-zag permutations of odd size”. The first few terms are0, 1, 0, 4, 0, 72.

The elements of F1 = 1:

The elements of F3 = 4:

The elements of F5 = 72:

26

5.2.3 F (z) = tan(ez − 1)

Since ez − 1 is the EGFs of non-empty sets, the interpretation is ”an increasing binarytree of sets”. The first few terms are 0, 1, 1, 3, 13.

The elements of F1 = 1:

The elements of F2 = 1:

The elements of F3 = 3:

27

The elements of F4 = 13:

5.3 Note III.17Leaves and node-degree profile in Cayley trees.

For Cayley trees, the bivariate EGF with u marking the number of leaves is thesolution to

T (z, u) = uz + z(eT (z,u) − 1)

(By Lagrange inversion, the distribution is expressible in terms of Stirling partitionnumbers.)

The mean number of leaves in a random Cayley tree is asymptotic to ne−1. Moregenerally, the mean number of nodes of outdegree k in a random Cayley tree of size nis asymptotic to

n · e−1 1

k!

Degrees are thus approximately described by a Poisson law of rate 1.SolutionCayley trees are modeled by the symbolic construction T = zSET (T ). We can

mark the leaves with u, resulting in the construction T = z(SET (T ) − SET0(T ) +uSET0(T )), arriving at the equation:

28

T (z, u) = z(eT (z,u) − 1 + u)

We can isolate z to prepare the equation for Lagrange Inversion:

z =T (z, u)

eT (z,u) − 1 + u

f(x) =x

ex − 1 + u

f ′(x) =ex + u− 1− xex

(ex + u− 1)2

Since f(0) = 0 and f ′(0) = 1u , the preconditions for Lagrange Inversion apply.

Let’s do it:

n![zn]T (z, u) = n!1

n[xn−1]

(x

f(x)

)n= (n− 1)![xn−1](ex − 1 + u)n

= (n− 1)![xn−1]∑k≥0

(n

k

)ekx(u− 1)n−k

= (n− 1)![xn−1]∑k≥0

(n

k

)(u− 1)n−k

∑m>0

1

m!xmkm

= (n− 1)!∑k≥0

(n

k

)(u− 1)n−k

1

(n− 1)!kn−1

=∑k≥0

(n

k

)(u− 1)n−kkn−1

Let’s isolate the coefficient for ur:

29

n![zn][ur]T (z, u) = [ur]∑k≥0

(n

k

)(u− 1)n−kkn−1

= [ur]∑k≥0

(n

k

)kn−1

∑m≥0

(n− km

)um(−1)n−k−m

=∑k≥0

(n

k

)(n− kr

)kn−1(−1)n−k−r

=∑k≥0

(n

n− k

)(n− kr

)kn−1(−1)n−k−r

=∑k≥0

(n

r

)(n− r

n− k − r

)kn−1(−1)n−k−r

=

(n

r

)∑k≥0

(n− r

n− r − k

)kn−1(−1)n−k−r

=

(n

r

)∑k≥0

(n− rk

)kn−1(−1)n−r−k

Now we can use this property of Stirling numbers:

m!

{n

m

}=∑k≥0

(m

k

)kn(−1)m−k

Applying it to our problem:

n![zn][ur]T (z, u) =

(n

r

)(n− r)!

{n− 1

n− r

}=n!(n− r)!(n− r)!r!

{n− 1

n− r

}=n!

r!

{n− 1

n− r

}

Therefore, the number of Cayley trees of size n with r leaves is:

t(n, r) =n!

r!

{n− 1

n− r

}Now we’ll analyse the number of nodes with k branches. The symbolic construc-

tion is T = z(SET (T )− SETk(T ) + uSETk(T ). The equation is:

30

T (u, z) = z(eT (u,z) + (u− 1)T (u, z)k

k!)

Let’s check the conditions for Lagrange Inversion:

z =T (u, z)

eT (u,z) + (u− 1)T (u,z)k

k!

f(x) =x

ex + (u− 1)xk

k!

f ′(x) =ex + (u− 1)x

k

k! − x(ex + (u− 1)kxk−1

k! )

(ex + (u− 1)xk

k! )2

We have f(0) = 0 and f ′(0) = 1u [k = 0] + [k > 0] 6= 0, therefore Lagrange

Inversion applies:

n![zn]T (u, z) = n!1

n[xn−1]

(ex + (u− 1)

xk

k!

)n= (n− 1)![xn−1]

∑m≥0

(n

m

)ex(n−m)(u− 1)m

(xk

k!

)m= (n− 1)![xn−1]

∑m≥0

(n

m

)(u− 1

k!

)mxkm

∑p≥0

1

p!xp(n−m)p

= (n− 1)![xn−1]∑m≥0

(n

m

)(u− 1

k!

)m∑p≥0

1

p!xp+km(n−m)p

= (n− 1)!∑m≥0

(n

m

)(u− 1

k!

)m1

(n− 1− km)!(n−m)n−1−km

We want the cumulative cost, and this is the same as ∂F (z,1)∂u :

n![zn]∂F (z, u)

∂u=∑m≥0

(n

m

)m(u− 1)m−1

(1

k!

)m(n− 1)!

(n− 1− km)!(n−m)n−1−km

Notice that (u − 1)m−1 is non-zero for u = 1 only when m = 1, so we can dropthe summation:

31

n![zn]∂F (z, 1)

∂u= n

1

k!

(n− 1)!

(n− 1− k)!(n− 1)n−1−k

= n

(n− 1

k

)(n− 1)n−1−k

We conclude that the average number of nodes with k branches on a random Cayleytree of n nodes is:

µ(n, k) =n

nn−1

(n− 1

k

)(n− 1)n−1−k

We know that, for small k, we can approximate the binomial by:(n

k

)=nk

k!

(1 +O

(1

n

))Back in our equation:

µ(n, k) ∼ n

nn−1(n− 1)k

k!(n− 1)n−1−k

∼ n

k!

(n− 1)n−1

nn−1

∼ n

k!

(1− 1

n

)n−1∼ n

k!e−1

The average number of nodes therefore follows a Poisson law as expected.

5.4 Note III.21After Bhaskara Acharya (circa 1150 AD).

Consider all the numbers formed in decimal with digit 1 used once, with digit 2used twice, ..., with digit 9 used nine times. Such numbers all have 45 digits. Computetheir sum S and discover, much to your amazement that S equals

45875559600006153219084769286399999999999999954124440399993846780915230713600000This number has a long run of nines (and further nines are hidden!). Is there a

simple explanation? This exercise is inspired by the Indian mathematician BhaskaraAcharya who discovered multinomial coefficients near 1150 AD.

Solution

32

A decimal integer can be modelled as a sequence of digits. We can mark each digitwith a different marker uk:

I = SEQ(Z1u1 + Z2u2 + Z3u3 + Z4u4 + Z5u5 + Z6u6 + Z7u7 + Z8u8 + Z9u9)

The MGF is:

I(z) =1

1− z(u1 + u2 + u3 + u4 + u5 + u6 + u7 + u8 + u9)

The number s of integers satisfying the condition is the same as the coefficient forz45u11u

22u

33u

44u

55u

66u

77u

88u

99:

s = [z45u11u22u

33u

44u

55u

66u

77u

88u

99]I(z)

= [z45u11u22u

33u

44u

55u

66u

77u

88u

99]∑k≥0

zk(u1 + u2 + u3 + u4 + u5 + u6 + u7 + u8 + u9)k

= [u11u2vu

3vu

4vu

5vu

6vu

7vu

8vu

9v](u1 + u2 + u3 + u4 + u5 + u6 + u7 + u8 + u9)45

= [u11u2vu

3vu

4vu

5vu

6vu

7vu

8vu

9v]∑k≥0

(45

k

)uk1(u2 + u3 + u4 + u5 + u6 + u7 + u8 + u9)45−k

= [u22u33u

44u

55u

66u

77u

88u

99]

(45

1

)(u2 + u3 + u4 + u5 + u6 + u7 + u8 + u9)44

=451

1![u22u

33u

44u

55u

66u

77u

88u

99]∑k≥0

(44

k

)uk2(u3 + u4 + u5 + u6 + u7 + u8 + u9)44−k

=451

1![u33u

44u

55u

66u

77u

88u

99]

(44

2

)(u3 + u4 + u5 + u6 + u7 + u8 + u9)42

=453

1!2![u33u

44u

55u

66u

77u

88u

99](u3 + u4 + u5 + u6 + u7 + u8 + u9)42

= · · ·

=4545

1!2!3!4!5!6!7!8!9!

=45!

1!2!3!4!5!6!7!8!9!

This is the definition of the multinomial:

s =

(45

1, 2, 3, 4, 5, 6, 7, 8, 9

)The sum of all digits in all integers of this set is:

D =

(45

1, 2, 3, 4, 5, 6, 7, 8, 9

) ∑1≤i≤9

i2

33

If we line up all numbers of set, one below the other, then the sum of the digits inthe every vertical must be the same. Therefore, the sum of digits in a single position isthe total sum of digits D divided by the length of the integers:

K =1

45

(45

1, 2, 3, 4, 5, 6, 7, 8, 9

) ∑1≤i≤9

i2

The sum of all integers in the set is this sum of digits K multiplied by all powersof 10:

S =1

45

(45

1, 2, 3, 4, 5, 6, 7, 8, 9

) ∑1≤i≤9

i2∑

0≤j≤44

10j

This will evaluate numerically to the value we wanted to prove:

S = 45875559600006153219084769286399999999999999954124440399993846780915230713600000

Now let’s analyse the digits of this number. Let dn be n-th digit of S, and let cnbe the carry generated by this digit. We have c0 = 0, and for n ≤ 45 the followingrecursions are valid:

dn = (K + cn) mod 10

cn+1 =

⌊K + cn

10

⌋We can sense a pattern by analysing the first values of cn:

c0 = 0

c1 =

⌊K

10

⌋c2 =

⌊⌊K10

⌋+K

10

⌋=

⌊K10 +K

10

⌋=

⌊K

100+K

10

Let’s use induction to prove that cn =⌊∑

1≤j≤nK10j

34

cn+1 =

⌊∑

1≤j≤nK10j

⌋+K

10

=

K10

+∑

1≤j≤n

K

10j+1

=

K10

+∑

2≤j≤n+1

K

10j

=

∑1≤j≤n+1

K

10j

The sequence cn will converge to a value, since 10j will eventually grow to bebigger than K, and when that happens cn won’t change anymore. This will happenonce j > log10K ∼ 35. After this value, the digits of S won’t change anymore, theywill be equal to (K + cn) mod 10, which is equal to 9. This explains why there’s arun of nines from digits 35 to 45.

5.5 Program II.1Write a program to simulate the Ehrenfest mode (see Note II.11) and use it to plot thedistribution of the number of balls in urn A after 103, 104 and 105 steps when startingwith 103 balls in urn A and none in urn B.

SolutionNotice that the number of balls in urn A must be an even number after an even

number of steps, therefore the distribution will have zeros for every odd step. We willplot only the even indexes.

The distributions for n = 500, 1000, 10000 are:

35

0

10

20

30

40

50

60

70

0 200 400 600 800 1000

n=500

n=1000

n=10000

This matches our intuition: the distribution begins with more balls on urn A, andthen, over time, it tends to be evenly distributed. In our graph, the mean will shift to500 as n increases.

5.6 Program III.1Write a program that generates 1000 random permutations of sizeN forN = 103, 104, ...(going as far as you can) and plots the distribution of the number of cycles, validatingthat the mean is concentrated at HN .

SolutionHere’s the distribution of cycles:

36

0

50

100

150

200

0 5 10 15 20 25

n=1000

n=10000

n=100000

The mean matches our expectation, since H103 = 7.48, H104 = 9.78 and H105 =12.09.

6 Week 1

6.1 Note I.23Alice, Bob, and coding bounds.

Alice wants to communicate n bits of information to Bob over a channel (a wire,an optic fibre) that transmits 0,1-bits but is such that any occurrence of 11 terminatesthe transmission. Thus, she can only send on the channel an encoded version of hermessage (where the code is of some length ` ≥ n) that does not contain the pattern 11.

SolutionLet’s assume we want only fixed-rate bitstreams (all codes have the same length).

The simplest code is 0→ 00, 1→ 10, which has a rate of transmission of 2 bits/signal.We can try to improve the rate by grouping input bits:

37

000→ 00000

001→ 00010

010→ 00100

011→ 01000

100→ 01010

101→ 10000

110→ 10100

111→ 10010

This code has 1.66 bits/signal. If we increase the input word size, perhaps we couldfind a code with a better rate.

We can find a lower bound for this rate by considering all the codes that doesn’tcontain the string 11. Also, either all codes end in zero, or all codes starts with zero(otherwise, even if no individual code has 11, we might get an 11 by joining the endand start of two consecutive codes).

We’ll enumerate all codes not containing 11, and ending in zero. The symbolicconstruction for this is:

F (z) = SEQ(Z0 + Z10) =1

1− z − z2

The smallest pole is 1/β = 1−√5

2 with multiplicity 1, we can use the transfertheorem for rational functions:

[zn]f(z)

g(z)∼ ν (−β)νf(1/β)

g(ν)(1/β)βnnν−1

[zn]f(z)

g(z)∼ 1

2(1/β) + 1βn+1

[zn]f(z)

g(z)∼ 1√

5

(1 +√

5

2

)n+1

We want to find the least s that is bigger than 2n:

38

2n ≤ βs+1

√5

n ≤ (s+ 1) lg β − lg√

5

s+ 1 ≥ n+ lg√

5

lg β

s ≥ n

lg β+

lg√

5

lg β− 1

The lower bound for the rate s/n is:

s

n≥ 1

lg β+

lg√

5

n lg β− 1

n

s

n≥ 1

lg β+O

(1

n

)s

n≥ 1.44 +O

(1

n

)

Therefore there is always an overhead of 44% mininum, when using a fixed-rateencoding.

6.2 Note I.43Fast determination of the Cayley-Polya numbers.

Logarithmic differentiation of H(z) provides for the Hn a recurrence by whichone computes Hn in time polynomial in n. (Note: a similar technique applies to thepartition numbers Pn.)

SolutionWe start with the definition of H(z), then take logarithm on both sides and differ-

entiate:

39

H(z) = z∏m≥1

(1− zm)−Hm

logH(z) = log z + log∏m≥1

(1− zm)−Hm

logH(z) = log z +∑m≥1

log(1− zm)−Hm

logH(z) = log z −∑m≥1

Hm log(1− zm)

H ′(z)

H(z)=

1

z−∑m≥1

Hm−mzm−1

1− zm

zH ′(z) = H(z) +∑m≥1

mHmzm

1− zmH(z)

Now we expand H(z) in a series and take the n-th element on each side:

zH ′(z) = H(z) +∑m≥1

mHmzm

1− zm∑k≥0

Hkzk

[zn]zH ′(z) = [zn]H(z) + [zn]∑m≥1

mHmzm

1− zm∑k≥0

Hkzk

nHn = Hn + [zn]∑m≥1

mHm

∑k≥0

zm+kHk1

1− zm

nHn = Hn + [zn]∑m≥1

mHm

∑k≥0

zm+kHk1

1− zm

(n− 1)Hn = [zn]∑m≥1

mHm

∑k≥0

zm+kHk

∑j≥0

zmj

(n− 1)Hn =∑m≥1

mHm[zn]

∑k≥0

∑j≥0

zm+k+mjHk

Let’s simplify the double sum:

40

[zn]

∑k≥0

∑j≥0

zm+k+mjHk

= [zn]

∑j≥0

∑k≥0

zk+m(j+1)Hk

[zn]

∑k≥0

∑j≥0

zm+k+mjHk

= [zn]

∑j≥1

∑k≥0

zk+mjHk[n = k +mj]

[zn]

∑k≥0

∑j≥0

zm+k+mjHk

= [zn]

∑j≥1

∑k≥0

zk+mjHk[k = n−mj]

[zn]

∑k≥0

∑j≥0

zm+k+mjHk

= [zn]

∑j≥1

zn−mj+mjHn−mj

[zn]

∑k≥0

∑j≥0

zm+k+mjHk

=∑j≥1

Hn−mj

Substituting back, we get a sum that looks like infinite, but is in reality finite, be-cause Hn = 0 if n < 0:

(n− 1)Hn =∑m≥1

mHm

∑j≥1

Hn−mj

(n− 1)Hn =∑m≥1

mHm

∑j≥1

Hn−mj [n ≥ mj]

(n− 1)Hn =∑m≥1

mHm

∑j≥1

Hn−mj [j ≤ n/m]

(n− 1)Hn =∑m≥1

mHm

∑1≤j≤n/m

Hn−mj

(n− 1)Hn =∑m≥1

mHm

∑1≤j≤n/m

Hn−mj [n/m ≥ 1]

(n− 1)Hn =∑m≥1

mHm

∑1≤j≤n/m

Hn−mj [m ≤ n]

(n− 1)Hn =∑

1≤m≤n

mHm

∑1≤j≤n/m

Hn−mj

Let’s isolate Hn:

41

(n− 1)Hn =∑

1≤m≤n

mHm

∑1≤j≤n/m

Hn−mj

(n− 1)Hn = nHnH0 +∑

1≤m<n

mHm

∑1≤j≤n/m

Hn−mj

(n− 1− nH0)Hn =∑

1≤m<n

mHm

∑1≤j≤n/m

Hn−mj

Hn =1

n(1 +H0)− 1

∑1≤m<n

mHm

∑1≤j≤n/m

Hn−mj

Since H0 = 0, we can simplify the expression:

Hn =1

n− 1

∑1≤m<n

mHm

∑1≤j≤n/m

Hn−mj

This expression leads to an O(n2 log n) implementation. We can improve it bychanging k = mj:

Hn =1

n− 1

∑1≤m<n

mHm

∑1≤k≤n

Hn−k[m|k]

Hn =1

n− 1

∑1≤k≤n

Hn−k∑

1≤m<n

mHm[m|k]

Hn =1

n− 1

∑1≤k≤n

Hn−k∑

1≤m≤k

mHm[m|k]

The last sum can be reused during each step of the computation of Hn, bringingthe implementation down to O(n2).

6.3 Program I.1Determine the choice of four coins that maximizes the number of ways to change adollar.

SolutionThe generating function for the number of ways to change a dollar, using four coins

of values a, b, c and d is:

C(z) =1

(1− za)(1− zb)(1− zc)(1− zd)We can use Polya’s insight that:

42

C(z) =1

1− zmB(z) =⇒ Cn = Cn−m +Bn

.If we have c different coins, this leads to anO(cn) algorithm to compute the number

of ways to change n cents.By computing all the

(1004

)combinations of coins, we determine that the best choice

is {1, 2, 3, 4} leading to 8237 ways to change a dollar.

6.4 Program I.2Write programs that estimate the rate of growth of the Cayley numbers and the partitionnumbers (Hn/Hn−1 and Pn/Pn−1).

SolutionWe’ll use the following recurrences for Hn and Pn:

Hn =1

n− 1

∑1≤k≤n

Hn−k∑

1≤m≤k

mHm[m|k]

Pn =1

n

∑j≥1

σ(j)Pn−j

Both can be implemented in O(n2). Calculating the ratios, we found that:

limn→∞

Hn+1

Hn∼ 2.95

limn→∞

Pn+1

Pn∼ 1

43