Has Path Sum (binary tree)
June 11, 2012
Basic Tree Traversals (Preorder, Inorder, Postorder)
June 11, 2012

Understanding Recursion

How it happens internally ?
Lets see what happens when we call the recursive function as sum(3);
Sum(3); will call Sum(2); which will in-turn call sum(1); So the memory stack will have activation records of three functions with each having a local variable n, something like below:

Also consider the fact that when a function calls another, then the state of the calling function (the value of temporary variables, the Instruction Pointer and other register values) is saved in the memory. This state will be reloaded into the memory when the called function will return the control to the calling function
(This is conceptually similar to the Context switch which happens in a muti-processing Operating system when one process is preempted to execute the another process and at sometime the control returns back to this process).
In the iterative version there will be only one function call to Sum(3) and two local variables i & s on the Activation Record(AR) of the function.
We have compared the two for n=3. You may want to compare them for n=1000. The recursive version will take

  • Time: O(n)
  • Memory: O(n)

While the Iterative version will take:

  • Time: O(n)
  • Memory: O(1)

The Time may look the same O(n) for both cases, but the constant multiplier with the recursive version will be much bigger than the Iterative version.
So recursion is a huge overhead. Both from memory point-of-view and execution time point-of-view.
Why should we use Recursion then?

Recursion wins in its Simplicity and Intuitive approach. Not every recursive function can be written in an iterative version with such an ease. For example, Tree traversal functions are very easy & intuitive to write in a recursive manner, but a pain to write the non-recursive version (By non-recursive I does not mean the Stack simulation version of recursive).

If the size of Input is large or Unbounded then recursion is not a good idea.

 
Next: Head and Tail Recursion …

8 Comments

  1. […] I have written in my earlier posts also, Tree problems are better solved using recursion (But recursion comes with its side effect). Finding the Mirror can also be visualized […]

  2. […] function to reverse the linked list is recursive, but as I said earlier that recursion comes with its costs. so its always better to recursive functions because of time and space […]

  3. […] Note, that Recursion comes with its own cost. to learn more, see this post… […]

  4. […] is a classical example of both head recursion and tail recursion happening in the same function. You may also want to see my previous post on ‘Recursive […]

  5. […] printing the remainders in reverse order (Tail Recursion). 00    Read more from C, C++ Recursion Click here to cancel […]

  6. […] is an example of Head Recursion. In Head recursion the recursive function is called first and then perform the […]

  7. anonymous says:

    Best explanation so far found on internet

Leave a Reply

Your email address will not be published. Required fields are marked *