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

Slides:



Advertisements
Similar presentations
Advanced Counting Techniques
Advertisements

Chapter 6 Advanced Counting 6.1 Recurrence Relations.
BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS
CS 2210 (22C:19) Discrete Structures Advanced Counting
April 9, 2015Applied Discrete Mathematics Week 9: Relations 1 Solving Recurrence Relations Another Example: Give an explicit formula for the Fibonacci.
Chapter Recurrence Relations
Part 5. Computational Complexity (3)
1 Copyright M.R.K. Krishna Rao Solving Recurrence Relations Steps for solving a linear homogeneous recurrence relation of degree 2 : Step #1.
CSE115/ENGR160 Discrete Mathematics 04/19/12 Ming-Hsuan Yang UC Merced 1.
Transparency No. 5-1 Discrete Mathematics Chapter 5 Advanced Counting Techniques.
Permutations r-permutation (AKA “ordered r-selection”) An ordered arrangement of r elements of a set of n distinct elements. permutation of a set of n.
CSE115/ENGR160 Discrete Mathematics 02/21/12
1 Section 6.1 Recurrence Relations. 2 Recursive definition of a sequence Specify one or more initial terms Specify rule for obtaining subsequent terms.
6.Advanced Counting Techniques 1 Copyright M.R.K. Krishna Rao 2003 Ch 6. Recurrence Relations A recurrence relation for the sequence {a n } is an equation.
Chapter 8 With Question/Answer Animations 1. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
Applied Discrete Mathematics Week 9: Relations
Advanced Counting Techniques
7.4 Generating Functions Definition 1: The generation function for the sequence a 0, a 1,...,a k,... of real numbers is the infinite series G(x) = a 0.
Jessie Zhao Course page: 1.
Discrete Math For Computing II
Chapter 8. Section 8. 1 Section Summary Introduction Modeling with Recurrence Relations Fibonacci Numbers The Tower of Hanoi Counting Problems Algorithms.
Chap. 7 (c) , Michael P. Frank1 Chapter 7: Advanced Counting Techniques.
Advanced Counting Techniques CSC-2259 Discrete Structures Konstantin Busch - LSU1.
Advanced Counting Techniques
4.6.2 Exponential generating functions
Chapter 8 With Question/Answer Animations 1. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
14.1 CompSci 102© Michael Frank Today’s topics Recurrence relationsRecurrence relations –Stating recurrences –LiHoReCoCo –Divide & conquer –Master’s method.
Recurrence Relation. Outline  What is a recurrence relation ?  Solving linear recurrence relations  Divide-and-conquer algorithms and recurrence relations.
CSE 2813 Discrete Structures Recurrence Relations Section 6.1.
R. Johnsonbaugh Discrete Mathematics 7 th edition, 2009 Chapter 7 Recurrence Relations Instructor Tianping Shuai.
15.1 CompSci 102© Michael Frank Today’s topics Recurrence relationsRecurrence relations –Stating recurrences –LiHoReCoCo Reading: Sections Reading:
Module #17: Recurrence Relations Rosen 5 th ed., §
1 Chapter 7 Generating functions. 2 Summary Generating functions Recurrences and generating functions A geometry example Exponential generating functions.
RECURRENCE Sequence Recursively defined sequence
Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 1/18 Module.
Chapter 7 Advance Counting Techniques. Content Recurrence relations Generating function The principle of inclusion-exclusion.
Chapter 5 The Binomial Coefficients
Module #1 - Logic 1 Based on Rosen, Discrete Mathematics & Its Applications. Prepared by (c) , Michael P. Frank and Modified By Mingwu Chen Recurrence.
Chapter 8 With Question/Answer Animations. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
after UCI ICS/Math 6A, Summer AdvancedCounting -1 Recurrence Relations (RRs) A “Recurrence Relation”
Advanced Counting Techniques CSC-2259 Discrete Structures Konstantin Busch - LSU1.
CHAPTER TWO RECURRENCE RELATION
7.2 Solving Linear Recurrence Relations Some of these recurrence relations can be solved using iteration or some other ad hoc technique. However, one important.
CSE 2813 Discrete Structures Solving Recurrence Relations Section 6.2.
Agenda Lecture Content:  Recurrence Relations  Solving Recurrence Relations  Iteration  Linear homogenous recurrence relation of order k with constant.
Discrete Mathematics Chapter 6 Advanced Counting Techniques.
ADVANCED ALGORITHMS REVIEW OF ANALYSIS TECHNIQUES (UNIT-1)
Recurrence Relations. Outline Recurrence relationsSolving recurrence relationsRecurrence and Divide-and-conquer algorithmsGenerating functions
Mathematical Analysis of Recursive Algorithm CSG3F3 Lecture 7.
Chapter 2 1. Chapter Summary Sets The Language of Sets - Sec 2.1 – Lecture 8 Set Operations and Set Identities - Sec 2.2 – Lecture 9 Functions and sequences.
Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar Dr Nazir A. Zafar Advanced Algorithms Analysis and Design.
Advanced Counting Techniques
Discrete Math For Computing II. Contact Information B. Prabhakaran Department of Computer Science University of Texas at Dallas Mail Station EC 31, PO.
CMSC Discrete Structures
Advanced Counting Techniques
Modeling with Recurrence Relations
Recurrence Relations.
Module #17: Recurrence Relations
Systems of First Order Linear Equations
Review 2.
Module #17: Recurrence Relations
CS 2210 Discrete Structures Advanced Counting
CMSC Discrete Structures
Solving Recurrence Relations
Applied Discrete Mathematics Week 7: Computation
CMSC Discrete Structures
Recurrence Relations Discrete Structures.
Chapter 7 Advanced Counting Techniques
Recurrence Relations Rosen 5th ed., §6.2 5/22/2019
ICS 253: Discrete Structures I
Presentation transcript:

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

