Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Counting Techniques

Similar presentations


Presentation on theme: "Advanced Counting Techniques"— Presentation transcript:

1 Advanced Counting Techniques
Chapter 7 Advanced Counting Techniques

2 7.2 Solving Recurrence Relations 7.4 Generating Functions (skipped)
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

3 #bacteria doubles every hour. Initially(t=0), there are 5 bacteria
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 = (1) initial condition an = 2 an-1 for n > (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 = 5xan/2 + n3.

4 Modeling counting problems in terms of recurrence relations
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.

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  n (*) 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 = 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.

6 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 = Pn-2=...= 1.11n P0 = 1.11n x ==> P30 = x = 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 fn = fn-1 + fn-2 for n > 1 , where fn-1: #old-rabbits and fn-2 is #new born rabbits.

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 H n-1 = 2Hn A--n-->B A--n-1-->C A--1-->B C--n-1-->B. Hence Hn = 2 Hn = 22 Hn = ... = 2n-1 H1 + 2n = 2 n -1. How big is H64 ? : assume one move takes 1 sec => = 1.8 x = 500 billion years!!

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.

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 an/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 .

10 Theorem 0: A recurrence relation of degree 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 at  bt. 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.

11 Proof of existence Pf: Proof by construction: Define a sequence {an} by the following process: 1. for ( t = 0 to k-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.

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.

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

14 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 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 inhomogeneous constant coefficients since all c1,…,ck are constants. k is the degree of the recurrence relation.

15 Ex: Let an = c1 an-1 + c2an-2+...+ ckan-k -- (1)
Solving linear homogeneous recurrence relations with constant coefficients (lhrrcc) Let an = c1 an-1 + c2an 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 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 ckrn-k iff rn-k (rn - c1 rn-1 - c2rn ck) = 0 iff (rn - c1 rn-1 - c2rn 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.

16 an = c1 an-1 + c2an-2 -- (3) a lhrrcc relation of degree 2.
Theorem 1 an = c1 an-1 + c2an (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

17 sol: char equ: r2 = r +2 has roots 2, -1.
Examples: Ex3: Find the solution of an = an 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.

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: a=(1+rt(5)) /2, // golden ratio b=(1-rt(5))/ note: 1< a1.618 < 2 and -1 < b <0. Hence fn = d1 an + d2 bn 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.

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

20 Ex5: find the solution of
an = 6an 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.

21 (i.e. rk - c1 rk-1 +...-ck = Pi=1..s (x-ri)mi ) then
Generalization Theorem 4: If the equation rk = c1 rk-1 + c2rk ck has solutions r1m1, r2m2,..,rsms (with m1+...+ms = k) where mi is the multiplicity of the root ri. (i.e. rk - c1 rk ck = Pi=1..s (x-ri)mi ) then {an} is a solution of the recurrence relation: an = c1 an 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 an-2 -7 an 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.

22 Simultaneous recurrence relations of degree 1
Ex: Solving the simultaneous recurrence relations: 1. an = 3 an bn-1 2. bn = an 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 (d1 l1Y1 + d2l2Y2) = ... = d1 l1n Y1 + d2l2n Y2.

23 has known how to solve lhrr with constant coefficients.
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)

24 an = an-1 + 2n an = an-1+an-2 + n2+n + 1 an = 3 an-1 + n 3n
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 constant coefficients. Their associated lhrr are : an = an-1 an = an-1+an-2 an = 3 an-1 an = an-1+an-2+an-3

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

26 Sol: The key to the problem is to find a particular solution 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 = a /2 and a=11/6. Hence an = 11/6 x 3n - n-3/2.

27 Find all solutions of the recurrence relation :
Example 11 Find all solutions of the recurrence relation : an = 5 an 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 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 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.

28 p(n) sn, where p(n) is a polynomial of degree t (= degree(b(n) ).
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 sn where p(n) is a polynomial of degree t.

29 Find the form of a particular solution for the recurrence relation
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, n3n n2 3n 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 (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.

30 So the form of the solution of the assocaited hrr is e1n = e.
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

31 Find all solutions of the recurrence relation:
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 (**).

32 The problem can be reduced to that of finding particular solutions to
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 (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).

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-2pift dt ---> F(f) s(t) <--- - + F(f)e2pift df F(f)

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 + … = 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.

35 Ex2: {ak }k = 0..5 = <1,1,1,1,1,1> Then
More Examples Ex2: {ak }k = = <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.

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

37 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}.

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

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: C(-2, 3) = / 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).

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

41 Replacing x by –x, we have
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.

42 Some useful generating functions
G(x) {ak} (1+x)n = Sk = 0..n C(n, k) xk = 1 + C(n,1)x + C(n,2) X2 + …+ xn C(n,k) (1+ax)n = Sk = 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 = Sk = 0..n C(n, k) xkr = 1 + C(n,1)xr + C(n,2) x2r + …+ xnr C(n, k/r ) if r | k and otherwise (1- x n + 1 ) / (1-x) = Sk = 0..n xk = 1 + x + x2 + …+xn 1 if k  n 0 if k > n.

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 = Sk = 0.. (k+1) xk = x + 3x2 + 4x3+… k+1 1/(1-x)n = Sk = 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)

44 Some generating functions (continued)
G(x) {ak} 1/(1+x)n = Sk = 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 = Sk = 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 = Sk = 0..  xk/k! = 1 + x/1! + x2 /2! + x3/3!+ … 1/k! ln(x+1) = Sk = 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

45 xG(x) =«0, a0,a1,a2,…» is the gf of {an} >>1. [shift right]
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.

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 4e37. 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= e2= e3=6

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, , ). 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.

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.

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) = 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).

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 Sk = 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.

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) = Sk = 0.. akxk. = «a0, a1,a2,… » => xG(x) = Sk = 0.. akxk+1 = St = 1.. at-1xt. = «0, a0,a1,… » => G(x) – 3xG(x) = Sk = 0.. akxk - St = 1.. 3 at-1 xt. = «a0, a1-3a0, a2-3a1,…» = a0 + Sk = 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.

