Recurrence Relations; General Inclusion-Exclusion

  MATEMATI KA DI SKRI T I I ( 2 SKS) Rabu, 18.50 – 20.20 Ruang Hard Disk PERTEMUAN VI I & VI I & I X RELASI Dosen L20 Lie Jasa 1 Recurrence Relations;

  General I nclusion-Exclusion

  Zeph Grunschlag

  

Copyright © Zeph Grunschlag, L20 3 Agenda Recurrence relations (Section 5.1) n Counting strings n Partition function Solving recurrence solutions (Section 5.2) n Fast numerical algorithm “ dynamic programming” n Closed solutions by telescoping (back-substitution) n Linear recurrences with constant coefficients w Homogeneous case (RHS = 0) w General case

  I nclusion-Exclusion (Sections 5.5, 5.6) n Standard (2 sets) n “I nclusion-Exclusion- I nclusion” (3 sets) n Generalized (n sets) w Counting onto functions w Derangements w Sieve of Erastothenes (blackboard)

  

Recurrence Relations

  Have already seen recursive definitions for… Number sequences n

  Fibonacci

  I nteger functions n

  Euclidean algorithm n Binomial coefficients

  Sets Sets of strings Mathematical definitions

  A recurrence relation is the recursive part of a

  recursive definition of either a number L20 5 Recursively Defined Sequences

  EG: Recall the Fibonacci sequence: { f n } = 0,1,1,2,3,5,8,13,21,34,55,… Recursive definition for { f n } : I NI TI ALI ZE: f = 0, f 1 = 1

  RECURSE: f n = f n-1 + f n-2 for n > 1.

  The recurrence relation is the recursive part

  f n

  = f n-1 + f n-2 .Thus a recurrence relation for a sequence consists of an equation that expresses each term in terms of lower terms. Q: I s there another solution to the Fibonacci recurrence relation?

  Recursively Defined Sequences

  A: Yes, for example could give a different set of initial conditions such as f = 1, f

  1

  = -1 in which case would get the sequence { f n } = 1,-1,0,-1,-2,-3,- 5,- 8,- 13,-21,… Q: How many solutions are there to the

  Fibonacci recursion relation?

  Recursively Defined Sequences

  A: I nfinitely many solutions as each pair of integer initial conditions (a,b) generates a unique solution.

  L20 7 Recurrence Relations for Counting

  Often it is very hard to come up with a closed formula for counting a particular set, but coming up with recurrence relation easier.

  EG: Geometric example of counting the number of points of intersection of n lines. Q: Find a recurrence relation for the number of bit strings of length n which contain the string 00.

  Recurrence Relations for Counting

  A: a = # (length n bit strings containing 00): n I f the first n-1 letters contain 00 then so does I . the string of length n. As last bit is free to choose get contribution of 2a n-1

  Else, string must be of the form u00 with u a I I . string of length n-2 not containing 00 and

  not

  (why not?). But the number of

  ending in 0

  strings of length n-3 which don’t contain 00 is the total number of strings minus the number n-3 that do. Thus get contribution of 2 -a n-3 n-3 Solution: a = 2a + 2 - a n n-1 n-3 Q: What are the initial conditions: L20 9 Recurrence Relations for

  Counting

  A: Need to give enough initial conditions to avoid ensure well-definedness. The smallest n for which length is well defined is n= 0. Thus the n-3 smallest n for which a = 2a + 2 - a n n-1 n-3 makes sense is n= 3. Thus need to give a , a 1 and a explicitly. 2

  = a = 0 (strings to short to contain 00)

  a 1 = 1 (must be 00). a 2 Note: example 6 on p. 313 gives the simpler

  recursion relation b = b + b for strings n n-1 n-2 which do not contain two consecutive 0’s. Financial Recursion Relation

  Most savings plans satisfy certain recursion relations. Q: Consider a savings plan in which $10 is deposited per month, and a 6% / year interest rate given with payments made every month. I f P represents the n amount in the account after n months, L20 find a recurrence relation for P . n 11 Financial Recursion Relation

  A: P = (1+ r)·P + 10 n n-1 where r = 1 + 6% / 12 = 1.005

  

