Professional Documents
Culture Documents
CSC 344 - Algorithms and Complexity: Lecture #10 - Recurrences
CSC 344 - Algorithms and Complexity: Lecture #10 - Recurrences
CSC 344 - Algorithms and Complexity: Lecture #10 - Recurrences
Recurrence Relations
• Overview
– Connection to recursive algorithms
• Techniques for solving them
– Methods for generating a guess
– Induction proofs
– Master Theorem
1
5/4/2016
2
5/4/2016
3
5/4/2016
4
5/4/2016
Recurrence Relations
• Overview
– Connection to recursive algorithms
• Techniques for solving them
– Methods for generating a guess
– Induction proofs
– Master Theorem
5
5/4/2016
n= 0 1 2 3 4 5 6 7
Tn=
• Guess:
6
5/4/2016
Back-substitution or Unrolling
Tn = 2Tn-1 + 1 ; T0 = 0
Tn = 2(2Tn-2 + 1) + 1
= 4Tn-2 + 2 + 1
Tn = 4(2Tn-3 + 1) + 2 + 1
= 8Tn-3 + 4 + 2 + 1
Tn = 8(2Tn-4 + 1) + 4 + 2 + 1
= 16Tn-4 + 8 + 4 + 2 + 1
Guess:
Recursion Trees
Tn = 2 Tn-1 + 1 , T0 = 0
Tn 1 1
Tn-1 Tn-1 1 1 2
Guess:
7
5/4/2016
n = 0 1 2 3 4 5 6 7 8 9 10 11 12 13
T(n)=
n = 0 1 4 16 64 256 1024
T(n)=
Guess:
Back-substitution or unrolling
T(n) = 3T(n/4) + n, T(0) = 0
≤ 3(3T(n/16)+ n/4) + n
= 9T(n/16) + 3n/4 + n
= 9(3T(n/64) + n/16) + 3n/4 + n
= 27T(n/64) +9n/16 + 3n/4 + n
Guess:
8
5/4/2016
Recursion Trees
Tn = 3Tn/4 + n, T0 = 0
Tn n
n
Guess:
Third Example
Example: Tn = 2 Tn/2 + n2 , T0 = 0
9
5/4/2016
n = 0 1 2 4 8 16 32
T(n)= 0 1 6 28 120 496 2016
n = 64 128 256
T(n)= 8128 32640 130816
Guess: ??
Guess: (2n-1)n
10
5/4/2016
11
5/4/2016
Recurrence Relations
• Overview
– Connection to recursive algorithms
• Techniques for solving them
– Methods for generating a guess
– Induction proofs
– Master Theorem
Induction Proof
Tn = 2Tn-1 + 1 ; T0 = 0
Prove: Tn = 2n - 1 by induction:
1. Base Case: n=0: T0 = 20 - 1 = 0
2. Inductive Hypothesis (IH): Tn = 2n – 1 for n ≥ 0
3. Inductive Step: Show Tn+1 = 2n+1 – 1 for n ≥ 0
Tn+1 = 2Tn + 1
= 2 ( 2n - 1 ) + 1 (applying IH)
= 2n+1 -1
12
5/4/2016
13
5/4/2016
Induction Step
Given : T(n/2 ) ≤ c (n/2 ) lg (n/2 )
14
5/4/2016
Recurrence Relations
• Overview
– Connection to recursive algorithms
• Techniques for solving them
– Methods for generating a guess
– Induction proofs
– Master Theorem
Master Theorem
• T(n) = a T(n/b) + f(n)
– Ignore floors and ceilings for n/b
– constants a ≥ 1 and b > 1
– f(n) any function
• If f(n) = O(nlog_b a-ε) for constant ε>0, T(n) = Θ(nlog_b a)
• If f(n) = Θ(nlog_b a), T(n) = Θ(nlog_b a lg n)
• If f(n) = Ω(nlog_b a+ε) for some constant ε >0, and if a f(n/b)
≤ c f(n) for some constant c < 1 and all sufficiently large n,
T(n) = Θ(f(n)).
• Key idea: Compare nlog_b a with f(n)
15
5/4/2016
Master Theorem
• The master theorem concerns recurrence relations
of the form:
– T(n) = aT(n/b) + f(n) , where a ≥ 1, b > 1
– In the application to the analysis of a recursive
algorithm, the constants and function take on the
following significance:
• n is the size of the problem.
• a is the number of subproblems in the recursion.
• n/b is the size of each subproblem. (Here it is assumed that all
subproblems are essentially the same size.)
• f (n) is the cost of the work done outside the recursive calls,
which includes the cost of dividing the problem and the cost of
merging the solutions to the subproblems.
16
5/4/2016
17
5/4/2016
18
5/4/2016
19
5/4/2016
Inadmissible Equations
• T(n) = 2nT(n/2) + nn
– a is not constant
• T(n) = 2T(n/2) + n/log n
– f(n)/nlog a must be a polynomial
b
• T(n) = 0.5T(n/2) + n
– a cannot be less than one
• T(n) = 64T(n/8) - n2 log n
– f(n) must be positive
• T(n) = T(n/2) + n (2-cos n)
– It’s case 3 but there is a regularity violation
20