L5_-_Recursion_and_Analysis_of_Algorithm_(4_on_1)

L5_-_Recursion_and_Analysis_of_Algorithm_(4_on_1) - Lecture...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Recursion Mirror, mirror on the wall 2 --- 71 [CS1102C AY0708S1 L5] Lecture Outline ± Recursion - Basic idea ± Iteration versus Recursion ± How recursion works ± Recursion - How to ± More examples on recursion ² Printing a Linked-List ² Printing a Linked-List in reverse ² Choose k out of n things ² Tower of Hanoi ² Fibonacci Numbers 3 --- 71 [CS1102C AY0708S1 L5] Recursion : The Basic Idea ± The process of solving a problem with a function that calls itself directly or indirectly ² The solution can be derived from solution of smaller problem of the same type ± Example: ² Factorial(4) = 4 * Factorial (3) ± This process can be repeated ² E.g. Factorial(3) can be solved in term of Factorial(2) ± Eventually, the problem is so simple that it can solve immediately ² E.g. Factorial(0) = 1 ± The solution to the larger problem can then be derived from this … 4 --- 71 [CS1102C AY0708S1 L5] Recursion: The Main Ingredients ± To formulate a recursive solution: ² Identify the “ simplest ” instance (The base case : so that we can solve it without recursion) ² Identify “ simpler ” instances of the same problem (The recursive case : so that we can make recursive calls to solve them) ± Identify how the solution from the simpler problem can help to construct the final result ² Be sure we are able to reach the “ simplest instance ± So that we will not get an infinite recursion
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
5 --- 71 [CS1102C AY0708S1 L5] Example : Factorial ± Lets write a recursive function factorial(k) that finds k! ² Base Case: ± Return 1 when k = 0 ± Corresponds to this bit of C code: if( k == 0 ) return 1; ² Recursive Case: ± Return k * (k-1)! return k * factorial(k-1); 6 --- 71 [CS1102C AY0708S1 L5] Example : Factorial (code) ± Full code for factorial : long factorial(int k) { if ( k == 0 ) return 1; else return k * factorial(k-1); } Base Case: factorial( 0 ) = 1 Recursive Case: factorial( k ) = k * factorial( k – 1) 7 --- 71 [CS1102C AY0708S1 L5] Understanding Recursion ± A recursion always goes through two phases: ² A wind-up phase: ± When the base case is not satisfied i.e. the function calls itself ± This phase carries on until we reach the base case ² An unwind phase: ± The recursively called functions return their values to previous “instances” of the function call ± i.e. the last function returns to its parent (the 2 nd last function), then the 2 nd last function returns to the 3 rd last function etc… ± Eventually reaches the very first function, which computes the final value. 8 --- 71 [CS1102C AY0708S1 L5] The Factorial : Windup Phase ± Lets trace the execution of factorial(3) ( factorial abbreviated as fact ) fact( 3 ) k = 3 fact( 2 ) k = 2 fact( 1 ) k = 1 fact( 0 ) k = 0 k is not zero returns 3 * fact( 2 ) k is not zero returns 2 * fact( 1 ) k is not zero returns 1 * fact( 0 )
Background image of page 2
9 --- 71 [CS1102C AY0708S1 L5] The Factorial : Unwind Phase fact( 3 ) k = 3 fact( 2 ) k = 2 fact( 1 ) k = 1 fact( 0 ) k = 0 k is not zero return 3 * fact( 2 ) k is not zero return 2 * fact( 1 ) k is not zero return 1 * fact(0) k is zero return 1 return 1 * 1 = 1 return 2 * 1 = 2 return 3 * 2 = 6 10 --- 71 [CS1102C AY0708S1 L5] Recursion vs. Loops ± Most recursions essentially accomplishes a loop (iterations) ± Recursions are usually much more elegant
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 18

L5_-_Recursion_and_Analysis_of_Algorithm_(4_on_1) - Lecture...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online