Online Class 9-Recurrence
Online Class 9-Recurrence
Recurrence relations
Examples
The base case
Recurrence relations for modeling the time
factor of recursive functions –for merge sort
• Time taken for n=1 is constant ,say Theta (1)
Comp 122
Repeated Substitution methods –examples
(for practice)
Continuing…
Recurrence relations
Examples
The base case
Simple ..
• https://www.khanacademy.org/math/
algebra/x2f8bb11595b61c86:sequences/
x2f8bb11595b61c86:constructing-arithmetic-
sequences/a/writing-recursive-formulas-for-
arithmetic-sequences
• Recurrence relations arise when we analyze the
running time of iterative or recursive algorithms.
– Ex: Divide and Conquer.
T(n) = (1) if n c
T(n) = a T(n/b) + D(n) + C(n) otherwise
(e.g.merge sort )
D(n) ..time for dividing
C(n) ..time for combining
• Equation or an inequality that characterizes a
function by its values on smaller inputs.
• Solution Methods
• Repeated Substitution
– Substitution Method.(guess the solution and verify by
induction)
– Recursion-tree Method.
– Master Method.
Comp 122
Repeated Substitution methods –examples
(for practice)
Continuing…
Another example
Solution to this is….
• Find the complexity of the below recurrence:
Coming back to merge sort ….
• Rewrite this as …
• T(n) =1 if n =1
• = 2T(n/2) +n for n>1
• Expand this…
• =2 (2T(n/4)+n/2) +n
• =2^2(T(n/4) +2n
• =…….
• =2^3(T(n/8) +3n ……observe the pattern
•
• T(n) =2^in T(n/2^i) +in
• =
•
for (i=log n) levels
Substitute
Comp 122
Example – Exact Function
Recurrence: T(n) = 1 if n = 1
T(n) = 2T(n/2) + n if n > 1
Guess: T(n) = n lg n + n.
Induction:
• Basis: n = 1 n lgn + n = 1 = T(n).
• Hypothesis: T(k) = k lg k + k for all k < n.
• Inductive Step: T(n) = 2 T(n/2) + n
= 2 ((n/2)lg(n/2) + (n/2)) + n
= n (lg(n/2)) + 2n
= n lg n – n + 2n
= n lg n + n
Comp 122
Recursion-tree Method
cn
cn
Cost of divide and
merge.
cn/2 cn/2
T(n/2) T(n/2)
T(n/4) T(n/4) T(n/4) T(n/4)
Cost of sorting
subproblems.
Comp 122