Recurrences - PowerPoint PPT Presentation

1 / 48
About This Presentation
Title:

Recurrences

Description:

Recurrences. COMP 482 / ELEC 420. 2. Math Background: ... T(n) = k n=1. T(n) = 4T(n/2) kn n 1. T'(n) = k' n=1. T'(n) = 3T' ... is irrelevant for asymptote. ... – PowerPoint PPT presentation

Number of Views:107
Avg rating:3.0/5.0
Slides: 49
Provided by: johngr7
Category:

less

Transcript and Presenter's Notes

Title: Recurrences


1
COMP 482 / ELEC 420
  • Recurrences

2
Math Background Review Beyond
  • Asymptotic concepts useful math
  • Recurrences
  • Probabilistic analysis

3
Your To-Do List
  • Read CLRS 4.
  • Assignment 2.

4
Obtaining Recurrences
Introductory multiplication examples
T(n) k n1 T(n) 4T(n/2) kn ngt1
T(n) k n1 T(n) 3T(n/2) kn ngt1
Obtained from straightforward reading of
algorithms.
  • Key observation Most deterministic algorithms
    lead to such recurrences.
  • Determine appropriate metric for the size n.
  • Usually obvious, but will see exceptions.
  • Examine algorithms recursion/iteration how
    problem sizes change.
  • Usually straightforward.

5
Solving Recurrences
T(n) k n1 T(n) 4T(n/2) kn ngt1
T(n) k n1 T(n) 3T(n/2) kn ngt1
Real goal Find closed form of defined functions.
How? In general, hard. Solutions not always
known. Will discuss techniques in a few
minutes First look at a couple side issues.
6
Recurrences
T(n) k n1 T(n) 4T(n/2) kn ngt1
T(n) k n1 T(n) 3T(n/2) kn ngt1
?
?
What if n is odd?
Next iteration, n is not integral. Nonsense.
7
Recurrences
T(n) k n1 T(n) 4T(n/2) kn ngt1
T(n) k n1 T(n) 3T(n/2) kn ngt1
Both are examples of the most common
form. Divide-and-conquer, with each step dividing
into a equal-size subproblems.
T(n) O(g(n)) nltb T(n) a?T(n/b) f(n) n?b
8
Recurrences
T(n) ?(1) nltb T(n) a?T(n/b) f(n) n?b
For constant-sized problem sizes, can bound
algorithm by some constant value.
This constant value is irrelevant for
asymptote. Thus, often skip writing the base case
equation.
9
Techniques for Solving Recurrences
  • Well use four techniques
  • Substitution
  • Recursion Tree
  • Master Method for divide conquer
  • Characteristic Equation for linear

10
Techniques Substitution
  • Guess a solution check it.
  • More detail
  • Guess the form of the solution, using unknown
    constants.
  • Use induction to find the constants verify the
    solution.

Completely dependent on making reasonable guesses.
11
Substitution Example 1
T(n) 1 n1 T(n) 4T(n/2) n ngt1
Simplified version of previous example.
Guess T(n) O(n3).
More specifically T(n) ? cn3, for all large
enough n.
12
Substitution Example 1
Prove by strong induction on n.
Assume T(k) ? ck3 for ?kgtn0, for
?kltn. Show T(n) ? cn3 for ?ngtn0.
13
Substitution Example 1
Assume T(k) ? ck3 for ?kgtn0, for ?kltn. Show T(n)
? cn3 for ?ngtn0.
  • Base case, nn01

Awkward. Fortunately, n00 works in these
examples.
14
Substitution Example 1
Assume T(k) ? ck3, for ?kltn. Show T(n) ? cn3.
  • Base case, n1
  • T(n) 1 Definition.
  • 1 ? c Choose large enough c for conclusion.

15
Substitution Example 1
Assume T(k) ? ck3, for ?kltn. Show T(n) ? cn3.
  • Inductive case, ngt1
  • T(n) 4T(n/2) n Definition.
  • ? 4c?(n/2)3 n Induction.
  • c/2 ? n3 n Algebra.

