transparency no. 7-1 discrete mathematics chapter 7 advanced counting techniques

70
Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Upload: solomon-rodgers

Post on 12-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Transparency No. 7-1

Discrete Mathematics

Chapter 7

Advanced Counting Techniques

Page 2: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-2

Contents

7.1 Recurrence Relations

7.2 Solving Recurrence Relations

7.4 Generating Functions (skipped)

7.3 Divide-and-conquer Relations

7.5 The Inclusion-Exclusion principle

7.6 Applications of the Inclusion-Exclusion principle

Page 3: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-3

7.1 Recurrence relations#bacteria doubles every hour. Initially(t=0), there are 5 bacteria

==> #bacteria after n hours = ?

sol: Let {an} be the sequence where an = #bacteria after n hours.

=> a0 = 5 --- (1) initial condition

an = 2 an-1 for n > 0. --- (2) recurrence relation

Notes: (1&2) is called a recursive definition of the sequence {an}. (1&2) uniquely determines a sequence {an}nN =<a0,a1,..>. (2) is the recurrence part of the definition. Most general form: an= f(n,an-1, an-2,…, a0), where f is a function of

n and previous terms an-1, an-2,…, a0 of an. More examples: an = an-1+an-2 + …+ a0.

an = 2 an-1 + an-2 x an-3 + n2. an = 5xan/2 + n3.

Page 4: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-4

Recurrence relations are very useful in the study and solving of counting problems. Many counting problems can be modeled (or

expressed) naturally by recurrence relations. Goal of section 7.1~7.3: Modeling counting problems in terms of

recurrence relations Solving recurrence relations. Namely, find explicit

formula f(n) for an (i.e., without using previous terms ) satisfying the recurrence relation (and initial conditions) Ex: an = 5x2n is an explicit solution of the above example.

Page 5: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-5

Definition of recurrence relations

A recurrence relation for a sequence {an} is a formula of the form: an = f(n, an-1,an-2,…,a0) for all n n0 ---- (*)

where n0 is a non-negative number and f is a function of n and an-1,…,a0. In other words, we express an in terms of one or more previous terms of an in the sequence.

A sequence {an} is called a solution to the recurrence relation (*) if its terms satisfy the recurrence relation. I.e., for all n n0 , an = f(n, an-1,an-2,…,a0) .

Ex1: If a sequence {an} satisfies

(1) an = an-1 - an-2 for n 2, and

(2) a0=3, a1 = 5. --- initial conditions

=> a2 = ? and a3 = ?

Hint: Initial conditions (2) tell us the values of the first two terms, while recurrence relation (1) tells us how the value of every other term is determined by its previous two terms.

Page 6: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-6

Recurrence relation exampleEx3: Deposit $10,000 in an account with 11% compound interest annually.==> How much will be in the account after 30 years ?

Sol: let Pn = amount of the account after n years

==> 1. p0 = 10,000

2. pn = pn-1 + pn-1 x 0.11.

==> Pn = 1.11 Pn-1 = 1.112 Pn-2=...= 1.11n P0 = 1.11n x 10000.

==> P30 = 1.1130 x 10000 = 22,892,297.Ex4: Rabbits and Fibonacci number.A pair of rabbits produce one pair per month after 2 month old.Initially there is only one pair.=> How many pairs of rabbits are there after n months?

sol: Let fn = #pairs after n months.

=>1. f0 = f1 = 1 --initial conditions 2. fn = fn-1 + fn-2 for n > 1 , where fn-1: #old-rabbits and fn-2 is #new born rabbits.

Page 7: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-7

More recurrence examples

(The tower of Hanoi) 3 pegs: A,B,C. Initially n disks placed on peg A. Disks can be moved from peg to peg only if the disk is on top of the original disk and smaller than that on top of the target peg.The goal is to have all disks moved to the 2nd pegs.

Let Hn = #moves required to solve the problems with n disks

=> Hn = ?

Sol: 1. H1 = 1.

2. Hn = H n-1 + 1 + H n-1 = 2Hn-1 + 1.

A--n-->B A--n-1-->C A--1-->B C--n-1-->B.

Hence Hn = 2 Hn-1 + 1 = 22 Hn-2 + 2 + 1 = ...

= 2n-1 H1 + 2n-2 + ... + 1 = 2 n -1.

How big is H64 ?

: assume one move takes 1 sec => 264 - 1 = 1.8 x 10 19 = 500 billion years!!

Page 8: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-8

More recurrence examples (continued)

Ex6: #bit strings of length n not containing two consecutive 0s.

sol: an = #bit strings of such kind of length n.

==> 1. a0 = 1, a1 = 2.

2. an = #bits string ending with 1 w/o two consecutive 0s

+ #bit-strings-ending with 00 or 10 w/o two consecutive 0s

= an-1 + an-2 if n 2

Ex7: an = # n-digit-strings containing an even number of 0.

Sol: valid strings must be in one of the forms:

1. x1 x2 ... xn-1 xn with xn = 1..9 or

2. x1 x2 ... xn-1 0 with x1 x2 ... xn-1 containing odd number of 0.

==> 1. a0 = 1, a1 = 9.

2. an = an-1 x 9 + (10n-1 - an-1) = 8an-1+ 10n-1 for n > 1.

Page 9: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-9

Degree of recurrence relations

A recurrence relation : an = f(n, an-1,an-2,…,a0) for all n n0 is said to has degree k > 0 if the farthest previous term f depends on is an-k .

i.e., f depends on an-k but not on an-s for any s > k.

Ex: an = an-1 an-1 + an-3 has degree 3,

an = a0 has no degree

an = 5 an/2 + n3 has no degree.

If the recurrence relation : an = f(n, an-1,an-2,…,a0) for all n n0 has degree k, then we can express it simply as

an = f(n, an-1,an-2,an-k) for all n n0

where f is a function of n and an-1,an-2,…,an-k .

Page 10: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-10

Well definition of sequences using recurrence relations of constant degree

Theorem 0: A recurrence relation of degree k:

an = f(n, an-1,an-2,an-k) for all n k,

and k initial conditions:

a0 = C0, ..., ak-1 = Ck-1, where C0,...,Ck-1 are arbitrary k numbers

uniquely determine a sequence satisfying all equations)Pf: 1. Uniqueness, 2. Existence. cf.:Exercise 56&57 of Section 4.3

1. Uniqueness: If {n} and {n} both satisfy all equations =>

{n} = {n} (i.e., n = n for all n .)

Assume {n} , {n} are distinct sequences, then by well-order property of natural numbers, there must exist a least t such that t t. Such t must not < k due to the initial conditions. But if t k, the minimum of t implies s = s for all s < t, and hence by the recurrence relation t = t. , As a result, t does not exist and there are no distinct sequences satisfying all equations.

Page 11: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-11

Proof of existencePf: Proof by construction: Define a sequence {n} by the following process:

1. for ( t = 0 to k-1) t Ct .

2. for ( t = k to ) t f(t, t-1,t-2,…,t-k)

1. Such process must assign at most one value t for each position t 0 since the index t in each execution of t … is distinct from one another.