Partition Function

  A partition of a set is a way of grouping all the elements disjointly. EG: All the partitions of { 1,2,3} are:

  { { 1,2,3} } 1. { { 1,2} , { 3} } 2. { { 1,3} , { 2} } 3. { { 2,3} , { 1} } 4. { { 1} ,{ 2} ,{ 3} } 5. The partition function p counts the number of n L20 partitions of { 1,2,…,n} . Thus p = 5. 3 13 Partition Function

  Let’s find a recursion relation for the partition function. There are n possible scenarios for the number of members on n’s team:

  L 0: n is all by itself (e.g. { { 1,2} ,{ 3} } ) 1:

  n has 1 friend (e.g. { { 1} ,{ 2,3} } )

  2:

  n has 2 friends (e.g. { { 1,2,3} } )

  … n-1: n has n-1 friends on its team. By the sum rule, we need to count the number of partitions of each kind, and then add together.

  L20 15 Partition Function

  Consider the k’ th case:

  k : n has k friends

  There are C (n-1,k) ways of choosing fellow members of n’s t eam. Furthermore, there are p n-k-1 ways of partitioning the rest of the n elements.

  Using the product and sum rules we get:

  ) , ) 1 ( 1 , 1 (

  ) 1 ,

1 (

1 1

  − ⋅ + + − − ⋅ = − − − ⋅ = − −

  = ∑

  C n p n n C p C i n n p p n n i i n L

  

Solving Recurrence Relations

  We will learn how to give closed solutions to certain kinds of recurrence relations.

  Unfortunately, most recurrence relations cannot be solved analytically. EG: I f you can find a closed formula for partition function tell me! However, recurrence relations can all be solved quickly by using dynamic

  programming. L20 17 Numerical Solutions Dynamic Programming

  Recursion + Lookup Table = Dynamic Programming

  Consider a recurrence relation of the form:

  a n

  = f (a ,a 1 ,…,a n-2 ,a n-1 ) Then can always solve the recurrence relation for first n values by using following pseudocode:

  integer-array a(integers n, a ) table = a for(i = 1 to n) table i

= f(table ,table

1 ,…,table i-1 ) return table Dynamic Program for String Example

  Solve a n = 2a n-1 + 2 n-3 - a n-3 up to n= 7. Pseudocode becomes:

  integer-array a(integer n) table = table 1 = 0 table 2 = 1 for(i = 3 to n) table i = 2 i-3

-table

i-3 +2*table i-1 return table

  

Dynamic Program

for String Example

n-3

  Solve a = 2a + 2 - a up to n= 7: n n-1 n-3 i-3 2 -a + 2a = a

  i i-3 i-1 i

  1

  2

  1

  3

  4

  5 L20

  6 19

7 Dynamic Program

  

for String Example

n-3

  Solve a = 2a + 2 - a up to n= 7: n n-1 n-3 i-3

  i

  2 -a + 2a = a i-3 i-1 i

  1

  2 1 3 1-0+ 2·1 = 3

  4

  5

  6

  

Dynamic Program

for String Example

n-3

  Solve a = 2a + 2 - a up to n= 7: n n-1 n-3 i-3 2 -a + 2a = a

  i i-3 i-1 i

  1

  2 1 3 1-0+ 2·1 = 3

  4 2-0+ 2·3 = 8

  5 L20

  6 21

7 Dynamic Program

  

for String Example

n-3

  Solve a = 2a + 2 - a up to n= 7: n n-1 n-3 i-3

  i

  2 -a + 2a = a i-3 i-1 i

  1

  2 1 3 1-0+ 2·1 = 3

  4 2-0+ 2·3 = 8 5 4-1+ 2·8 = 19

  6

  

Dynamic Program

for String Example

n-3

  Solve a = 2a + 2 - a up to n= 7: n n-1 n-3 i-3 2 -a + 2a = a

  i i-3 i-1 i

  1

  2 1 3 1-0+ 2·1 = 3

  4 2-0+ 2·3 = 8 5 4-1+ 2·8 = 19 L20 6 8-3+ 2·19 = 43 23

7 Dynamic Program

  

for String Example

n-3

  Solve a = 2a + 2 - a up to n= 7: n n-1 n-3 i-3

  i

  2 -a + 2a = a i-3 i-1 i

  1

  2 1 3 1-0+ 2·1 = 3

  4 2-0+ 2·3 = 8 5 4-1+ 2·8 = 19 6 8-3+ 2·19 = 43

  Dynamic Program for String Example n 1

  Solve up to n= 6.

  p = pC ( nn i 1 , n − 1 − i ) ∑ i

  =

  Pseudocode becomes:

  integer-array p(integer n)

  / / unique partition of empty set

  table = 1 for(i = 1 to n) sum = 1 for(j = 1 to n-1) sum += table *C(n-1,n-1-j) j table = sum n L20 return table 25 Dynamic Program for String Example n 1

  − p p C n n i

  = ⋅ ( − 1 , − 1 − )

  Solve n i up to n= 6: i

  = ∑ j 1

  − p p C ( i 1 , i 1 j ) i = ⋅ − − − i j

  ∑ j =

  1

  1

  2

  3

  4

  5

  Dynamic Program for String Example n1 p = pC nn − − i

  Solve ( n i i 1 , 1 ) up to n= 6:

  = ∑ j 1

  − p p C ( i 1 , i 1 j ) i = ⋅ − − − i j

  ∑ j =

  1

  1

  1

  2

  3

  4 L20

  5 27

6 Dynamic Program

  for String Example n 1p p C n n i

  = ⋅ ( − 1 , − 1 − )

  Solve n i up to n= 6: i

  = ∑ j 1

  − p p C ( i 1 , i 1 j ) i = ⋅ − − − i j

  ∑ j =

  1

  1 1 2 1+ 1·C(1,0) = 2

  3

  4

  5

  Dynamic Program for String Example n1 p = pC nn − − i

  Solve ( n i i 1 , 1 ) up to n= 6:

  = ∑ j 1

  − p p C ( i 1 , i 1 j ) i = ⋅ − − − i j

  ∑ j =

  1

  1 1 2 1+ 1·C(1,0) = 2

  3 1+ 1·C(2,1)+ 2·C(2,0) = 5

  4 L20

  5 29

6 Dynamic Program

  for String Example n 1p p C n n i

  = ⋅ ( − 1 , − 1 − )

  Solve n i up to n= 6: i

  = ∑ j 1

  − p p C ( i 1 , i 1 j ) i = ⋅ − − − i j

  ∑ j =

  1

  1 1 2 1+ 1·C(1,0) = 2

  3 1+ 1·C(2,1)+ 2·C(2,0) = 5 4 1+ C(3,2)+ 2C(3,1) + 5C(3,0) = 15

  5

  Dynamic Program for String Example n1 p = pC nn − − i

  Solve ( n i i 1 , 1 ) up to n= 6:

  = ∑ j 1

  − p p C ( i 1 , i 1 j ) i = ⋅ − − − i j

  ∑ j =

  1

  1 1 2 1+ 1·C(1,0) = 2

  3 1+ 1·C(2,1)+ 2·C(2,0) = 5 4 1+ C(3,2)+ 2C(3,1) + 5C(3,0) = 15 L20 5 1+ C(4,3)+ 2C(4,2) + 5C(4,1)+ 15 = 52 31

6 Dynamic Program

  for String Example n 1p p C n n i

  = ⋅ ( − 1 , − 1 − )

  Solve n i up to n= 6: i

  = ∑ j 1

  − p p C ( i 1 , i 1 j ) i = ⋅ − − − i j

  ∑ j =

  1

  1 1 2 1+ 1·C(1,0) = 2

  3 1+ 1·C(2,1)+ 2·C(2,0) = 5 4 1+ C(3,2)+ 2C(3,1) + 5C(3,0) = 15 5 1+ C(4,3)+ 2C(4,2) + 5C(4,1)+ 15 = 52 L20 33 Closed Solutions by Telescoping

  We’ve already seen technique in the past:

  

1) Plug recurrence into itself repeatedly for

smaller and smaller values of n. 2) See the pattern and then give closed formula in terms of initial conditions.

