700 likes | 812 Views
Chapter 7. Advanced Counting Techniques. 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. 7.1 Recurrence relations.
E N D
Chapter 7 Advanced Counting Techniques
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
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}nÎN =<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.
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.
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.
Recurrence relation example Ex3: 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.
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!!
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.
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 .
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 {an} and {bn} both satisfy all equations => {an} = {bn} (i.e., an = bn for all n .) Assume {an} , {bn} are distinct sequences, then by well-order property of natural numbers, there must exist a least t such that atbt. Such t must not < k due to the initial conditions. But if t k, the minimum of t implies as = bs for all s < t, and hence by the recurrence relation at = bt. , As a result, t does not exist and there are no distinct sequences satisfying all equations.
Proof of existence Pf: Proof by construction: Define a sequence {an} by the following process: • 1. for ( t = 0 tok-1) at Ct . • 2. for ( t = k to ) at f(t, at-1, at-2,…,at-k) • Such process must assign at most one value at for each position t 0 since the index t in each execution of at … is distinct from one another. • Such process must assign exactly one value at for each position t 0 s.t. at = Ct for 0 t k-1 and at = f(t, at-1, at-2,…,at-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 at-1, at-2,…,a0 have been assigned values by Line1, • f(t, at-1, at-2,…,at-k) is well-defined and execution of at f(…) results in a new position t to be assigned value at = f(t, at-1, at-2,…,at-k). • Now for t > k, by Ind.hyp., all at-1, at-2,…,at-k have been assigned values, hence f(t, at-1, at-2,…,at-k) is well-defined and execution of at f(…) assigns the value at = f(t, at-1, at-2,…,at-k) to a new position t. By 2, {an} is a sequence satisfying all equations.
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.
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]; }
7.2 Solving linear recurrence relations • A 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-5is 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.
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-5has characteristic equation r5 = 3r4 + 1.
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 r2n 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+d2r2k-1) + c2 (d1r1k-2 +d2r2k-2) = d1(c1r1k-1+c2r1k-2) +d2(c1r2k-1+c2r2k-2) = d1r1k-2(c1r1+c2) +d2r2k-2 (c1r2+c2) = (by **) d1 r1k + d2 r2 k. QED
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.
Solution of the Fibonacci sequence Ex4: Fins 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: a=(1+rt(5)) /2, // golden ratio b=(1-rt(5))/2. note: 1< a1.618 < 2 and -1 < b <0. Hence fn = d1an + d2bn with f0 = 1 = d1 + d2 and f1 = 1 = d1a + d2b • => d1 = rt(5)/5 and d2 = -rt(5)/5. => fn = d1an + d2bn = O(an) grows exponentially.
Solving recurrence relation Theorem 2: If r2 = c1 r + c2has only one root r0 (with duplicate multiplicity) then any sequence {an} is a solu of (3) iff an = d1 r0n + d2n r0n 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) r0n-1 a n-2 = d1 r0n-2 + d2 (n-2) r0n-2 ==> c1 an-1 + c2 an-2 = d1 (c1 r0n-1 + c2 r0n-2) + d2 (c1(n-1) r0n-1 + c2(n-2) r0n-2) = d1 r0n-2(c1 ro + c2) + d2 r0n-2( (n-1)(c1ro+c2) - c2) = d1 r0n + d2 r0n-2((n-1) r02 + r02) -- since r2- c1r -c2= (r-r0)2 = r2 - 2 r0 r + r02. = d1 r0n + d2 n r0n
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.
Generalization Theorem 4: If the equation rk = c1 rk-1 + c2rk-2+...+ ck has solutions r1m1, r2m2,..,rsms (with m1+...+ms = k) where mi is the multiplicity of the root ri. (i.e. rk - c1 rk-1 +...-ck = Pi=1..s (x-ri)mi ) then {an} is a solution of the recurrence relation: an = c1 an-1 +...+ck an-k iff an = Si=1,s (Sj=0,mi-1 dij nj ) rin , where dij's are constants and (Sj=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.
Simultaneous recurrence relations of degree 1 (skipped) 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 = lY for some l.) => (A-lI)Y = 0 => det(A-lI) = 0 => (3-l)(2-l) - 2 = 0 => l =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 (d1l1Y1 + d2l2Y2) = ... = d1l1n Y1 + d2l2n Y2.
Linear Nonhomogeneous Recurrecne Relations (lnhrr) with Constant Coefficients • 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)
Examples • an = an-1 + 2n • an = an-1+an-2 + n2+n + 1 • an = 3 an-1 + n 3n • an = an-1+an-2+an-3 + n! • All the above 4 rrs are lnhrr with constatn coefficients. Their associated lhrr are : • an = an-1 • an = an-1+an-2 • an = 3 an-1 • an = an-1+an-2+an-3
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}.
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 a 3n. Hence by Theorem 5, all solutions are of the form: an=a3n-n-3/2. If a1 = 3, then 3 = a31 - 1 -3/2 and a=11/6. Hence an = 11/6 x 3n - n-3/2.
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 a 2n + b 3n, and the general solution to (*) is a2n+b3n+(49/20)7n.
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. • 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) ). • If s is a root of the characteristic equation with multiplicity m, then there is a particular solution of the form : p(n) nm snwhere p(n) is a polynomial of degree t.
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) = • 3n, 2. n3n 3. n2 3n4. n2 2nor 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 • (cn+d) // (cn+d)1n + (an+b) 3n • (cn+d)n23n //(cn+d) n23n +(an+b)3n =(cn3+dn2+an+b) 3n • (cn2+dn+e)n23n //(cn2+dn+e)n23n +(an+b) 3n • (cn2+dn+e)2n //(cn2+dn+e)2n + (an+b) 3n • (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.
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.
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 (**).
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 a2n + b 3n + (-2) n 2n + (3n/2+ 21/4).
7.4 Generating Functions • 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-2pift dt ---> F(f) • s(t) <--- - + F(f)e-pift df ------ F(f)
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 + … = Sk = 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!+ … = Sk = 0.. akxk/k! . • EX: • {an} = 3,3,…,3,… => G(x) = Sk = 0.. 3xk. • {bn} =1,2,3,… => G(x) = 1 + 2x + 3x2+… = Sk = 0.. (k+1)xk. • {cn} = 1,2,4,8,… => G(x) = 1 + 2x + 4x2+… = Sk = 0.. 2kxk.
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.
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| .
Sum and product of generating functions • Theorem 1 : Let f(x) = Sk = 0.. akxk, g(x) = Sk = 0.. bkxk. Then • f(x) + g(x) = Sk = 0.. (ak+bk ) xk. • f(x) g(x) = (a0 + a1x + a2x2+…) * (b0 + b1x1+ b2x2+…) • = Sk = 0..(Sj= 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 = Sj= 0..k akbk-j , • { ck} is called the convolution of {ak} and {bk}.
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) + … = Sk = 0.. (Sj= 0..k 1)xk = Sk = 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 + … = Sk = 0.. (k+1)xk.
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).
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 = Sk = 0.. C(u, k) xk, and hence (x+y)u = yu(1+ (x/y))u = Sk = 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) = Sk = 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 Sk = 0.. C(u, k) xk = Sk = 0..u C(u, k) xk.
Ex 9 Ex9 : Find the formal power series for (1+x) –n and (1-x)-n,where n > 0. Sol: • (1+x) –n = Sk = 0.. C(-n, k) xk. • = Sk = 0.. (-1)k C(n+k-1, k) xk. • = Sk = 0.. (-1)k H(n, k) xk. • Replacing x by –x, we have • (1-x) -n = Sk = 0.. (-1)k C(n+k-1, k) (-x) k • = Sk = 0.. C(n+k-1, k) xk. • = Sk = 0.. H(n, k) xk.
More facts • Let G(x) = Sk = 0.. akxk = «a0,a1,…» is the generating function (gf) of the seq {an}. Note we use «a0,a1,…» to abbreviate Sk = 0.. akxk . Then • xG(x) =«0, a0,a1,a2,…» is the gf of {an} >>1. [shift right] • xk G(x) =«0,0,…,0, a0,a1,…» is the gf of {an} >> k. • (G(x)-a0)/x = «a1,a2,…» is the gf of {an} << 1 [shift left] • d G(x) / dx = «a1, 2a2, 3a3,…» is the gf of { (n+1) an+1 }n=0… • G(x) dx = a0 x + a1x2/2 + a2 x3/3 + … = «0, a0, a1/2, a2/3,…» • is the gf of { an /(n+1) } >> 1.
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+x13+x14+x15)(x23+x24+x25+x26)(x34+x35+x36+x37) The #Solutions is 3 (x5x5x7, x4x6x7 , x5x6x6) e1=5 e2=6 e3=6
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.
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.
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) = Sk = 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. = Sk = 0.. C(-n,k)(-1)k xk , Thus #r-comb with repetition = C(n+r-1, r) = H(n,r).
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. = xnSk = 0.. H(n,k)xk = Sk = 0.. H(n,k) xk+n = St = n.. H(n,t-n) xt => there are H(n, r-n) = C(r-1, r-n) ways to select r objects.