2. Such process must assign exactly one value t for each position t 0 s.t. t = Ct for 0 t k-1 and t = f(t, t-1,t-2,…,t-k) for t k since

for 0 t k-1, position t will be assigned a value Ct during the execution of Line 1, and

for t = k, since all t-1,t-2,…,0 have been assigned values by Line1,

f(t, t-1,t-2,…,t-k) is well-defined and execution of t f(…) results in a new position t to be assigned value t = f(t, t-1,t-2,…,t-k).

Now for t > k, by Ind.hyp., all t-1,t-2,…,t-k have been assigned values, hence f(t, t-1,t-2,…,t-k) is well-defined and execution of t f(…) assigns the value t = f(t, t-1,t-2,…,t-k) to a new position t.

By 2, {n} is a sequence satisfying all equations.

Page 12: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-12

Compute recursive sequences

Given a recursive definition of a sequence: an = f(n, an-1,an-2,an-k) for all n k, and

a0 = C0, ..., ak-1 = Ck-1.

It is very easy to derive recursive and/or iterative algorithms to compute the value of any item an in the sequence.

A1(n : int) : real { // recursive version switch(n) { case 0 : return C0 ;

… case k-1 : return Ck-1 }

return f(n, A(n-1),…, A(n-k)); }

A1 is not effieicnt since it requires exponential time if k > 1.

Page 13: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-13

Iterative algoritm for computing recursive sequences

A2( n: int) : real { // iterative version

real[] A = new real[n+1] ;

A[0]=C0; …,A[k-1] = Ck-1;

if(n < k) return A[n] ;

for(int t = k; t <= n; t++) A[t] = f(t, A[t-1],…,A[t-k]) ;

return A[n]; }A2 requires space O(n) and time O(n) if f(...) uses constant

time. A3 is a more space efficient version which uses O(1) space if

the recurrence relation has a degree k.

A3(n: int) :real { real[] A = new real[k] ;

A[0]=C0; …,A[k-1] = Ck-1; if(n < k) return A[n] ;

for(int t=k; t <= n; t++) A[t % k ] = f(t, A[(t-1)%k],…,A[(t-k)%k]) ;

return A[n%k]; }

Page 14: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-14

7.2 Solving linear recurrence relationsA linear homogeneous recurrence relation of degree k with constant

coefficients(lhrrcc) is any recurrence relation of the form:

an = c1 an-1 + c2an-2+...+ckan-k, ---- (1)

where k > 0, every cj is a constant number with ck 0.

Ex: an = an-1 + an-2 is a lhrrcc of degree 2

an = an-5 is a lhrrcc of degree 5

Notes: (1) is linear since c1,…,ck are functions of n only. Hence an is a linear combination of an-1,an-2,an-k.

counter example: an = an-1*an-2+ an-3

(1) is homogenous since every term at RHS is a multiple of some previous term an-j.counter example: Hn = 2Hn-1 + 1 -- inhomogeneous

constant coefficients since all c1,…,ck are constants.

k is the degree of the recurrence relation.

Page 15: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-15

Solving linear homogeneous recurrence relations with constant coefficients (lhrrcc)

Let an = c1 an-1 + c2an-2+...+ ckan-k -- (1)

be a lhrrcc relation of degree k > 0.

Let {an} be a sequence such that an = rn where r is a number 0, then {an} (or {rn}) is a solution of (1) iff

rk = c1 rk-1 + c2 rk-2+...+ck rk-k -------(2)

(2) is called the characteristic equation of (1)

pf: {rn} is a solution of (1) iff rn = c1 rn-1 + c2rn-2+...+ ckrn-k

iff rn-k (rn - c1 rn-1 - c2rn-2-...- ck) = 0

iff (rn - c1 rn-1 - c2rn-2-...- ck) = 0

Ex: an = an-1 + an-2 has characteristic equation r2 = r + 1.

an = 3an-1 + an-5 has characteristic equation r5 = 3r4 + 1.

Page 16: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-16

Theorem 1

an = c1 an-1 + c2an-2 -- (3) a lhrrcc relation of degree 2.

Theorem 1: If r2 = c1 r + c2 (**) has two distinct roots r1, r2 , then

sequence {an} is a solution of (3)

iff an = d1 r1n + d2 r2

n for n = 0,1,2,.. (*)

where d1 and d2 are some/any constants.pf: <= : simple substitution (like the proof in previous slide).

=>: Let <b0,b1,...> be any solution of (3).

For {bn} to satisfy (*), we must have:

b0 = d1 + d2; b1 = d1r1 + d2 r2 (Note since r1 r2, (d1,d2) must exist!) and for any k > 1,

bk = c1bk-1 + c2bk-2

= (by ind.hyp.) c1(d1r1k-1+d2r2

k-1) + c2 (d1r1k-2 +d2r2

k-2)

= d1(c1r1k-1+c2r1

k-2) +d2(c1r2k-1+c2r2

k-2)

= d1r1k-2(c1r1+c2) +d2r2

k-2 (c1r2+c2) = (by **) d1 r1k + d2 r2

k. QED

Page 17: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-17

Examples:

Ex3: Find the solution of an = an-1 + 2 an-2 for n > 1, with initial conditions: a0 = 2 and a1 = 7.

sol: char equ: r2 = r +2 has roots 2, -1.

Hence an = d1 2n + d2 (-1)n for all n for some d1, d2.

==> a0 = d1 + d2

a1 = 2 d1 - d2 => d1 = 3; d2 = -1

==> an = 3 x 2n - (-1)n for n 0.

Page 18: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-18

Solution of the Fibonacci sequence

Ex4: Find the solution to the Fibonacci sequence: f1=f0=1 and

fn = fn-1 + fn-2 for n > 1.

Sol: The char equ: r2 = r + 1 has roots:

=(1+rt(5)) /2, // golden ratio

=(1-rt(5))/2. note: 1< < 2 and -1 < <0.

Hence fn = d1 n + d2 n with

f0 = 1 = d1 + d2 and

f1 = 1 = d1 + d2 => d1 = rt(5)/5 and d2 = -rt(5)/5.

=> fn = d1n + d2n = O(n) grows exponentially.

Page 19: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-19

Solving recurrence relation

Theorem 2: If r2 = c1 r + c2 has only one root r0 (with duplicate multiplicity) then any sequence {an} is a solu of (3) iff

an = d1 r0n + d2 n r0

n for n = 0,1,2,.. (*)

where d1 and d2 are some/any constants.

Pf: => : Similar to Theorem 1 (see textbook).

<= : a n-1 = d1 r0n-1 + d2 (n-1) r0

n-1

a n-2 = d1 r0n-2 + d2 (n-2) r0

n-2

==> c1 an-1 + c2 an-2

= d1 (c1 r0n-1 + c2 r0

n-2) + d2 (c1(n-1) r0n-1 + c2(n-2) r0

n-2)

= d1 r0n-2(c1 ro + c2) + d2 r0

n-2( (n-1)(c1ro+c2) - c2)

= d1 r0n + d2 r0

n-2((n-1) r02 + r0

2) -- since r2- c1r -c2 = (r-r0)2

= r2 - 2 r0 r + r02.

