0% found this document useful (0 votes)
15 views80 pages

Chapter 8

Uploaded by

soovamsg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views80 pages

Chapter 8

Uploaded by

soovamsg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 80

Chapter 08: Advanced

Counting Techniques
Recurrence Relations
Definition: A recurrence relation for the sequence {an}
is an equation that expresses an in terms of one or more
of the previous terms of the sequence, namely, a0, a1, …,
an-1, for all integers n with n ≥ n0, where n0 is a
nonnegative integer.
A sequence is called a solution of a recurrence relation
if its terms satisfy the recurrence relation.
The initial conditions for a sequence specify the terms
that precede the first term where the recurrence
FREE: For Complete Playlist:
relation takes effect. https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Chapter 08: Advanced
Counting Techniques
Recurrence Relations

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Chapter 08: Advanced
Counting Techniques
Recurrence Relations

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
8.1 Applications of Recurrence Relations
Rabbits and the
Fiobonacci Numbers
Example: A young pair of rabbits (one of each
gender) is placed on an island. A pair of rabbits
does not breed until they are 2 months old. After
they are 2 months old, each pair of rabbits
produces another pair each month.
Find a recurrence relation for the number of
pairs of rabbits on the island after n months,
assuming that rabbits never die.

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Rabbits and the Fiobonacci Numbers
(cont.)

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Rabbits and the Fibonacci
Numbers (cont.)
Solution: Let fn be the the number of pairs of rabbits after n
months.
● There are is f1 = 1 pairs of rabbits on the island at the end of the
first month.
●We also have f2 = 1 because the pair does not breed during the
first month.
● To find the number of pairs on the island after n months,
add the number on the island after the previous month,
fn-1, and the number of newborn pairs, which equals fn-2,
because each newborn pair comes from a pair at least
two months old.

Consequently
fn = fn-1 +
for sequence
the n ≥ 3 with{f
the initial conditions f1 = 1 and
n } satisfies the recurrence
relation f2 = 1.
The
fn-2 number of pairs of rabbits on the island after n months
is given by the nth Fibonacci number.

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
The Tower of
Hanoi
Rules: You are allowed to move the disks one at a
time from one peg to another as long as a larger
disk is never placed on a smaller.
Goal: Using allowable moves, end up with all the
disks on the second peg in order of size with
largest on the bottom.

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
The Tower of Hanoi
(continued)
Solution: Let {H } denote the number of moves needed to solve the
n
Tower of Hanoi Puzzle with n disks. Set up a recurrence relation
for the sequence {Hn}. Begin with n disks on peg 1. We can transfer the
top n −1 disks, following the rules of the puzzle, to peg 3 using Hn−1
moves.

First, we use 1 move to transfer the largest disk to the second peg.
Then we transfer the n −1
disks from peg 3 to peg 2 using Hn−1 additional moves. This can not
be done in fewer steps.
Hence,
Hn = 2Hn−1 + 1.
The initial condition is H 1 = 1 since a single disk can be transferred from peg 1 to
peg 2 in one move.
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
The Tower of Hanoi (continued)
●We can use an iterative approach to solve this recurrence relation by
repeatedly expressing Hn in terms of the previous terms of the sequence.
Hn = 2Hn−1 + 1
= 2(2Hn−2 + 1) + 1 = 22 Hn−2 +2 + 1
= 22(2Hn−3 + 1) + 2 + 1 = 23 Hn−3 +2 2 + 2 + 1

= 2n-1
1
H + 2n−2 + 2n−3 + …. + 2 + 1
= 2n−1 + 2n−2 + 2n−3 + …. + 2 +1 1 because H = 1
= a2myth
● There was − 1 created with
n
usingthe
thepuzzle.
formula for the
Monks in sum of the
a tower terms are
in Hanoi of atransferring
geometric series
64
gold disks from one peg to another following the rules of the puzzle. They move one disk
each day. When the puzzle is finished, the world will end.
Using this formula for the 64 gold disks of the myth,
264 −1 = 18,446, 744,073, 709,551,615
days are needed to solve the puzzle, which is more than 500 billion years.
● Reve’s puzzle (proposed in 1907 by Henry Dudeney) is similar but has 4 pegs. There is a well-
known unsettled conjecture for the the minimum number of moves needed to solve this
puzzle. (see Exercises 38-45)
Counting Bit
Strings
Example 3: Find a recurrence relation and give initial
conditions for the number of bit strings of length n without two
consecutive 0s. How many such bit strings are there of length
five?

Solution: Let an denote the number of bit strings of length n


without two consecutive 0s. To obtain a recurrence relation for
{an } note that the number of bit strings of length n that do not
have two consecutive 0s is the number of bit strings ending
with a 0 plus the number of such bit strings ending with a 1.

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Bit Strings
(continued)
The initial conditions
are:
a1 = 2, since both the bit strings 0 and 1 do not have consecutive 0s.
a2 = 3, since the bit strings 01, 10, and 11 do not have consecutive 0s, while 00 does.
To obtain a5 , we use the recurrence relation three times to find that:

a3 = a2 + a1 = 3 + 2 = 5
a4 = a3 + a2 = 5+ 3 = 8
a5 = a4 + a3 = 8+ 5 = 13

Note that {an } satisfies the same recurrence relation as


the Fibonacci sequence. Since a1 = f3 and a2 = f4 , we
conclude that an = fn+2 .

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Counting the Ways to Parenthesize a Product

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Algorithms and Recurrence Relations Recurrence
relations play an important role in many aspects of the study of
algorithms and their complexity. In Section 8.3, we will show
how recurrence relations can be used to analyze the
complexity of divide-and-conquer algorithms, such as the
merge sort algorithm introduced in Section 5.4. As we will see
in Section 8.3, divide-and-conquer algorithms recursively
divide a problem into a fixed number of non-overlapping
subproblems until they become simple enough to solve
directly. We conclude this section by introducing another
algorithmic paradigm known as dynamic programming, which
can be used to solve many optimization problems efficiently

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Solving
Linear
Recurrence
Relations
Section: 8.2
Section 8.2

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Linear Homogeneous Recurrence Relations

• It is linear because the right-hand side is a sum of the previous terms of the
sequence each multiplied by a function of n.

• It is homogeneous because no terms occur that are not multiples of the ajs. Each
coefficient constant.

• The degree is k because an is expressed in terms of the previous k terms of the


sequence.

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Example Linear
Homogeneous Recurrence
Relations
●Pn = (1.11)Pn-1 linear homogeneous
recurrence relation of degree one
linear homogeneous
●fn = fn-1 + recurrence
relation of degree two
fn-2
● not linear
●Hn = 2Hn−1 + 1 not homogeneous

●Bn = nBn−1 coefficients are not


constants
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Solving Linear Homogeneous
Recurrence Relations

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
THEOREM 1

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Second Order R.R Examples using
Theorem 1

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
An Explicit Formula for the Fibonacci
Numbers
We can use Theorem 1 to find an explicit formula
for the Fibonacci numbers. The sequence of
satisfies the
Fibonacci recurrence relation
numbers + fn−2 with
fn =
initial
the conditions: f0 = 0 and f1 = 1.
fn−1

Solution: The roots of the characteristic


equation
r2 – r – 1 = 0 are

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Fibonacci Numbers
(continued)
Therefore by Theorem 1

for some constants α1 and α2.


Using the initial conditions f0 = 0 and f1 = 1 ,
we have

Solving, we , .
obtain Hence,
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
THEOREM 2

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Using
Theorem 2

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
THEOREM 3

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Using
Theorem 3

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
First Order R.R Examples

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Theorem 4

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Using
Theorem 4
Linear Nonhomogeneous Recurrence
Relations
with Constant Coefficients
We have seen how to solve linear homogeneous recurrence relations with constant
coefficients. Is there a relatively simple technique for solving a linear, but not homogeneous,
recurrence relation with constant coefficients, such as ? We will see that
the answer is yes for certain families of such recurrence relations. The recurrence relation
is an example of a linear nonhomogeneous recurrence relation with
constant coefficients, that is, a recurrence relation of the form

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Theorem 5

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Using
Theorem 5

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Linear Nonhomogeneous Recurrence
Relations with Constant Coefficients
(cont.)
The following are linear nonhomogeneous recurrence
relations with constant coefficients:
n
an = an−1 + 2 ,
2
an = an−1 + an−2 + n + n + 1,
n
an = 3an−1 + n3 ,
an = an−1 + an−2 + an−3 + n!
where the following are the associated linear homogeneous
recurrence relations, respectively:
an = an−1 ,

an = an−1 +

an−2, a =
n
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
3an−1 ,For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Divide-and-Conquer
Algorithms and
Recurrence Relations
Section 8.3
Section 8.3
Divide-and-Conquer Algorithmic Paradigm
Definition: A divide-and-conquer algorithm
works by first dividing a problem into one or
more instances of the same problem of smaller
size and then conquering the problem using the
solutions of the smaller problems to find a
solution of the original problem.

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Divide-and-Conquer Recurrence Relation

●Suppose that a recursive algorithm divides a


problem of size n into a subproblems.
●Assume each subproblem is of size n/b.
●Suppose g(n) extra operations are
needed in the conquer step.
●Then f(n) represents the number of operations
to solve a problem of size n satisisfies the
following recurrence relation:
f(n) = af(n/b) + g(n)
●This is called a divide-and-conquer recurrence
relation.

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Example: Binary
Search
●Binary search reduces the search for an element in a
sequence of size n to the search in a sequence of
size n/2. Two comparisons are needed to
implement this reduction;
● one to decide whether to search the upper or
lower half of the sequence and
● the other to determine if the sequence has
elements.
●Hence, if f(n) is the number of comparisons required to
search for an element in a sequence of size n,
then

f(n) = f(n/2) + 2
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Example: Merge
Sort
●The merge sort algorithm splits a list of n
(assuming n is even) items to be sorted into two
lists with n/2 items. It uses fewer than n
comparisons to merge the two sorted lists.

●Hence, the number of comparisons required


to sort a sequence of size n, is no
more than than M(n) where

M(n) = 2M(n/2) + n.

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Estimating the Size of Divide-and-
Conquer Functions
Theorem 1: Let f be an increasing function
that satisfies the recurrence relation
f(n) = af(n/b) + cnd
whenever n is divisible by b, where a≥ 1, b is an
integer greater than 1, and c is a positive real
number. Then

Furthermore, when n = bk and a ≠1,


where k is a positive integer,

where
FREE: For C1 =
Complete Playlist: f(1)https://www.youtube.com/c/FahadHussaintutorial/playlists
+ c/(a−1) and C1 =
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
−c/(a−1).
Complexity of Binary
Search
Binary Search Example: Give a big-O
estimate for the number of comparisons used
by a binary search.

Solution: Since the number of comparisons


used by binary search is f(n) = f(n/2) + 2
where n is even, by Theorem 1, it follows that f(n) is O(log
n).

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Estimating the Size of Divide-and-
conquer Functions (continued)
Theorem 2. Master Theorem: Let f be an
increasing function that satisfies the
recurrence relation
f(n) = af(n/b) + cnd
whenever n = bk, where k is a positive
integer greater than 1, and c and d are real
numbers with c positive and d nonnegative.
Then

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Complexity of Merge Sort
Merge Sort Example: Give a big-O estimate for the
number of comparisons used by merge sort.
Solution: Since the number of comparisons used by
merge sort to sort a list of n elements is less than M(n)
where M(n) = 2M(n/2) + n, by the master theorem M(n) is
O(n log n).
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Complexity of Fast Integer
Multiplication Algorithm
Integer Multiplication Example: Give a big-O
estimate for the number of bit operations used
needed to multiply two n-bit integers using the fast
multiplication algorithm.

Solution: We have shown that f(n) = 3f(n/2) + Cn,


when n is even, where f(n) is the number of
bit operations needed to multiply two n-bit
integers. Hence by the master theorem with a
= 3, b = 2, c = C, and d = 0 (so that we have
the case where a > bd), it follows that f(n) is
O(nlog 3).
Note that log 3 ≈ 1.6. Therefore the fast multiplication
algorithm is a substantial improvement over the
conventional
FREE: algorithm
For Complete Playlist: that uses O(n2) bit operations.
https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Generating
Functions
8.4
Section 8.4
Generating
Functions
Definition: The generating function for the
sequence a0 , a1 ,
…, ak, … of real numbers is the infinite series

Examples:
● The sequence {ak} with ak = 3 has the generating
function
● The sequence {ak} with ak = k + 1 has the
generating function has the
generating function
●The sequence {ak} with ak = 2k has the
FREE:generating
For Complete Playlist:
function https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Generating Functions for
Finite Sequences
●Generating functions for finite sequences of
real numbers can be defined by extending a
finite sequence a0,a1, … , an into an infinite
sequence by setting
an+1 = 0, an+2 = 0, and so on.

●The generating function G(x) of this infinite


sequence
{an} is a polynomial of degree n because no
terms of the
form ajxj with j > n occur, that is,
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
n
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Generating Functions for Finite
Sequences (continued)
Example: What is the generating
function for the sequence 1,1,1,1,1,1?

Solution: The generating function of


1,1,1,1,1,1 is 1 + x + x2 + x3 + x4 + x5.
By Theorem 1 of Section 2.4, we
have (x6 − 1)/(x −1) = 1 + x + x2 + x3
+ x4 + x5
when x ≠ 1.
Consequently G(x) = (x6 − 1)/(x −1) is the
generating function of the sequence.
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Useful Facts About Power
Series
When generating functions are used to solve counting problems, they are usually
considered to be formal power series. Questions about the convergence of these
series are ignored. However, to apply some results from calculus, it is sometimes
important to consider for which x the power series converges. The fact that a
function has a unique power series around x = 0 will also be important.

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
THEOREM 1

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
DEFINITION 2

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
THEOREM 2
Counting Problems and
Generating Functions

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Counting Problems and
Generating Functions
Example: 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.

Solution: The number of solutions is the coefficient of


x17 in the expansion of

(x2 + x3 + x4 + x5) (x3 + x4 + x5 + x6) (x4 + x5 + x6 + x7).


This follows because a term equal to is obtained in
the product by picking a term in the first sum xe , a 1

term in the second sum xe , and a term in the third


2

sum xe , where
3 e1 + e2 + e3 = 17.

There are three solutions since the coefficient of x17 in the product is
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
3. For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Counting Problems and
Generating Functions

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Counting Problems and
Generating Functions
Example: Use generating functions to find the number of k-
combinations of a set with n elements, i.e., C(n,k).
Solution: Each of the n elements in the set contributes the term
(1 + x) to the generating function

Hence f(x) = (1 + x)n where f(x) is the generating function for


{ak}, where ak
represents the number of k-combinations of a set with n
elements.
By the binomial theorem, we have

where

Henc
e,

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Using Generating Functions to
Solve Recurrence Relations
We can find the solution to a recurrence relation and its initial conditions by finding an explicit
formula for the associated generating function.
8.5 Inclusion-
Exclusion
Introduction
In combinatorics, a branch of mathematics,Section 8.5
the inclusion–
exclusion principle is a counting technique which generalizes
the familiar method of obtaining the number of elements in
the union of two finite sets; symbolically expressed as

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
T wo Finite
Sets

Example: 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 the
class? Solution: |AB| = |A| + |B| −|AB|
Three Finite
Sets

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Three Finite Sets
(Continued)
Example: A total of 1232 students have taken a course in Spanish, 879
have taken a course in French, and 114 have taken a course in Russian.
Further, 103 have taken courses in both Spanish and French, 23 have
taken courses in both Spanish and Russian, and 14 have taken courses
in both French and Russian. If 2092 students have taken a course in at
least one of Spanish French and Russian, how many students have
taken a course in all 3 languages.

Solution: Let S be the set of students who have taken a course in


Spanish, F the set of students who have taken a course in French, and
R the set of students who have taken a course in Russian. Then, we
have

|S| = 1232, |F| = 879, |R| = 114, |SF| = 103, |SR| = 23, |FR| = 14, and
|SFR| = 23.

Using the equation


|SFR| = |S|+ |F|+ |R| − |SF| − |SR| − |FR| + |SFR|,
we obtain 2092 = 1232 + 879 + 114 −103 −23 −14 + |SFR|.
FREE: ForSolving
Completefor
Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
|SFR| yields 7.
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Illustration of Three Finite Set
Example

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
The Principle of Inclusion-
Exclusion
Theorem 1. The Principle of Inclusion-
Exclusion: Let A1, A2, …, An be finite sets.
Then:

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
The Principle of Inclusion-
Exclusion (continued)
Proof: An element in the union is counted
exactly once in the right-hand side of the
equation. Consider an element a that is a
member of r of the sets A1,…., An
where 1≤ r ≤ n.
● It is counted C(r,1) times by Σ|A | i

