3. Generating Functions http://aofa.cs.princeton.edu A N A L Y T I - - PowerPoint PPT Presentation

3 generating functions
SMART_READER_LITE
LIVE PREVIEW

3. Generating Functions http://aofa.cs.princeton.edu A N A L Y T I - - PowerPoint PPT Presentation

A N A L Y T I C C O M B I N A T O R I C S P A R T O N E 3. Generating Functions http://aofa.cs.princeton.edu A N A L Y T I C C O M B I N A T O R I C S P A R T O N E 3. Generating Functions OGFs Solving recurrences Catalan


slide-1
SLIDE 1

A N A L Y T I C C O M B I N A T O R I C S P A R T O N E

http://aofa.cs.princeton.edu

  • 3. Generating Functions
slide-2
SLIDE 2

A N A L Y T I C C O M B I N A T O R I C S P A R T O N E

OF http://aofa.cs.princeton.edu

  • 3. Generating Functions
  • OGFs
  • Solving recurrences
  • Catalan numbers
  • EGFs
  • Counting with GFs

3a.GFs.OGFs

slide-3
SLIDE 3

() =

  • , , , . . . , , . . .

Definition. is the ordinary generating function (OGF)

  • f the sequence
  • Notation. [zN ]A(z) is “the coefficient of zN in A(z)”

Ordinary generating functions

  • Significance. Can represent an entire sequence with a single function.

sequence OGF 1, 1, 1, 1, 1, ... 1, 1/2, 1/6, 1/24, ...

=

  • ! =

[zN ]ez = 1/N !

3

slide-4
SLIDE 4

Operations on OGFs: Scaling

sequence OGF 1, 1, 1, 1, 1, ... 1, 2, 4, 8, 16, 32, ...

=

=

[]

  • − =

4

, , , . . . , , . . .

() =

  • , , , , . . .

If is the OGF of then is the OGF of

() =

slide-5
SLIDE 5

Operations on OGFs: Addition

sequence OGF 1, 1, 1, 1, 1, ... 1, 2, 4, 8, 16, 32, ... 0, 1, 3, 7, 15, 31, ...

=

=

  • − −

5

If is the OGF of and is the OGF of

() + ()

, , , . . . , , . . .

, , , . . . , , . . .

() =

  • () =
  • + , + , + , . . . , + . . .

then is the OGF of

Example:

slide-6
SLIDE 6

OGF sequence 1, 1, 1, 1, 1, ... 0, 1, 2, 3, 4, 5, ... 0, 0, 1, 3, 6, 10, ... 0, ..., 1, M+1, (M+2)(M+1)/2, ... 1, M+1, (M+2)(M+1)/2, ...

Operations on OGFs: Differentiation

  • − =
  • ( − ) =
  • ( − ) =
  • ( − )+ =
  • ( − )+ =

+

  • 6

, , , . . . , , . . .

() =

  • ′() =
  • , , , , . . . , , . . .

If is the OGF of then is the OGF of

slide-7
SLIDE 7

OGF sequence 1, 1, 1, 1, 1, ... 0, 1, 1/2, 1/3, 1/4, 1/5, ...

Operations on OGFs: Integration

, , , . . . , , . . .

() =

  • − =
  • () =

  • , ,

, , . . . , −

  • , . . .

ln

  • − =
  • 7

If is the OGF of then is the OGF of

Example:

slide-8
SLIDE 8

Operations on OGFs: Partial sum

8

Proof.

  • − () =

  • Distribute

=

+

Change n to n−k

=

Switch order of summation.

=

≤≤

  • Change k to n−k

=

≤≤

  • If is the OGF of

then is the OGF of

() =

  • , , , . . . , , . . .

, + , + + , . . .

  • − ()
slide-9
SLIDE 9
  • − () =
  • =
+ =
− =
≤≤ −
  • =
≤≤
  • OGF

sequence 1, 1, 1, 1, 1, ... 0, 1, 1/2, 1/3, 1/4, 1/5, ... 1, 1 + 1/2, 1 + 1/2 + 1/3, ...

Operations on OGFs: Partial sum

  • − =
  • ln
  • − =
  • − ln
  • − =
  • 9

If is the OGF of then is the OGF of

() =

  • , , , . . . , , . . .

, + , + + , . . .

  • − ()

Example:

slide-10
SLIDE 10

Operations on OGFs: Convolution , , , . . . , , . . .

If is the OGF of

() =

  • then is the OGF of

and is the OGF of , , , . . . , , . . .

() =

  • ()()

