edited by
406 views
0 votes
0 votes
consider the following c program

A(n)

{

if(n<=1)

return(n2 +n+1)

else

return(5A(n/2)+3A(n/2)+MA(n))

}

where MA(n) has complexity O(n2).

1.what is the recurrence relation for value.?

2.what is the the recurrence relation for time complexity?
edited by

1 Answer

Best answer
2 votes
2 votes

For Time Complexity of recursive programs

we only consider the statements which call the function again and again

so, in the statement below

return(5A(n/2)+3A(n/2)+MA(n))

Now above expression means 5 times of A(n/2) + 3 times of A(n/2) + MA(n)

now how many times our function A(n) has been called again and with what subproblem sizes?

(1) First with sub-problem size of n/2 whose result is to be multiplied by 5.

(2) Second again with the sub-problem size of A(n/2) with the result multiplied by 3.

The recurrence for this program is

2T(n/2) + O($n^2$)(This is complexity if MA(n)) which evaluates to $\theta(n^2)$ by master's theorem.

$n^{\log _b a} = n^{\log _2 2}=n$

and f(n)=$O(n^2)$

This is for time complexity.

And for the value , we need to consider what exact value the program will return at each step

So for the statement

return(5A(n/2)+3A(n/2)+MA(n))

we take into account constants 5 and 3 when we are calling function A(n)

So value recurrence relation is :

5T(n/2) + 3T(n/2) +MA(n) = 8T(n/2) + MA(n) if n>1

$n^2 -n+1$ if n<=1.

selected by

Related questions

1 votes
1 votes
0 answers
1
srestha asked May 19, 2019
629 views
Let $A(n)$ denotes the number of $n$ bit binary strings which have no pair of consecutive $1’s.$ what will be recurrence relation for it and what will be it’s Time Co...
0 votes
0 votes
3 answers
2
Nisha Bharti asked Sep 26, 2022
728 views
What is the time & space complexity of this algorithm?Main(){ for(i=n; i>10; i=i^1/4) { for(j=201; j<n^3; j=j+400) ...
0 votes
0 votes
1 answer
3
tusharb asked Feb 18, 2022
691 views
As we know the time complexity of solving the greedy knapsack algorithm depends mainly on the sorting algorithm used, Can we use counting sort as the sorting algorithm to...
1 votes
1 votes
1 answer
4
sumitr asked Apr 10, 2019
1,298 views
What is the best case and worst case of the algorithm? And when will best case and worst case will happen??int main() { for(i=1 ; i<=n ; i++) { if(n%i == 0) { for(j=1 ; j...