Professional Documents
Culture Documents
1
Recurrence Relations
2
Recurrence Relations
} Recall Recursive Definitions:
} BASIS STEP: Specify the value of the function at zero, i.e.,: f(0)(first
element in the domain)
} RECURSIVE STEP: Give a rule for finding f(n) based on the value of
f(i) for i < n
3
Rabbits Riddle
} 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.
4
Modeling the Rabbits Population Growth
5
Rabbits Solution
} Solution: Let fn be the the number of pairs of rabbits after n
months
} There are 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 the sequence {fn } satisfies the recurrence
relation fn = fn-1 + fn-2 for n ≥ 3 with the initial conditions f1
= 1 and f2 = 1
} The number of pairs of rabbits on the island after n months is
given by the nth Fibonacci number
6
The Tower of Hanoi
} In the late nineteenth century, the French mathematician Édouard
Lucas invented a puzzle consisting of three pegs on a board with disks
of different sizes
} Initially all of the disks are on the first peg in order of size, with the
largest on the bottom
} 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
7
The Tower of Hanoi (cont.)
} How many moves are needed to move a Tower of Hanoi of
size n from peg 1 to peg 2?
} Solution:
} Let {Hn} denote the number of moves needed
} We need to 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
} Now we can directly transfer the largest disk from peg 1 to peg 2 in 1
move
} Finally, we transfer the n −1 disks from peg 3 to peg 2
using Hn−1 additional moves.
8
The Tower of Hanoi (cont.)
} Now we know that moving a Tower of Hanoi of size n requires
Hn = 2Hn−1 + 1
} We need to find a closed form equation for Hn (i.e doesn’t depend on Hn-1)
} To do that, 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 +22 + 2 + 1
⋮
= 2n-1H1 + 2n−2 + 2n−3 + …. + 2 + 1
= 2n−1 + 2n−2 + 2n−3 + …. + 2 + 1 because H1= 1
= 2n − 1 using the formula for the sum of the n i a(r n+1 −1)
terms of a geometric series ∑ ar =
r −1
,r ≠1
i=0
9
The Tower of Hanoi (cont.)
} There was a myth created with this puzzle
} Monks in a tower in Hanoi are transferring 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
10
Counting Bit Strings
} Find a recurrence relation for the number of bit strings of
length n without two consecutive 0s
11
Counting Bit Strings
} The bit strings of length n ending with 1 without two consecutive 0s are the bit
strings of length n −1 with no two consecutive 0s with a 1 at the end
Hence, there are an−1 such bit strings
} The bit strings of length n ending with 0 without two consecutive 0s are the bit
strings of length n −2 with no two consecutive 0s with 10 at the end
Hence, there are an−2 such bit strings
12
Counting Bit Strings (cont.)
} 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
} For n ≥ 3
} an = an−1 + an−2
} Solution: 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
(You can use the Tree Diagram to reach the same results)
13
Complexity of Recursive Algorithms
14
Complexity of Recursive Algorithms
} Similar to iterative algorithms, we wish to find a way to
compute the time complexity of recursive algorithms
} However, counting the number of operations can be
hard to estimate for recursive algorithms
} To overcome this, express the number of operations as
a recurrence relation, and then solve it to get a closed
form
} Using the iteration method
} Using the Master Theorem
15
Iteration Method
16
Complexity of Recursive Factorial
} Give a recurrence relation for the complexity of the
recursive factorial method
procedure factorial(n: nonnegative integer)
if n = 0 then
return 1
else
return n∙factorial(n − 1)
{output is n!}
17
Complexity Recursive Factorial (cont.)
} We need to solve the following recurrence relation using
the iteration method
T(n) = T(n-1) + 1, and T(0) = 1
T(n) = T(n-1) + 1
= T(n-2) + 2
= T(n-3) + 3
⋮
= T(0) + n
= n +1
T(n) is Q(n)
18
Complexity of Recursive Binary Search
} Give a recurrence relation for the complexity of the recursive binary search
procedure binary search(A: a1,a2,…, an , i, j, x : integers, 1≤ i ≤ j ≤n)
m := ⌊(i + j)/2⌋
if i > j then
return 0
else if x = am then
return m
else if x < am then
return binary search(A, i,m−1,x)
else
return binary search(A, m+1,j,x)
{output is location of x in a1, a2,…,an if it appears, 0 otherwise}
19
Complexity of Recursive Binary Search
(cont.)
} We need to solve the following recurrence relation using the iteration
method
T(n) = T(n/2) + 3, and T(0) = 1
20
Complexity of the Recursive Merge Sort
} Give a recurrence relation for the complexity of the recursive merge
sort
21
Complexity of the Recursive Merge Sort
(cont.)
} What is f(n), i.e., the number of comparisons needed for merge?
} Solution:
} In the worst case, we would have to compare every element in L1 with every
element in L2
} L1 and L2 are of size n/2 each
} So f(n) = n -1 in the worst case (last element won’t need to be compared)
22
Complexity of the Recursive Merge Sort
(cont.)
} We need to solve the following recurrence relation using the iteration method
T(n) = 2T(n/2) + n -1 + 1 = 2T(n/2) + n , and T(1) = 1
} Solution:
T(n) = n + 2T(n/2)
= n + 2(n/2 + 2T(n/4)) T(n/2) = n/2 + 2T(n/4)
= n + n + 4T(n/4)
= n + n + 4(n/4 + 2T(n/8))
= n + n + n + 8T(n/8)
⋮
= in + 2iT(n/2i) Assume: n = 2k à k = lgn
⋮
= kn + 2kT(1)
= nlgn + nT(1)
= nlgn + n
T(n) is Q(nlgn)
23
Common Recurrence Relation Solutions
} T(n) = T(n-1) + 1 T(n) is O(n)
} Solution:
T(n) = n + T(n-1)
= n + (n-1) + T(n-2)
= n + (n-1) + (n-2) + T(n-3)
= n + (n-1) + (n-2) + … + 2 + T(1)
= n(n+1)/2 - 1 + T(1)
= n2 + T(1)
T(n) is Q(n2)
25
Master Theorem
26
The Master Theorem
} The Master Theorem is a “cookbook” recipe that can be
used to solve recurrences of the form: 𝑇(𝑛)=𝑎𝑇(𝑛/𝑏)+𝑓(𝑛)
} such that a ≥ 1, b > 1, and f(n) > 0
} where a is the number of recursive calls,
} b is the number of chunks the original input is split into
} f(n) is the total time needed for operations during the recursive
calls
} Notes:
} The Master Theorem gives an estimate of the solution of the
recurrence relation using asymptotic notations (i.e., big-O
notation)
} The Master Theorem assumes that all recursive calls operate on
equal size subsets of the original input
} The Master Theorem has different variations based on the
function f(n)
27
The Master Theorem
} Let T(n) be an increasing function that satisfies the
recurrence relation 𝑇(𝑛)=𝑎𝑇(𝑛/𝑏)+cnd, and such that:
} a≥1
} b>1
} c>0
} d≥0
} Then
28
Complexity of Recursive Binary Search
} Give a recurrence relation for the complexity of the recursive binary search
and solve it using the Master Theorem
} Solution:
} We know that the recurrence relation for the complexity of binary search
is: T(n) = T(n/2) + 3
} Using the Master Theorem, we have: a = 1, b = 2, c = 3, d = 0
è1 = 20 è a = bd è T(n) is O(lgn)
29
Complexity of the Recursive Merge Sort
} Give a recurrence relation for the complexity of the recursive
merge sort
procedure mergesort(L = a1, a2,…,an )
if n > 1 then
m := ⌊n/2⌋
L1 := a1, a2,…,am
L2 := am+1, am+2,…,an
L := merge(mergesort(L1), mergesort(L2 ))
{L is now sorted into elements in increasing order}
} Solution:
} We know that the recurrence relation for the complexity of merge sort is:
T(n) = 2T(n/2) + n
} Using the Master Theorem, we have: a = 2, b = 2, c = 1, d = 1
è2 = 21 è a = bd è T(n) is O(nlgn)
30
Example
} Use the Master Theorem to solve the recurrence
T(n) = 2T(n/2) + n2
} a = 2, b = 2, c =1, d = 2
} Compare a with bd
i.e.,: 2 with 22
Þ a < bd Þ Case 1
Þ T(n) is O(n2)
31
Example
} Use the Master Theorem to solve the recurrence
T(n) = 2T(n/2) + sqrt(n)
} a = 2, b = 2, c = 1, d = 0.5
} Compare a with bd
i.e.,: 2 with 20.5 = 1.41
Þ a > bd Þ Case 3
Þ logba = log22 =1
Þ T(n) is O(n)
32
Example
} Use the Master Theorem to solve the recurrence
T(n) = 2T(n/2) + 3n2 - 5n + 6lgn - 4
} a = 2, b = 2, c =3, d = 2
} Compare a with bd
i.e: 2 with 22
Þ a < bd Þ Case 1
Þ T(n) is O(n2)
33
Example
} Use the Master Theorem to solve the recurrence
T(n) = 3T(n/4) + nlgn
34
The recursion-tree method
35
Example 1
W(n) = 2W(n/2) + n2
• Total cost:
log4 n 1 i i
3 log4 3 3 1
T ( n) cn 2 n cn 2 n log4 3 cn 2 n log4 3 O(n 2 )
16 16 3
i 0 i 0
1
Þ T(n) = O(n2) 16
38
Any Questions?
39