, + , . . . ,

  • ≤≤

−, . . .

Proof.

10

()() =

  • Distribute

=

+

Change n to n−k

=

Switch order of summation.

=

  • ≤≤

slide-11
SLIDE 11

Operations on OGFs: Convolution , , , . . . , , . . .

If is the OGF of

() =

  • then is the OGF of

and is the OGF of , , , . . . , , . . .

() =

  • ()()

, + , . . . ,

  • ≤≤

−, . . .

11

OGF sequence 1, 1, 1, 1, 1, ... 1, 2, 3, 4, 5, ...

  • − =
  • Example:
  • ( − ) =

( + )

slide-12
SLIDE 12

Expanding a GF (summary)

The process of expressing an unknown GF as a power series (finding the coefficients) is known as expanding the GF . Techniques we have been using:

Example.

= + + ! + ! + ! + . . .

12

Example.

[]

  • ( − ) ln
  • − =
  • ≤≤
  • =
  • 2. Reduce to known GFs.

() = () + ′() + ′′() ! + ′′′() ! + ′′′′() ! + . . . .

  • 1. Taylor theorem:

ln

then convolve with

ln

Integrate to get

slide-13
SLIDE 13

In-class exercise

Exercise 3.4 Prove that

  • ≤≤

= ( + )(+ − )

ln

[]

  • ( − ) ln
  • − =
  • ≤≤
  • ( + − )
  • ( − )
  • 2. Expand GF to find RHS coefficients (convolve with )
  • − ln
  • ( − ) ln
  • 1. Find GF for LHS (convolve with )

13

= ( + ) − = ( + )(+ −

  • + ) −

= ( + )(+ − )

  • 3. Do some math
slide-14
SLIDE 14

A N A L Y T I C C O M B I N A T O R I C S P A R T O N E

OF http://aofa.cs.princeton.edu

  • 3. Generating Functions
  • OGFs
  • Solving recurrences
  • Catalan numbers
  • EGFs
  • Counting with GFs

3a.GFs.OGFs

slide-15
SLIDE 15

A N A L Y T I C C O M B I N A T O R I C S P A R T O N E

OF http://aofa.cs.princeton.edu

  • 3. Generating Functions
  • OGFs
  • Solving recurrences
  • Catalan numbers
  • EGFs
  • Counting with GFs

3b.GFs.recurrences

slide-16
SLIDE 16

Solving recurrences with OGFs

General procedure:

  • Make recurrence valid for all n.
  • Multiply both sides of the recurrence by zn and sum on n.
  • Evaluate the sums to derive an equation satisfied by the OGF

.

  • Solve the equation to derive an explicit formula for the OGF

. (Use the initial conditions!)

  • Expand the OGF to find coefficients.

recurrence

⬅ ⬅

sequence

16

slide-17
SLIDE 17

Solving recurrences with GFs

Example 4 from previous lecture.

= − − − ≥ = = = −

Expand. Make recurrence valid for all n.

= − − − + δ

Solve.

() =

  • − +

Use partial fractions: solution must be of the form

() =

  • − +

Solve for coefficients.

+ = + = −

Solution is c0 = 1 and c1=−1

() =

  • − −

For linear recurrences with constant coefficients, the GF equation is a polynomial, so the general procedure is a algorithm.

Multiply by zn and sum on n.

() = () − () +

17

slide-18
SLIDE 18

Solving linear recurrences with GFs

Example with multiple roots. Make recurrence valid for all n.

= − − − + − + δ − δ = − − − + − ≥ = , = =

Multiply by zn and sum on n.

() = () − () + () + −

Solve.

() = − − + −

Simplify.

() = ( − ) ( − )( − ) =

  • ( − )

For linear recurrences with constant coefficients, the GF equation is a polynomial, so the general procedure is a algorithm.

18

Expand. multiplicity 3 gives terms

  • f the form n2βn, etc.

= −

slide-19
SLIDE 19

Solving linear recurrences with GFs

= − − − + − ≥ = , = = −

Make recurrence valid for all n.

= − − − + − + δ − δ

Multiply by zn and sum on n.

() = () − () + () + −

Simplify.

() = − ( − )( + ) =

  • ( + )

Solve.

() = − − + −

Expand.

= ( + (−)) = ( + (−)) () =

  • − +
  • +
  • Use partial fractions.

1, 0, -1, 0, 1, 0, -1, 0, 1...

For linear recurrences with constant coefficients, the GF equation is a polynomial, so the general procedure is a algorithm.

Example with complex roots.

