Lec 8
Lec 8
Algorithms-I
Lecture 8
Asymptotic Notation-II
= 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
Θ(1)
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
2
n n2
(n/4)2 (n/2)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
Τ (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/