= d1 r0n + d2 n r0

n

Page 20: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-20

Ex5: find the solution of an = 6an-1 - 9 an-2 with

a0 = 1 and a1 = 6.

sol: the char. equation: r2 = 6 r – 9 has only one root 3.

=> an = d1 3n + d2 n 3 n.

=> d1 = 1, and

3d1 + 3 d2 = 6

=> an = 3n + n 3n for n ≥ 0.

Page 21: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-21

Generalization

Theorem 4: If the equation rk = c1 rk-1 + c2rk-2+...+ ck has solutions r1

m1, r2m2,..,rs

ms (with m1+...+ms = k) where mi is the multiplicity of the root ri.

(i.e. rk - c1 rk-1 +...-ck = i=1..s (x-ri)mi ) then

{an} is a solution of the recurrence relation:

an = c1 an-1 +...+ck an-k iff

an = i=1,s (j=0,mi-1 dij nj ) ri

n , where dij's are constants and (j=0,mi-1 dij

nj ) is a polynomial of order mi -1.

Ex: The recurrence relation an = 5 an-1 + 9 an-2 -7 an-3 + 2 an-4

has char. equ. r4 = 5r3 -9r2 -7r + 2, which is equ. to

(r-2)(r-1)3 = 0. Hence r has roots: 2, 13.

Then the relation has general solution:

an = d02n + (e01+e1n+e2 n2 )1n, where d0, e0,e1 and e2 are constants determined by initial conditions.

Page 22: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-22

Simultaneous recurrence relations of degree 1

Ex: Solving the simultaneous recurrence relations:

1. an = 3 an-1 + 2 bn-1

2. bn = an-1 + 2 bn-1, with a0 = 1 and b0 = 2.sol: (1,2) can be represented in matrix form:

Let Y be the eigenvector of A: (I.e., AY =Y for some .)=> (A-I)Y = 0 => det(A-I) = 0 => (3-)(2-) - 2 = 0 => =1,4.

=> Y1 = (1,-1)T and Y2 = (2,1)T.

Now assume X0 = d1Y1 + d2 Y2

=> Xn = A Xn-1 = ... = An Xo = An-1 (AX0) = An-1 (d1AY1 + d2AY2) = An-1 (d1 1Y1 + d22Y2) = ... = d1 1

n Y1 + d22n Y2.

.021

1-n

1-n