Discrete Mathematics advanced counting techniques Transparency No. 6-2 Contents Recurrence Relations Solving Recurrence Relations Divide-and-conquer Relations The Inclusion-Exclusion principle Applications of the Inclusion-Exclusion principle Generating Functions

Discrete Mathematics advanced counting techniques Transparency No Recurrence relations #bacteria doubles every hour. Initially (t=0), there are 5 bacteria ==> #bacteria after n hours = ? sol: Let a n = #bacteria after n hours. => a 0 = (1) initial condition a n = 2 a n-1 for n > (2) recurrence relation (1) and (2) uniquely determine a n for all n  N. -- called a recurrence definition of the sequence a 0,a 1,..={a n } n  N Goal: find explicit formula for a n satisfying the recurrence relation (and initial conditions)

Discrete Mathematics advanced counting techniques Transparency No. 6-4 Definition of recurrence relations A recurrence relation for the sequence {a n } is a formula that express a n in terms of one or more of the previous terms of the sequence (i.e., some a j 's with j < n). A sequence is called a solution of a recurrence relation if its terms satisfy the recurrence relation. Ex1: If {a n } satisfies (1) a n = a n-1 - a n-2 for n = 2,3,... (2) a 0 =3, a 1 = 5. => a 2 = ? and a 3 = ? Many problems can be modeled(or expressed) more naturally by recurrence relations.

Discrete Mathematics advanced counting techniques Transparency No. 6-5 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 P n = amount of the account after n years ==> 1. p 0 = 10, p n = p n-1 + p n-1 x ==> P n = 1.11 P n-1 = P n-2 =...= 1.11 n P 0 = 1.11 n x ==> P 30 = 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: f n = #pairs after n months. =>1. f 0 = f 1 = 1 --initial case 2. f n = f n-1 + f n-2 for n > 1, where f n-1 : #old-rabbits and f n-2 is #new born rabbits.

Discrete Mathematics advanced counting techniques Transparency No. 6-6 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 H n = #moves required to solve the problems with n disks => H n = ? Sol: 1. H 1 = H n = H n H n-1 = 2H n A--n-->B A--n-1-->C A--1-->B C--n-1-->B. Hence H n = 2 H n = 2 2 H n =... = 2 n-1 H n = 2 n -1. How big is H 64 ? : assume one move takes 1 sec => = 1.8 x = 500 billion years!!

Discrete Mathematics advanced counting techniques Transparency No. 6-7 more recurrence examples Ex6: #bit strings of length n not containing two consecutive 0s. sol: a n = #bit strings of such kind of length n. ==> 1. a 0 = 1, a 1 = a n = #bits string ending with 1 + #bit-strings-ending with 10 = a n-1 + a n-2. Ex7: a n = # n-digit-strings containing an even number of 0. Sol: valid strings must be in one of the forms: 1. x 1 x 2... x n-1 x n with x n = 1..9 or 2. x 1 x 2... x n-1 0 with x 1 x 2... x n-1 containing odd number of 0. ==> 1. a 0 = 1, a 1 = a n = a n-1 x 9 + (10 n-1 - a n-1 ) for n > 1.

Discrete Mathematics advanced counting techniques Transparency No Solving recurrence relations Linear homogeneous recurrence relation of degree k with constant coefficients: any recurrence of the form: a n = c 1 a n-1 + c 2 a n c k a n-k, where k > 0 and every c j is a constant. Theorem: The recurrence relation: a n = c 1 a n-1 + c 2 a n c k a n-k, and initial conditions: a 0 = t 0,..., a k-1 = t k-1 uniquely determine a sequence (satisfying both conditions) Pf: 1. Existence: trivial. (by Math Ind) 2. Uniqueness: If {  n } and {  n } satisfy the relation => {  n } = {  n } (i.e.,  n =  n for all n.) simple math. ind. Left as an exercise.

Discrete Mathematics advanced counting techniques Transparency No. 6-9 Solving linear homogeneous recurrence relations with constant coefficients a n = c 1 a n-1 + c 2 a n c k a n-k (1) : a recurrence relation => a n = r n is a solution of (1) iff r k = c 1 r k-1 + c 2 r k c k r k-k (2) (2) is called the characteristic equation of (1) a n = c 1 a n-1 + c 2 a n (3) Theorem 1: If r 2 = c 1 r + c 2 has two distinct roots r 1, r 2 then sequence {a n } is a solu of (3) iff a n = d 1 r 1 n + d 2 r 2 n for n = 0,1,2,.. (*) where d 1 and d 2 are constants. pf: <= : simple substitution. =>: Let b 0,b 1,... be any solution of (3). ==> For {b n } to satisfy (*) ==> b 0 = d 1 + d 2 ; b 1 = d 1 r 1 + d 2 r 2 ==> (by ind.) for any k > 1: b k = c 1 b k-1 + c 2 b k-2 = (by ind.hyp.) c 1 (d 1 r 1 k-1 +d 2 r 2 k-1 ) + c 2 (d 1 r 1 k-2 +d 2 r 2 k-2 ) = d 1 (c 1 r 1 k-1 +c 2 r 1 k-2 ) +d 2 (c 1 r 2 k-1 +c 2 r 2 k-2 ) = (by *) d 1 r 1 k + d 2 r 2 k. QED

Discrete Mathematics advanced counting techniques Transparency No Examples: Ex3: Find solu of a n = a n a n-2 for n > 1 and a 0 = 2 and a 1 = 7. sol: char equ: r 2 = r +2 has roots 2, -1. Hence a n = d 1 2 n + d 2 (-1) n for all n for some d 1, d 2. ==> a 0 = d 1 + d 2 a 1 = 2 d 1 - d 2 => d 1 = 3; d 2 = -1 ==> a n = 3 x 2 n - (-1) n for n  0. Ex4: find solu of the Fibonacci sequence: f 1 =f 0 =1 and f n = f n-1 + f n-2 for n > 1. Sol: The char equ: r 2 = r + 1 has roots:  =(1+rt(5)) /2,  =(1- rt(5))/2. note: 1<  <2 and -1 <  <0. Hence fn = d 1  n + d 2  n with f 0 = 1 = d 1 + d 2 and f 1 = 1 = d 1  + d 2  => d 1 = rt(5)/5 and d 2 = -rt(5)/5. => f n = d 1  n + d 2  n = O(  n ) grows exponentially.

Discrete Mathematics advanced counting techniques Transparency No Solving recurrence relation Theorem 2: If r 2 = c 1 r + c 2 has only one r 0 then any sequence {a n } is a solu of (3) iff a n = d 1 r 0 n + d 2 n r 0 n for n = 0,1,2,.. (*) where d 1 and d 2 are any constants. Pf: => : Similar to Theorem 1. <= : a n-1 = d 1 r 0 n-1 + d 2 (n-1) r 0 n-1 a n-2 = d 1 r 0 n-2 + d 2 (n-2) r 0 n-2 ==> c 1 a n-1 + c 2 a n-2 = d 1 (c 1 r 0 n-1 + c 2 r 0 n-2 ) + d 2 (c 1 (n-1) r 0 n-1 + c 2 (n-2) r 0 n-2 ) = d 1 r 0 n-2 (c 1 r o + c 2 ) + d 2 r 0 n-2 ((n-1)(c 1 r o +c 2 ) -c 2 ) = d 1 r 0 n + d 2 r 0 n-2 ((n-1) r r 0 2 ) -- since r 2 - c 1 r -c 2 = (r-r 0 ) 2. Ex5: find solu. of a n = 6a n a n-2 with a 0 = 1 and a 1 = 6. sol: the char equ has one root 3. => a n = d 1 3 n + d 2 n 3 n. => d 1 = 1 and 3d d 2 = 6 => a n = 3 n + n 3 n. for n >= 0.

Discrete Mathematics advanced counting techniques Transparency No Generalization Theorem 3': If the equ. r k = c 1 r k-1 + c 2 r k c k has solutions r 1 m1, r 2 m2,..,r s ms (with m m s = k) where m i is the multiplicity of the root r i. (i.e. r k - c 1 r k c k =  i=1..s (x-r i ) mi ) then {a n } is a solution of the recurrence relation: a n = c 1 a n c k a n-k iff a n =  i=1,s (  j=0,m i -1 d ij n j r i n ), where d ij 's are constants. Ex: The recurrence relation a n = 5 a n a n-2 -7 a n a n-4 has char. equ. r 4 = 5r 3 -9r 2 -7r + 2, which is equ. to (r-2)(r-1) 3 = 0. Hence r has roots: 2, 1 3. Then the relation has general solu: a n = d 0 2 n + e 0 1 n + e 1 n1 n + e 2 n 2 1 n. where d 0, e 0,e 1 and e 2 are constants determined by initial conditions.

Discrete Mathematics advanced counting techniques Transparency No simultaneous recurrence relations Ex24: Solving the simultaneous recurrence relations: 1. a n = 3 a n b n-1 2. b n = a n b n-1, with a 0 = 1 and b 0 = 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. => Y 1 = (1,-1) T and Y 2 = (2,1) T. Now assume X 0 = d 1 Y 1 + d 2 Y 2 => X n = A X n-1 =... = A n X o = A n-1 (AX 0 ) = A n-1 (d 1 AY 1 + d 2 AY 2 ) = A n-1 (d 1 1 Y 1 + d 2 2 Y 2 ) =... = d 1 1 n Y 1 + d 2 2 n Y 2.

Discrete Mathematics advanced counting techniques Transparency No 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) = a 2 f(n/b 2 ) + a g(n/b) + g(n) =.... = a k f(n/b k ) +  j=0,k-1 a j g(n/b j ). Hence if n = b k ==> f(n) = a k f(1) +  j=0,k-1 a j g(n/b j ) (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  (log b a)) if a > 1 and = O(log n) if a = 1.

