Algo Lecture8 SolvingRecurrence
Algo Lecture8 SolvingRecurrence
Tanjina Helaly
RECURRENCE
Recurrences are a major tool for analysis of
algorithms
Divide and Conquer algorithms which are
analyzable by recurrences.
RECURRENCES AND RUNNING TIME
An equation or inequality that describes a
function in terms of its value on smaller inputs.
T(n) = T(n-1) + n
4
METHODS FOR SOLVING RECURRENCES
Iteration method
Substitution method
Master method
5
ITERATION METHOD
THE ITERATION METHOD
Convert the recurrence into a summation and try
to bound it using known series
Iterate the recurrence until the initial condition is
reached.
Use back-substitution to express the recurrence in
terms of n and the initial (boundary) condition.
7
THE ITERATION METHOD
T(n) = c + T(n/2)
T(n) = n + 2T(n/2)
T(n/2) = n/2 + 2T(n/4)
= 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)
Assume 2 k
= in + 2iT(n/2i) n/2k = 1 => k = log n
= kn + 2kT(1)
= nlgn + nT(1) = Θ(nlgn) [T(1) = constant]
SUBSTITUTION METHOD
THE SUBSTITUTION METHOD
Guess a bound/solution
T(n)
EXAMPLE – MERGE SORT CONT…
T(n) = 2T(n/2) + cn
cn
T(n/2) T(n/2)
EXAMPLE – MERGE SORT CONT…
T(n) = 2T(n/2) + cn
cn/2 cn/2
cn
cn/2 cn/2
# of level=log n+1
……..
c c c c c c c c
n leaves
EXAMPLE – MERGE SORT CONT…
T(n) = 2T(n/2) + cn Cost for each node = cn/2i .
Cost at each level = 2i * cn/2i
i=0 cn/2i = cn
# of level=log n+1
……..
c c c c c c c c At last level, n/2i = 1
So, 2i = n
n leaves => i = log n
EXAMPLE – MERGE SORT CONT…
T(n) = 2T(n/2) + c Cost
-----
cn cn
cn/2 cn/2 cn
# of level=log n+1
……..
c c c c c c c c cn
n log 4 3
(n log 4 3
)
RECURSION TREE – EXAMPLE 4 CONT…
3 3 3
T (n) cn cn ( ) cn ..... ( )
2 2 2 2 log 4 n 1
cn (n
2 log 4 3
)
16 16 16
3
log 4 n 1
( ) cn (n ) i 2 log 4 3
i 0
16
3
( ) cn (n ) i 2 log 4 3
i 0
16
1
cn (n ) 2 log 4 3
1 3 / 16
16
cn (n ) 2 log 4 3
13
O( n ) 2
RECURSION TREE – EXAMPLE 4 CONT…
Or solve the following way
3 2 3 2 2 3 log4 n1 2
T (n) cn cn ( ) cn ..... ( )
2
cn (nlog4 3 )
16 16 16
log 4 n 1
3
( )i cn 2 (nlog4 3 )
i 0 16
1 (3 / 16)log n 2
cn (nlog4 3 )
1 3 / 16
16 2
cn (nlog4 3 ) [as n , (3 / 16)log n 0]
13
(n 2 )
MASTER’S METHOD
MASTER’S METHOD
“Cookbook” for solving recurrences of the form:
n
T (n) aT f (n)
b
where, a ≥ 1, b > 1, and f(n) > 0
n
T (n) aT f (n)
b
where, a ≥ 1, b > 1, and f(n) > 0
Case 1: if f(n) = O(nlogba -) for some > 0, then: T(n) = (nlogba)
Case 2: if f(n) = (nlogba), then: T(n) = (nlogba lgn)
Case 3: if f(n) = (nlogba +) for some > 0, and if
af(n/b) ≤ cf(n) for some c < 1 and all sufficiently large n,
then:
regularity condition T(n) = (f(n))
WHY NLOGBA?
n
T (n) aT
b
n
a 2T 2
b
n
a 3T 3
b
n
T (n) a iT i i
b
Assume n = bk k = logbn
At the end of iteration i = k:
i
b
T (n) a T i a logb nT (1) a logb n nlogb a
logb n
b
MASTER’S METHOD – EXAMPLE 1
T(n) = 2T(n/2) + n
a = 2, b = 2, log22 = 1
T(n) = (nlgn)
MASTER’S METHOD – EXAMPLE 2
T(n) = 2T(n/2) + n2
a = 2, b = 2, log22 = 1
Compare n with f(n) = n2
f(n) = (n1+) Case 3 verify regularity cond.
a f(n/b) ≤ c f(n)
2 n2/4 ≤ c n2 c = ½ is a solution (c<1)
T(n) = (n2)
MASTER’S METHOD – EXAMPLE 3
REFERENCE
Chapter 4 (Cormen)
https://www.cse.unr.edu/~bebis/CS477/Lect/Recurr
ences.ppt
https://courses.csail.mit.edu/6.046/spring04/lectures
/l2.ppt
https://www.cs.cornell.edu/courses/cs3110/2012sp/lectu
res/lec20-master/lec20.html