...)(

.21

23,)1(

21

23

XAAXAAXX

AXb

aLet

b

a

b

a

nnnn

nn

n

n

n

Page 23: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-23

Linear Nonhomogeneous Recurrecne Relations (lnhrr) with Constant Coefficients ( 以下至 7.4 skipped!)

has known how to solve lhrr with constant coefficients. Is there a similar method to solve lnhrr with constant coefficients

like an = 3an-1 + 4n ?

Ans: yes but only for some families of lnhrrcc.

Definition: A linear nonhomogeneous recurrence relation with constant coefficients (of degree k) is a recurrence relation of the form:

an = c1an-1 + c2 an-2 + … + ck an-k + F(n) --- (4) where

c1,c2,…,ck are constant numbers, ck 0,

F(n) 0 is a function depending on n only.Note: In the above definition, the lhrrcc: an = c1an-1 + c2 an-2 + … + ck an-k ----- (5)

is called the associated homogeneous recurrence relation of (4)

Page 24: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-24

Examples

1. an = an-1 + 2n

2. an = an-1+an-2 + n2+n + 1

3. an = 3 an-1 + n 3n

4. an = an-1+an-2+an-3 + n!

All the above 4 rrs are lnhrr with constant coefficients. Their associated lhrr are :

1. an = an-1

2. an = an-1+an-2

3. an = 3 an-1

4. an = an-1+an-2+an-3

Page 25: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-25

Theorem 5 (Find all solutions from any given soltution) If {pn} is a (particular) solution of the lnhrr with constant coefficients:

an = c1an-1 + c2 an-2 + … + ck an-k + F(n) ---(4)

Then {bn} is a solution of (4) iff {bn} = {pn+hn} where {hn} is a solution of the associated lhrr of (4).

Pf: If-part: Since {bn} = {pn+hn} , we have

bn = pn + hn = c1pn-1 + c2 pn-2 + … + ck pn-k + F(n)

+ c1hn-1 + c2 hn-2 + … + ck hn-k

= c1(pn-1 + hn-1)+ c2(pn-2 + hn-2)+…+ ck(pn-k + hn-k)+ F(n)

= c1bn-1 + c2 bn-2 + … + ck bn-k + F(n) .

Hence {bn} is a solution of (4)

Only-if: On the other hand, if {bn} is a solution of (4), since {pn} is also a solution, we have bn–pn = (c1bn-1+c2bn-2+…+ckbn-k+F(n)) - (c1pn-1+c2pn-2+… + ckpn-k+F(n)) = c1(bn-1-pn-1) + c2 (bn-1-pn-2)+ … + ck(bn-k-pn-k). Hence {bn–pn} is a solution of the associated lhrr of (4) and

{bn} = {pn + (bn–pn)} = {pn+hn} where {hn} = {bn–pn}.

Page 26: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-26

Example 10

Find all solutions of the recurrence relation an = 3 an-1 + 2n, and what is the solution with a1 = 3 ?

Sol: The key to the problem is to find a particular solution pn.

We first guess that pn is in linear form, i.e., pn = cn + d for some constants c and d.

We then have cn+d = 3 (c(n-1) + d) + 2n = (3c + 2) n + (3d – 3c).

As a result (2c+2) n + (2d – 3c) = 0.

Hence c = -1 and d =-3/2.

Next Find general solution of the associated lhrr: an = 3 an-1 .

Which we know is of the form 3n.

Hence by Theorem 5, all solutions are of the form: an=3n-n-3/2.

If a1 = 3, then 3 = 31 - 1 -3/2 and =11/6.

Hence an = 11/6 x 3n - n-3/2.

Page 27: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-27

Example 11

Find all solutions of the recurrence relation :

an = 5 an-1 - 6 an-2 + 7n --- (*)

Sol: First try to find a particular solution pn . We guess pn = c 7n for some c. Then we require

pn = c7n = 5 pn-1 - 6 pn-2 + 7n.

= 5c7n-1 -6c7n-2 +7n

= (35c - 6c +49) 7n-2 .

So 49c = 29c + 49 and c = 49/20.

Then find the solution of the associated lhrr: an = 5 an-1 - 6 an-2.

Its characteristic equation is r2 =5r-6, which has roots:2 and 3.

As a result, the homogeneous solution is 2n + 3n,

and the general solution to (*) is

2n+3n+(49/20)7n.

Page 28: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-28

Theorem 6

Let an = c1an-1 + c2 an-2 + … + ck an-k + F(n) be a lnhrr with constant coefficients with F(n) = b(n) sn, where b(n) = btnt + bt-1nt-1 +…+ b1n1 + b0 is a polynomial of degree t,

and s is a constant. Notes: In Ex10, b(n) = 2n and s = 1; In Ex11, b(n) = 1 and s = 7.

1. if s is not the root of the characteristic equation of the associated lhrr, then there is a particular solution of the form:

p(n) sn, where p(n) is a polynomial of degree t (= degree(b(n) ).

2. If s is a root of the characteristic equation with multiplicity m, then there is a particular solution of the form :

p(n) nm sn where p(n) is a polynomial of degree t.

Page 29: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-29

Example 12

Find the form of a particular solution for the recurrence relation

an = 6 an-1 – 9 an-2 +F(n), when F(n) =

1. 3n, 2. n3n 3. n2 3n 4. n2 2n or 5. (n2+1) 3n.

Sol: The associated hrr has char. equation r2 = 6r -9, whose roots are 3,3. So the form of a particular solution of each of the above are : particular solution general solution

1. (cn+d) // (cn+d)1n + (an+b) 3n

2. (cn+d)n23n // (cn+d) n23n +(an+b)3n =(cn3+dn2+an+b) 3n

3. (cn2+dn+e)n23n // (cn2+dn+e)n23n +(an+b) 3n

4. (cn2+dn+e)2n // (cn2+dn+e)2n + (an+b) 3n

5. (cn2+dn+e)n23n. // (cn2+dn+e) n23n + (an+b) 3n =

(cn4+dn3+en2+an+b) 3n

Notes: The coefficients(c,d,e) of particular solutions are determined by the lnrrcc

(5) while remaining coefficients(a,b) are determined by initial conditions.

Page 30: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-30

Example 13

Find the solution of the recurrence relation an = an-1 + n (*) with a1 = 1.

sol: In this case we have F(n) = n x 1n. The characteristic equation of the associated hrr is r = 1, which has root 1.

So the form of the solution of the assocaited hrr is e1n = e.

By Theorem 6 a particular solution is of the form

pn = (cn+d) n11n = cn2 + dn.

Substitute pt for at in (*), we get cn2 + dn = c(n-1)2 + d(n-1) + n.

-2cn+c –d + n = 0 => c = ½, d=c=1/2. The general solution is of the form

an = e + (n2+n)/2.

But since a1 = 1, we thus have e= 0 and an = (n2+n)/2.

Exercises: Find solutions of an = an-1 + F(n) with a1 = 1, where

F(n) = n2 and n3, respectively. –solu: n(n+1)(2n+1)/6, n2(n+1)2/4

Page 31: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-31

Exercise 31 of Section 7.2

Find all solutions of the recurrence relation: an = 5 an-1 – 6 an-2 + 2n + 3n --- (*).

Note we cannot apply Theorem 6 to this case since here F(n) = 2n + 3nx1n,which is not of the form p(n)sn for some number s.

Lemma: if {gn} is a particular solution of

an = c1an-1 + c2 an-2 + … + ck an-k + F1(n) and

{hn} a particular solution of

an = c1an-1 + c2 an-2 + … + ck an-k + F2(n), then

{gn+hn} is a particular solution of the recurrence relation

an = c1an-1 + c2 an-2 + … + ck an-k + F1(n) + F2(n) ---(**).

Pf: gn+hn = c1gn-1 + c2 gn-2 + … + ck gn-k + F1(n) +

c1hn-1 + c2 hn-2 + … + ck hn-k + F2(n)

= c1(gn-1+hn-1)+c2(gn-2+hn-2)+ … + ck(gn-k+hn-k)+ F1(n)+F2(n).

Hence {gn+hn} satisfies (**).

Page 32: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-32

Solution of Exercise 31

The problem can be reduced to that of finding particular solutions to an = 5 an-1 – 6 an-2 + 2n --- (1), and an = 5 an-1 – 6 an-2 + 3n --- (2), respectively.

Since the associated homogeneous relation an = 5 an-1 – 6 an-2 --- (3) has char equation r2 = 5 r – 6, with root r

= 2 and 3.

The particular solution of (1) has the form gn = c n1 2n. Hence cn2n=5c(n-1)2n-1–6c(n-2)2n-2+2n -- div both sides by 2n-2

=> 4cn = 5c(n-1)x2 – 6c(n-2) + 4 => (4c-10c + 6c)n=-10c+12c + 4=2c+4 => 0n=2c+4=> c = -2.

Similarly, particular solution of (2) has the form hn = (dn+e) 1n. Hence (dn+e)1n = 5[d(n-1)+e]1n-1–6[d(n-2)+e]1n-2 + 3n => (d-5d+6d-3)n = -e – 5d +5e + 12d – 6e = 7d – 2e = 0. => d = 3/2 and e = 7d/2 = 21/4 => the general solution is 2n + 3n + (-2) n 2n + (3n/2+ 21/4).

Page 33: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-33

7.4 Generating Functions (skipped!)

Motivation: Condition: A problem in one domain is hard to solve. Strategy: transform the problem into another domain, solve it with

easier method and then transform the result to the original domain as a solution to the original problem.

Such strategy is a very popular technique in CS and Math.

Ex: Hard to perform a great amount of multiplications on a list of numbers a1,… an, then

Transform input <a1,..,an> to <log a1,…,log an>,

replace each multiplication a x b by log a + log b, take 10R as the final solution where R is the result in the new domain.

Ex: Fourier transform: time domain s(t) <-> frequency domain F(f) s(t) ----- - + s(t)e-2ift dt ---> F(f)

s(t) <--- - + F(f)e2ift df ------ F(f)

Page 34: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-34

Definition of oridinary generating functions

Goal: transform counting/sequence problems into algebraic problems.

Definition 1: The generating function for the sequence a0,a1,…,an,… of real numbers is the infinite series:

G(x) = a0 + a1 x + … + ak xk + … = k = 0.. akxk.

also called the ordinary generating function to distinguish it from other kind of generating functions such as exponential generating functions: E(x) = a0 + a1 x/1! + … + ak xk/k!+ … = k = 0.. akxk/k! .

EX: {an} = 3,3,…,3,… => G(x) = k = 0.. 3xk.

{bn} =1,2,3,… => G(x) = 1 + 2x + 3x2+… = k = 0.. (k+1)xk.

{cn} = 1,2,4,8,… => G(x) = 1 + 2x + 4x2+… = k = 0.. 2kxk.

Page 35: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-35

More Examples

Ex2: {ak }k = 0..5 = <1,1,1,1,1,1> Then

G(x) = 1 + x + x2 + x3 + x4 + x5.

= (x6 -1) / (x -1)

Ex3: m: a positive integer, ak = C(m,k) for k = 0..m. Then

G(x) = C(m,0) + C(m,1)x + C(m,2) x2 +…+ C(m,m) xm

= (1+x)m.

Page 36: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-36

Some facts about formal power series

When generating functions are used to solve counting problems, they are usually represented as formal power series(形式冪級數 ). convergence problem ignorable here.

Ex4: f(x) = 1/(1-x) is the generating function of the series:

1,1,1,… since

1/(1-x) = 1 + x + x2+… for |x| < 1.

Ex5: f(x) = 1/(1-ax) is the generation function of the series:

1,a,a2,a3,… since

1/(1-ax) = 1 + ax + a2x2+ … for |x| < 1 / |a| .

Page 37: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-37

Sum and product of generating functions

Theorem 1 : Let f(x) = k = 0.. akxk, g(x) = k = 0.. bkxk. Then

f(x) + g(x) = k = 0.. (ak+bk ) xk.

f(x) g(x) = (a0 + a1x + a2x2+…) * (b0 + b1x1+ b2x2+…)

= k = 0.. (j= 0..k akbk-j) xk.

I.e., f(x) + g(x) is the generating function of the sum of both sequences: { ak+bk },

while f(x)g(x) is the generating function of the sequence: {ck} where ck = j= 0..k akbk-j ,

{ ck} is called the convolution of {ak} and {bk}.

Page 38: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-38

Ex 6

Since 1/(1-x) = 1 + x + x2 + x3+ … (*)

we have 1/(1-x)2

= (1 + x + x2 + x3+ …)(1 + x + x2 + x3+ … )

= 1 + (1x + x 1) + (1 x2 + x x + x2 1) + …

= k = 0.. (j= 0..k 1)xk = k = 0.. (k+1)xk.

Hence 1/(1-x)2 is the generating function of the seq:

1,2,3,4,… = { k+1 }k = 0..

Another approach: take derivatives on both sides of (*), we get

(1-x)-2 = 1 + 2x + 3x2 + … = k = 0.. (k+1)xk.

Page 39: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-39

Extended Binominal Coefficients

Definition 2: u: real number, k : nonnegative integer. Then the extended binominal coefficient C(u,k) is defined by

C(u,k) = u (u-1) … (u-k+1) / k! if k > 0, and 1 if k = 0.

EX 7: 1. C(-2, 3) = -2 -3 -4 / 3! = - 4. 2. C(1/2, 3 ) = (1/2)(1/2-1) (1/2 – 2) /3! = 1 (-1) (-3) / (2 * 2 * 2 * 6) = 1/16.

EX 8: n, r: nonnegative integers . Then C(-n, r) = (-n) (-n-1) … (-n –r+1) /r! = (-1) r P(n+r-1, r) /r! = (-1)r C(n+r-1, r) = (-1)r H(n,r).

Page 40: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-40

The Extended Binominal Theorem

Theorem 2: Let x be a real number with |x| < 1 and let u be a real number. Then

(1+x)u = k = 0.. C(u, k) xk, and hence

(x+y)u = yu(1+ (x/y))u = k = 0.. C(u, k) xkyu-k.

pf: Let f(x) = (1+x)u. By Tayler ‘s theorem

f(x) = f(a) + (x-a)f'(a) + (x-a)2f(2)(a)/2! +...+ (x-a)nf(n)(a)/n! + …

Now select a = 0, we have

f(x) = f(0) + f'(0) x + f(2)(0)/2! x2 +...+ f(n)(0)/n! xn + …

where f(k)(0) = u (u-1) … (u-k+1) (1+x)u-k |x = 0 = P(u,k) .

hence f(x) = k = 0.. C(u, k) xk.

Note: when u is a positive integer, Theorem 2 reduces to the normal Binominal theorem since ,when k > u,

C(u,k) = 0 and k = 0.. C(u, k) xk = k = 0..u C(u, k) xk.

Page 41: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-41

Ex 9

Ex9 : Find the formal power series for (1+x) –n and (1-x)-n,where n > 0.

Sol: (1+x) –n = k = 0.. C(-n, k) xk.

= k = 0.. (-1)k C(n+k-1, k) xk.

= k = 0.. (-1)k H(n, k) xk.

Replacing x by –x, we have

(1-x) -n = k = 0.. (-1)k C(n+k-1, k) (-x) k

= k = 0.. C(n+k-1, k) xk.

= k = 0.. H(n, k) xk.

Page 42: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-42

Some useful generating functions

G(x) {ak}

(1+x)n = k = 0..n C(n, k) xk

= 1 + C(n,1)x + C(n,2) X2 + …+ xn

C(n,k)

(1+ax)n = k = 0..n C(n, k)ak xk

= 1 + C(n,1)ax + C(n,2)a2 X2 + …+ anxn

C(n,k)ak

(1+xr)n = k = 0..n C(n, k) xkr

= 1 + C(n,1)xr + C(n,2) x2r + …+ xnr

C(n, k/r ) if r | k and

0 otherwise

(1- x n + 1 ) / (1-x) = k = 0..n xk

= 1 + x + x2 + …+xn

1 if k n

0 if k > n.

Page 43: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-43

Some useful generating functions (continued)

G(x) {ak}

1/(1-x) = 1+x+ x2 + x3+… 1

1/(1-ax) = 1 + ax + (ax)2 + … ak

1/(1-xr) = 1 + xr + x2r + … 1 if r | k,

0 O/W

1/(1-x)2 = k = 0.. (k+1) xk

= 1 + 2x + 3x2 + 4x3+…

k+1

1/(1-x)n = k = 0.. C(n+k-1,k) xk

= 1 + C(n,1) x + C(n+1,2) x2 + C(n+2,3)x3+…

C(n+k-1, k)

or H(n,k)

Page 44: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-44

Some generating functions (continued)

G(x) {ak}

1/(1+x)n = k = 0.. C(n+k-1,k) (-1)kxk

= 1 - C(n,1) x + C(n+1,2) x2 - C(n+2,3)x3+…

(-1)kC(n+k-1, k)

(-1)kH(n, k)

1/(1-ax)n = k = 0.. C(n+k-1,k) akxk

= 1 + C(n,1) ax + C(n+1,2) a2x2 + C(n+2,3)a3x3+…

C(n+k-1, k) ak

H(n, k) ak

ex = k = 0.. xk/k!

= 1 + x/1! + x2 /2! + x3/3!+ …

1/k!

ln(x+1) = k = 1.. (-1)k+1xk/k

= x – x2/2 + x3 /3 - x4/4+ …

since dkln(1+x)/dx(0)= (-1)k-1(k-1)! (1+x)–k|x=0 = (-1)k-1(k-1)!

(-1)k+1 /k

Page 45: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-45

More facts

Let G(x) = k = 0.. akxk = «a0,a1,…» is the generating function (gf) of the seq {an}. Note we use «a0,a1,…» to abbreviate k = 0.. akxk . Then

1. xG(x) =«0, a0,a1,a2,…» is the gf of {an} >>1. [shift right]

2. xk G(x) =«0,0,…,0, a0,a1,…» is the gf of {an} >> k.

3. (G(x)-a0)/x = «a1,a2,…» is the gf of {an} << 1 [shift left]

4. d G(x) / dx = «a1, 2a2, 3a3,…» is the gf of { (n+1) an+1 }n=0…

5. G(x) dx = a0 x + a1x2/2 + a2 x3/3 + …

= «0, a0, a1/2, a2/3,…»

is the gf of { an /(n+1) } >> 1.

Page 46: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-46

Counting problems and generating functions

Ex10: Find the number of solutions to e1 + e2 + e3 = 17, where e1,e2 and e3 are nonnegative integers with

2 e1 5, 3 e2 6 and 4e37.

Sol: The #solutions is equal to the coefficient of x17 in the expansion of the product:

(x12+x1

3+x14+x1

5)(x23+x2

4+x25+x2

6)(x34+x3

5+x36+x3

7)

The #Solutions is 3 (x5x5x7, x4x6x7 , x5x6x6)

e1=5 e2=6 e3=6

Page 47: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-47

Ex11: 8 identical cookies distributed among 3 different children with each child receiving at lest 2 cookies and no more than 4 cookies. #possible distributions = ?

Sol: the number is the coefficient of x8 in the product:

(x2+x3+x4)3,

which is equal to 6

( 4+2+2, 2+4+2, 2+2+4, 2+3+3, 3+3+2, 2+3+3).

Ex12: Determine the number of ways to insert coins worth $1,$2 and $5 into a vending machine to pay for an item costing $r, in case (1) the insertion order matters and (2) does not matter.

Sol: (1) order matters: => The possible ways of inserting n coins can be represented by (x + x2 + x5)n.

Page 48: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-48

So the possible ways of inserting coins is

1 + (x+x2+x3) + (x + x2 + x5)2+ (x + x2 + x5)3+ …

= 1/(1 –x – x2 –x3)

=> possible ways of inserting coins with total value r is

the coeff. of the term xr in the above generating function. for example, if r = 7, then the answer is 26. can be computed by computer algebra system (CAS) (Some CASs: Yacas, Maxima, Axioms, Euler, Maple).

Order does not matter: possible ways of inserting coins : x1 $1 + x2 $2 + x3 $5 = $r.

(1+x+x2+x3+…) (1+ x2+ x4 + …) (1+ x5 + x10+ x15+…) = 1/(1-x ) 1/(1-x2) 1/(1-x5) if r = 7, then xr has coefficient 6.

Page 49: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-49

Ex13 : Find #k-combinations of a set of size n.solu: = coefficient of xk in the product: (x0+x1) (x0+x1) … (x0+x1) = (1+x)n = f(x). By Binominal theorem : f(x) = k = 0..n C(n, k) xk where C(n,k) = n! / (k!(n-k)!)

hence #k-combinations = C(n,k).

Ex14: Find #r-comb from a set with n objects when repetition is allowed.

Sol: Let {ar} be the seq where ar = #r-comb with repetition allowed. Then {ar} has generating function:

(1+x+x2+…)… (1+x+x2+…) = (1+x+x2+…)n = (1+(-x))-n.

= k = 0.. C(-n,k)(-1)k xk , Thus #r-comb with repetition

= C(n+r-1, r) = H(n,r).

Page 50: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-50

Ex15: Find the number of ways to select r objects of n different kinds if each kind must be selected at least 1 object.

Sol: G(x) = (x + x2+ x3+ …)n.

= Xn (1+x+x2+…)n = Xn / (1-x)n

= xn(1-x)-n.

= xn k = 0.. H(n,k)xk

= k = 0.. H(n,k) xk+n

= t = n.. H(n,t-n) xt

=> there are H(n, r-n) = C(r-1, r-n) ways to select r objects.

Page 51: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-51

Using generating functions to solve recurrence relations

Ex16: Solve the recurrence relation : ak = 3 a k-1 for k >0 , and

a0 = 2.

Sol: Let G(x) be the generating function of the seq {ak}.

I.e., G(x) = k = 0.. akxk. = «a0, a1,a2,… »

=> xG(x) = k = 0.. akxk+1 = t = 1.. at-1xt. = «0, a0,a1,… »

=> G(x) – 3xG(x) = k = 0.. akxk - t = 1.. 3 at-1 xt.

= «a0, a1-3a0, a2-3a1,…»

= a0 + k = 1.. (ak - 3 ak-1 )xk = «2, 0,0,0…» = 2.

=> G(x) = 2/ (1-3x)

= 2(1+ 3x + (3x)2 + … )

Hence ak = 2 3k.

Page 52: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-52

Ex17: Solve the recurrence relation: a0 = 1, an = 8an-1 + 10n-1 for n > 0. (*)

Sol: Let G(x) be the generating function of the seq {an}.

By (*) we have an xn = 8an-1 xn + 10n-1 xn if n > 0

Since G(X) = «a0=1, a1,…»

G(x) – 1 = « 0,a1,a2,…» = « 0, 8a0 + 100, 8 a1+ 101,…»

= 8 « 0, a0,a1,…» + « 0, 100,101,102,…» = 8x G(x) + (x + 10x2 + 102x3 + … ) = 8x G(x) + x/(1-10x). G(X) (1-8x) = 1+ x/(1-10x) => G(x) = (1-9x) /(1-8x)(1-10x) = (½) (1/(1-8x) + 1/(1-10x)) = (½) « 1,8,82,…»+ (½) « 1,10,102,..»

Hence an = ½ (8n + 10n). Note: Using the method introduced in 7.2 is easier than using

generating function.

Page 53: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-53

Ex19: Show the Pascal’s identity C(n+1, k) = C(n,k) + C(n,k-1).sol: Since (1+x)n+1 = (1+x)(1+x)n = (1+x)n + x(1+x)n, => C(n+1,k) xk = C(n,k) xk+ x C(n, k-1) xk-1

=> C(n+1, k) = C(n,k) + C(n,k-1).

Ex18: Show that k = 1..n C(n,k)2 = C(2n,n) using generating functions.

pf: (1+x) 2n = [ (1+x)n ]2. = «C(n,0), C(n,1),…,C(n,n)»«C(n,0), C(n,1),…,C(n,n)» = «C(n,0)C(n,0), C(n,0)C(n,1)+C(n,1)C(n,0), …, C(n,0)C(n,k)+C(n,1)C(n,k-1)+…+C(n,k)C(n,0),… »Hence C(2n,n) = C(n,0)C(n,n)+C(n,1)C(n,n-1)+…+C(n,n)C(n,0) = C(n,0)C(n,0)+C(n,1)C(n,1)+…+C(n,n)C(n,n) -- C(n,k) = C(n,n-k).

= k = 1..n C(n,k)2

Page 54: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-54

Catalan Numbers

Find the number of ways to compute the product of a sequence of n+1 numbers ? Ex: input = 2,3,4 => (2x3) x 4, 2 x ( 3 x 4) input = 2,3,4,5 => (((2x3)x4)x5), ((23)(45)), ((2(34))5), (2((34)5)), (2(3(45)))

Equivalent problem(Section 7.1,Ex8): The number of ways to parenthesize the product of n+1 number a0,a1,…,an.

Let Cn denote the number of ways to multiply n+1 numbers.

Then C0 =1 ---(1) ; C1 = 1, C2 = 2, C3 = 5.

To find Cn, let the last multiplication be applied to the results of the products of (a0,…,ak) and (ak+1,…,an).

As a result, Cn = k=0..n-1 CkCn-k-1. --- (2) // nonlinear

Problem: Is there a simple expression for the Catalan number Cn? (Yes!)

Page 55: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-55

Solve the recurrence relation for Catalan numbers

Let G(X) = «C0,C1,…,Cn,…» be the generating function of {Cn}.

Then since Cn = k=0..n-1 CkCn-k-1,

G(x)G(x) = «C0,C1,…,Cn,…» «C0,C1,…,Cn,…»

= «C0C0, C0C1+C1C0, … ,k=0..n-1 CkCn-k-1, … »

= «C1, C2, C3,…, »

G(x)2 x = «0, C1, C2,…» = «C0,C1,…»- C0 = G(x) – 1.

As a result, we have x G(x)2 –G(x) + 1 = 0,

and G(x) is [1+(1 – 4x)1/2] / 2x or [1 - (1-4x)1/2] /2x.

But we require C0 = 1 = lim x 0 G(x) = 1,

Hence G(x) = [1 - (1-4x)1/2]/2x.

(1-4x)1/2 = n=0…C(1/2,n)(-4x)n =1+n=1… (-2/n) C(2(n-1), n-1) xn

since C(1/2, n) = 1/2n C(2(n-1),n-1) / (-4)n-1 if n > 0 –see next slide

Page 56: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-56

C(-1/2, k) = -½ (-½ -1)(-½ -2) … (-½ -(k-1)) / k!

= (-1)k (1 3 5 … (2k -1)) /2k k!

= (-1)k (1 3 5 … (2k -1)) (2 4 6 … 2k) / 2k k! (2 4 … 2k)

= (-1)k (2k)! / 2k 2k k! k!

= C(2k, k) / (-4)k.

Hence if n > 0, C(1/2, n)

= ½ (-½)(-½ -1)(-½ -2) … (-½ -(n-1-1)) / n!

= ½ 1/n C(-1/2, n-1)

= 1/2n C(2(n-1), n-1)/(-4)n-1

Hence G(x) = G(x) = [1 - (1-4x)1/2]/2x.

x n=0..C(1/2, n)(-4x)n ] =1/(2x) n=1.. 2/n C(2(n-1), n-1) xn

n=1.. 1/n C(2(n-1), n-1) xn-1

= n=0.. 1/(n+1) C(2n, n) xn

As a result, Cn = C(2n,n) / n+1.

Page 57: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Transparency No. 7-1

Discrete Mathematics

Chapter 7

Advanced Counting Techniques

Page 58: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-58

Contents

7.1 Recurrence Relations

7.2 Solving Recurrence Relations

7.3 Divide-and-conquer Relations

7.4 Generating Functions

7.5 The Inclusion-Exclusion principle

7.6 Applications of the Inclusion-Exclusion principle

Page 59: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-59

7.3 Divide-and-conquer relations

f(n): resources (time or space) needed to solve a problem of size n. Then

f(n) = a f(n/b) + g(n) : where a : the number of subproblems n/b : size of each subproblem g(n) : cost for splitting problem and combining solutions

Problem: How to estimate the size of f(n) ? f(n) = a f(n/b) +g(n) = a2f(n/b2) + a g(n/b) + g(n)

= .... = akf(n/bk) + j=0,k-1 ajg(n/bj).

Hence if n = bk ==> f(n) = akf(1) + j=0,k-1 ajg(n/bj) ----- (1)

Theorem 1: If f(n) = a f(n/b) + c, where a 1, b > 1 and c > 0, is an increasing function, then

f(n) = O(n(logba)) if a > 1 and

= O(log n) if a = 1.

Notations:log n = log10 n ln n = loge n lg n = log2 n

Page 60: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-60

proof:

Pf: If n = bk, by (1), f(n) = akf(1) + j=0,k-1 aj c.

If a = 1 ==> f(n) = f(1)+j=0,k-1 c

= f(1)+ck = f(1)+c logb10 log10 n = O(log n).

If a > 1 ==> f(n) = akf(1) + j=0,k-1 aj c

= akf(1) + c(ak-1) /(a-1) = ak [f(1) + c/(a-1)] - c/(a-1)

= c1 algbn + c2 = c1 nlogba + c2 = O(nlogba).

If bk < n < bk+1 is not a power of b.

==> f(n) < f(bk+1) = c1 ak+1 + c2 < c1a alogbn + c2 = O(nlogba ) .

Ex: If f(n) = 5 f(n/2) + 3.

=> a = 5 > 1; c = 3; b = 2. ==> f(n) = O(nlogba ) = O(nlg 5 )

If f(n) = 2 f(n/2) + 2

==> f(n) = O(nlg 2) = O(n) is linear.

if f(n) = f(n/2) + 2 --- binary search, then f(n) = O(log n).

Page 61: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-61

divide-and-conquer relations(cont'd)

Theorem 2: If f(n) = a f(n/b) + cnd, where a 1, b > 1 and c,d > 0, is increasing, then

f(n) = O(nd) if a < bd // nd dominates if #subproblems is small

= O(nd log n) if a=bd //every level of problems requires nd.

= O(nlogba ) if a > bd. // nd (conquer effort) has no effect.

Pf: f(bk) = a f(bk-1) + cbkd = a2f(bk-2) + c a1bd(k-1) + c bdk.

= .... = akf(1)+j=0,k-1 cajbd(k-j) = akf(1) + j=0,k-1cbdk(a/b)j

= f(1) ak + cbdk (1-(a/bd)k) / (1-(a/bd)).

case 1: n = bk. (k = logb n)

Hence if a < bd ==> f(n) = f(1) ak + cbdk (1-(a/bd)k)/(1 - (a/bd))

O(bkd) = O( (bk)d) = O(nd).

If a = bd =>f(n) = f(1) ak+ckbkd = O(kbkd) = O(nd logbn) =O(nd log n)

If a > bd => f(n) = f(1) ak + cbkd (1-(a/bd)k)/(1 - (a/bd)).

f(1) ak + cbkd (a/bd)k = O(ak) = (nlogba).

Page 62: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-62

The master theorem:

case 2: bk < n < b k+1:

=> f(bk) f(n) f(bk+1).

But O(f(bk+1)) = O(f(bk)), hence O(f(n)) =O(f(bk)). QED

Ex8:Fast integer multiplication:

A = (a2n-1 a2n-2 ... a1 a0)

B = (b2n-1 b2n-2 ... b1 b0)

AH = (a2n-1...an); AL = (an-1,...,a0); BH = ..., BL = ...

=> A x B = (2n AH + AL) x (2nBH + BL) = 22n AHBH + 2n(AHBL + ALBH) + (ALBL)

=> f(n) = 4f(n/2) + O(n) => f(n) = O(n lg 4) = O(n2) - no improving !!

But AXB = 22n AHBH + 2n(AHBL + ALBH) + (ALBL) -- 4 multiplications

= 22n AHBH + 2n((AH+AL)(BH+BL)) - AHBH - ALBL) + (ALBL)

-- 3 multiplications

=> f(n) = 3f(n/2) + O(n) => f(n) = O(nlg 3) =O(n1.73)< O(n2).

Page 63: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-63

More examples:

Fast matrix multiplication:(Ex5)

one nxn matrix multiplication can be divided into

7 (n/2)x(n/2) multiplications + 15 (n/2)x(n/2) additions.

=> f(n) = 7 f(n/2) + 15n2/4

=> a = 7, b = 2, d = 2. => a > bd = 4.

=> f(n) = O(n lg 7) = O(n2.81).

Better than direct multiplication(=O(n3)) !!

Although even better result (O(n2.376 )) is possible.mergeSort (A) { n = A.length ;

if(n < 2) return A;

B = mergeSort(A[0.. n/2 -1]); C = mergeSort(A[n/2..n-1]);

return merge(B,C); // this step takes time O(n)!!

} // f(n) = 2 f(n/2) + O(n) => f(n) = O(n log n).

Page 64: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-64

7.5 Inclusion-Exclusion principle

The principle: A, B : two finite sets

=> |AUB| = |A| + |B| - |A B|.

Ex2: #positive integers < 1000 and dividable by 7 or 11 = ?

sol: let A ={x | x < 1000 and 7 | x}

B = {x | x < 1000 and 11 |x}.

=> |AB| ={x | x < 1000 and 77 | x}

=> |AUB| = |A| + |B| - |AB|

= 1000/7 + 1000/11 - 1000/77 ⌊ ⌋ ⌊ ⌋ ⌊ ⌋ = 220.

Problems:1. |AUBUC| = ?;

2. |A1UA2UA3UA4| = ?

ans: 1. |A|+|B|+|C|-|AB|-|AC|-|BC|+|ABC|

1 1

1

2

2 23

1 1

1

1

1 10

|A|+|B|+|C|

|A|+|B|+|C| -|AB|-|AC|-|BC|

Page 65: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-65

The general inclusion-exclusion principle

Theorem 1: |A1 U A2 ...UAn | = 1in |Ai| - 1i<jn|AiAj| +

1i<j<kn|AiAjAk| - ... +(-1)n+1 |A1A2...An| ---(*)

pf: Let a be any element belonging to exactly Ad1,

Ad2,...,Adr. (i.e., ∉ Ad for d ∈ {1,…,n} - {d1,…,dr}.)

==> It is counted C(r,s) times by the sum:

1≤j1<j2<...<js ≤n |Aj1 Aj2 ...Ajs|.

(note: C(r,s) = 0 if r < s).

==> # a counted by (*) = C(r,1) - C(r,2) + ... +(-1)n+1 C(r,n)

= C(r,1) - C(r,2) + ... +(-1)r+1 C(r,r)

= (-1) [ C(r,0) - C(r,1) + C(r,2) + ... +(-1)r C(r,r) ] + C(r,0)

= - (1-1)r + C(r,0) = 1

Page 66: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-66

7.6 Applications of the IE principle

Alternative form:

|~A1~A2... An)| = |~(A1UA2...UAn)|

