About Recursion
Recursion is the technique to solve problems by dividing the problem into :
1. A Base Case / Termination Condition.
2. A Sub-Problem / Recursive Call.
3. Combining the results of Sub-Problems to obtain the final result.
Wanna Experience Recursion ? Click the following link …
http://s4sagar.wordpress.com/2009/07/30/about-recursion/
Another funny example for recursion ,
Try googling “recursion”
http://www.google.co.in/search?hl=en&rlz=1C1CHMI_en-USIN307IN308&ei=6ARySvSgB5zW7AOB7oyrCw&sa=X&oi=spell&resnum=0&ct=result&cd=1&q=recursion&spell=1
You will see a “Did you mean : recursion” ![]()
Try clicking “recursion” in “Did you mean: recursion” and you will understand what recursion is
Examples:
1. Factorial Problem
Problem Statement : To evaluate n !
Recursive Solution :
unsigned int fact ( int n ) {
if( n <= 1 ) return 1;
return n * fact ( n -1 ) ;
}
Terminating Condition /Base Case : n <= 1
Sub-Problem : To evaluate fact( n – 1 )
Final Solution : n * fact ( n - 1 )
You can see that it combines all the subproblems together .
Advantages :
1. Small source code.
2. Easy to code and understand.
Disadvantages:
1.Take a lot of memory if the recursion depth is high.
For Example, if fact( 500 ) is called,it has to push 499 intermediate results into the System’s Stack before it can evaluate the result.
Simple Optimisation:
unsigned int fact( int n ) {
int result = 1 ;
for( int i = 2 ; i <= n ; ++ i )
result *= i;
return result;
}
You can see that there is no time wasted in the flow of controll between functions and its sub-functions , and storing the intermediate values into the stack. You can thus save the Storing and Fetching time into the stack.
Hmm .. Its 3 : 17 A.M already , Guess i ll link you guys to more resources …
2. Fibonacci Series :
http://www.cs.northwestern.edu/academics/courses/110/html/fib_rec.html
3.Towers of Hanoi:
http://www.cs.cmu.edu/~cburch/survey/recurse/hanoi.html
You also get the solve the Tower Of Hanoi by urself in the above link
(Browser should JavaScript 1.2 Enabled)
4.Eight Queens Problem:
http://www.animatedrecursion.com/advanced/the_eight_queens_problem.html
More general n-Queens problem :
http://theory.cs.uvic.ca/amof/e_queeI.htm
5. An example TopCoder Problem :
Problem Statement |
||||||||
| There is nothing more beautiful than just an integer number.
You are given an integer n. Write down n in decimal notation with no leading zeroes, and let M be the number of written digits. Perform the following operation exactly ktimes:
Return the maximal possible number you can get at the end of this procedure. If it’s not possible to perform k operations, return -1 instead. |
||||||||
Constraints |
||||||||
| - | n will be between 1 and 1,000,000, inclusive. | |||||||
| - | k will be between 1 and 10, inclusive. | |||||||
Examples |
||||||||
| 0) | ||||||||
|
||||||||
| 1) | ||||||||
|
||||||||
More Links:
http://www.csse.monash.edu.au/~lloyd/tildeAlgDS/Recn/
http://www.topcoder.com/tc?module=ProblemArchive&sr=&er=&sc=&sd=&class=&cat=Recursion&div1l=&div2l=&mind1s=&mind2s=&maxd1s=&maxd2s=&wr=
- Vidya Sagar S
TopCoder Profile : http://www.topcoder.com/tc?module=MemberProfile&cr=22652618
s4sagar@gmail.com
August 11, 2009 at 10:43 am
awsome…!!!!
i have become your fan…:)
August 26, 2009 at 8:11 pm
Great Sagar.Really useful
September 21, 2009 at 4:39 pm
simply a perfect guidance blog…pls continue to drop in the programming tips..
October 11, 2009 at 3:41 pm
your doing a great job man!!!!
April 17, 2010 at 6:06 pm
My experience shows that recursion helps only when you are doing something that involves a stack, like dfs. It does NOT reduce the code when you are trying to solve recurrences. When you are writing an iterative version, you just have to figure out the order in which you have to write the loops in the required order. On the other hand, in recursion, you will have to write a separate function, take care of memoization, etc.