19

slide-20
SLIDE 20

Solving linear recurrences with GFs (summary)

Solution to is a linear combination of t terms. Suppose the roots of the polynomial are β1, β2,..., βr where the multiplicity of βi is mi so Solution is The t constants cij are determined from the initial conditions. Note: complex roots (and −1) introduce periodic behavior.

= − + − + . . . + − − + + . . . +

+ + . . . + =

  • ≤<

β

+

  • ≤<

β

+ . . . +

  • ≤<

β

  • t terms

20

slide-21
SLIDE 21

Solving the Quicksort recurrence with OGFs

Multiply by zN and sum.

=

( + ) +

  • ≤≤

Multiply both sides by N.

= ( + ) +

  • ≤≤

− = + +

  • ≤≤

Solve the ODE.

(( − )()) = ( − )() − ( − )() = ( − ) () − () −

  • =

Integrate.

() =

  • ( − ) ln

Expand.

= []

  • ( − ) ln
  • − = ( + )(+ − )

() =

  • ( − ) + ()

Evaluate sums to get an

  • rdinary differential equation

ρ() = ρ()/( − )

ρ() = /( − )

homogeneous equation solution (integration factor)

21

slide-22
SLIDE 22

A N A L Y T I C C O M B I N A T O R I C S P A R T O N E

OF http://aofa.cs.princeton.edu

  • 3. Generating Functions
  • OGFs
  • Solving recurrences
  • Catalan numbers
  • EGFs
  • Counting with GFs

3b.GFs.recurrences

slide-23
SLIDE 23

A N A L Y T I C C O M B I N A T O R I C S P A R T O N E

OF http://aofa.cs.princeton.edu

  • 3. Generating Functions
  • OGFs
  • Solving recurrences
  • Catalan numbers
  • EGFs
  • Counting with GFs

3c.GFs.Catalan

slide-24
SLIDE 24

T2 = 2

Catalan numbers

How many triangulations of an (N+2)-gon?

T4=14

=

  • ≤<

−− + δ

Tk

TN−1−k

T0 = 1 T1 = 1 T3 = 5

24

slide-25
SLIDE 25

Catalan numbers

How many gambler’s ruin sequences with N wins?

  • +--

+-+-- ++--- +-+-+-- +-++--- ++-+--- ++--+-- +++----

Tk

TN−1−k

25

T2 = 2 T4=14 T0 = 1 T1 = 1 T3 = 5

=

  • ≤<

−− + δ

slide-26
SLIDE 26

Catalan numbers

How many binary trees with N nodes?

Tk

TN−1−k

26

=

  • ≤<

−− + δ

k nodes N−1−k nodes

slide-27
SLIDE 27

Catalan numbers

How many trees with N+1 nodes?

Tk

TN−1−k

k+1 nodes N−k nodes

27

=

  • ≤<

−− + δ

slide-28
SLIDE 28

Solving the Catalan recurrence with GFs

Recurrence that holds for all N.

=

  • ≤<

−− + δ

Multiply by zN and sum.

() ≡

=

  • ≤<

−− +

Switch order of summation

() = +

  • >

−−

Change N to N+k+1

() = +

++

Distribute.

() = +

  • () = + ()

28

convolution (backwards)

slide-29
SLIDE 29

Common-sense rule for working with GFs

It is always worthwhile to check your math with your computer. Known from initial values: Check:

() = + ()

sage: ZP.<z> = ZZ[] sage: 1 + z*(1+z+2*z^2+5*z^3+14*z^4)*(1+z+2*z^2+5*z^3+14*z^4) 196*z^9 + 140*z^8 + 81*z^7 + 48*z^6 + 42*z^5 + 14*z^4 + 5*z^3 + 2*z^2 + z + 1

() = + + + + + . . .

not valid because z^5 and beyond missing in factors

29

slide-30
SLIDE 30

Solving the Catalan recurrence with GFs (continued)

Functional GF equation.

() = + ()

Solve with quadratic formula.

() = ( ± √ − )

Expand via binomial theorem.

() = −

  • (−)

30

Set coefficients equal

= −

  • +
  • (−)+

Expand via definition.

= −

  • (

− )( − ) . . . ( − )(−)+

( + )!

Distribute (−2)N among factors.

= · · · · · ( − ) · ( + )!

Substitute (2/1)(4/2)(6/3)... for 2N.

=

  • +

· · · · · ( − ) ! · · · · · · · · · · =

  • +
slide-31
SLIDE 31

A N A L Y T I C C O M B I N A T O R I C S P A R T O N E