= |U| - | A1UA2...UAn |

= |U| - |Ai| + |AiAj| -... + (-1)n |A1...An|.

Ai : set of elements having property Pi.

N(Pj1,...,Pjk) = #elements with properties Pj1,...,Pjk.

(i.e. N(Pj1,...,Pjk) = |Aj1 ...Ajk|. )

~Pj : the negation of property Pj.

N(~Pj1,...,~Pjk)) = #elements without any property of Pj1,..,pjk.

= N(~(Pj1\/...\/Pjk)) = |U| - |Aj1.. Ajk|.

N(~P1,...~Pn)

= |U| – (Pi) + (PiPj) - (PiPjPk) +... +(-1)n N(P1P2...Pn).

Page 67: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-67

Examples:

Ex1: x1+x2+x3 = 11, 0 x1 3, 0 x2 4, 0 x3 6.

==> #integer solutions = ?

Sol: let P1 = "x1 > 3"; P2 = "x2 > 4"; P3 = "x3 > 6".

=>#sol = N(~p1/\~p2/\~p3) = N(~(P1\/P2\/P3))

= N - N(p1) - N(P2) - N(P3) +N(p1/\P2) +N(P1/\P3) + N(P2/\P3)

- N(N(p1/\P2/\P3)

=> N = C(11+2, 2);

N(p1) = #sol with x1 > 3 = #sol of "x1' + x2 + x3 = 7" = C(9,2)

N(p2) = C(8,2); N(P3) = C(6,2).

N(p1/\P2) = #slo with X1> 3 and X2 > 4 = #sol of "x1'+x2'+X3 = 2" = C(4,2) = 6.

N(P1/\P3) = C(0+2,2) = 0; N(P2/\P3) = 0.

N(P1/\P2/\P3) = 0.

=> #sol = 78 - 36 - 28 - 15 + 6+ 1 + 0 - 0 = 6.

Page 68: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-68

More example

Ex2: #positive integers 100 and dividable by 4, 5 or 6.

A = {x : 4|x}; B = {x: 5|x }; C= {x: 6|x}.

=> |A| +|B|+|C| = [100/4] + [100/5] + [100/6] = 25 + 20 + 16 =61.

|AB|+|AC|+|BC| = [100/20] +[100/12] + [100/30] =16

|ABC| = [100/60] = 1. => #sol = 61-16+1 =46.

Ex3': |A|= m, |B| = n, m n. #onto function f:A -> B = ?

Let B = {b1,...,bn} and Pi = "bi is not in the range of the fun"

=> N(Pi) = |Ci|= |{f | f:A->B and bi f(A)}|.∉

#ontos = N(~P1/\~P2.../\~Pn) = N(~(P1\/...\/Pn))

= |U| - |C1 U C2 U... U Cn|

= nm - |Ci| + |Ci Cj| - ... +(-1)n |C1C2...Cn|

= nm - C(n,1)(n-1)m + C(n,2)(n-2)m -... +(-1)n-1 C(n,n-1) 1 m.

Page 69: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-69

more examples (cont'd)

Ex3: #ways to assign 5 jobs to 4 people s.t. each one is assigned at least one job.

sol: m =5, n = 4.

#ways = 45 -C(4,1)35 + C(4,2)25+C(4,3)15

= 1024 - 972 + 192 - 4 = 240.

Ex4: [The hatcheck problem:] n hats randomly returned to the customers. => What is the probability that no one receives his own hat?

Page 70: Transparency No. 7-1 Discrete Mathematics Chapter 7 Advanced Counting Techniques

Advanced counting techniques

Transparency No. 7-70

Derangementsx1,x2,...,xn: a list

Any permutation a1,...,an of {x1,...,xn} s.t. aj xj for all j = 1..n is called a derangement of the list.

Ex: 1,2,3,4,5 has derangement 21453 but not 21543

Let Dn = # derangements of n objects.

=> D1 = 0, D2 = 1, D3 = 2. (123 has derangements: 231, 312)

Theorem: Dn = n! (1- 1/1! + 1/2! +... + (-1)n /n! )

Pf: let Pi =def "a=a1...an is a perm of x1...xn s.t. ai = xi."

=> Dn = N(~P1/\~P2/\../\~Pn) = N(~(P1\/P2\/..\/Pn))

= N -N(Pi) + N(Pi/\Pj) -... + (-1)nN(p1/\../\Pn) = n! -C(n,1)(n-1)! +C(n,2)(n-2)! + ... +(-1)nC(n,n)(n-n)! = n![1 - 1/1! + 1/2! .. ].

=> solu of the hatcheck problem = Dn / P(n,n) = Dn /n! [0.3,0.5]∈

= (1 -1/1! + 1/2!-...) e-1 = 0.368 as n .