Discrete Mathematics advanced counting techniques Transparency No proof: Pf: If n = b k, by (1), f(n) = a k f(1) +  j=0,k-1 a j c. If a = 1 ==> f(n) = f(1) +  j=0,k-1 c = f(1) + ck = O(log n). If a > 1 ==> f(n) = a k f(1) +  j=0,k-1 a j c = a k f(1) + c(a k -1) /(a-1) = a k [f(1) + c/(a-1)] - c/(a-1) = c 1 a lg b n + c 2 = c 1 n log b a + c 2 = O(n log b a ). If b k < n < b k+1 is not a power of b. ==> f(n) < f(b k+1 ) = c 1 a k+1 + c 2 < c 1 a a log b n + c 2 = O(n log b a ). Ex: If f(n) = 5 f(n/2) + 3. => a = 5 > 1; c = 3; b = 2. ==> f(n) = O(n log b a ) = O(n lg 5 ) If f(n) = 2 f(n/2) + 2 ==> f(n) = O(n lg 2 ) = O(n) is linear.

Discrete Mathematics advanced counting techniques Transparency No divede-and-conquer relations(cont'd) Theorem 2: If f(n) = a f(n/b) + cn d, where a  1, b > 1 and c,d > 0, is increasing, then f(n) = O(n d ) if a < b d // n d dominates if #subproblems is small = O(n d lg n) if a = b d // every level of problems requires n d. = O(n log b a ) if a > b d. // n d (conquer effort) has no effect. Pf: f(b k ) = a f(b k-1 ) + cb kd = a 2 f(b k-2 ) + c a b d(k-1) + c b dk. =.... = a k f(1) +  j=0,k-1 cb kd (a/b d ) j = f(1) a k + cb kd (1-(a/b d ) k )/(1 - (a/b d )). case 1: n = b k. (k = log b n) Hnece if a f(n) = f(1) a k + cb kd (1-(a/b d ) k )/(1 - (a/b d ))  O(b kd ) = O(b d log b n ) = O(n d ). If a = b d =>f(n) = f(1) a k + ckb kd = O(kb kd ) = O(n d lg n). If a > b d => f(n) = f(1) a k + cb kd (1-(a/b d ) k )/(1 - (a/b d )).  f(1) a k + cb kd (a/b d ) k = O(a k ) = (n log b a ).

Discrete Mathematics advanced counting techniques Transparency No The master theorem: case 2: b k < n < b k+1 : => f(b k )  f(n)  f(b k+1 ). But O(f(b k )) = O(f(b k+1 )), hence O(f(n)) =O(f(b k )). QED Ex8:Fast integer multiplication: A = (a 2n-1 a 2n-2... a 1 a 0 ) B = (b 2n-1 b 2n-2... b 1 b 0 ) A H = (a 2n-1...a n ); A L = (a n-1,...,a 0 ); B H =..., B L =... => A x B = (2 n A H + A L ) x (2 n B H + B L ) = 2 2n A H B H + 2 n (A H B L + A L B H ) + (A L B L ) => f(n) = 4f(n/2) + O(n) => f(n) = O(n  lg 4) = O(n 2 ) - no improving !! But AXB = 2 2n A H B H + 2 n (A H B L + A L B H ) + (A L B L ) = 2 2n A H B H + 2 n( (A H +A L )(B H +B L )) - A H B H - A L B L ) + (A L B L ) ==> f(n) = 3f(n/2) + O(n) => f(n) = O(n lg 3 ) < O(n 2 ).

