Module8-Adv Counting II
Module8-Adv Counting II
26/12/2021
College of Computing and
Informatics
BS in Computer Science
CS243
Discrete Mathematics in Computer Science
Discrete Mathematics in Computer Science
Week 9
Chapter 8: Advanced Counting
Techniques II
Contents
Recommended Reading
Generating functions
https://math.mit.edu/~goemans/18310S15/generating-function-notes.pdf
Recommended videos
Inclusion-Exclusion Principle-Discrete Mathematics
https://www.youtube.com/watch?v=GS7dIWA6Hpo
This Presentation is mainly dependent on the textbook: Discrete Mathematics and Its Application by Kenneth H. Rosen
8.4 Generating Functions
EXAMPLE 5 The function f (x) = 1∕(1 − ax) is the generating function of the sequence
1, a, , ,… because 1∕(1 − ax) = 1 + ax + +⋯ when |ax| < 1, or equivalently, for |x| <
1∕|a| for a ≠ 0.
We also will need some results on how to add and multiply two
generating functions.
Exercises
1. Find the generating function for the finite sequence 2, 2, 2, 2, 2, 2.
2. Find the generating function for the finite sequence 1, 4, 16, 64, 256.
3. Find the coefficient of x 9 in the power series of each of (1 + x 3 + x 6 + x 9 +⋯)3, by using
(1 + x)n = 1 + C (n, 1)x + C (n, 2)x 2 +⋯+ x n // r-combinations: Page 448
Table 1 presents a useful summary of some generating functions that arise frequently. // Page 568
8.5 Inclusion–Exclusion
A discrete mathematics class contains 30 women and 50 sophomores (A student in his second year of study).
How many students in the class are either women or sophomores?
• This question cannot be answered unless more information is provided. Adding the
number of women in the class and the number of sophomores probably does not give
the correct answer, because women sophomores are counted twice.
• This observation shows that the number of students in the class that are either
sophomores or women is the sum of the number of women and the number of
sophomores in the class minus the number of women sophomores.
• A technique for solving such counting problems was introduced in Section 6.1.
• In this section we will generalize the ideas introduced in that section to solve
problems that require us to count the number of elements in the union of more than
two sets.
The Principle of Inclusion-Exclusion
• The number of elements in the union of the two sets A and B is the
sum of the numbers of elements in the sets minus the number of
elements in their intersection. That is,
The Principle of Inclusion–Exclusion
Example 1: In a discrete mathematics class every student is a major in computer science
or mathematics or both. The number of students having computer science as a major
(possibly along with mathematics) is 25; the number of students having mathematics as
a major (possibly along with computer science) is 13, and the number of students
majoring in both computer science and mathematics is 8. How many students are in this
class?
Solution: Let A be the set of students in the class majoring in computer science and B be
the set of students in the class majoring in mathematics.
• Then A ∩ B is the set of students in the class who are joint mathematics and computer
science majors.
• Because every student in the class is majoring in either computer science or
mathematics (or both), it follows that the number of students in the class is |A ∪ B|.
Therefore,
= 25 + 13 − 8 = 30.
• Therefore, there are 30 students in the class. This computation is illustrated as follows
(next slide).
The Principle of Inclusion–Exclusion
The Principle of Inclusion–Exclusion
Example 3: Suppose that there are 1807 freshmen at your school. Of these, 453 are taking a course in
computer science, 567 are taking a course in mathematics, and 299 are taking courses in both computer
science and mathematics.
How many are not taking a course either in computer science or in mathematics?
• Solution: To find the number of freshmen who are not taking a course in either mathematics or computer
science, subtract the number of freshmen who are taking a course in either of these subjects from the total
number of freshmen.
• Let A be the set of all freshmen taking a course in computer science, and let B be the set of all freshmen taking
a course in mathematics. It follows that
• Consequently, there are 1807 − 721 = 1086 freshmen who are not taking a course in computer science or
mathematics.
The Principle of Inclusion–Exclusion
• We will now begin our development of a formula for the number of elements in the union
of a finite number of sets. The formula we will develop is called the principle of
inclusion–exclusion.
• For concreteness, before we consider unions of n sets, where n is any positive integer, we
will derive a formula for the number of elements in the union of three sets A, B, and C. To
construct this formula, we note that |A| + |B| + |C| counts each element that is in exactly
one of the three sets once, elements that are in exactly two of the sets twice, and
elements in all three sets three times. This is illustrated in the first panel in Figure 3.
• To remove the overcount of elements in more than one of the sets, we subtract the
number of elements in the intersections of all pairs of the three sets. We obtain |A| + |B|
+ |C| − |A ∩ B| − |A ∩ C| − |B ∩ C|.
• This expression still counts elements that occur in exactly one of the sets once. An
element that occurs in exactly two of the sets is also counted exactly once, because this
element will occur in one of the three intersections of sets taken two at a time.
• However, those elements that occur in all three sets will be counted zero times by this
expression, because they occur in all three intersections of sets taken two at a time. This is
illustrated in the second panel in Figure 3.
The Principle of Inclusion–Exclusion
• EXAMPLE 4
Therefore, there are seven students who have taken courses in Spanish, French, and Russian.
This principle tells us that we can count the elements in a union of n sets (where n
is a positive integer) by adding the number of elements in the sets, then
subtracting the sum of the number of elements in all intersections of two of these
sets, then adding the number of elements in all intersections of three of these sets,
and so on, until we reach the number of elements in the intersection of all the sets.
The Principle Of Inclusion–exclusion
8.6 Applications of Inclusion–Exclusion
Many counting problems can be solved using the principle of inclusion-exclusion.
• For instance, we can use this principle to find the number of primes less than a positive integer.
• Many problems can be solved by counting the number of onto functions from one finite set to another. The
inclusion-exclusion principle can be used to find the number of such functions.
• The well-known hatcheck problem can be solved using the principle of inclusion-exclusion. This problem asks for
the probability that no person is given the correct hat back by a hatcheck person who gives these hats back
randomly.
An onto function from a set with m elements to a set with n elements corresponds to a way to
distribute m elements in the domain to n indistinguishable boxes so that no box is empty, and then to
associate each of the n elements of the codomain to a box.
This means that the number of onto functions from a set with m elements to a set with n elements is
the number of ways to distribute m distinguishable objects to n indistinguishable boxes so that no box
is empty multiplied by the number of permutations of a set with n elements.
• There is an alternative form of the principle of inclusion-exclusion that is useful in
counting problems. In particular, this form can be used to solve problems that ask for the
number of elements in a set that have none of n properties P1, P2,…, Pn.
• Let Ai be the subset containing the elements that have property Pi. The number of
elements with all the properties Pi1 , Pi2 ,…, Pik will be denoted by N (Pi1 ,Pi2 …Pik). Writing
• If the number of elements in the set is denoted by N and the number of elements with
none of the properties P1, P2,…, Pn is denoted by N (P ′1P ′ 2…P ′n), it follows that
1+4+6 3+2+6
2+3+6 3+3+5
2+4+5 3+4+4
P(n,r)=n!/
8.6.4 The Number of Onto Functions
• The principle of inclusion-exclusion can also be used to determine the number of onto
functions from a set with m elements to a set with n elements.
An onto function from a set with m elements to a set with n elements corresponds to a way to distribute m
elements in the domain to n indistinguishable boxes so that no box is empty, and then to associate each of the
n elements of the codomain to a box.
This means that the number of onto functions from a set with m elements to a set with n elements is the
number of ways to distribute m distinguishable objects to n indistinguishable boxes so that no box is empty
multiplied by the number of permutations of a set with n elements.
m n
Thank
You