3) Plug values into initial conditions getting

final formula.

  

Telescoping also called back- substitution

Telescope Example

  Find a closed solution to a n = 2a n-1 , a = 3:

  a n

  = 2a n-1

  

Telescope Example

  Find a closed solution to a = 2a , a = 3: n n-1

  2

  = 2a = 2

  a a n n-1 n-2 L20 35 Telescope Example

  Find a closed solution to a = 2a , a = 3: n n-1

  2 3

  = 2a = 2 = 2

  a a a n n-1 n-2 n-3

  

Telescope Example

  Find a closed solution to a = 2a , a = 3: n n-1

  2 3

  = 2a = 2 = 2 = …

  a a a n n-1 n-2 n-3 L20 37 Telescope Example

  Find a closed solution to a = 2a , a = 3: n n-1

  2 3 i

  = 2a = 2 = 2 = … = 2

  a a a a n n-1 n-2 n-3 n-i

  L20 39 Telescope Example

  Find a closed solution to a n = 2a n-1 , a = 3:

  = … = 2 n

  a n-i

  = … = 2 i

  a n-3

  = 2 3

  a n-2

  = 2a n-1 = 2 2

  a n

  

Telescope Example

  Find a closed solution to a n = 2a n-1 , a = 3:

  = …

  a n-i

  = … = 2 i

  a n-3

  = 2 3

  a n-2

  = 2a n-1 = 2 2

  a n

  a L20 41 Telescope Example

  Find a closed solution to a n = 2a n-1 , a = 3: Plug in a = 3 for final answer:

  a n

  = 3 ·

  2 n

  a n

  = 2a n-1 = 2 2

  a n-2

  = 2 3

  a n-3

  = … = 2 i

  a n-i

  = … = 2 n

  a Blackboard Exercise for 5.1

  5.1.21: Give a recurrence relation for the number of ways to climb n stairs if the climber can take one or two stairs at a time.

  Closed Solutions by Telescoping

  The only case for which telescoping works with a high probability is when the recurrence give the next value in terms of a single previous value.

  There is a class of recurrence relations which