Discrete Mathematics advanced counting techniques Transparency No More examples: Fast matrix multiplication:(Ex4 & 9) 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) + 15n 2 /4 => a = 7, b = 2, d = 2. => a > b d = 4. => f(n) = O(n lg 7 ) = O(n 2.81 ). Better than direct multiplication(=O(n 3 )) !! Although even better result (O(n )) is possible.

Discrete Mathematics advanced counting techniques Transparency No 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. Problem: |AUBUC| = ? |A| +|B|+|C| -|A  B|-|A  C||B  C|+|A  B  C| |A|+|B|+|C| |A|+|B|+|C| -|A  B|-|A  C|-|B  C|

Discrete Mathematics advanced counting techniques Transparency No The general inclusion-exclusion principle Theorem 1: |A 1 U A 2...UA n | =  1  i  n |A i | -  1  i<j  n |A i  A j | +  1  i<j<k  n |A i  A j  A k | (-1) n+1 |A 1  A 2 ...  A n | ---(*) pf: Let a be any element belonging to exactly A d1, A d2,...,A dr. ==> It is counted C(r,s) times by the sum:  0<j1<j2<...<js< n+1 |A j1  A j2 ...  A js |. (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

Discrete Mathematics advanced counting techniques Transparency No Applications of the IE principle Alternative form: |~A 1  ~A 2...  A n )| = |~(A 1 UA 2...UA n )| = |U| - | A 1 UA 2...UA n | = |U| -  |A i | +  |A i  A j | (-1) n |A 1 ...  A n |. Ai : set of elements having property Pi. N(Pj 1,...,Pj k ) = #elements with properties Pj 1,...,Pj k. (i.e. N(Pj 1,...,Pj k ) = |A j1 ...  A jk |. ) ~Pj : the negation of property Pj. N(~Pj 1,...,~Pj k )) = #elements without any property of Pj 1,..,pj k. = N(~(Pj 1 \/...\/Pj k )) = |U| - |Aj 1 ..  Aj k |.  N(~P 1,...~P n )) = |U| –  (Pi) +  (PiPj) -  (PiPjPk) (-1) n N(P1P2...Pn).

