Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to calculate the time complexity of a recursive function

Time complexity of a recursive function depends on the number of times the 
function is calling himself multiply this by the each time how many time 
complexity is taken on each call it can be O(1), O(log n), O(n) or more. 

Let's say our function is calling n-1 times and each it is doing some 
comparisons and call it self again. So function itself has a time complexity of
O(1). 
So the total time complexity will be (n-1)*1 which is o(N).
Comment

Time complexity in recursion

void recursiveFun4(int n, int m, int o)
{
    if (n <= 0)
    {
        printf("%d, %d
",m, o);
    }
    else
    {
        recursiveFun4(n-1, m+1, o);
        recursiveFun4(n-1, m, o+1);
    }
}
Here, it's O(2^n), or exponential, since each function call calls itself twice unless it has been recursed n times.
Comment

Time complexity of recursive function

Recursion Tree Method
(To solve recurrence and get time complexity of recursive function)

1)We write non-recursive part as root of tree and recursive part as it's children.
Then take sum of nodes in each level & find Sum of these sums of each level 
generally terms of thi new Sum will be in ap,gp etc with number of terms as height of tree.
Note : if recursive part is T(n/2) then height of tree is log(n)
if it's T(n-1) then height is n.

2)We keep on expanding Children untill we reach the base case ( aka leaves ).

T(n) = 2T(n-1) + cn
T(1) = c
Where cn is non recursive part.

Note : instead of 0(1) we write constant c, for 0(n) we write cn 
Keep c same everywhere.
Comment

PREVIOUS NEXT
Code Example
Javascript :: unwind check after null or undefined 
Javascript :: how to change root variable css 
Javascript :: sum all numbers in a range javascript 
Javascript :: how to get element by id in node js 
Javascript :: mongoose join multiple collections 
Javascript :: or inside if javascript 
Javascript :: convert 24 hour to 12 hour moment js 
Javascript :: Random Integer 1-10 
Javascript :: jqery get text 
Javascript :: javascript get item in array by id 
Javascript :: localstorage vs sessionstorage 
Javascript :: insertadjacenthtml javascript 
Javascript :: javascript on window resize 
Javascript :: how to copy value instead of reference js 
Javascript :: replace many chracters js 
Javascript :: javascript encode base64 
Javascript :: get name of input jquery 
Javascript :: convert json to dataframe 
Javascript :: javascript anagram 
Javascript :: This version of CLI is only compatible with Angular versions 
Javascript :: react current path 
Javascript :: javascript max array 
Javascript :: how set default value for react-select 
Javascript :: array filter 
Javascript :: upload image react 
Javascript :: javascript calculator 
Javascript :: react script 
Javascript :: how remove the spaces from the string, then return the resultant string 
Javascript :: array js fill 
Javascript :: Link vs NavLink in react-router-dom 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =