View Single Post
Recursion
Old
chickentree
Super Moderator
 
Status: Offline
Posts: 210
Join Date: Dec 2012
Location: Frosno, Ca
Default Recursion - 01-31-2016, 04:20 PM

In order to forget recursion,you must first forget recursion.
Is a toung in cheek way to remember it.

On the one hand recursion can be an elegant solution to a complex problem while on the other hand, it's execution can be slow and resource intensive (using lot of memory, disk space or both.)

Basically, if you can beappreak a problem's solution into smaller and easier to solve problems, you can use recursion to solve it. At the same time it should probably be made clear that any recursive method can be rewritten in a non-recursive way.
A silly example:
Code:
count(int)
  if(int>0)
    return(count(int-1)+1) // the call that makes this a recurrence 
  else
    return(0)  // the condition that cause the recursion to end.

So count(3) would result in:
  count(3)
    return(count(2))+1
      return count(1)+1
        return(count(0)+1)
          return(0)
Which would call itself 4 times until the argument was 0 and then begin ending each recursive step back up the call chain returning 0, 1 , 2, and finally 3. Note that while the function is recurring, none of the previous calls can end. This is what uses up resources.

Hope this helped,


Mark Henwood
mhenwood@ieee.org
   
Reply With Quote