Instructor Monica Nicolescu Lecture 4 CS 477677 Lecture 4 2 Methods for Solving Recurrences Iteration method Substitution method Recursion tree method Master method CS 477677 Lecture 4 ID: 760660
Download Presentation The PPT/PDF document "Analysis of Algorithms CS 477/677" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Analysis of AlgorithmsCS 477/677
Instructor: Monica
Nicolescu
Lecture 4
Slide2CS 477/677 - Lecture 4
2
Methods for Solving Recurrences
Iteration method
Substitution method
Recursion tree method
Master method
Slide3CS 477/677 - Lecture 4
3
Iteration Method – Example
T(n
) =
n
+ T(n-1)
T(n
) =
n
+ T(n-1)
=
n
+ (n-1) + T(n-2)
=
n
+ (n-1) + (n-2) + T(n-3)
… =
n
+ (n-1) + (n-2) + … + 2 + T(1)
= n(n+1)/2 - 1 + T(1)
= n
2
+ T(1) =
Θ
(n
2
)
Slide4CS 477/677 - Lecture 4
4
The substitution method
Guess a solution
Use induction to prove that the solution works
Slide5CS 477/677 - Lecture 4
5
Substitution method
Guess a solution
T(n) = O(g(n))
Induction goal:
apply the definition of the asymptotic notation
T(n)
≤
d g(n)
, for some
d > 0
and
n
≥ n
0
Induction
hypothesis:
T(k)
≤
d g(k) for all k < n
Prove the induction goal
Use the
induction hypothesis
to
find some values of the constants
d
and
n
0
for which the
induction goal
holds
Slide6CS 477/677 - Lecture 4
6
Example: Binary Search
T(n) = c + T(n/2)
Guess:
T(n) = O(lgn)
Induction goal:
T(n)
≤
d lgn
, for some
d
and
n
≥ n
0
Induction
hypothesis:
T(n/2)
≤
d lg(n/2)
Proof of induction goal:
T(n) = T(n/2) + c
≤ d
lg(n/2) + c
= d lgn – d + c
≤ d lgn
if:
– d + c ≤ 0, d ≥ c
Slide7CS 477/677 - Lecture 4
7
Example: Binary Search
T(n) = c + T(n/2)
Boundary conditions:
Base case:
n
0
= 1
⇒
T(1) = c
has to verify condition:
T(1) ≤ d
lg
1
⇒
c ≤ d
lg
1 = 0
– contradiction
Choose
n
0
= 2
⇒
T(2) = 2c
has to verify condition:
T(2) ≤ d
lg
2
⇒
2c ≤ d
lg
2 = d
⇒
choose
d
≥ 2c
We can similarly prove that
T(n) =
𝝮(
lgn
)
and therefore:
T(n) =
Θ
(
lgn
)
Slide8CS 477/677 - Lecture 4
8
Example 2
T(n) = T(n-1) + n
Guess:
T(n) = O(n
2
)
Induction goal:
T(n)
≤
c n
2
, for some
c
and
n
≥ n
0
Induction
hypothesis:
T(n-1)
≤
c(n-1)
2
Proof of induction goal:
T(n) = T(n-1) + n
≤ c
(n-1)
2
+ n
= cn
2
– (2cn – c - n)
≤ cn
2
if: 2cn – c – n ≥ 0
⇒ c
≥ n/(2n-1)
⇒ c
≥ 1/(2 – 1/n)
For n
≥ 1
⇒
2 – 1/n ≥ 1
⇒
any
c ≥ 1
will work
Slide9CS 477/677 - Lecture 4
9
Example 2
T(n) = T(n-1) + n
Boundary conditions:
Base case:
n
0
= 1
⇒
T(1) = 1
has to verify condition:
T(1)
≤ c (1)
2
⇒
1 ≤ c
⇒
OK!
We can similarly prove that
T(n) =
𝝮(n
2
)
and therefore:
T(n) =
Θ
(n
2
)
Slide10CS 477/677 - Lecture 4
10
Example 3
T(n) = 2T(n/2) + n
Guess:
T(n) = O(
nlgn
)
Induction goal:
T(n)
≤
cn
lgn
, for some
c
and
n
≥ n
0
Induction
hypothesis:
T(n/2)
≤
cn
/2
lg
(n/2)
Proof of induction goal:
T(n) = 2T(n/2) + n
≤ 2c (n/2)
lg
(n/2) + n
=
cn
lgn
–
cn
+ n
≤
cn
lgn
if: -
cn
+ n ≤ 0
⇒
c
≥ 1
Slide11CS 477/677 - Lecture 4
11
Example 3
T(n) = 2T(n/2) + n
Boundary conditions:
Base case:
n
0
= 1
⇒
T(1) = 1
has to verify condition:
T(1) ≤ cn
0
lgn
0
⇒
1 ≤ c * 1 * lg1 = 0
– contradiction
Choose n
0
= 2
⇒
T(2) = 4
has to verify condition:
T(2) ≤ c * 2 * lg2
⇒
4 ≤ 2c
⇒
choose c = 2
We can similarly prove that
T(n) =
𝝮(
nlgn
)
and therefore:
T(n) = 𝝮(
nlgn
)
Slide12CS 477/677 - Lecture 4
12
Changing variables
Rename: m = lgn ⇒ n = 2mT (2m) = 2T(2m/2) + mRename: S(m) = T(2m)S(m) = 2S(m/2) + m ⇒S(m) = O(mlgm) (demonstrated before)T(n) = T(2m) = S(m) = O(mlgm)=O(lgnlglgn)Idea: transform the recurrence to one that you have seen before
T(n) = 2T( ) + lgn
Slide13CS 477/677 - Lecture 4
13
Changing variables (cont.)
Rename: n = 2m (n/2 = 2m-1)T (2m) = 2T(2m-1) + 1Rename: S(m) = T(2m)S(m) = S(m-1) + 1 = 1 + S(m-1) = 1 + 1 + S(m-2) = 1 + 1 + …+ 1 + S(1) S(m) = O(m) ⇒ T(n) = T(2m) = S(m) = O(m) = O(lgn)
T(n) = 2T(n/2) + 1
m -1 times
Slide14CS 477/677 - Lecture 4
14
The recursion-tree method
Convert the recurrence into a tree:Each node represents the cost incurred at that level of recursionSum up the costs of all levels
Used to “guess” a solution for the recurrence
Slide15CS 477/677 - Lecture 4
15
Readings
Chapter 4