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

13-Time Complexity For Recursive Algorithms-10!04!2023

The document discusses recursion and recurrence relations for different recursive algorithms: 1. A simple recursive function test(n) that prints n and recursively calls itself with n-1. This has a time complexity of O(n) as it prints and calls the function n times. 2. A modified test(n) function that prints inside a loop from 0 to n-1 before recursively calling. This has a time complexity of O(n^2) as the number of operations increases quadratically with n. 3. A dividing recursive function test(n) that calls itself with n/2. This has a time complexity of O(logn) as it divides n by half in each recursive call
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

13-Time Complexity For Recursive Algorithms-10!04!2023

The document discusses recursion and recurrence relations for different recursive algorithms: 1. A simple recursive function test(n) that prints n and recursively calls itself with n-1. This has a time complexity of O(n) as it prints and calls the function n times. 2. A modified test(n) function that prints inside a loop from 0 to n-1 before recursively calling. This has a time complexity of O(n^2) as the number of operations increases quadratically with n. 3. A dividing recursive function test(n) that calls itself with n/2. This has a time complexity of O(logn) as it divides n by half in each recursive call
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Recursion

void test (int n)


{]

if (n>0)
{
printf("%d",n);
test(n-1);
}
}

n=3

recursion tree

test(3)

test(2)
3

2 test(1)

1 test(0)

n=3

for printf = 3 for recursive call 3+1

for n=5

printf =5 for recursive call 5+1

for n

printf =n for recursive call n+1

n+n+1= O(n)
Recurrence relation for the above algorithm

T(n)= { 1 n=0

{ T(n-1) +1 n>0

T(n) = T(n-1) + 1 ------- 1

Back substitution method or Induction method

T(n-1) = T(n-2) +1

substitute in eqn 1

T(n) = T(n-2)+2

sub T(n-2) in eq 1

T(n-2)= T(n-3) + 3

now T(n) = T(n-3) +3

continue for k times

T(n) = T(n-k) + k

Assume n-k =0

therefore n=k

T(n) = T(n-n) +n

= T(0) +n

= 1+n

T(n) = O (n)

void test (int n)


{
if (n>0)
{
for(i=0; i<n; i++)
{
printf("%d",n);
}
test(n-1);
}
}

Recursion tree method

T(n)

n T(n-1)

n-1 T(n-2)

n-2 T(n-3)

T(2)
2 T(1)

1 T(0)

0+1+2+...................+n-1+n = n(n+1)/2

=O(n^2)

Substitution method

T(n) = T(n-1) +n --- eqn 1

T(n-1)=T(n-2) + n-1

subs in eqn 1

T(n) = [ T(n-2)+n-1] +n

= T(n-2) + (n-1) +n -------eqn 2

T(n-2) = T(n-3) + (n-2)

subs in eqn 2

T(n) = [T(n-3) + (n-2) ] + (n-1) +n

= T(n-3) + (n-2) + (n-1) + n

continue for k times

= T(n-k) + T( n-(k-1)) + T(n-(k-2)) +........+ (n-1) +n

n-k=0
n=k

= T(n-n) + T(n-n+1) + T(n-n+2) +...........+ (n-1) +n

= T(0) + T(1) + T(2) +.................................+ (n-1) +n

T(n) = T(0)+ 1+2+3+.............................+ (n-1) +n

T(n)=1+n(n+1)/2

= O(n^2)

Dividing function

Algorithm test (int n)

if (n>1)

printf( "%d" ,n);

test (n/2);

T(n) = T(n/2) +1

Recurrence Relation

T(n) = { 1 n=1

{ T(n/2) +1 n>1

Recursion tree method


T(n)

1 T(n/2)

1 T(n/2^2)

1 T(n/2^3)

1
T(n/2^k)

total k steps

n/2^k=1

n= 2^k

k= log n [base 2]

Substitution Method

T(n) = T(n/2) +1 ------------------- 1


T(n/2) = T(n/2^2) + 1

substitute in eqn 1

T(n) = [ T(n/2^2) +1 ] + 1

T(n) = T(n/2^2) + 2

= T3(n/2^3) + 3

continue for k times

T(n) = T(n/2^k) + K

Assume n/2^k = 1

n= 2^k

k= log n [base 2]

T(n) = T(1) + log n

= 1 + log n

= log n [base 2]

Algorithm test (int n)

if (n>1)

for(i=0; i<n; i++)

//stmts; }

test (n/2);

test (n/2);
}

T(n) = 2T(n/2) + n

Recurrence Relation

T(n) = { 1 n=1

{ 2T(n/2) + n n>1

tree method

T(n)

T(n/2) T(n/2) n

n/2 n/2

n/2^2 n/2^2 n/2^2 n/2^2

n/2^3 n/2^3 n/2^3 n/2^3 n/2^3 n/2^3 n/2^3 n/2^3

continue for k times

n/2^k .............................................. n/2^k

totallly for eac step n. k times


n/2^k=1

n= 2^k

k = log n [base2]

so O(nlogn)

induction method

T(n) = 2T(n/2) + n ------ 1

T(n/2) = 2T(n/2^2) + n/2

subs in eqn 1

T(n) = 2 [ 2T(n/2^2) + n/2] + n

= 2^2 T(n/2^2) +n + n

= 2^2 T(n/2^2) +2n

T(n/2^2) = 2T(n/2^3) + n/2^2

= 2^2[ 2T(n/2^3) +n/2^2] + 2n

= 2^3 T (n/2^3) +n+2n

continue for k times

= 2^k T(n/2^k) +kn

n/2^k = 1 -> 2^k = n -> k = log n [base 2]

= n T(1) + n.log n

= n+ nlogn

= O(nlogn)

You might also like