DSA class 6
DSA class 6
L1.4
Example-1: Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
T(n)
L1.5
Example-1: Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn
T(n/2) T(n/2)
L1.6
Example-1: Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn
cn/2 cn/2
L1.7
Example-1: Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn
cn/2 cn/2
Θ(1)
L1.8
Example-1: Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn
cn/2 cn/2
h = lg n
cn/4 cn/4 cn/4 cn/4
Θ(1)
L1.9
Example-1: Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn cn
cn/2 cn/2
h = lg n
cn/4 cn/4 cn/4 cn/4
Θ(1)
L1.10
Example-1: Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn cn
cn/2 cn/2 cn
h = lg n
cn/4 cn/4 cn/4 cn/4
Θ(1)
L1.11
Example-1: Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn cn
cn/2 cn/2 cn
h = lg n
cn/4 cn/4 cn/4 cn/4 cn
…
Θ(1)
L1.12
Example-1: Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn cn
cn/2 cn/2 cn
h = lg n
cn/4 cn/4 cn/4 cn/4 cn
…
Θ(1) #leaves = n Θ(n)
L1.13
Example-1: Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn cn
cn/2 cn/2 cn
h = lg n
cn/4 cn/4 cn/4 cn/4 cn
…
Θ(1) #leaves = n Θ(n)
Total = Θ(n lg n)
L1.14
Example-2: Recursion tree
Solve T (N ) = 3T (N/4) + cn2, where c > 0 is constant.
L1.15
Example-2: Recursion tree
Solve T (N ) = 3T (N/4) + cn2, where c > 0 is constant.
16
Example-2: Recursion tree
Solve T (N ) = 3T (N/4) + cn2, where c > 0 is constant.
17
The master method for solving recurrences
• The master method provides a “cookbook” method for solving recurrences of the
form
• where a>=1 and b>1 are constants and f (n) is an asymptotically positive
function.
• Then, T(n) has following asymptotic bounds according to Master Theorem.
• Example-1: consider
• For this recurrence, we have a a=9, b=3, f (n) = n, and thus we have
that Since we can apply case 1 of the
master theorem and conclude that the solution is T(n) = Ө (n)
• Example-2: Consider