0% found this document useful (0 votes)
3 views

Week 3

This document discusses various methods for solving recurrence relations in data structures, including the substitution method, homogeneous and inhomogeneous recurrences, and the master method. It provides examples and explains the use of recursion trees to analyze the costs associated with recursive algorithms. Additionally, it covers the quicksort algorithm and its time complexity in different scenarios.

Uploaded by

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

Week 3

This document discusses various methods for solving recurrence relations in data structures, including the substitution method, homogeneous and inhomogeneous recurrences, and the master method. It provides examples and explains the use of recursion trees to analyze the costs associated with recursive algorithms. Additionally, it covers the quicksort algorithm and its time complexity in different scenarios.

Uploaded by

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

DATA STRUCTURES

CSO102
WEEK-3 (DRAFT)
Solutions to Some Recurrence
Relations

■ Substitution Method:
– Problem 1:
• tn=tn-1+n n>2, t1=1
• tn=tn-2+n-1+n
• =1+2+3+.................+n=O(n2)
– Problem 2:
• tn=tn/2+n
• =tn/4+n/2+n , k=log(n)
• =1+2+..........+2k=2k+1-1=O(n)
Homogenous Recurrence
■ a0tn+a1tn-1+------+aktn-k=0
■ tn =
■ ci’s are determined by intial conditions
– Let tn=xn ,where x is a constant
– a0xn+a1xn-1+-----+akxn-k=0
– X=0 (trivial solution )
– If r1,r2,r3,..rk, are k roots of above characteristic equation.
■ Problem 1:
– tn-3tn-1-4tn-2=0,t0=0 and t1=1
– x2-3x-4=0 => tn=c1(-1)n+c24n

T(n) = O(4n)
InHomogenous Recurrence
■ a0tn+a1tn-1+---+aktn-k=F(n)
■ Problem :
– tn=2tn-1+n
– tn-2tn-1=n ----eq(1)
– Replacing n by n+1 ,tn+1-2tn=n+1
– 2tn+1-4tn=2n+2 ---eq(2)
– tn+2-2tn+1=n+2 ---eq(3)
– Solving eq(3)+eq(1)-eq(2) by homogenous recurrence
– tn=O(2n)
– eg : 1) tn=2tn-1+n+2

– eg: 2) tn=tn-1+tn-2+n2+n+1
Change of Variable
■ T(n)=4T(n/2)+n
– T(2k)=4T(2k-1)+2k
– tk=4tk-1+2k
– O(n2)
=

■ Many recursive Algorithms work by dividing the input in half and


calling itself on the new half sized input
– Let’s have T(n)=aT(n/b)+f(n) , b>0
■ We define a new Function S(k) by the rule s(k)=T(bk)
– Then S(k)=T(bk)=aT(bk-1)+f(bk)
– S(k)=aS(k-1)+f(bk)
– Which is first order linear recurrence relation of S
Recursion Tree

■ In a recursion tree,each node represent the cost of a single


subproblem somewhere in the set of recursive function invocation.
■ We sum the costs within each level of the tree to obtain a set of per
level costs.
■ Then we sum all the per level costs to determine the total costs of all
levels of the recursion.
■ Recursion trees are particularly useful , when the recurrence
describes running time of divide and conquer algorithm.
■ T(n)= 2T(n/2) + cn , T(1)=c
■ Draw recursion tree which shows successive expansion of the
recurrence .
■ For original Problem we have a cost of cn plus the two sub-problems
each costing T(n/2).
Example: T(n) = 2T(n/2) + C
Example: T(n) = 2T(n/2) + C
The master method

The master method applies to recurrences of


the form
T(n) = a T(n/b) + f (n) ,
where a >= 1, b > 1, and f is asymptotically
positive.
The master method
T(n) = aT(n/b) + f(n)
where, T(n) has the following
asymptotic bounds:
1. If f(n) = O(nlogb a -ϵ), then T(n) =
Θ(nlogb a).
2. If f(n) = Θ(nlogb a), then T(n) =
Θ(nlogb a log n).
3. If f(n) = Ω(nlogb a+ϵ), then T(n) =
Θ(f(n)). {ϵ > 0 is a constant.}
Examples

■ Ex : 1
– T(n) = 4T(n/2) + n
– a = 4, b = 2 nlogba = n2; f (n) = n.
– CASE 1: T(n) = Ο(n2).

■ Ex : 2
– T(n) = 4T(n/2) + n2
– a = 4, b = 2 , nlogba = n2; f (n) = n2.
– CASE 2: T(n) = O(n2lg n).
PARTITION
QUICK SORT
QUICK SORT

/* low –> Starting index, high –> Ending index */


quickSort(arr[], low, high) {
if (low < high) {
/* pi is partitioning index, arr[pi] is now at right place */
pi = partition(arr, low, high);
quickSort(arr, low, pi – 1); // Before pi
quickSort(arr, pi + 1, high); // After pi
QUICK SORT

■ TIME COMPLEXITY:
■ WORST CASE: T(N)= T(N-1) + N
■ BEST & AVERAGE CASES: T(N) = 2T(N/2) + N

You might also like