can be solved analytically in general.

  These are called linear recurrences and L20 include the Fibonacci recurrence. 43 Linear Recurrences The only case for which telescoping works with a high probability is when the recurrence gives the next value in terms of a single previous value. But…

  There is a class of recurrence relations which

can be solved analytically in general.

  These are called linear recurrences and include the Fibonacci recurrence. Begin by showing how to solve Fibonacci:

  Solving Fibonacci

  Recipe solution has 3 basic steps: n

  1) Assume solution of the form a = r n

2) Find all possible r’s that seem to make

  1 r

  this work. Call these and r . Modify

  1

  2

  assumed solution to general solution n n

  a n = Ar + Br where A,B are constants.

  1

  2

3) Use initial conditions to find A,B and

  L20 obtain specific solution. 45 Solving Fibonacci

  1) Assume exponential solution of the n

  form a = r : n Plug this into a = a + a : n n-1 n-2 n n-1 n-2

  r = r + r

  Notice that all three terms have a n-2 common r factor, so divide this out: n n-2 n-1 n-2 n-2

  2 è r / r = (r + r )/ r r = r + 1

  This equation is called the characteristic

equation of the recurrence relation.

  

Solving Fibonacci

  Find all possible r’s that solve characteristic 2) 2

  = r + 1

  r

1

Call these r and r . General solution is n n 1 2 = Ar + Br where A,B are constants. a n 1 2 2 Quadratic formula gives: r = (1 5)/ 2

  ± √

  So r = (1+ 5)/ 2, r = (1- 5)/ 2 1 √ √ 2 General solution: n n = A [ (1+ 5)/ 2] + B [ (1- 5)/ 2]

  a L20 n √ √ 47 Solving Fibonacci

  Use initial conditions a = 0, a = 1 to find 3) 1 A,B and obtain specific solution.

  0= a = A [ (1+ 5)/ 2] + B [ (1- 5)/ 2] = A + B

  √ √ 1 1

  1= a = A [ (1+ 5)/ 2] + B [ (1- 5)/ 2] 1 √ √ = A(1+ 5)/ 2 + B (1- 5)/ 2

  √ √

  = (A+ B )/ 2 + (A-B ) 5/ 2

  

nd

  First equation give B = -A. Plug into 2 : 1 = 0 + 2A 5/ 2 so A = 1/ 5, B = -1/

  5

  

√ √ √

n n

  Final answer:

   

  1

  1

  5

  1

  1

  5  +  − a n = −

  

