Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS1120: Recursion. 2 What is Recursion? A method is said to be recursive if the method definition contains a call to itself A recursive method is a.

Similar presentations


Presentation on theme: "1 CS1120: Recursion. 2 What is Recursion? A method is said to be recursive if the method definition contains a call to itself A recursive method is a."— Presentation transcript:

1 1 CS1120: Recursion

2 2 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 (…)

3 3 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.

4 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!!! 4

5 5 Recursion Example Fig. 6.14Recursive evaluation of 5!. (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 5! 5 * 4! 4 * 3! 3 * 2! 2 * 1! 1

6 6 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

7 7 Factorial Method public static long Factorial(long number) { // base case if (number <= 1) return 1; // recursion step else return number * Factorial(number -1); }

8 8 public static long Factorial(long number) { if (number <= 1) return 1; else { int temp; temp = Factorial(number -1); return number * temp; } number=4 Factorial(4) is Called public static long Factorial(long number) { if (number <= 1) return 1; else { int temp; temp = Factorial(number -1); return number * temp; } number=3 public static long Factorial(long number) { if (number <= 1) return 1; else { int temp; temp = Factorial(number -1); return number * temp; } number=2 public static long Factorial(long number) { if (number <= 1) return 1; else { int temp; temp = Factorial(number -1); return number * temp; } number=1

9 9 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

10 10 Example Using Recursion: The Fibonacci Sequence What is importance of Fibonacci Sequence –http://en.wikipedia.org/wiki/Fibonacci_numberhttp://en.wikipedia.org/wiki/Fibonacci_number –http://people.bath.ac.uk/lec20/uses.htmlhttp://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

11 11 6.15 Example Using Recursion: The Fibonacci Sequence Fig. 6.17Set of recursive calls to method Fibonacci (abbreviated as F ). return 1return 0 F( 1 )F( 0 )return 1 F( 3 ) F( 2 )F( 1 ) + return +

12 12 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)

13 13 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 )

14 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 14 I IIIII I IIIII

15 How to Solve the Problem Recall DCG 15 I IIIII I IIIII IIIIII I IIIII

16 How to Solve the Problem Specify the following method 16 moveDiscs(int n, int fromPeg, int tempPeg, int toPeg) { } 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. 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); }

17 What is Wrong? 17 moveDiscs(int n, int fromPeg, int tempPeg, int toPeg) { } 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. 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!!!

18 What is Correct Spec? 18 moveDiscs(int n, int fromPeg, int tempPeg, int toPeg) { } 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. 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?


Download ppt "1 CS1120: Recursion. 2 What is Recursion? A method is said to be recursive if the method definition contains a call to itself A recursive method is a."

Similar presentations


Ads by Google