Discrete Mathematics advanced counting techniques Transparency No 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.

Discrete Mathematics advanced counting techniques Transparency No More example Ex2: #positive integers < 101 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 = {b 1,...,b n } and Pi = "b i is not in the range of the fun" => N(Pi) = |Ci|= |{f | f:A->B and bi not in f(A)}|. #ontos = N(~P1/\~P2.../\~Pn) = N(~(P1\/...\/Pn)) = |U| - |C1 U C2 U... U Cn| = n m -  |Ci| +  |Ci  Cj| (-1) n |C1  C2 ...  Cn| = n m - C(n,1)(n-1) m + C(n,2)(n-2) m (-1) n-1 C(n,n-1) 1 m.

Discrete Mathematics advanced counting techniques Transparency No 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 = 4 5 -C(4,1)3 5 + C(4,2)2 5 +C(4,3)1 5 = = 240. Ex4: [The hatcheck problem:] n hats randomly returned to the customers. => What is the probability that no one receives his own hat?

Discrete Mathematics advanced counting techniques Transparency No Derangements x 1,x 2,...,x n : a list Any permutation a 1,...,a n of {x 1,...,X n } s.t. a j  x j for all j = 1..n is called a derangement of the list. Ex: 1,2,3,4,5 has derangement but not Let D n = # derangements of n objects. => D 1 = 0, D 2 = 1, D 3 = 2. (123 has derangements: 231, 312) Theorem: D n = n![1- 1/1! + 1/2! (-1) n /n!] Pf: let P i = def "a=a 1...a n is a perm of x 1...x n s.t. a i = x i." => D n = N(~P 1 /\~P 2 /\../\~P n ) = N(~(P 1 \/P 2 \/..\/P n )) = N -  N(P i ) +  N(P i /\P j ) (-1) n N(p 1 /\../\P n ) = n! -C(n,1)(n-1)! +C(n,2)(n-2)! (-1) n C(n,n)(n-n)! = n![1 - 1/1! + 1/2!.. ]. => solu of the hatcheck problem = D n / P(n,n) = D n /n! in [0.3,0.5] = (1 -1/1! + 1/2!-...) -> e -1 = as n -> .

