CS1120: Recursion
What is Recursion? A method is said to be recursive if the method definition contains a call to itself A recursive method is a method that calls itself method A (…)
Why Recursion? In pursuit of absolute simplicity!!! An important idea behind recursion is to Divide, Conquer, and Glue (DCG) Divide the Problem P into subproblems P1, P2,…, Pn Conquer the subproblem by solving them S1, S2,…, Sn Glue subsolutions S1, S2,…, Sn together into the solution S to the whole problem.
Example of Recursion Often these subtasks are similar, but get smaller in size. Next, we further break these subtasks into sub-subtasks until the sub-subtasks cannot be further broken. For example, factorial(3) can be calculated if factorial(2) can be. factorial(2) can be calculated if factorial(1) can be calculated. factorial(1) is not further broken since everyone know it is 1!!!
Recursion Example (a) Procession of recursive calls. 5! 5 * 4! 4 * 3! 3 * 2! 2 * 1! 1 (b) Values returned from each recursive call. Final value = 120 5! = 5 * 24 = 120 is returned 4! = 4 * 6 = 24 is returned 2! = 2 * 1 = 2 is returned 3! = 3 * 2 = 6 is returned 1 returned Fig. 6.14 Recursive evaluation of 5!.
Some Terminology Base Case: the simplest case In factorial function: the value of factorial(1) is 1. Recursion Call/Recursive Step: the method calls a “fresh” copy of itself (this is used to work on a smaller version of the problem) and often includes a return statement
Factorial Method public static long Factorial(long number) { // base case if (number <= 1) return 1; // recursion step else return number * Factorial(number -1); }
Factorial(4) is Called number=4 number=3 public static long Factorial(long number) { if (number <= 1) return 1; else { int temp; temp = Factorial(number -1); return number * temp; } public static long Factorial(long number) { if (number <= 1) return 1; else { int temp; temp = Factorial(number -1); return number * temp; } number=1 number=2 public static long Factorial(long number) { if (number <= 1) return 1; else { int temp; temp = Factorial(number -1); return number * temp; } public static long Factorial(long number) { if (number <= 1) return 1; else { int temp; temp = Factorial(number -1); return number * temp; }
Why a recursive method works? A recursive method has its own fresh copy when it is called. All variables such as parameters and local variables have their own copies. In Factorial method, variables such as number and temp have their own copies. The copies of number and temp when number=4 is different from the copies of number and temp when number=3
Example Using Recursion: The Fibonacci Sequence What is importance of Fibonacci Sequence http://en.wikipedia.org/wiki/Fibonacci_number http://people.bath.ac.uk/lec20/uses.html Fibonacci Sequence F(0) = 0 F(1) = 1 F(n) = F(n - 1) + F(n - 2) Recursion is used to evaluate F(n) Complexity theory How hard computers need to work to perform algorithms
6.15 Example Using Recursion: The Fibonacci Sequence return 1 return 0 F( 1 ) F( 0 ) F( 3 ) F( 2 ) + return Fig. 6.17 Set of recursive calls to method Fibonacci (abbreviated as F).
How to Ensure DCG When Using Recursion The key to use the recursion is to find whether the subtask is the same as the original task but in a small size. To do so, you need to write the clear and unambiguous method specification. (What does the method do, what does each parameter denote, and what does the method return)
Exercise Given an integer array A=[2,1,44,3,2,54,1,66], calculate the sum of the first n elements CalulateSumOfArray(int[] A, int n )
Hanoi Tower There are three pegs and 3 disks are initially placed on peg I. Goal is to move all 3 disks to peg III using peg II. But every move must follow the conditions: You can only move one disk at a time (from any peg to any other peg), and You may not stack a larger disk on top of a smaller disk I II III I II III
How to Solve the Problem Recall DCG I II III I II III I II III I II III
How to Solve the Problem Specify the following method n: is the number of disks to be moved fromPeg, and toPeg denote the pegs where disks are moved from and to respectively. tempPeg is the temporary peg used during the movement. moveDiscs(int n, int fromPeg, int tempPeg, int toPeg) { } if (n >0) { moveDiscs(n-1, fromPeg, toPeg, tempPeg); system.out.print(“disc’’+ n + “is moved from”+fromPeg + “ to ” + toPeg); moveDiscs(n-1, tempPeg, fromPeg, toPeg); }
What is Wrong? In OO Programming, You must write a clear specification n: is the number of disks to be moved fromPeg, and toPeg denote the pegs where disks move from and to respectively. tempPeg is the temporary peg used during the movement. ??? moveDiscs(int n, int fromPeg, int tempPeg, int toPeg) { } if (n >0) { moveDiscs(n/2, fromPeg, toPeg, tempPeg); moveDiscs(n-n/2, fromPeg,tempPeg, toPeg); moveDiscs(n/2, tempPeg, fromPeg, toPeg); } Illegal Move!!! In OO Programming, You must write a clear specification for what you have programmed!!!
What is Correct Spec? All disks has been number from 1, 2,..,n with 1 denoting the smallest one and n the largest one n: is the largest disk number to be moved, i.e. disk 1, disk 2,…,disk n are to be moved fromPeg, and toPeg denote the pegs where disks move from and to respectively. tempPeg is the temporary peg used during the movement. moveDiscs(int n, int fromPeg, int tempPeg, int toPeg) { } if (n >0) { moveDiscs(n/2, fromPeg, toPeg, tempPeg); moveDiscs(n-n/2, fromPeg,tempPeg, toPeg); moveDiscs(n/2, tempPeg, fromPeg, toPeg); } This is not a correct solution!!! Based on the above specification, can you find another Solution to the Hanoi Tower Problem via DCG?