0% found this document useful (0 votes)
17 views40 pages

Lec 8

The document discusses various methods for analyzing algorithms, focusing on the substitution method and the master method for solving recurrences. It provides examples of how to apply these methods to determine time complexity, including the use of recursion trees. The document emphasizes the importance of verifying assumptions and handling initial conditions in the analysis process.

Uploaded by

munshijubair7
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)
17 views40 pages

Lec 8

The document discusses various methods for analyzing algorithms, focusing on the substitution method and the master method for solving recurrences. It provides examples of how to apply these methods to determine time complexity, including the use of recursion trees. The document emphasizes the importance of verifying assumptions and handling initial conditions in the analysis process.

Uploaded by

munshijubair7
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/ 40

CSE-209

Algorithms-I

Lecture 8

Asymptotic Notation-II

Md. Rafsan Jani


Assistant Professor
Department of Computer Science and Engineering
Jahangirnagar University
Substitution method
The most general method:
1. Guess the form of the solution.
2. Verify by induction.
3. Solve for constants.
Substitution method
The most general method:
1. Guess the form of the solution.
2. Verify by induction.
3. Solve for constants.
EXAMPLE: T(n) = 4T(n/2) + n
•[Assume that T(1) = Θ(1).]
•Guess O(n3) . (Prove O and Ω
separately.)
•Assume that T(k) ≤ ck3 for k < n .
Example ofsubstitution
T (n) = 4T (n / 2) +
n
≤ 4c(n / 2)3 + n
3
= (c / 2)n + n
= cn3 − ((c / 2)n3 − n) desired – residual
3 desired
≤ cn
whenever (c/2)n3 – n ≥ 0, for example,
if c ≥ 2 and n ≥ 1.
residual
Example (continued)
•We must also handle the initial conditions,
that is, ground the induction with base
cases.
•Base: T(n) = Θ(1) for all n < n0, where n0
is a suitable constant.
•For 1 ≤ n < n0, we have “Θ(1)” ≤ cn3, if we
pick c big enough.
Example (continued)
•We must also handle the initial conditions,
that is, ground the induction with base
cases.
•Base: T(n) = Θ(1) for all n < n0, where n0
is a suitable constant.
•For 1 ≤ n < n0, we have “Θ(1)” ≤ cn3, if we
pick c big enough.

This bound is not tight!


A tighter upper bound?
We shall prove that T(n) = O(n2).
A tighter upper bound?
We shall prove that T(n) = O(n2).
Assume that T(k) ≤ ck2 for k < n:
T (n) = 4T (n / 2) + n
≤ 4c(n / 2) + n
2

= cn2 + n
= O(n2 )
A tighter upper bound?
We shall prove that T(n) = O(n2).
Assume that T(k) ≤ ck2 for k < n:
T (n) = 4T (n / 2) + n
≤ 4c(n / 2)2 + n
= cn2 + n
= O(n2 )
Wrong! We must prove the
I.H.
A tighter upper bound?
We shall prove that T(n) = O(n2).
Assume that T(k) ≤ ck2 for k < n:
T (n) = 4T (n / 2) + n
≤ 4c(n / 2)2 + n
= cn2 + n
= O(n2 )
Wrong! We must prove the
= cn − (−n) [ desired – residual ]
2
I.H. 2
≤ cn for no choice of c > 0.Lose!
A tighter upper bound!
IDEA: Strengthen the inductive hypothesis.
•Subtract a low-order term.
Inductive hypothesis: T(k) ≤ c1k2 – c2k for k < n.
A tighter upper bound!
IDEA: Strengthen the inductive hypothesis.
•Subtract a low-order term.
Inductive hypothesis: T(k) ≤ c1k2 – c2k for k < n.
T(n) = 4T(n/2) + n
= 4(c1(n/2)2 – c2(n/2)) + n
= c1n2 – 2c2n + n
= c1n2 – c2n – (c2n – n)
≤ c1n2 – c2n if c2 ≥ 1.
A tighter upper bound!
IDEA: Strengthen the inductive hypothesis.
•Subtract a low-order term.
Inductive hypothesis: T(k) ≤ c1k2 – c2k for k < n.
T(n) = 4T(n/2) + n
= 4(c1(n/2)2 – c2(n/2)) + n
= c1n2 – 2c2n + n
= c1n2 – c2n – (c2n – n)
≤ c1n2 – c2n if c2 ≥ 1.
Pick c1 big enough to handle the initial conditions.
Recursion-tree method
•A recursion tree models the costs (time) of a
recursive execution of an algorithm.
•The recursion-tree method can be unreliable,
just like any method that uses ellipses (…).
•The recursion-tree method promotes intuition,
however.
•The recursion tree method is good for
generating guesses for the substitution method.
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
T(n)
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
2
n
T(n/4) T(n/2)
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
2
n
(n/4)2 (n/2)2

T(n/16) T(n/8) T(n/8) T(n/4)


Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
2
n
(n/4)2 (n/2)2

(n/16)2 (n/8)2 (n/8)2 (n/4)2


Θ(1)
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
2
n n2
(n/4)2 (n/2)2

(n/16)2 (n/8)2 (n/8)2 (n/4)2


Θ(1)
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
2
n n2
(n/4)2 (n/2)2 5 2
16 n
(n/16)2 (n/8)2 (n/8)2 (n/4)2

Θ(1)
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
2
n n2
(n/4)2 (n/2)2 5 2
16 n
(n/16)2 (n/8)2 (n/8)2 (n/4)2 25 2
256n


