CS 207m: Discrete Structures (Minor)
Lecture 11 – Counting and Combinatorics
Generating functions and Principle of Inclusion-Exclusion
Feb 16 2018
Last few classes
Basic counting techniques and applications
1. Sum and product, bijection, double counting principles 2. Binomial coefficients and binomial theorem, Pascal’s
triangle
3. Permutations and combinations with/without repetitions 4. Counting subsets, relations, Handshake lemma
5. Stirling’s approximation: Estimating n!
6. Recurrence relations and one method to solve them.
7. Solving recurrence relations via generating functions.
Properties of generating functions
Definition
The(ordinary) generating function for a sequencea0, a1, . . .∈R is the infinite seriesφ(x) =P∞
k=0akxk.
I Let f(x) =P∞
k=0akxk,g(x) =P∞
k=0bkxk. Then 1. Iff(x) =g(x), thenak=bk for allk.
2. f(x) +g(x) =P∞
k=0(ak+bk)xk, 3. f(x)g(x) =P∞
k=0(Pk
j=0ajbk−j)xk, 4. dxd (P∞
k=0akxk) =P∞
k=1(kak)xk−1
I Let u∈R,k∈Z≥0, Thenextended binomial coefficient uk is defined as uk
= u(u−1)...(u−k+1)
k! ifk >0 and = 1 ifk= 0.
I What if u=−nforn∈N?
The extended binomial theorem Letu∈R, (1 +x)u =P∞
k=0 u k
xk. If you don’t like this, takex∈R,|x|<1.
Properties of generating functions
Definition
The(ordinary) generating function for a sequencea0, a1, . . .∈R is the infinite seriesφ(x) =P∞
k=0akxk.
I Let f(x) =P∞
k=0akxk,g(x) =P∞
k=0bkxk. Then 1. Iff(x) =g(x), thenak=bk for allk.
2. f(x) +g(x) =P∞
k=0(ak+bk)xk, 3. f(x)g(x) =P∞
k=0(Pk
j=0ajbk−j)xk, 4. dxd (P∞
k=0akxk) =P∞
k=1(kak)xk−1
I Let u∈R,k∈Z≥0, Thenextended binomial coefficient uk is defined as uk
= u(u−1)...(u−k+1)
k! ifk >0 and = 1 ifk= 0.
I What if u=−nforn∈N?
The extended binomial theorem Letu∈R, (1 +x)u =P∞
k=0 u k
xk. If you don’t like this, takex∈R,|x|<1.
Properties of generating functions
Definition
The(ordinary) generating function for a sequencea0, a1, . . .∈R is the infinite seriesφ(x) =P∞
k=0akxk.
I Let f(x) =P∞
k=0akxk,g(x) =P∞
k=0bkxk. Then 1. Iff(x) =g(x), thenak=bk for allk.
2. f(x) +g(x) =P∞
k=0(ak+bk)xk, 3. f(x)g(x) =P∞
k=0(Pk
j=0ajbk−j)xk, 4. dxd (P∞
k=0akxk) =P∞
k=1(kak)xk−1
I Let u∈R,k∈Z≥0, Thenextended binomial coefficient uk is defined as uk
= u(u−1)...(u−k+1)
k! ifk >0 and = 1 ifk= 0.
I What if u=−nforn∈N?
The extended binomial theorem Letu∈R, (1 +x)u =P∞
k=0 u k
xk.
If you don’t like this, takex∈R,|x|<1.
Properties of generating functions
Definition
The(ordinary) generating function for a sequencea0, a1, . . .∈R is the infinite seriesφ(x) =P∞
k=0akxk.
I Let f(x) =P∞
k=0akxk,g(x) =P∞
k=0bkxk. Then 1. Iff(x) =g(x), thenak=bk for allk.
2. f(x) +g(x) =P∞
k=0(ak+bk)xk, 3. f(x)g(x) =P∞
k=0(Pk
j=0ajbk−j)xk, 4. dxd (P∞
k=0akxk) =P∞
k=1(kak)xk−1
I Let u∈R,k∈Z≥0, Thenextended binomial coefficient uk is defined as uk
= u(u−1)...(u−k+1)
k! ifk >0 and = 1 ifk= 0.
I What if u=−nforn∈N? The extended binomial theorem
Simple examples using generating functions
Standard identities:
I 1
1−ax =P∞ k=0akxk
I 1
1−xr =P∞ k=0xrk
I ex=P∞ k=0xk
k!
Solving Catalan numbers using generating functions
Catalan Numbers C(n) =
n−1
X
i=1
C(i)C(n−i) for n >1,C(0) = 0, C(1) = 1.
Solving Catalan numbers using generating functions
Catalan Numbers C(n) =
n−1
X
i=1
C(i)C(n−i) for n >1,C(0) = 0, C(1) = 1.
I Let φ(x) =P∞
k=1C(k)xk.
Solving Catalan numbers using generating functions
Catalan Numbers C(n) =
n−1
X
i=1
C(i)C(n−i) for n >1,C(0) = 0, C(1) = 1.
I Let φ(x) =P∞
k=1C(k)xk.
I Now considerφ(x)2.
I φ(x)2 = (P∞
k=1C(k)xk)(P∞
k=1C(k)xk)
= (P∞ k=2
Pk−1
i=1 C(i)C(k−i)xk)
= (P∞
k=2C(k)xk) =φ(x)−x
Solving Catalan numbers using generating functions
Catalan Numbers C(n) =
n−1
X
i=1
C(i)C(n−i) for n >1,C(0) = 0, C(1) = 1.
I Let φ(x) =P∞
k=1C(k)xk.
I Now considerφ(x)2.
I φ(x)2 = (P∞
k=1C(k)xk)(P∞
k=1C(k)xk)
= (P∞ k=2
Pk−1
i=1 C(i)C(k−i)xk)
= (P∞
k=2C(k)xk) =φ(x)−x
I Solving for φ(x) we get, φ(x) = 12(1±(1−4x)1/2)
I But since φ(0) = 0, we have
φ(x) = 12(1−(1−4x)1/2) = 12+ (−12(1−4x)1/2).
Catalan numbers
Recall: Extended binomial theorem Letα∈R,(1 +x)α=P∞
n=0 α n
xn, where αn
= α(α−1)...(α−n+1)
n! .
Catalan numbers
Recall: Extended binomial theorem Letα∈R,(1 +x)α=P∞
n=0 α n
xn, where αn
= α(α−1)...(α−n+1)
n! .
I P∞
k=1C(k)xk=φ(x) = 12 + (−12(1−4x)1/2) =
1
2 + (−12P∞ k=0
1/2 k
(−4x)k).
Catalan numbers
Recall: Extended binomial theorem Letα∈R,(1 +x)α=P∞
n=0 α n
xn, where αn
= α(α−1)...(α−n+1)
n! .
I P∞
k=1C(k)xk=φ(x) = 12 + (−12(1−4x)1/2) =
1
2 + (−12P∞ k=0
1/2 k
(−4x)k).
I The coefficient of xk is C(k) =−12 1/2k (−4)k
=−12(12(12−1)(12 −2). . .(12 −k+ 1))(−4)k!k
=−12(12)(−32)(−52). . .(−2k−32 ))(−4)k!k
Catalan numbers
Recall: Extended binomial theorem Letα∈R,(1 +x)α=P∞
n=0 α n
xn, where αn
= α(α−1)...(α−n+1)
n! .
I P∞
k=1C(k)xk=φ(x) = 12 + (−12(1−4x)1/2) =
1
2 + (−12P∞ k=0
1/2 k
(−4x)k).
I The coefficient of xk is C(k) =−12 1/2k (−4)k
=−12(12(12−1)(12 −2). . .(12 −k+ 1))(−4)k!k
=−12(12)(−32)(−52). . .(−2k−32 ))(−4)k!k
I C(k) =(−1)2k+1k(−4)k! k ·1·3· · ·(2k−3)
Catalan numbers
Recall: Extended binomial theorem Letα∈R,(1 +x)α=P∞
n=0 α n
xn, where αn
= α(α−1)...(α−n+1)
n! .
I P∞
k=1C(k)xk=φ(x) = 12 + (−12(1−4x)1/2) =
1
2 + (−12P∞ k=0
1/2 k
(−4x)k).
I The coefficient of xk is C(k) =−12 1/2k (−4)k
=−12(12(12−1)(12 −2). . .(12 −k+ 1))(−4)k!k
=−12(12)(−32)(−52). . .(−2k−32 ))(−4)k!k
I C(k) =(−1)2k+1k(−4)k! k ·1·3· · ·(2k−3)
I C(k) =2k+11·4k·k!·1.2....(2k−3)(2k−2)
2k−1(k−1)! = k!(k−1)!(2k−2)!.
Catalan numbers
Recall: Extended binomial theorem Letα∈R,(1 +x)α=P∞
n=0 α n
xn, where αn
= α(α−1)...(α−n+1)
n! .
I P∞
k=1C(k)xk=φ(x) = 12 + (−12(1−4x)1/2) =
1
2 + (−12P∞ k=0
1/2 k
(−4x)k).
I The coefficient of xk is C(k) =−12 1/2k (−4)k
=−12(12(12−1)(12 −2). . .(12 −k+ 1))(−4)k!k
=−12(12)(−32)(−52). . .(−2k−32 ))(−4)k!k
I C(k) =(−1)2k+1k(−4)k! k ·1·3· · ·(2k−3)
I C(k) =2k+11·4k·k!·1.2....(2k−3)(2k−2)
2k−1(k−1)! = k!(k−1)!(2k−2)!. Thus, thenth Catalan number is given by C(n) = n!(n−1)!(2n−2)! = 1n 2n−2n−1
Other applications of generating functions
I What is the number of ways ak of selecting kelements from an nelement set if repetitions are allowed?
I Letφ(x) =P∞ k=0akxk.
I Observe thatφ(x) = (1 +x+x2+. . .)n= (1−x)−n
I Expand this by the extended binomial theorem and compare coefficients ofxk.
I ak= −nk
(−1)k = (−1)k n+k−1k
(−1)k= n+k−1k .
I (H.W)What if there must be ≥1 element of each type?
I Proving binomial identities: Show thatPn k=0
n k
2
= 2nn .
I Compare coefficients ofxn in (1 +x)2n= ((1 +x)n)2.
Other applications of generating functions
I What is the number of ways ak of selecting kelements from an nelement set if repetitions are allowed?
I Letφ(x) =P∞ k=0akxk.
I Observe thatφ(x) = (1 +x+x2+. . .)n= (1−x)−n
I Expand this by the extended binomial theorem and compare coefficients ofxk.
I ak= −nk
(−1)k = (−1)k n+k−1k
(−1)k= n+k−1k .
I (H.W)What if there must be ≥1 element of each type?
I Proving binomial identities: Show thatPn k=0
n k
2
= 2nn .
I Compare coefficients ofxn in (1 +x)2n= ((1 +x)n)2.
Other applications of generating functions
I What is the number of ways ak of selecting kelements from an nelement set if repetitions are allowed?
I Letφ(x) =P∞ k=0akxk.
I Observe thatφ(x) = (1 +x+x2+. . .)n= (1−x)−n
I Expand this by the extended binomial theorem and compare coefficients ofxk.
I ak= −nk
(−1)k = (−1)k n+k−1k
(−1)k= n+k−1k .
I (H.W)What if there must be ≥1 element of each type?
I Proving binomial identities: Show thatPn k=0
n k
2
= 2nn .
I Compare coefficients ofxn in (1 +x)2n= ((1 +x)n)2.
Other applications of generating functions
I What is the number of ways ak of selecting kelements from an nelement set if repetitions are allowed?
I Letφ(x) =P∞ k=0akxk.
I Observe thatφ(x) = (1 +x+x2+. . .)n= (1−x)−n
I Expand this by the extended binomial theorem and compare coefficients ofxk.
I ak= −nk
(−1)k = (−1)k n+k−1k
(−1)k= n+k−1k .
I (H.W)What if there must be ≥1 element of each type?
I Proving binomial identities: Show thatPn k=0
n k
2
= 2nn .
I Compare coefficients ofxn in (1 +x)2n= ((1 +x)n)2.
Other applications of generating functions
I What is the number of ways ak of selecting kelements from an nelement set if repetitions are allowed?
I Letφ(x) =P∞ k=0akxk.
I Observe thatφ(x) = (1 +x+x2+. . .)n= (1−x)−n
I Expand this by the extended binomial theorem and compare coefficients ofxk.
I ak= −nk
(−1)k = (−1)k n+k−1k
(−1)k= n+k−1k .
I (H.W)What if there must be ≥1 element of each type?
I Proving binomial identities: Show thatPn k=0
n k
2
= 2nn .
I Compare coefficients ofxn in (1 +x)2n= ((1 +x)n)2.
Other applications of generating functions
I What is the number of ways ak of selecting kelements from an nelement set if repetitions are allowed?
I Letφ(x) =P∞ k=0akxk.
I Observe thatφ(x) = (1 +x+x2+. . .)n= (1−x)−n
I Expand this by the extended binomial theorem and compare coefficients ofxk.
I ak= −nk
(−1)k = (−1)k n+k−1k
(−1)k= n+k−1k .
I (H.W)What if there must be ≥1 element of each type?
I Proving binomial identities: Show thatPn k=0
n k
2
= 2nn .
I Compare coefficients ofxn in (1 +x)2n= ((1 +x)n)2.
Other applications of generating functions
I What is the number of ways ak of selecting kelements from an nelement set if repetitions are allowed?
I Letφ(x) =P∞ k=0akxk.
I Observe thatφ(x) = (1 +x+x2+. . .)n= (1−x)−n
I Expand this by the extended binomial theorem and compare coefficients ofxk.
I ak= −nk
(−1)k = (−1)k n+k−1k
(−1)k= n+k−1k .
I (H.W)What if there must be ≥1 element of each type?
I Proving binomial identities: Show thatPn k=0
n k
2
= 2nn .
I Compare coefficients ofxn in (1 +x)2n= ((1 +x)n)2.
Other applications of generating functions
I What is the number of ways ak of selecting kelements from an nelement set if repetitions are allowed?
I Letφ(x) =P∞ k=0akxk.
I Observe thatφ(x) = (1 +x+x2+. . .)n= (1−x)−n
I Expand this by the extended binomial theorem and compare coefficients ofxk.
I ak= −nk
(−1)k = (−1)k n+k−1k
(−1)k= n+k−1k .
I (H.W)What if there must be ≥1 element of each type?
I Proving binomial identities: Show thatPn k=0
n k
2
= 2nn .
I Compare coefficients ofxn in (1 +x)2n= ((1 +x)n)2.
Applications of generating functions
Solving recurrence relations
I Number of subsets of a set of size n,
I Fibonacci sequence,
I Catalan numbers: C(n) = n!(n−1)!(2n−2)! = n1 2n−2n−1
Some more practice problems:
1. (H.W) How many ways can a convex n-sided polygon be cut into triangles by adding non-intersecting diagonals (i.e., connecting vertices with non-crossing lines)? Write a recurrence and solve it!
2. (H.W) Write a recurrence for the number of derrangements. That is, no. of ways to arrange nletters inton addressed envelopes such that no letter goes to the correct envelope.
Applications of generating functions
Solving recurrence relations
I Number of subsets of a set of size n,
I Fibonacci sequence,
I Catalan numbers: C(n) = n!(n−1)!(2n−2)! = n1 2n−2n−1
Some more practice problems:
1. (H.W) How many ways can a convex n-sided polygon be cut into triangles by adding non-intersecting diagonals (i.e., connecting vertices with non-crossing lines)? Write a recurrence and solve it!
2. (H.W) Write a recurrence for the number of derrangements. That is, no. of ways to arrange nletters inton addressed envelopes such that no letter goes to the correct envelope.
Applications of generating functions
Solving recurrence relations
I Number of subsets of a set of size n,
I Fibonacci sequence,
I Catalan numbers: C(n) = n!(n−1)!(2n−2)! = n1 2n−2n−1
Some more practice problems:
1. (H.W) How many ways can a convex n-sided polygon be cut into triangles by adding non-intersecting diagonals (i.e., connecting vertices with non-crossing lines)? Write a recurrence and solve it!
2. (H.W) Write a recurrence for the number of derrangements.
That is, no. of ways to arrange nletters inton addressed envelopes such that no letter goes to the correct envelope.
Principle of Inclusion-Exclusion (PIE)
A simple example:
I If in a class nstudents like physics,m students like biology and k students who like both, and` like neither, then how many students are there in the class?
I Of course, this also counts the no. who were too lazy to lift their hands!
Theorem: Principle of Inclusion-Exclusion (PIE) LetA1, A2, . . . , Anbe finite sets. Then,
|A1∪. . .∪An|= X
1≤i≤n
|Ai| − X
1≤i<j≤n
|Ai∩Aj|
+ X
1≤i<j<k≤n
|Ai∩Aj ∩Ak| −. . .+ (−1)n+1|A1∩. . .∩An|
Principle of Inclusion-Exclusion (PIE)
A simple example:
I If in a class nstudents like physics,m students like biology and k students who like both, and` like neither, then how many students are there in the class?
I Of course, this also counts the no. who were too lazy to lift their hands!
Theorem: Principle of Inclusion-Exclusion (PIE) LetA1, A2, . . . , Anbe finite sets. Then,
|A1∪. . .∪An|= X
1≤i≤n
|Ai| − X
1≤i<j≤n
|Ai∩Aj|
+ X
1≤i<j<k≤n
|Ai∩Aj ∩Ak| −. . .+ (−1)n+1|A1∩. . .∩An|
Principle of Inclusion-Exclusion (PIE)
A simple example:
I If in a class nstudents like physics,m students like biology and k students who like both, and` like neither, then how many students are there in the class?
I Of course, this also counts the no. who were too lazy to lift their hands!
Theorem: Principle of Inclusion-Exclusion (PIE) LetA1, A2, . . . , An be finite sets. Then,
|A1∪. . .∪An|= X
1≤i≤n
|Ai| − X
1≤i<j≤n
|Ai∩Aj|
+ X
1≤i<j<k≤n
|Ai∩Aj ∩Ak| −. . .+ (−1)n+1|A1∩. . .∩An|
Number of surjections
I How many surjections are there from [n] ={1, . . . , n}to [m] ={1, . . . , m}?
Number of surjections
I How many surjections are there from [n] ={1, . . . , n}to [m] ={1, . . . , m}?
I # surjections = total #functions - those that miss some element in range.
Number of surjections
I How many surjections are there from [n] ={1, . . . , n}to [m] ={1, . . . , m}?
I # surjections = total #functions - those that miss some element in range.
I Let Ai={f : [n]→[m]|i6∈Range(f)}
I Then, # surjections =mn− | ∪i∈[m]Ai|.
Number of surjections
I How many surjections are there from [n] ={1, . . . , n}to [m] ={1, . . . , m}?
I # surjections = total #functions - those that miss some element in range.
I Let Ai={f : [n]→[m]|i6∈Range(f)}
I Then, # surjections =mn− | ∪i∈[m]Ai|.
Theorem: Principle of Inclusion-Exclusion (PIE) LetA1, A2, . . . , An be finite sets. Then,
|A1∪. . .∪An|= X
1≤i≤n
|Ai| − X
1≤i<j≤n
|Ai∩Aj|
+ X
1≤i<j<k≤n
|Ai∩Aj ∩Ak| −. . .+ (−1)n+1|A1∩. . .∩An|
Number of surjections
I How many surjections are there from [n] ={1, . . . , n}to [m] ={1, . . . , m}?
I # surjections = total #functions - those that miss some element in range.
I Let Ai={f : [n]→[m]|i6∈Range(f)}
I Then, # surjections =mn− | ∪i∈[m]Ai|.
I | ∪i∈[m]Ai|=P
1≤i≤m|Ai| −P
1≤i<j≤m|Ai∩Aj|+ P
1≤i<j<k≤m|Ai∩Aj∩Ak| −. . .+ (−1)m+1|A1∩. . .∩Am|
Number of surjections
I How many surjections are there from [n] ={1, . . . , n}to [m] ={1, . . . , m}?
I # surjections = total #functions - those that miss some element in range.
I Let Ai={f : [n]→[m]|i6∈Range(f)}
I Then, # surjections =mn− | ∪i∈[m]Ai|.
I | ∪i∈[m]Ai|=P
1≤i≤m|Ai| −P
1≤i<j≤m|Ai∩Aj|+ P
1≤i<j<k≤m|Ai∩Aj∩Ak| −. . .+ (−1)m+1|A1∩. . .∩Am|
I But now what is |Ai|,|Ai∩Aj|,|Ai∩Aj∩Ak|, . . .?
Number of surjections
I How many surjections are there from [n] ={1, . . . , n}to [m] ={1, . . . , m}?
I # surjections = total #functions - those that miss some element in range.
I Let Ai={f : [n]→[m]|i6∈Range(f)}
I Then, # surjections =mn− | ∪i∈[m]Ai|.
I | ∪i∈[m]Ai|=P
1≤i≤m|Ai| −P
1≤i<j≤m|Ai∩Aj|+ P
1≤i<j<k≤m|Ai∩Aj∩Ak| −. . .+ (−1)m+1|A1∩. . .∩Am|
I But now what is |Ai|,|Ai∩Aj|,|Ai∩Aj∩Ak|, . . .?
I |Ai|= (m−1)n,|Ai∩Aj|= (m−2)n...
Number of surjections
I How many surjections are there from [n] ={1, . . . , n}to [m] ={1, . . . , m}?
I # surjections = total #functions - those that miss some element in range.
I Let Ai={f : [n]→[m]|i6∈Range(f)}
I Then, # surjections =mn− | ∪i∈[m]Ai|.
I | ∪i∈[m]Ai|=P
1≤i≤m|Ai| −P
1≤i<j≤m|Ai∩Aj|+ P
1≤i<j<k≤m|Ai∩Aj∩Ak| −. . .+ (−1)m+1|A1∩. . .∩Am|
I But now what is |Ai|,|Ai∩Aj|,|Ai∩Aj∩Ak|, . . .?
I |Ai|= (m−1)n,|Ai∩Aj|= (m−2)n...
I What about the summation? terms 1≤i < j ≤m=
Number of surjections
I How many surjections are there from [n] ={1, . . . , n}to [m] ={1, . . . , m}?
I # surjections = total #functions - those that miss some element in range.
I Let Ai={f : [n]→[m]|i6∈Range(f)}
I Then, # surjections =mn− | ∪i∈[m]Ai|.
I | ∪i∈[m]Ai|=P
1≤i≤m|Ai| −P
1≤i<j≤m|Ai∩Aj|+ P
1≤i<j<k≤m|Ai∩Aj∩Ak| −. . .+ (−1)m+1|A1∩. . .∩Am|
I But now what is |Ai|,|Ai∩Aj|,|Ai∩Aj∩Ak|, . . .?
I |Ai|= (m−1)n,|Ai∩Aj|= (m−2)n...
I What about the summation? terms 1≤i < j ≤m= m2
Proof of PIE
Theorem: Principle of Inclusion-Exclusion (PIE) LetA1, A2, . . . , An be finite sets. Then,
|A1∪. . .∪An|= X
1≤i≤n
|Ai| − X
1≤i<j≤n
|Ai∩Aj|
+ X
1≤i<j<k≤n
|Ai∩Aj ∩Ak| −. . .+ (−1)n+1|A1∩. . .∩An|
Proof: (H.W): Prove PIE by induction.
I We will show that each element in the union is counted exactly once in the r.h.s
I Let abelong to exactlyr of the setsA1, . . . , An.
I Then ais counted 1r
times byP
|Ai|, etc.
I Thus, overall count = r1
− r2
+. . .(−1)r+1 rr .
I What is this number?! =1!
Proof of PIE
Theorem: Principle of Inclusion-Exclusion (PIE) LetA1, A2, . . . , An be finite sets. Then,
|A1∪. . .∪An|= X
1≤i≤n
|Ai| − X
1≤i<j≤n
|Ai∩Aj|
+ X
1≤i<j<k≤n
|Ai∩Aj ∩Ak| −. . .+ (−1)n+1|A1∩. . .∩An|
Proof:
I We will show that each element in the union is counted exactly once in the r.h.s
I Let abelong to exactlyr of the setsA1, . . . , An.
I Then ais counted 1r
times byP
|Ai|, etc.
I Thus, overall count = r1
− r2
+. . .(−1)r+1 rr .
I What is this number?! =1!
Proof of PIE
Theorem: Principle of Inclusion-Exclusion (PIE) LetA1, A2, . . . , An be finite sets. Then,
|A1∪. . .∪An|= X
1≤i≤n
|Ai| − X
1≤i<j≤n
|Ai∩Aj|
+ X
1≤i<j<k≤n
|Ai∩Aj ∩Ak| −. . .+ (−1)n+1|A1∩. . .∩An|
Proof:
I We will show that each element in the union is counted exactly once in the r.h.s
I Let abelong to exactlyr of the setsA1, . . . , An.
I Then ais counted 1r
times byP
|Ai|, etc.
I Thus, overall count = r1
− r2
+. . .(−1)r+1 rr .
I What is this number?! =1!
Proof of PIE
Theorem: Principle of Inclusion-Exclusion (PIE) LetA1, A2, . . . , An be finite sets. Then,
|A1∪. . .∪An|= X
1≤i≤n
|Ai| − X
1≤i<j≤n
|Ai∩Aj|
+ X
1≤i<j<k≤n
|Ai∩Aj ∩Ak| −. . .+ (−1)n+1|A1∩. . .∩An|
Proof:
I We will show that each element in the union is counted exactly once in the r.h.s
I Let abelong to exactlyr of the setsA1, . . . , An.
I Then ais counted 1r
times byP
|Ai|, etc.
I Thus, overall count = r1
− r2
+. . .(−1)r+1 rr .
I What is this number?! =1!
Proof of PIE
Theorem: Principle of Inclusion-Exclusion (PIE) LetA1, A2, . . . , An be finite sets. Then,
|A1∪. . .∪An|= X
1≤i≤n
|Ai| − X
1≤i<j≤n
|Ai∩Aj|
+ X
1≤i<j<k≤n
|Ai∩Aj ∩Ak| −. . .+ (−1)n+1|A1∩. . .∩An|
Proof:
I We will show that each element in the union is counted exactly once in the r.h.s
I Let abelong to exactlyr of the setsA1, . . . , An.
I Then ais counted 1r
times byP
|Ai|, etc.
I Thus, overall count = r1
− r2
+. . .(−1)r+1 rr .
I What is this number?! =1!
Proof of PIE
Theorem: Principle of Inclusion-Exclusion (PIE) LetA1, A2, . . . , An be finite sets. Then,
|A1∪. . .∪An|= X
1≤i≤n
|Ai| − X
1≤i<j≤n
|Ai∩Aj|
+ X
1≤i<j<k≤n
|Ai∩Aj ∩Ak| −. . .+ (−1)n+1|A1∩. . .∩An|
Proof:
I We will show that each element in the union is counted exactly once in the r.h.s
I Let abelong to exactlyr of the setsA1, . . . , An.
I Then ais counted 1r
times byP
|Ai|, etc.
=1!
Proof of PIE
Theorem: Principle of Inclusion-Exclusion (PIE) LetA1, A2, . . . , An be finite sets. Then,
|A1∪. . .∪An|= X
1≤i≤n
|Ai| − X
1≤i<j≤n
|Ai∩Aj|
+ X
1≤i<j<k≤n
|Ai∩Aj ∩Ak| −. . .+ (−1)n+1|A1∩. . .∩An|
Proof:
I We will show that each element in the union is counted exactly once in the r.h.s
I Let abelong to exactlyr of the setsA1, . . . , An.
I Then ais counted 1r
times byP
|Ai|, etc.
I Thus, overall count = r1
− r2
+. . .(−1)r+1 rr .
I What is this number?! =1!
Applications of PIE
I How many integral solutions does x1+x2+x3 = 11 have where 0≤x1 ≤3,0≤x2≤4,0≤x3 ≤6?
I Number of derangements of a set with nelements
I That is, no. of ways to arrange n letters into n addressed envelopes such that no letter goes to the correct envelope.
Number of derangements
Formally, aderangement is a permutation of objects that leaves no object in its original position.
Theorem
LetDn denote the number of derangements of [n].
Dn=n!
n
X
i=0
(−1)i i!
I Dn = (total # permutations of [n]) - (# permutations of [n] that fix at least 1 element)
I Apply PIE on latter term, lets call itP, let P(i, j) denote permutations which fixi, j and so on.
I P = X
1≤i≤n
P(i)− X
1≤i<j≤n
P(i, j) +. . .+ (−1)nP(1, . . . , n).
I But P(i) = (n−1)!,P(i, j) = (n−2)!, . . .
I P = n
1
(n−1)!− n
2
(n−2)!. . .+ (−1)n+1 n
n
(n−n)!
I Thus,Dn=n!(1−1!1 +2!1 . . .+ (−1)nn!1)
Number of derangements
Formally, aderangement is a permutation of objects that leaves no object in its original position.
Theorem
LetDn denote the number of derangements of [n].
Dn=n!
n
X
i=0
(−1)i i!
I Dn = (total # permutations of [n]) - (# permutations of [n] that fix at least 1 element)
I Apply PIE on latter term, lets call itP, let P(i, j) denote permutations which fixi, j and so on.
I P = X
1≤i≤n
P(i)− X
1≤i<j≤n
P(i, j) +. . .+ (−1)nP(1, . . . , n).
I But P(i) = (n−1)!,P(i, j) = (n−2)!, . . .
I P = n
1
(n−1)!− n
2
(n−2)!. . .+ (−1)n+1 n
n
(n−n)!
I Thus,Dn=n!(1−1!1 +2!1 . . .+ (−1)nn!1)
Number of derangements
Theorem
LetDn denote the number of derangements of [n].
Dn=n!
n
X
i=0
(−1)i i!
I Dn = (total # permutations of [n]) - (# permutations of [n] that fix at least 1 element)
I Apply PIE on latter term, lets call itP, let P(i, j) denote permutations which fixi, j and so on.
I P = X
1≤i≤n
P(i)− X
1≤i<j≤n
P(i, j) +. . .+ (−1)nP(1, . . . , n).
I But P(i) = (n−1)!,P(i, j) = (n−2)!, . . .
I P = n
1
(n−1)!− n
2
(n−2)!. . .+ (−1)n+1 n
n
(n−n)!
I Thus,Dn=n!(1−1!1 +2!1 . . .+ (−1)nn!1)
Number of derangements
Theorem
LetDn denote the number of derangements of [n].
Dn=n!
n
X
i=0
(−1)i i!
I Dn = (total # permutations of [n]) - (# permutations of [n] that fix at least 1 element)
I Apply PIE on latter term, lets call itP, let P(i, j) denote permutations which fixi, j and so on.
I P = X
1≤i≤n
P(i)− X
1≤i<j≤n
P(i, j) +. . .+ (−1)nP(1, . . . , n).
I But P(i) = (n−1)!,P(i, j) = (n−2)!, . . .
I P = n
1
(n−1)!− n
2
(n−2)!. . .+ (−1)n+1 n
n
(n−n)!
I Thus,Dn=n!(1−1!1 +2!1 . . .+ (−1)nn!1)
Number of derangements
Theorem
LetDn denote the number of derangements of [n].
Dn=n!
n
X
i=0
(−1)i i!
I Dn = (total # permutations of [n]) - (# permutations of [n] that fix at least 1 element)
I Apply PIE on latter term, lets call itP, let P(i, j) denote permutations which fixi, j and so on.
I P = X
1≤i≤n
P(i)− X
1≤i<j≤n
P(i, j) +. . .+ (−1)nP(1, . . . , n).
I But P(i) = (n−1)!,P(i, j) = (n−2)!, . . .
I P = n
1
(n−1)!− n
2
(n−2)!. . .+ (−1)n+1 n
n
(n−n)!
I Thus,Dn=n!(1−1!1 +2!1 . . .+ (−1)nn!1)
Number of derangements
Theorem
LetDn denote the number of derangements of [n].
Dn=n!
n
X
i=0
(−1)i i!
I Dn = (total # permutations of [n]) - (# permutations of [n] that fix at least 1 element)
I Apply PIE on latter term, lets call itP, let P(i, j) denote permutations which fixi, j and so on.
I P = X
1≤i≤n
P(i)− X
1≤i<j≤n
P(i, j) +. . .+ (−1)nP(1, . . . , n).
I But P(i) = (n−1)!,P(i, j) = (n−2)!, . . .
I P = n
1
(n−1)!− n
2
(n−2)!. . .+ (−1)n+1 n
n
(n−n)!
I Thus,Dn=n!(1−1!1 +2!1 . . .+ (−1)nn!1)
Number of derangements
Theorem
LetDn denote the number of derangements of [n].
Dn=n!
n
X
i=0
(−1)i i!
I Dn = (total # permutations of [n]) - (# permutations of [n] that fix at least 1 element)
I Apply PIE on latter term, lets call itP, let P(i, j) denote permutations which fixi, j and so on.
I P = X
1≤i≤n
P(i)− X
1≤i<j≤n
P(i, j) +. . .+ (−1)nP(1, . . . , n).
I But P(i) = (n−1)!,P(i, j) = (n−2)!, . . .
I P = n
1
(n−1)!− n
2
(n−2)!. . .+ (−1)n+1 n
n
(n−n)!
I Thus,Dn=n!(1−1!1 +2!1 . . .+ (−1)nn!1)