16
Substitution Example 1
Assume T(k) ? ck3, for ?kltn. Show T(n) ? cn3.
  • Inductive case, ngt1
  • T(n) ? c/2 ? n3 n From before.
  • cn3 - (c/2 ? n3 - n) Algebra.
  • ? cn3 Since ngt0, if c?2.

17
Substitution Example 1
T(n) 1 n1 T(n) 4T(n/2) n ngt1
Proved T(n) ? 2n3 for ?ngt0
Thus, T(n) O(n3).
18
Substitution Example 2
T(n) 1 n1 T(n) 4T(n/2) n ngt1
Guess T(n) O(n2). Same recurrence, but now
try tighter bound.
More specifically T(n) ? cn2 for ?ngtn0.
19
Substitution Example 2
Follow same steps, and we get...
Assume T(k) ? ck2, for ?kltn. Show T(n) ? cn2.
  • T(n) 4T(n/2) n
  • ? 4c?(n/2)2 n
  • cn2 n

20
Substitution Example 2
T(n) 1 n1 T(n) 4T(n/2) n ngt1
Solution Use a tighter guess inductive
hypothesis. Subtract a lower-order term a
common technique.
21
Substitution Example 2
Assume T(k) ? ck2 - dn, for ?kltn. Show T(n) ?
cn2 - dn.
  • Base case, n1
  • T(n) 1 Definition.
  • 1 ? c-d Choosing c, d appropriately.

22
Substitution Example 2
Assume T(k) ? ck2 - dn, for ?kltn. Show T(n) ?
cn2 - dn.
  • Inductive case, ngt1
  • T(n) 4T(n/2) n Definition.
  • ? 4(c(n/2)2 - d(n/2)) n Induction.
  • cn2 - 2dn n Algebra.
  • cn2 - dn - (dn - n) Algebra.
  • ? cn2 - dn Choosing d?1.

23
Substitution Example 2
T(n) 1 n1 T(n) 4T(n/2) n ngt1
Proved T(n) ? 2n2 1n for ?ngt0
Thus, T(n) O(n2).
24
Substitution Summary
  • Ability to guess effectively comes with
    experience.
  • Examples used O(), guessing T() ?
  • Can use ?(), guessing T() ?

25
Techniques Recursion Tree
  • Unroll the recurrence to obtain a summation.
  • Solve or estimate summation.
  • Use solution as a guess in substitution.

26
Recursion Tree Example 1
T(n) 1 n1 T(n) 4T(n/2) n ngt1
Now, turn picture into a summation
27
Recursion Tree Example 1
T(n) 1 n1 T(n) 4T(n/2) n ngt1
Cost at this level
T(k)
k
k
k/2
k/2
k/2
k/2
2k
k/4
4k
k/4
k/4
k/4
k/4
k/4
k/4
k/4










1

4lg k
1
?(k2)
T(k) k 2k 4k 2lg k 1k 4lg k
k(1 2 4 2lg k 1) klg 4
28
Recursion Tree Example 2
T(n) 1 n1 T(n) T(n/3) T(2n/3) n ngt1
Makes cost near the leaves hard to
calculate. Estimate!
29
Recursion Tree Example 2
T(n) 1 n1 T(n) T(n/3) T(2n/3) n ngt1
k
k
Overestimate. Consider all branches to be of max
depth.
k/3
2k/3
k
k
k/9
2k/9
2k/9
4k/9





T(k) ? k?(log3/2 k - 1) k T(k) O(k log k)
k
1
1

levels log3/2 k
30
Recursion Tree Example 2
T(n) 1 n1 T(n) T(n/3) T(2n/3) n ngt1
k
k
Underestimate. Count the complete levels,
ignore the rest.
k/3
2k/3
k
k
k/9
2k/9
2k/9
4k/9