Θ(1)
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
2
n n2
(n/4)2 (n/2)2 5 2
16 n
(n/16)2 (n/8)2 (n/8)2 (n/4)2 25 2
256n


Θ(1)
Total =
n 2
(
2
5
1 ++ 5
16 16 16
25
( ) +( ) 3

= Θ(n ) geometric series


+· )
The master method

The master method applies to recurrences of


the form
T(n) = a T(n/b) + f (n) ,
where a ≥ 1, b > 1, and f is
asymptotically positive.
Three common cases
Compare f (n) with nlogba:
1. f (n) = O(nlogba – ε) for some constant ε > 0.
•f (n) grows polynomially slower than
nlogba
(by an nε factor).
Solution: T(n) = Θ(nlogba) .
Three common cases
Compare f (n) with nlogba:
1. f (n) = O(nlogba – ε) for some constant ε > 0.
•f (n) grows polynomially slower than nlogba
(by an nε factor).
Solution: T(n) = Θ(nlogba) .
2. f (n) = Θ(nlogba lgkn) for some constant k ≥
0.
•f (n) and nlogba grow at similar rates.
Solution: T(n) = Θ(nlogba lgk+1n) .
Three common cases (cont.)
Compare f (n) with nlogba:
3. f (n) = Ω(nlogba + ε) for some constant ε > 0.
•f (n) grows polynomially faster than nlogba
(by an nε factor),
and f (n) satisfies the regularity condition that
af (n/b) ≤ cf (n) for some constant c < 1.
Solution: T(n) = Θ( f (n)) .
Examples

EX. T(n) = 4T(n/2) + n


a = 4, b = 2 ⇒ nlogba = n2; f (n) =
n.
CASE 1: f (n) = O(n2 – ε) for ε = 1.
∴ T(n) = Θ(n2).
Examples

EX. T(n) = 4T(n/2) + n


a = 4, b = 2 ⇒ nlogba = n2; f (n) = n.
CASE 1: f (n) = O(n2 – ε) for ε = 1.
∴ T(n) = Θ(n2).

EX. T(n) = 4T(n/2) + n2


a = 4, b = 2 ⇒ nlogba = n2; f (n) = n2.
CASE 2: f (n) = Θ(n2lg0n), that is, k = 0.
∴ T(n) = Θ(n2lg n).
Examples
EX. T(n) = 4T(n/2) + n3
a = 4, b = 2 ⇒ nlogba = n2; f (n) = n3.
CASE 3: f (n) = Ω(n2 + ε) for ε = 1
and 4(n/2)3 ≤ cn3 (reg. cond.) for c = 1/2.
∴ T(n) = Θ(n3).
Examples
EX. T(n) = 4T(n/2) + n3
a = 4, b = 2 ⇒ nlogba = n2; f (n) = n3.
CASE 3: f (n) = Ω(n2 + ε) for ε = 1
and 4(n/2)3 ≤ cn3 (reg. cond.) for c = 1/2.
∴ T(n) = Θ(n3).

EX. T(n) = 4T(n/2) + n2/lg n


a = 4, b = 2 ⇒ nlogba = n2; f (n) = n2/lg n.
Master method does not apply. In particular,
for every constant ε > 0, we have nε = ω(lg
Idea of master theorem
Recursion tree:
f a
(n)
f f … f
(n/b) (n/b) (n/b)
2 …
2
f (n/b ) af (n/b2)
f (n/b )

Τ (1)
Idea of master theorem
Recursion tree:
f f
a
(n) (n)
f f … f af (n/b)
(n/b) (n/b) (n/b)
2 … a2 f
2
f (n/b ) a f (n/b2)
f (n/b ) (n/b2)


Τ
(1)
Idea of master theorem
Recursion tree:
f f
a
(n) (n)
f f … f af (n/b)
h = logbn (n/b) (n/b) (n/b)
2 … a2 f
2
f (n/b ) a f (n/b2)
f (n/b ) (n/b2)


Τ
(1)
Idea of master theorem
Recursion tree:
f f
a
(n) (n)
f f … f af (n/b)
h = logbn (n/b) (n/b) (n/b)
2 … a2 f
2
f (n/b ) a f (n/b2)
f (n/b ) (n/b2)
#leaves = ah


log n
Τ = a b nlogbaΤ
log a
(1) = n b (1)
Idea of master theorem
Recursion tree:
f f
a
(n) (n)
f f … f af (n/b)
h = logbn (n/b) (n/b) (n/b)
2 … a2 f
2
f (n/b ) a f (n/b2)
f (n/b ) (n/b2)


Τ nlogbaΤ
(1) (1)
log a
Θ(n b )
Idea of master theorem
Recursion tree:
f f
a
(n) (n)
f f … f af (n/b)
h = logbn (n/b) (n/b) (n/b)
2 … a2 f
2
f (n/b ) a f (n/b2)
f (n/b ) (n/b2)


Τ nlogbaΤ
(1) (1)logba
Θ(n lg n)
Idea of master theorem
Recursion tree:
f f
a
(n) (n)
f f … f af (n/b)
h = logbn (n/b) (n/b) (n/b)
2 … a2 f
2
f (n/b ) a f (n/b2)
f (n/b ) (n/b2)


Τ nlogbaΤ
(1) (1)
Θ( f
(n))
Appendix: geometric series

1 + x + x2 · + x n =1− for x ≠ 1
+
n+1
1 −
x x
2 1
1 + x + x +· = for |x| < 1
1−
x
Return to last
slide viewed.
Reference
https://ocw.mit.edu/courses/6-046j-intr
oduction-to-algorithms-sma-5503-fall-2
005/pages/readings/

You might also like