2

  2

  5

  5

  (CHECK I T!)    

  L20 49 Linear Recurrences with Constant Coefficients

  = 2a n-1 2.

  ∑ − =

  ) 1 , 1 ( 1 C i n n p p n i i n

− − − ⋅ =

  2

  3. a n = a n-1

  = 2a n-1 + 2 n-3 - a n-3

  a n

  1. a n

  Previous method generalizes to solving “linear

  Q: Which of the following are linear with constant coefficients?

  Linear Recurrences with Constant Coefficients

  is a linear combination of the previous terms plus a function of n. I .e. no squares, cubes or other complicated function of the previous a i can occur. I f in addition all the coefficients are constants then the recurrence relation is said to have constant coefficients .

  a n

  DEF: A recurrence relation is said to be linear if

  recurrence relations with constant coefficients” :

4. Partition function:

  

Linear Recurrences with

Constant Coefficients

  A:

  a 1. = 2a : YES n n-1 n-3 a

  2. = 2a + 2 - a : YES n n-1 n-3

  2 a = a

  

3. : NO. Squaring is not a linear

n n-1

  operation. Similarly a = a a and n n-1 n-2 a = cos(a ) are non-linear. n n-2 n

  − 1 p p C n n i

  4. Partition function: = ⋅ ( − n i 1 , − 1 − ) ∑ i

  =

  NO. This is linear, but coefficients are not constant as C (n -1, n -1-i ) is a non- L20 constant function of n. 51 Homogeneous Linear

  

Recurrences

  To solve such recurrences we must first know how to solve an easier type of recurrence relation: DEF: A linear recurrence relation is said to be

  homogeneous if it is a linear combination of

  the previous terms of the recurrence without an additional function of n. Q: Which of the following are homogeneous?

  a = 2a 1. n n-1 n-3

  = 2a + 2 - a 2. a n n-1 n-3 − n 1 Partition function: p = pC nn − − i 3. n i ( 1 , 1 )

  ∑ i =

A: 1.

  L20 53 Linear Recurrences with Constant Coefficients

  a n

  = 2a n-1 : YES

  2. a n

  = 2a n-1 + 2 n-3 - a n-3 : No. There’s an extra term f (n) = 2 n-3

3. Partition function:

  YES. No terms appear not involving the previous p i

  ) 1 , 1 ( 1 C i n n p p n i i n − − − ⋅ =

  ∑ − =

  

Homogeneous Linear

Recurrences with Const. Coeff.’s

  The 3-step process used for the Fibonacci recurrence works well for general homogeneous linear recurrence relations with constant coefficients. There are a few instances where some modification is necessary.

  

Homogeneous

  • -Complications

    1) Repeating roots in characteristic equation.

  Repeating roots imply that don’t learn anything new from second root, so may not have enough information to solve formula with given initial conditions. We’ll see how to deal with this on next slide.

  2) Non-real number roots in characteristic equation. I f the sequence has periodic behavior, may get complex roots (for 1 example a = -a ) . We won’t worry about n n-2 this case (in principle, same method works L20 as before, except use complex arithmetic). 55 Complication: Repeating Roots

  EG: Solve a = 2a -a = 1, a = 2 n n-1 n-2 , a 1 n Find characteristic equation by plugging in a = r : 2 n 2

r - 2r + 1 = 0

2

Since r - 2r + 1 = (r -1) the root r = 1 repeats.

  I f we tried to solve by using general solution n n n n

  

a = Ar + Br = A1 + B1 = A+ B

n 1 2

  àß which forces a to be a constant function ( ). n SOLUTI ON: Multiply second solution by n so general solution looks like: n n

  

a = Ar + Bnr

n

1

1

  

Complication: Repeating Roots

  Solve a = 2a -a , a = 1, a = 2 n n-1 n-2 n n

  1 General solution: a = A1 + Bn1 = A+ Bn n

  Plug into initial conditions 1 = a = A+ 0·1 = A

  1

  1

  2 = a = 1 + 1·1 = A+ B Plugging first equation A = 1 into second: 2 = 1+ B implies B = 1.

  Final answer: a = 1+ n n (CHECK I T!) L20 57 The Nonhomogeneous Case

  Consider the Tower of Hanoi recurrence (see Rosen p. 311- 313) a = 2a + 1. n n-1 Could solve using telescoping. I nstead let’s solve it methodically. Rewrite:

  a n n-1 - 2a = 1

1) Solve with the RHS set to 0, i.e. solve the

homogeneous case.

  2) Add a particular solution to get general

  solution. I .e. use rule:

  

General General Particular

  • =

  

Nonhomogeneous homogeneous Nonhomogeneous

  • 2a n-1

  = 1

