0% found this document useful (0 votes)
8 views24 pages

Week 04 (Reccurence02)

Reccurence 02

Uploaded by

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

Week 04 (Reccurence02)

Reccurence 02

Uploaded by

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

Lecture Title: Recursion (Cont.

Course Code: CSC 2211 Course Title: Algorithms

Dept. of Computer Science


Faculty of Science and Technology

Lecture No: 04 Week No: 04 Semester:

Lecturer: Name & email:


Recurrences
&
Master Method

MD. MANZURUL HASAN

SLIDES ARE ADOPTED FROM MASHIOUR RAHMAN [ASSO DEAN, FST]


Lecture Outline

1. Divide and Conquer (Previous Week)


2. Recurrences in Divide and Conquer and methodologies
for recurrence Solutions (Previous Week)
3. Repeated Backward Substitution Method (Previous Week)
4. Substitution Method (Previous Week)
5. Recursion Tree
6. Master Method
Recursion Tree
 A recursion tree is a convenient way to visualize
what happens when a recurrence is iterated.
 Good for ”guessing” asymtotic solutions to recurrences

T (n) = T (n / 4) + T (n / 2) + n 2
Height and leaves of a recursion
tree
• Height
The top of the tree begins with n and for every step down it is divided by b. So it
goes n, n/b, n/b2,...,1. To find the height we need to find a k such that n / bk = 1
or bk = n, which gives k = logbn.
Verify our guess [substitution]
Master Method
 The idea is to solve a class of recurrences that have the
form
T(n) = aT(n/b) + f(n)
 Assumptions: a  1 and b > 1, and f(n) is asymptotically
positive.
 Abstractly speaking, T(n) is the runtime for an algorithm
and we know that
 a number of subproblems of size n/b are solved recursively, each
in time T(n/b).
 f(n) is the cost of dividing the problem and combining the results.
e.g., In merge-sort .
T (n) = 2T (n / 2) + (n)
…Master Method

alogb n

alogb n

alogb n

Split problem into a parts. There are


logbn levels. There are a logb n = nlogb a leaves.
…Master Method
 Iterating the recurrence (expanding the tree) yields
T(n) = f(n) + aT(n/b)
= f(n) + af(n/b) + a2T(n/b2)
= f(n) + af(n/b) + a2f(n/b2) + …
alogbn-1f(n/blogbn-1) + alogbnT(1)
T(n) =logb n −1 j
 j= 0
a f(n / b j
) + (n logb a
)
 The first term is a division/recombination cost (totaled
across all levels of the tree).
 The second term is the cost of doing all subproblems of
size 1 (total of all work pushed to leaves).
Master Method, Intuition
 Three common cases:

1. Running time dominated by cost at leaves.

2. Running time evenly distributed throughout the tree.

3. Running time dominated by cost at the root.

 To solve the recurrence, we need to identify the


dominant term.

 In each case compare with


Master Method, Case 1

 If f (n) = O (n logb a − ) for some constant  0 then


logb a 
 f(n) grows polynomially slower than n (by factor n ).
 The work at the leaf level dominates

Cost of all the leaves


Master Method, Case 2

 If f (n) = (nlogb a ) then


 f ( n) and
n logb a are asymptotically the same

 The work is distributed equally throughout the tree

(level cost)(number of levels)


Master Method, Case 3

log b a +
 If f ( n ) = ( n ) for some constant  0 then
 Inverse of Case 1
logb a
 f(n) grows polynomially faster than n
 Also need a “regularity” condition
c  1 and n0  0 such that af (n / b)  cf (n) n  n0
 The work at the root dominates

division/recombination cost
Master Theorem, Summarized

Given: recurrence of the form

(
1. f (n) = O nlogb a − )
(
 T (n) =  nlogb a )
(
2. f (n) =  nlogb a )
(
 T (n) =  nlogb a lg n )
( )
3. f (n) =  nlogb a + and af (n / b)  cf (n), for some c  1, n  n0
 T ( n) =  ( f ( n) )
Strategy
1. Extract a, b, and f(n) from a given recurrence
2. Determine nlogb a
3. Compare f(n) and nlogb a asymptotically
4. Determine appropriate MT case and apply it

Merge sort: T(n) = 2T(n/2) + (n)


1. a=2, b=2, f(n) = (n)
2. nlog22 = n
3. (n) = (n)
➔ Case 2: T(n) = (nlogbalog n) = (n log n)
Examples of Master Method

BinarySearch(A, l, r, q):
m := (l+r)/2
if A[m]=q then return m
else if A[m]>q then
BinarySearch(A, l, m-1, q)
else BinarySearch(A, m+1, r, q)

T(n) = T(n/2) + 1
1. a=1, b=2, f(n) = 1
2. nlog21 = 1
3. 1 = (1)
➔ Case 2: T(n) = (nlogbalog n) =(log n)
...Examples of Master
Method
T(n) = 9T(n/3) + n
1. a=9, b=3, f(n) = n
2. nlog39 = n2
3. n = (nlog39 - ) with  = 1
➔ Case 1: T(n) = (n2)
...Examples of Master
Method
T(n) = 3T(n/4) + n log n
1. a=3, b=4, f(n) = n log n

2. nlog43 = n0.792

3. n log n = (nlog43 + ) with  = 0.208

➔ Case 3:

regularity condition: af(n/b) <= cf(n)


af(n/b) = 3(n/4)log(n/4) <= (3/4)nlog n = cf(n)
;with c=3/4
T(n) = (n log n)
 T(n) = 8T(n/2) + n^3
References & Readings

 CLRS
 Chapter: 4 (4.1-4.3)
 4.4 for bedtime reading
 Exercises
♦ 4.1, 4.2, 4.3
 Problems
♦ 4-1, 4-4
 HSR
 Chapter: 3 (3.1-3.6)
 Examples: 3.1-3.5
 Exercises: 3.1 (1, 2), 3.2 (1, 3-6),

You might also like