OF http://aofa.cs.princeton.edu

  • 3. Generating Functions
  • OGFs
  • Solving recurrences
  • Catalan numbers
  • EGFs
  • Counting with GFs

3c.GFs.Catalan

slide-32
SLIDE 32

A N A L Y T I C C O M B I N A T O R I C S P A R T O N E

OF http://aofa.cs.princeton.edu

  • 3. Generating Functions
  • OGFs
  • Solving recurrences
  • Catalan numbers
  • EGFs
  • Counting with GFs

3d.GFs.EGFs

slide-33
SLIDE 33

Exponential generating functions (EGFs)

Definition. is the exponential generating function (EGF)

  • f the sequence , , , . . . , , . . .

sequence EGF 1, 1, 1, 1, 1, ... 1, 2, 4, 8, 16, 32, ... 1, 1, 2, 6, 24, 120 ...

  • ! =

() =

  • !

! ! =

! =

33

slide-34
SLIDE 34

Proof.

()() =

  • !
  • !

Operations on EGFs: Binomial convolution

, , , . . . , , . . .

If is the EGF of then is the EGF of and is the EGF of

, , , . . . , , . . . ()() , + , . . . ,

  • −, . . .

() =

  • !

() =

  • !

34

Distribute.

=

  • !
  • ! +

Change n to n−k

=

  • !

− ( − )!

Multiply and divide by n!

=

  • !

Switch order of summation.

=

≤≤

  • !
slide-35
SLIDE 35

Solving recurrences with EGFs

Choice of EGF vs. OGF is typically dictated naturally from the problem.

Example.

=

  • Switch order of summation.

() =

  • !

() =

  • !

Multiply by zn /n! and sum on n.

() =

+

  • +

( + )!

Change n to n+k. Simplify.

() =

  • (/)

!

  • !

() = (/) = +/+/+/+... =

Evaluate and telescope.

convergence not assured

Expand.

=

Check.

=

  • Distribute.

() =

  • (/)

!

  • !
  • binomial

convolution (backwards)

35

slide-36
SLIDE 36

A N A L Y T I C C O M B I N A T O R I C S P A R T O N E

OF http://aofa.cs.princeton.edu

  • 3. Generating Functions
  • OGFs
  • Solving recurrences
  • Catalan numbers
  • EGFs
  • Counting with GFs

3d.GFs.EGFs

slide-37
SLIDE 37

A N A L Y T I C C O M B I N A T O R I C S P A R T O N E

OF http://aofa.cs.princeton.edu

  • 3. Generating Functions
  • OGFs
  • Solving recurrences
  • Catalan numbers
  • EGFs
  • Counting with GFs

3e.GFs.counting

slide-38
SLIDE 38

Counting with generating functions

An alternative (combinatorial) view of GFs

  • Define a class of combinatorial obects with associated size function.
  • GF is sum over all members of the class.

=

  • = + ()

38

Example.

() ≡

  • ∈T

||

Distribute

= +

||

||

T ≡ set of all binary trees

|t | ≡ number of internal nodes in t ∈ T

TN ≡ number of t ∈ T with |t | = N

tL

tR

tL ●nodes

Decompose from definition

() = +

||+||+

tR ●nodes

slide-39
SLIDE 39

Combinatorial view of Catalan GF

Each term zN in the GF corresponds to an object of size N. Collect all the terms with the same exponent to expose counts. Each term zi zj in a product corresponds to an object of size i + j.

() = + + + + + + + + + . . . = + +

  • + + . . .

39

= + ( + + + + . . .)( + + + + . . .) = + + + + + + + + + . . .

× × × × × × × × × × × × × × × ×

() = + ()

slide-40
SLIDE 40

Values of parameters ("costs")

are often the object of study in the analysis of algorithms.

40

leaf

How many leaves in a random binary tree? (Not so easy) How many 1 bits in a random bitstring? (Easy) 01110100100010001110101000001010000

slide-41
SLIDE 41

Computing expected costs by counting

An alternative (combinatorial) view of probability

  • Define a class of combinatorial obects.
  • Model: All objects of size N are equally likely

41

P ≡ || ≡ ∈ P ≡ ∈ P || = () ≡ ≡ ∈ P || =

Expected cost of an object of size N

  • PNk/PN is the probability that the

cost of on object of size N is k

  • Def. Cumulated cost is total cost of all objects of a given size.

Expected cost is cumulated cost divided by number of objects.

=

  • “cumulated cost”
slide-42
SLIDE 42