1) Solve with the RHS set to 0, i.e. solve

  L20 59 The Nonhomogeneous Case a n

  a n

  • 2a n-1

  = 0 Characteristic equation: r - 2 = 0 so unique root is r = 2. General solution to homogeneous equation is

  a n

  The Nonhomogeneous Case

  2) Add a particular solution to get general solution for a n - 2a n-1 = 1. Use rule:

  There are little tricks for guessing particular nonhomogeneous solutions. For example, when the RHS is constant, the guess should also be a constant. 1 So guess a particular solution of the form b n = C.

  Plug into the original recursion: 1 = b n – 2b n-1 = C – 2C = -C. Therefore C = -1.

  General Nonhomogeneous

  =

  General homogeneous Particular Nonhomogeneous

  = 2 n

  2 + r -1).

  EG: Find the general solution to recurrence from the bit strings example:

  3

  ± 1 as integer roots divide. r = 1 works, so divide out by (r -1) to get r

  Guess root r =

  3 - 2r + 1 = 0.

  = 2 n-3 and solve homogeneous part: Characteristic equation: r

  1) Rewrite as a n

  = 2a n-1 + 2 n-3 - a n-3

  a n

  More Complicated

  L20 61 The Nonhomogeneous Case

  Therefore, 2 = 2A, so A = 1. Final answer: a n = 2 n -1

  1 - 1 = 2A –1.

  = 2

  1

  = 1 Using general solution a n = 2 n - 1 we get: 1 = a

  

1

  a

  Finally, use initial conditions to get closed solution. I n the case of the Towers of Hanoi recursion, initial condition is:

  • 2a n-1
    • a n-3

  • 2r + 1 = (r -1)(r

  More Complicated

  3

  

2

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

  2 Quadratic formula on r + r -1:

r = (-1 5)/ 2

  ± √

  So r = 1, r = (-1+ 5)/ 2, r = (-1- 5)/ 2

  √ √

  1

  2

  3 General homogeneous solution: n n

a = A + B [ (-1+ 5)/ 2] + C [ (-1- 5)/ 2]

n √ √

  L20 63 More Complicated

2) Nonhomogeneous particular solution to

n-3 a n n-1 n-3 - 2a + a = 2 n

  Guess the form b = k 2 . Plug guess in: n n-1 n-3 n-3 n

  

k 2 - 2k 2 + k 2 = 2

Simplifies to: k = 1. n

  So particular solution is b = 2 n

  

General General Particular

  • =

  

Nonhomogeneous homogeneous Nonhomogeneous

  Final answer: n n n

  Blackboard Exercise for 5.2

  Solve the following recurrence relation in terms of a assuming n is odd:

  1

a = (n-1)a

n n-2

  L20 65 Sections 5.4, 5.5 Crazy Bagel Example

  Suppose need to buy bagels for 13 students (1 each) out of 17 types but George and Harry are totally in love with Francesca and will only want the type of bagel that she has. Furthermore, Francesca only likes garlic or onion bagels.

  Q: How many ways are there of buying 13 bagels from 17 types if repetitions are allowed, order doesn’t matter and at least 3 are garlic or at least 3 are onion?

  

