0% found this document useful (0 votes)
23 views

Module 1-Lecture4

The document discusses solving recurrence relations using the recursion tree method and the master's theorem. It provides examples of using recursion trees to solve recurrence relations of the form T(n) = aT(n/b) + f(n). It then introduces the master's theorem, which provides a general formula to solve recurrences of this form based on comparing values of a, b, and f(n). The document outlines the three cases of the master's theorem and provides examples of determining the runtime of functions using each case.

Uploaded by

Vivek Garg
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Module 1-Lecture4

The document discusses solving recurrence relations using the recursion tree method and the master's theorem. It provides examples of using recursion trees to solve recurrence relations of the form T(n) = aT(n/b) + f(n). It then introduces the master's theorem, which provides a general formula to solve recurrences of this form based on comparing values of a, b, and f(n). The document outlines the three cases of the master's theorem and provides examples of determining the runtime of functions using each case.

Uploaded by

Vivek Garg
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Algorithms and Problem Solving (15B11CI411)

EVEN 2022

Module 1: Lecture 4

Jaypee Institute of Information Technology (JIIT)


A-10, Sector 62, Noida
Solving Recurrence relation using Recursion Tree method

Recursion Tree - A visual representation of recursive call hierarchy where each node
represents the cost of a single subproblem

2
T(n) = 2T(n/2) + c

• Size of problem at last level = 1 = n/2i


• i = log2(n)
• No. of levels = 1+ log2(n)
• No. of nodes at level 0 = 20 = 1
• No. of nodes at level 1 = 21 = 2
• …
• No. of nodes at level log2(n) = 2^log2(n) = n^log2(2) = n
• Cost of sub problems at last level = nxT(1) = cn
T(n) = c + 2c + 4c + —- (no. of levels-1) times + last level cost
= c + 2c + 4c + —- log2(n) times + Θ(n)
= c(1 + 2 + 4 + —- log2(n) times) + Θ(n) \\ 20 + 21 + 22 + —– + 2k = 2k+1-1 (k=log2(n)-1)
= c(n) + Θ(n)
• T(n) = Θ(n)

3
T(n) = 2T(n/2) + cn

• Size of problem at last level = 1 = n/2i


• i = log2(n)
• No. of levels = 1+ log2(n)
• No. of nodes at level 0 = 20 = 1
• No. of nodes at level 1 = 21 = 2
• …
• No. of nodes at level log2(n) = 2^log2(n) = n^log2(2) = n
• Cost of sub problems at last level = cn
T(n) = cn + cn + cn + —- (no. of levels-1) times + last level cost
= cn + cn + cn + —- log2(n) times + Θ(n)
= cnlog2(n) + Θ(n)
• T(n) = Θ(nlog2n)
T(n) = 4T(n/2) + n

• Size of problem at last level = 1 = n/2i


• i = log2(n)
• No. of levels = 1+ log2(n)
• No. of nodes at level 0 = 20 = 1
• No. of nodes at level 1 = 21 = 2
• …
• No. of nodes at level log2(n) = 2^log2(n) = n^log2(2) = n
• Cost of sub problems at last level = cn
T(n) = n + 2n + 4n + —- (no. of levels-1) times + last level cost
= n(1+2+4 + —- log2(n) times) + Θ(n)
= n*n + Θ(n)
• T(n) = Θ(n2)
T(n) = T(n/10) + T(9n/10) + n

• To find no. of levels: Consider the longest path from root to leaf
• (9/10)0n –> (9/10)1n –> (9/10)2n –> ……… –> (9/10)in
• Size of problem at last level = 1 = (9/10)i
• i = log10/9(n)
• No. of levels = 1+ log10/9(n)
• No. of nodes at level 0 = 20 = 1
• No. of nodes at level 1 = 21 = 2
• …
• No. of nodes at level log2(n) = 2^log10/9(n) = n^log10/9(2)
• Cost of sub problems at last level = n^log10/9(2)
T(n) = n + n + n + —- (no. of levels-1) times + last level cost
= n + n + n + —- log10/9(n) times + Θ(n^log10/9(2))
= cnlog10/9(n) + Θ(n^log10/9(2))
• T(n) = Θ(nlog10/9n)
Master Theorem
• General formula that works if recurrence has the form T(n) = aT(n/b) + f(n)
• a is number of sub-problems
• n/b is size of each sub-problem
• f(n) is cost of non-recursive part

7
Master Theorem

8
Case-1

9
10
The master theorem cannot be used if:

• T(n) is not monotone. eg. T(n) = sin n


• f(n) is not a polynomial. eg. f(n) = 2n
• a is not a constant. eg. a = 2n
• a<1

11
Examples on Masters Theorem
13
• Recurrence relation for mater’s method can also be represented as
Where, a >= 1, b > 1, k >= 0 and p is a real number

Case-01: Case-03:
• If a > bk, then T(n) = θ (nlogba) • If a < bk and
Case-02: • If p < 0, then T(n) = O (nk)
• If a = bk and • If p >= 0, then T(n) = θ (nklogpn)
• If p < -1, then T(n) = θ (nlogba)
• If p = -1, then T(n) = θ (nlogba.log2n)
• If p > -1, then T(n) = θ (nlogba.logp+1n)

14
T(n) = 3T(n/2) + n2 T(n) = 2T(n/2) + nlogn
• a = 3, b = 2, k = 2, p = 0 • a = 2, b = 2, k = 1, p = 1
• Now, a = 3 and bk = 22 = 4. • Now, a = 2 and bk = 21 = 2.
• Clearly, a < bk. • Clearly, a = bk.
• So, we follow case-03. • So, we follow case-02.
• Since p = 0, so we have- • Since p = 1, so we have-
• T(n) = θ (nklogpn) = θ (n2log0n) = θ (n2) • T(n) = θ (nlogba.logp+1n) = θ (nlog22.log1+1n)
= θ (nlog2n)

15
T(n) = √2T(n/2) + logn T(n) = 8T(n/4) – n2logn
• a = √2, b = 2, k = 0, p = 1 • The given recurrence relation does not
• Now, a = √2 = 1.414 and bk = 20 = 1. correspond to the general form of Master’s
theorem.
• Clearly, a > bk.
• So, it can not be solved using Master’s
• So, we follow case-01. theorem.
• So, we have-
• T(n) = θ (nlogba) = θ (nlog2√2) = θ (n1/2)

16
T(n) = T(√n) + 1
• We can not directly apply Master’s Theorem on this recurrence relation.
• However, we can modify and bring it in the general form to apply Master’s theorem.
• Let n = 2m  T(2m) = T(2m/2) + 1
• Now, let T(2m) = S(m), then T(2m/2) = S(m/2)
• So we have: S(m) = S(m/2) +1 (Now, we can easily apply Master’s Theorem)
• We compare the given recurrence relation with S(m) = aS(m/b) + θ (mklogpm).
• a = 1, b = 2, k = 0, p = 0
• Now, a = 1 and bk = 20 = 1 and Clearly, a = bk.
• So, we follow case-02.
• Since p = 0, so we have-
• S(m) = θ (mlogba.logp+1m) = θ (mlog21.log0+1m) = θ (logm)
•  T(n) = θ (loglog2n)
17

You might also like