T(k) ? k?(log3 k 1) T(k) ?(k log k)
levels log3 k
Thus, T(n) ?(n log n)
31
Recursion Tree Example 3
T(n) 1 n1 T(n) T(n-1) n ngt1
T(k) k T(k-1) k k-1 T(k-2)
k k-1 k-2 T(k-3)
Linear recurrences with only one recurrence are
relatively easy.
k k-1 k-2 2 1
32
Techniques Master Method
  • Cookbook solution for some recurrences of the
    form
  • T(n) a ? T(n/b) f(n)
  • where
  • a?1, bgt1, f(n) asymptotically positive
  • First describe its cases, then outline proof.

33
Master Method Case 1
T(n) a ? T(n/b) f(n) f(n) O(nlogb a - ?)
for some ?gt0 ? T(n) ?(nlogb a)
T(n) 7T(n/2) cn2 a7, b2 E.g., Strassen
matrix multiplication.
cn2 ? O(nlogb a - ?) O(nlog2 7 - ?) ? O(n2.8 -
?) Yes, for any ? ? 0.8.
T(n) ?(nlg 7)
34
Master Method Case 2
T(n) a ? T(n/b) f(n) f(n) ?(nlogb a) ?
T(n) ?(nlogb a lg n)
T(n) 2T(n/2) cn a2, b2 E.g., mergesort.
cn ? ?(nlogb a) ?(nlog2 2) ?(n) Yes.
T(n) ?(n lg n)
35
Master Method Case 3
T(n) a ? T(n/b) f(n) f(n) ?(nlogb a ?)
for some ?gt0 and a?f(n/b) ? c?f(n) for
some clt1 and all large enough n ? T(n) ?(f(n))
T(n) 4?T(n/2) n3 a4, b2
n3 ? ?(nlogb a ?) ?(nlog2 4 ?) ?(n2
?) Yes, for any ? ? 1.
4(n/2)3 ½?n3 ?? cn3 Yes, for any c ? ½.
T(n) ?(n3)
36
Master Method Case 4
T(n) a ? T(n/b) f(n) None of previous
apply. Master method doesnt help.
T(n) 4T(n/2) n2/lg n a4, b2
Case 1? n2/lg n ? O(nlogb a - ?) O(nlog2 4 -
?) O(n2 - ?) O(n2/n?) No, since lg n is
asymptotically less than n?. Thus, n2/lg n is
asymptotically greater than n2/n?.
37
Master Method Case 4
T(n) a ? T(n/b) f(n) None of previous
apply. Master method doesnt help.
T(n) 4T(n/2) n2/lg n a4, b2
Case 2? n2/lg n ? ?(nlogb a) ?(nlog2 4)
?(n2) No.
38
Master Method Case 4
T(n) a ? T(n/b) f(n) None of previous
apply. Master method doesnt help.
T(n) 4T(n/2) n2/lg n a4, b2
Case 3? n2/lg n ? ?(nlogb a ?) ?(nlog2 4
?) ?(n2 ?) No, since 1/lg n is
asymptotically less than n?.
39
Master Method Proof Outline
T(n) a ? T(n/b) f(n)
logb k
f(k)
f(k)
f(k/b)
f(k/b)
a?f(k/b)
a2?f(k/b2)
f(k/b2)
f(k/b2)
f(k/b2)
f(k/b2)






1
1
alevels
Cases correspond to determining which term
dominates how to compute sum.
40
Techniques Characteristic Equation
  • Applies to linear recurrences
  • Homogenous
  • an c1an-1 c2an-2 ckan-k
  • Nonhomogenous
  • an c1an-1 c2an-2 ckan-k F(n)

41
Homogenous With Example
  • an c1an-1 c2an-2 ckan-k
  • rn c1rn-1 c2rn-2 ckrn-k
  • rk - c1rk-1 - c2rk-2 - - ck 0
  • an 6an-1 - 11an-2 6an-3
  • rn 6rn-1 - 11rn-2 6rn-3
  • r3 - 6r2 11r - 6 0