Crazy Bagel Example

  A: Same approach as before Let x = no. of bagels bought of type i. i Let i = 1 represents garlic and i = 2 onion.

  I nterested in counting the set

  RHS

  { x + x + + x = | x 3 or x 3} 1 2 17 13 ≥ ≥ 1 2 I nclusion-Exclusion principle gives: | { RHS= 13 with x 3 } | + | { RHS= 13 with x 3} | 1 ≥ 2 ≥

  • | { RHS= 13 with x 3 and x 3} |
  • 1

    ≥ ≥ 2 = | { RHS= 10} | + | { RHS= 10} | } | - | { RHS= 7} | = C (16+ 10,10)+ C (16+ 10,10)-C (16+ 7,7) = 10,378,313. L20 67 Standard I nclusion-Exclusion

      U B

    A-A B A B B-A

      ∩ ∩

      I nclusion-Exclusion principle:

      | | | | | | | | ∪ = − ∩

    • +

      A B A B A B
    • ( B

    • ( A
    • A

      B

    • AB
    • AB
    • (

    • = ∪ ∪

      4 ) 2 (

      7

      6

      5 | 4 ( | | | | |

      7

      6 4 )

      7

      6

      5 3 ( 7 4 )

      7

      6

      7

      7

      5

      4 1 (

      7

      6

      5

      4

      3

      2

      1 | | C B A C B C A B A C B A C B A C B A C B A

    • − + + =
    I nclusion-Exclusion-I nclusion

      

    ∩ ∩ − ∩ + ∩ + ∩ − + + =

    − + + + + + − + + =

      7 ] 5 [ 7 | 4 ([ | | | | |

      )

      

    B

      L20 69 I nclusion-Exclusion-I nclusion

      “I nclusion-Exclusion-I nclusion” principle:

      

    | | | | | | | | | | | | | | | |

    C B A C B C A B A C B A C B A

      

    ∩ ∩ + ∩ − ∩ − ∩ − + + = ∪ ∪

    A

      ∪

    C ) B

      ∪ C ) C

      

    A

      B

      ) A

      ∩ C U A

      ) 7 ] 7 ] 6 [

      ∩ C

      ∩ C BC

      ∩ C A

    B

    C

    Proof of

    I nclusion-Exclusion-I nclusion

      1

      2

      3

      4

      5

      6

      7 |) | | | | | | (| | | | | | |

      

    − − − + + + + − − + + + + + + + =

      Q: How many numbers between 1 and 1000 are divisible by 3, 5, or 7.

      L20 71 I nclusion-Exclusion-I nclusion

      A: Use the formula that the number of positive integers up to N which are

      N/ d

      divisible by d is . With I-E-I principle

       

      get and the fact that for relatively prime a,

      

    b both numbers divide x iff their product

    ab divides x :

      Total = 1000/ 3 1000/ 5 1000/ 7 + +

           

      1000/ 15 1000/ 21 1000/ 35 - - -

           

    • = 333 + 200 + 142 - 66 - 47 - 28 + 9

      1000/ 105

       

    • all pairs
      • ∩ ∩ + ∩ ∩ + − ∩ − ∩ −
      • = ∪ ∪ ∪
      • all triples
      • / - total intersection

      L20 73 I nclusion-Exclusion-I nclusion

      Using induction, could prove: THM: General I nclusion-Exclusion Principle: union = all terms

      …

      | | ) 1 ( | | | | | | | |

      | | | | | | | |

      2 1 1 4 2 1 3 2 1 3 1 2 1 2 1 2 1 n n n n A A A A A A A A A A A A A A A A A A A

      ∩ ∩ ∩ − +

      − L M L L L L

      Counting Pigeon Feedings

      Suppose you throw 6 crumbs in the park and 3 pigeons eat all the crumbs. How many ways could the 3 pigeons have eaten the crumbs if we only care about which pigeons ate which crumbs?

      Q: What are we counting?

      Crumb 1 Crumb 2 Crumb 3 Crumb 4 Crumb 5 Crumb 6

      Crumb 1 Crumb 2 Crumb 3 Crumb 4 Crumb 5 Crumb 6

      Pigeon 1 Pigeon 2 Pigeon 3 Pigeon 1

      Pigeon 2 Pigeon 3 Valid I nvalid Counting Pigeon Feedings

      A: Functions from crumbs to pigeons, so the

      6

      answer is 3 = 729 Next, insist that every pigeon gets fed:

      Valid I nvalid Crumb 1 Crumb 1 Crumb 2 Crumb 2 Pigeon 1

      Pigeon 1 Crumb 3 Crumb 3 Pigeon 2 Pigeon 2

      Crumb 4 Crumb 4 Pigeon 3 Pigeon 3 Crumb 5 Crumb 5

      Crumb 6 Crumb 6

      Q: What sort of function are we counting L20 now? 75 Counting Onto Functions A: Onto functions from crumbs to pigeons.

      We calculate this number using generalized I nclusion-Exclusion. | { onto functions} | = | { arbitrary} | -| { non-onto} | A function is non-onto if it misses some element in the codomain. So consider following sets for each pigeon i :

      A = { functions which miss pigeon no. i } i

      | { non-onto} | = | A | = 1 ∪ A A 2 ∪ 3 | A | | A | | A | -| A | -| A | | A |

      A A A 1 + - + 2 3 1 ∩ 2 1 ∩ 3 2 ∩ 3

      (don’t need | A | term because impossible) 1 ∩ A A 2 ∩ 3

      Counting Onto Functions

      By symmetry, | A | | A | | A | as no pigeon is 1 = = 2 3 special. Also, A is the set of functions which 1 ∩ A 2 miss both 1 and 2. Again, by symmetry | A | = | A | | A | . So:

      A A A 1 ∩ 2 1 ∩ 3 = 1 ∩ 3

      | { non-onto} | = 3| A | - 3| A A | 1 12 Finally, A is just the set of functions into { 2,3} 1 while A is the set of functions into { 3} so: 1 ∩ A 2 6

    6

    | { non-onto} | = 3·2 - 3· 1 Taking the complement: 6 6 6