Discrete Mathematics advanced counting techniques Transparency No Generating Functions Definition 1: The generating function for the sequence a 0,a 1,…,a n,… of real numbers is the infinite series: G(x) = a 0 + a 1 x + … + a k x k + … =  k = 0..  a k x k. also called the ordinary generating function to distinguish it from other kind of generating functions. EX: {a n } = 3,3,…,3,… => G(X) =  k = 0..  3x k. {b n } =1,2,3,… => G(x) = 1 + 2x + 3x 2 +… =  k = 0..  (k+1)x k. {C n } = 1,2,4,8,… => G(x) = 1 + 2x + 2x 2 +… =  k = 0..  2 k x k.

Discrete Mathematics advanced counting techniques Transparency No More Examples Ex2: {a k } k = 0..5 = 1,1,1,1,1,1. Then G(X) = 1 + x + x 2 + x 3 + x 4 + x 5. = (X 6 -1) / (x -1) Ex3: m: a positive integer, a k = C(m,k) for k = 0..m. Then G(x) = C(m,0) + C(m,1)x + C(m,2) x 2 +…+ C(m,m) x m = (1+x) m.

Discrete Mathematics advanced counting techniques Transparency No 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 + x 2 +… for |x| < 1. Ex5: f(x) = 1/(1-ax) is the generatign function of the series: 1,a,a 2,a 3,… since 1/(1-ax) = 1 + ax + a 2 x 2 + … for |x| < 1 / |a|.

Discrete Mathematics advanced counting techniques Transparency No sum and product of generating functions Theorem 1 : Let f(x) =  k = 0..  a k x k, g(x) =  k = 0..  b k x k. Then f(x) + g(x) =  k = 0..  (a k +b k ) x k. f(x) g(x) = (a 0 + a 1 x + a 2 x 2 +…) x (b 0 + b 1 x 1 + b 2 x 2 +…) =  k = 0..  (  j= 0..k a k b k-j )x k. I.e., f(x) + g(x) is the generating function of the sum of both sequences: { a k +b k }, while f(x)g(x) is the generating function of the sequence: {c k } where c k =  j= 0..k a k b k-j, { c k } is called the convolution of {a k } and {b k }.

