0% found this document useful (0 votes)
18 views32 pages

Online Class 9-Recurrence

Uploaded by

Soutik Dey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views32 pages

Online Class 9-Recurrence

Uploaded by

Soutik Dey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

RECURRENCE RELATIONS

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)

• T(n) = solving the base case for n=1


• = no_pieces T( sub_problem size factor)
+dividing +combining for n>1
• For merge sort
• T(n) =Theta (1) for n =1
=2T(n/2) +Theta(n) for n>1
IN general –
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.
• 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

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

T(n) = 2lgn T(1)+n lg n


=n +nlgn
Which is order ( nlg n)
It is theta (n lg n)
Because combining takes ‘n’ times.
So it has an upper and lower bound (both are the
same)

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

• Making a good guess is sometimes difficult with


the substitution method.
• Use recursion trees to devise good guesses.
• Recursion Trees
– Show successive expansions of recurrences using trees.
– Keep track of the time spent on the subproblems of a
divide and conquer algorithm.
– Help organize the algebraic bookkeeping necessary to
solve a recurrence.
Comp 122
Recursion Tree for Merge Sort
For the original problem, we have Each of the size n/2 problems has a cost
a cost of cn, plus two of cn/2 plus two subproblems, each
subproblems each of size (n/2) costing T(n/4).
and running time T(n/2).

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

You might also like