Question

I have a doubt related with time and space complexity in following 2 case

Blockquote

Case I:

Recurion: Factorial calculation.

int fact(int n)
{
    if(n==0)
      return 1;
    else
      return (n*fact(n-1));
}

here how come time complexity become 2*n and space complexity proportional to n.

and

Case II:

Iterative:-

int fact(int n)
{
    int i, result = 1;
    if(n==0)
    result = 1;
    else
         {
           for(1=1;i<=n;i++)
              result*=i;
         }
     return (result);
}

Time complexity proportional to n and space complexity is constant. This always remain confusing to me.

Was it helpful?

Solution

If my reasoning is faulty, somebody please correct me :)

For the space complexity, here's my explanation:

For the recursive solution, there will be n recursive calls, so there will be n stacks used; one for each call. Hence the O(n) space. This is not the case for the iterative solution - there's just one stack and you're not even using an array, there is only one variable. So the space complexity is O(1).

For the time complexity of the iterative solution, you have n multiplications in the for loop, so a loose bound will be O(n). Every other operation can be assumed to be unit time or constant time, with no bearing on the overall efficiency of the algorithm. For the recursive solution, I am not so sure. If there are two recursive calls at each step, you can think of the entire call stack as a balanced binary tree, and the total number of nodes will be 2*n - 1, but in this case I am not so sure.

OTHER TIPS

From: https://cs.nyu.edu/~acase/fa14/CS2/module_extras.php

Space Complexity

Below we're going to compare three different calls to both the iterative and recursive factorial functions and see how memory gets used. Keep in mind that every variable that we declare has to have space reserved in memory to store it's data. So the space complexity of an algorithm in its simplest form is the number of variables in use. So in this simplest situation we can calculate approximate space complexity using this equation:

space complexity = number of function calls * number of variables per call

Time Complexity: The number of (machine) instructions which a program executes during its running time is called its time complexity in computer science.

Space Complexity:This is essentially the number of memory cells which an algorithm needs.

Case 1: In the program is of recursively calculating the factorial , so there will be one direct call to the function and than there will be backtracking, so the time complexity becomes 2*n.

Talking about the space complexity there will be n stacks declared during the point of execution of program, so it is n.

Case 2: This case is pretty simple here you have n iteration inside the for loop so time complexity is n

This is not the case for the iterative solution - there's just one stack and you're not even using an array, there is only one variable. So the space complexity is O(1)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top