● It is counted C(r,2) times by Σ|A ⋂A |i j

● In general, it is counted C(r,m) times by the


summation of m of the sets Ai.
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
The Principle of Inclusion-
Exclusion (cont)
● Thus the element is counted exactly
C(r,1) − C(r,2) + C ( r , 3 ) − …⋯ + (−1)r+ 1 C(r,r)
times by the right hand side of the equation.
● By Corollary 2 of Section 6.4, we have
C(r,0) − C(r,1) + C (r,2) − …⋯ + (−1)r C(r,r) = 0.
● Hence,
1= C ( r , 0 ) = C ( r , 1 ) − C(r,2) + …⋯ +
(−1)r+ 1 C(r,r).

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
The Principle of Inclusion-
Exclusion (cont)

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Applications
of Inclusion-
Exclusion
8.5
Section 8.6
An Alternative Form of Inclusion–
Exclusion

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
An Alternative Form of Inclusion–
Exclusion
How many positive integer not exceeding 500 are NOT even or divisible by 3

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
The Number of Onto
Functions
Example: How many onto functions are there from a set with six elements to a
set with three elements?

Solution: Suppose that the elements in the codomain are b1, b2, and b3. Let P1, P2,
and P3 be the properties that b1, b2, and b3 are not in the range of the function,
respectively. The function is onto if none of the properties P1, P2, and P3 hold.

By the inclusion-exclusion principle the number of onto functions from a set with
six elements to a set with three elements is

N − [N(P1) + N(P2) + N(P3)] +


[N(P1P2) + N(P1P3) + N(P2P3)] − N(P1P2P3)

● Here the total number of functions from a set with six elements to one with three elements is N = 36.
● The number of functions that do not have in the range is N(P1) = 26. Similarly, N(P2) = N(31) = 26 .
● Note that N(P1P2) = N(P1P3) = N(P2P3) = 1 and N(P1P2P3)= 0.

Hence, the number of onto functions from a set with six elements to a set with three elements

is: 36 − 3· 26 + 3 = 729 − 192 + 3 = 540


FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
The Sieve of Eratosthenes

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
The Number of Onto Functions
(continued)
Theorem 1: Let m and n be positive integers
with
m ≥ n. Then there are

onto functions from a set with m elements to a set with n


elements.

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Theorem 1, based Exercise
How many ways are there to assign five different jobs to four
different employees if every employee is assigned at least one job?

Solution: Consider the assignment of jobs as a function from the set


of five jobs to the set of four employees. An assignment where every
employee gets at least one job is the same as an onto function from
the set of jobs to the set of employees. Hence, by Theorem 1 it
follows that there are

ways to assign the jobs so that each employee is assigned at least


one job.

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Derangements
Definition: A derangement is a
permutation of objects that leaves no object
in the original position.

The Hatcheck Problem A new employee


checks the hats of n people at a restaurant,
forgetting to put claim check numbers on the
hats. When customers return for their hats,
the checker gives them back hats chosen at
random from the remaining hats. What is the
probability that no one receives the correct
FREE:hat?
For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Derangements
(continued)
Theorem 2: The number of derangements
of a set with n elements is

Let Ai be the number of permutations that number i is in position i. Number


of dearrangements is n!-|A1  A2  A3  A4…..  An|
Then |Ai|=(n-1)!,
|Ai ⋂ Aj|=(n-2)!
|Ai1⋂ Ai2⋂ Ai3……⋂ Aik|=(n-k)!

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Derangements
(continued)
The Hatcheck Problem: A new employee checks the
hats of n people at restaurant, forgetting to put
claim check numbers on the hats. When customers
return for their hats, the checker gives them back
hats chosen at random from the remaining hats.
What is the probability that no one receives the
correct hat.
Solution: The answer is the number of ways the hats
can be arranged so that there is no hat in its original
position divided by n!, the number of permutations of
n hats.
Remark: It can be
shown that the
probability of a
derangement
approaches 1/e as n
grows without bound.

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/

You might also like