a0 2 a1 5 a2 15
Guess an rn. (Not necessarily true, but leads
to a soln.) Plug in.
Divide by rn-k. Move terms. Characteristic Equati
on
42
Homogenous With Example
  • rk - c1rk-1 - c2rk-2 - - ck 0
  • r1, r2, , rk
  • an ?1r1n ?2r2n ?krkn
  • ?1, ?2, , ?k
  • r3 - 6r2 11r - 6 0
  • r11, r22, r33
  • an ?1 ?22n ?33n
  • ?11, ?2-1, ?32
  • an 1 - 2n 2?3n

Find roots. (Well assume distinct.)
(r-1)(r-2)(r-3)
Plug into this eqn
Use initial conditions to solve.
a0 2 ?1?2?3 a1 5 ?1?22?33 a2 15
?1?24?39
43
Lets Verify This Works
  • Assume r1, r2, , rk are roots of
  • rk - c1rk-1 - c2rk-2 - - ck 0.
  • an c1an-1 c2an-2 ckan-k
  • ?
  • an ?1r1n ?2r2n ?krkn
  • General proof omitted.
  • a0 2, a1 5, a2 15
  • ?n3,
  • an 6an-1 - 11an-2 6an-3
  • ?
  • an 1 - 2n 2?3n
  • Strong induction on n.

44
Lets Verify This Works
  • Inductive Assume ?kn
  • ak 6ak-1 - 11ak-2 6ak-3
  • ak 1 2k 2?3k
  • Show
  • 6an-11an-16an-2 1-2n12?3n1
  • 6an-11an-16an-2
  • 6(1 - 2n 2?3n)-11(1 - 2n-1 2?3n-1)6(1 -
    2n-2 2?3n-2)
  • (6-116) (6?2n-11?2n-16?2n-2)
    (12?3n-22?3n-112?3n-2)
  • 1 (6?2n-8?2n-1) (12?3n-18?3n-1)
  • 1 - (2?2n) (6?3n) 1-2n12?3n1

a0 2, a1 5, a2 15 ?n3, an 6an-1 -
11an-2 6an-3 ? an 1 - 2n 2?3n Base,
n3 a3 6a2-11a16a0 90-5512 47 a3
1-232?33 1-854 47
45
Nonhomogenous With Example
  • an c1an-1 c2an-2 ckan-k F(n)
  • rn c1rn-1 c2rn-2 ckrn-k
  • rk - c1rk-1 - c2rk-2 - - ck 0
  • r1, r2, , rk
  • an ?1r1n ?2r2n ?krkn
  • an 3an-1 2n
  • rn 3rn-1
  • r - 3 0
  • r13
  • an ?13n

a0 1/3
Solve as before, ignoring F(n).
This is only a partial solution.
46
Nonhomogenous With Example
  • an c1an-1 c2an-2 ckan-k F(n)
  • an ?1r1n ?2r2n ?krkn
  • F(n)
  • an ...something like F(n)
  • an 3an-1 2n
  • an ?13n
  • F(n) 2n
  • an cn d
  • cn d 3(c(n-1)d) 2n
  • (22c)n (2d-3c) 0
  • c -1, d -3/2
  • an -n - 3/2

a0 1/3
Now look at F(n)
Guess a solution of similar form, solve.
This is another partial solution.
47
Nonhomogenous With Example
  • an c1an-1 c2an-2 ckan-k F(n)
  • an ?1r1n ?2r2n ?krkn
  • an
  • an ?1r1n ?2r2n ?krkn
  • ?1, ?2, , ?k
  • an 3an-1 2n
  • an ?13n
  • an -n - 3/2
  • an -n - 3/2 ?13n
  • ?1 11/6
  • an -n - 3/2 (11/6)3n

a0 1/3
Combine two linear partial solutions.
Use initial conditions to solve.
a0 1/3 -0 - 3/2 ?130 -3/2
?1
48
Dealing With Other Recurrence Forms
T(n) 1 n?2 T(n) T(lg n) n ngt2
Write a Comment
User Comments (0)
About PowerShow.com