Counting with generating functions: cumulative costs

An alternative (combinatorial) view of GFs

  • Define a class of combinatorial obects.
  • Model: All objects of size N are equally likely
  • GF is sum over all members of the class.

P ≡ || ≡ ∈ P ≡ ∈ P || = () ≡

42

Counting GF

() ≡

  • ∈P

|| =

  • Cumulative cost GF

() ≡

  • ∈P

()|| =

  • Average cost

[]()/[]()

Bottom line: Reduces computing expectation to GF counting

slide-43
SLIDE 43

Warmup: How many 1 bits in a random bitstring?

43

Cumulative cost GF.

() =

()||

B is the set of all bitstrings. |b| is the number of bits in b.

  • nes(b) is the number of 1 bits in b.

BN is the # of bitstrings of size N (2N). CN is the total number of 1 bits in all bitstrings of size N.

= () + () =

  • ( − )
  • ( − ) =

()

Average # 1 bits in a random bitstring of length N.

[]() []() = −

  • =

Counting GF.

() =

|| =

=

b’

1

b’

=

  • + · ()
  • ||+
slide-44
SLIDE 44

Leaves in binary trees

are internal nodes whose children are both external.

44

  • Q. How many leaves in a random binary tree?

t21=2 C2 = 1 C0 = 0 t31=4 t32=1 C3 = 1.2 t41=8 t42=6 C4 = 1.42857 t11=1 C1 = 1

Definitions: TN is the # of binary trees with N nodes. tNk is the # of N-node binary trees with k leaves CN is the average # of leaves in a random N-node binary tree

slide-45
SLIDE 45

How many leaves in a random binary tree?

45

T is the set of all binary trees. |t| is the number of internal nodes in t. leaves(t ) is the number of leaves in t. TN is the # of binary trees of size N (Catalan). CN is the total number of leaves in all binary trees of size N.

Counting GF.

() =

  • ∈T

|| =

=

  • +
  • Cumulative cost GF.

() =

()||

Average # leaves in a random N-node binary tree.

[]() []() = []()

  • Next: Derive a functional equation for the CGF

.

slide-46
SLIDE 46

CGF functional equation for leaves in binary trees

46

Decompose from definition.

tL

tR

tL nodes leaves(tL) leaves tR nodes leaves(tR) leaves

CGF.

() =

()|| () = +

  • () + ()
  • ||+||+

= +

()||

|| +

||

()|| = + ()()

slide-47
SLIDE 47

How many leaves in a random binary tree?

47 = []

− = − −

  • / =

  • +
  • = ( + ) · ·

( − ) ∼ /

Compute average number of leaves. CGF.

() =

()||

Decompose from definition.

() = +

  • () + ()
  • ||+||+

= + ()()

Compute number of trees TN.

= [] ( − √ − ) =

  • +
  • Catalan numbers

() = () − = ( − √ − )

Compute cumulated cost CN.

() = + ()() =

  • − () =

slide-48
SLIDE 48

A N A L Y T I C C O M B I N A T O R I C S P A R T O N E

OF http://aofa.cs.princeton.edu

  • 3. Generating Functions
  • OGFs
  • Solving recurrences
  • Catalan numbers
  • EGFs
  • Counting with GFs

3e.GFs.counting

slide-49
SLIDE 49

Exercise 3.20

Solve a linear recurrence. Initial conditions matter.

49

ALGORITHMS ANALYSIS

OF

S E C O N D E D I T I O N AN INTRODUCTION TO THE R O B E R T S E D G E W I C K P H I L I P P E F L A J O L E T
slide-50
SLIDE 50

Exercise 3.28

The art of expanding GFs.

50

ALGORITHMS ANALYSIS

OF

S E C O N D E D I T I O N AN INTRODUCTION TO THE R O B E R T S E D G E W I C K P H I L I P P E F L A J O L E T
slide-51
SLIDE 51

Assignments for next lecture

  • 1. Use a symbolic mathematics system

to check initial values for C(z) = z + 2C(z)T(z).

  • 2. Read pages 89-147 in text.
  • 3. Write up solutions to Exercises 3.20 and 3.28.

51

ALGORITHMS ANALYSIS

OF

S E C O N D E D I T I O N AN INTRODUCTION TO THE R O B E R T S E D G E W I C K P H I L I P P E F L A J O L E T
slide-52
SLIDE 52

A N A L Y T I C C O M B I N A T O R I C S P A R T O N E

http://aofa.cs.princeton.edu

  • 3. Generating Functions