Chapter 8
Chapter 8
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
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.
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?
a3 = a2 + a1 = 3 + 2 = 5
a4 = a3 + a2 = 5+ 3 = 8
a5 = a4 + a3 = 8+ 5 = 13
• 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.
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
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.
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.
M(n) = 2M(n/2) + n.
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.
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.
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
where
Henc
e,
|S| = 1232, |F| = 879, |R| = 114, |SF| = 103, |SR| = 23, |FR| = 14, and
|SFR| = 23.
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
● 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