52 Ex17: Solve the recurrence relation:
an = 8an n-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, 8a , 8 a1+ 101,…» = 8 « 0, a0,a1,…» + « 0, 100,101,102,…» = 8x G(x) + (x + 10x x3 + … ) = 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.

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 Sk = 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). = Sk = 1..n C(n,k)2

54 Ex: input = 2,3,4 => (2x3) x 4, 2 x ( 3 x 4)
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 = S k=0..n-1 CkCn-k (2) // nonlinear Problem: Is there a simple expression for the Catalan number Cn? (Yes!)

55 Solve the recurrence relation for Catalan numbers
Let G(X) = «C0,C1,…,Cn,…» be the generating function of {Cn}. Then since Cn = S k=0..n-1 CkCn-k-1, G(x)G(x) = «C0,C1,…,Cn,…» «C0,C1,…,Cn,…» = «C0C0, C0C1+C1C0, … , S 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 = Sn=0…C(1/2,n)(-4x)n =1+Sn=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

56 C(-1/2, k) = -½ (-½ -1)(-½ -2) … (-½ -(k-1)) / k
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. = 1/2x [1- Sn=0..C(1/2, n)(-4x)n ] =1/(2x) Sn=1.. 2/n C(2(n-1), n-1) xn = Sn=1.. 1/n C(2(n-1), n-1) xn-1 = Sn=0.. 1/(n+1) C(2n, n) xn As a result, Cn = C(2n,n) / n+1.

57 Advanced Counting Techniques
Chapter 7 Advanced Counting Techniques

58 7.2 Solving Recurrence Relations 7.3 Divide-and-conquer Relations
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

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) + Sj=0,k-1 ajg(n/bj). Hence if n = bk ==> f(n) = akf(1) + Sj=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

60 Pf: If n = bk, by (1), f(n) = akf(1) + Sj=0,k-1 aj c.
proof: Pf: If n = bk, by (1), f(n) = akf(1) + Sj=0,k-1 aj c. If a = 1 ==> f(n) = f(1)+Sj=0,k-1 c = f(1)+ck = f(1)+c logb10 log10 n = O(log n). If a > 1 ==> f(n) = akf(1) + Sj=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) binary search, then f(n) = O(log n).

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)+Sj=0,k-1 cajbd(k-j) = akf(1) + Sj=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).

62 case 2: bk < n < b k+1: => f(bk) £ f(n) £ f(bk+1).
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 a1 a0) B = (b2n-1 b2n 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) 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).

63 Fast matrix multiplication:(Ex5)
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).

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}. => |AÇB| ={x | x < 1000 and 77 | x} => |AUB| = |A| + |B| - |AÇB| =⌊ 1000/7⌋ + ⌊1000/11 ⌋- ⌊1000/77 ⌋ = 220. Problems:1. |AUBUC| = ?; 2. |A1UA2UA3UA4| = ? ans: 1. |A|+|B|+|C|-|AÇB|-|AÇC|-|BÇC|+|AÇBÇC| 1 2 3 |A|+|B|+|C| 1 |A|+|B|+|C| -|AÇB|-|AÇC|-|BÇC|

65 The general inclusion-exclusion principle
Theorem 1: |A1 U A2 ...UAn | = S1£i£n |Ai| - S1£i<j£n|AiÇAj| + S1£i<j<k£n|AiÇAjÇAk| (-1)n+1 |A1ÇA2Ç...Ç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: S1≤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

66 7.6 Applications of the IE principle
Alternative form: |~A1Ç~A2... Ç~An)| = |~(A1UA2...UAn)| = |U| - | A1UA2...UAn | = |U| - S|Ai| + S|AiÇAj| (-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| – SN(Pi) + SN(PiPj) - S N(PiPjPk) (-1)n N(P1P2...Pn).

67 ==> #integer solutions = ?
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 = = 6.

68 Ex2: #positive integers  100 and dividable by 4, 5 or 6.
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] = =61. |AÇB|+|AÇC|+|BÇC| = [100/20] +[100/12] + [100/30] =16 |AÇBÇC| = [100/60] = 1. => #sol = =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 - S|Ci| + S|Ci Ç Cj| (-1)n |C1ÇC2Ç...ÇCn| = nm - C(n,1)(n-1)m + C(n,2)(n-2)m (-1)n-1 C(n,n-1) 1 m.

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 = = 240. Ex4: [The hatcheck problem:] n hats randomly returned to the customers. => What is the probability that no one receives his own hat?

70 Let Dn = # derangements of n objects.
x1,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 -SN(Pi) + SN(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 = as n ¥ .


Download ppt "Advanced Counting Techniques"

Similar presentations


Ads by Google