Discrete Mathematics advanced counting techniques Transparency No Ex 6 Since 1/(1-x) = 1 + x + x 2 + x 3 + … (*) we have 1/(1-x) 2 = (1 + x + x 2 + x 3 + …)(1 + x + x 2 + x 3 + … ) = 1 + (1x + x 1) + (1 x 2 + x x + x 2 1) + … =  k = 0..  (  j= 0..k 1)x k =  k = 0..  (k+1)x k. 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 + 3x 2 + … =  k = 0..  (k+1)x k.

Discrete Mathematics advanced counting techniques Transparency No 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) = / 3! = 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).

Discrete Mathematics advanced counting techniques Transparency No 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) x k, and (x+y) u =  k = 0..  C(u, k) x k y u-k. pf: Let f(x) = (1+x) u. By Tayler ‘s theorem f(x) = f(a) + (x-a)f'(a) + (x-a) 2 f (2) (a)/2! (x-a) n f (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! x n + … 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) x k. 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) x k =  k = 0..u C(u, k) x k.

Discrete Mathematics advanced counting techniques Transparency No 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) x k. =  k = 0..  (-1) k C(n+k-1, k) x k. 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) x k.

Discrete Mathematics advanced counting techniques Transparency No Some useful generating functions G(x) {a k } (1+x) n =  k = 0..n C(n, k) x k = 1 + C(n,1)x + C(n,2) X 2 + …+ x n C(n,k) (1+ax) n =  k = 0..n C(n, k)a k x k = 1 + C(n,1)ax + C(n,2)a 2 X 2 + …+ a n x n C(n,k)a k (1+x r ) n =  k = 0..n C(n, k) x kr = 1 + C(n,1)x r + C(n,2) x 2r + …+ x nr C(n, k/r ) if r | k and 0 otherwise (1- x n + 1 ) / (1-x) =  k = 0..n x k = 1 + x + x2 + …+x n 1 if k  n 0 if k > n.

Discrete Mathematics advanced counting techniques Transparency No Some useful generating functions (continued) G(x){a k } 1/(1-x) = 1+x+ x 2 + x 3 +…1 1/(1-ax) = 1 + ax + (ax) 2 + …akak 1/(1-x r ) = 1 + x r + x 2r + …1 if r | k, 0 O/W 1/(1-x) 2 =  k = 0..  (k+1) x k = 1 + 2x + 3x 2 + 4x 3 +… k+1 1/(1-x) n =  k = 0..  C(n+k-1,k) x k = 1 + C(n,1) x + C(n+1,2) x 2 + C(n+2,3)x 3 +… C(n+k-1, k)

Discrete Mathematics advanced counting techniques Transparency No Some generating functions (continued) G(x){a k } 1/(1+x) n =  k = 0..  C(n+k-1,k) (-1) k x k = 1 - C(n,1) x + C(n+1,2) x 2 - C(n+2,3)x 3 +… (-1) k C(n+k-1, k) 1/(1-ax) n =  k = 0..  C(n+k-1,k) a k x k = 1 + C(n,1) ax + C(n+1,2) a 2 x 2 + C(n+2,3)a 3 x 3 +… C(n+k-1, k) a k e x =  k = 0..  x k /k! = 1 + x/1! + x 2 /2! + x 3 /3!+ … 1/k! ln(x+1) =  k = 1..  (-1) k+1 x k /k = x – x 2 /2 + x 3 /3 - x 4 /4+ … (-1) k+1 /k

Discrete Mathematics advanced counting techniques Transparency No More facts If G(X) = is the generating function of the seq {a n }. Then 1.xG(x) = is the gf of {a n } >>1. [shift right] 2.x k G(x) = is the gf of {a n } >> k. 3.(G(x)-a 0 )/x = is the gf of {a n } << 1 [shift left] 4.d G(x) / dx = is the gf of { (n+1) a n+1 } n=0… 5.  G(x) dx = a 0 x + a 1 x 2 /2 + a 2 x 3 /3 + … = is the gf of { a n /(n+1) } >> 1.

Discrete Mathematics advanced counting techniques Transparency No Counting problems and generating functions Ex10: Find the number of solutions of 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 x 17 in the expansion of the product: (x 2 +x 3 +x 4 +x 5 )(x 3 +x 4 +x 5 +x 6 )(x 4 +x 5 +x 6 +x 7 ) The #Solutions is 3 (x 5 x 5 x 7, x 4 x 6 x 7, x 5 x 6 x 6 ) e1=5 e2=6 e3=6

Discrete Mathematics advanced counting techniques Transparency No 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 x 8 in the product: (x 2 +x 3 +x 4 ) 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 is (x + x 2 + x 5 ) n.

Discrete Mathematics advanced counting techniques Transparency No So the possible ways of inserting coins is 1 + (x+x 2 +x 3 ) + (x + x 2 + x 5 ) 2 + (x + x 2 + x 5 ) 3 + … = 1/(1 –x – x 2 –x 3 ) => possible ways of inserting coins with total value r is the coeff. of the term x r in the above generating function. for example, if r = 7, then the answer is 26. can be computed by computer algebra system. Order does not matter: possible ways of inserting coins (1+x+x 2 +x 3 +…) (1+ x 2 + x 4 + …) (1+ x 5 + x 10 + x 15 +…) = 1/(1-x ) 1/(1-x 2 ) 1/(1-x 5 ) if r = 7, then x r has coefficient 6.

Discrete Mathematics advanced counting techniques Transparency No Ex13 : Find #k-combinations of a set of size n. solu: = coefficient of x k in the product: (x 0 +x 1 ) (x 0 +x 1 ) … (x 0 +x 1 ) = (1+x) n = f(x). By Binominal theorem : f(x) =  k = 0..n C(n, k) x k 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 {a r } be the seq where a r = #r-comb with repetition allowed. Then {a r } has generating function: (1+x+x 2 +…)… (1+x+x 2 +…) = (1+x+x 2 +…) n = (1+(-x)) -n. =  k = 0..  C(-n,k)(-1) k x k, Thus #r-comb with repetition = C(n+r-1, r) = H(n,r).

Discrete Mathematics advanced counting techniques Transparency No 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 + x 2 + x 3 + …) n. = X n (1+x+x 2 +…) n = X n / (1-x) n = x n (1-x) -n. = x n  k = 0..  C(-n,k)(-1) k x k =  k = 0..  C(n+k-1,k) x k+n =  t = n..  C(t-1,t-n) x t => there are C(r-1, r-n) = H(n, r-n) ways to select r objects.

Discrete Mathematics advanced counting techniques Transparency No Using generating functions to solve recurrence relations Ex16: Solve the recurrence relation : a k = 3 a k-1 for k >0, and a 0 = 2. Sol: Let G(x) be the generating function of the seq {a k }. I.e., G(x) =  k = 0..  a k x k. = => xG(x) =  k = 0..  a k x k+1 =  t = 1..  a t-1 x t. = => G(x) – 3xG(x) =  k = 0..  a k x k -  t = 1..  3 a t-1 x t. = = a 0 +  k = 1..  (a k - 3 a k-1 )x k = = 2. => G(x) = 2/ (1-3x) = 2(1+ 3x + (3x) 2 + … ) Hence a k = 2 3 k.

Discrete Mathematics advanced counting techniques Transparency No Ex17: Solve the recurrence relation: a 0 = 1, a n = 8a n n-1 for n > 0. (*) Sol: Let G(x) be the generating function of the seq {a n }. By (*) we have a n x n = 8a n-1 x n + 10 n-1 x n if n > 0 Since G(X) = G(x) – 1 = = = 8 + = 8x G(x) + (x + 10x x 3 + … ) = 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)) = (½) + (½) Hence a n = ½ (8 n + 10 n ).

Discrete Mathematics advanced counting techniques Transparency No 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,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 Ex19: Show the Pascal’s identity C(n+1, k) = C(n,k) + C(n,k-1). Hint: (1+x) n + 1 = (1+x)(1+x) n = (1+x) n + x(1+x) n. Similarly, (1+x) m+n = (1+x) m (1+x) n => C(m+n,r) =  k=0..r C(m,k)C(n,r-k)