Presentation is loading. Please wait.

Presentation is loading. Please wait.

Important Notes Remaining Schedule: recall the 2 bad weather days (Jan 8th, Jan 17th) we will need to make up. In providing the 2 weeks needed for the.

Similar presentations


Presentation on theme: "Important Notes Remaining Schedule: recall the 2 bad weather days (Jan 8th, Jan 17th) we will need to make up. In providing the 2 weeks needed for the."— Presentation transcript:

1 Important Notes Remaining Schedule: recall the 2 bad weather days (Jan 8th, Jan 17th) we will need to make up. In providing the 2 weeks needed for the big project (project 3 worth 12%) and lectures covering Ch 19 and Ch 20, I will do the following: You will have 2 weeks (or 4 class meetings) to work on Project 3 as promised (weeks of 4/9 and 4/16) The project will be rolled out on 4/9 Also in providing the remaining lectures, an online for Ch 19 will be posted on 4/11 and an online for Ch 20 will be posted on 4/18 China Trip/Final Exam Issue: Will be leaving May 1st for China for 13 days. Will set up exam with lockdown browser with cam – closed book – must pre-determine a suitable machine (labs ?) – practice exam will be setup – all must try it prior to the actual exam. I will determine and submit your final grade while abroad. Dr. Clincy - Lecture

2 Chapter 18 Recursion What is Recursion ?: When a thing, process or algorithm is defined in terms of itself or of its type A recursive method is one that can invoke itself directly or indirectly in solving some problem. Difference between “recursion” and “iteration” Recursion: function calling itself Iteration: controlled loop implementing function What are the benefits of recursion ?: It enables the programmer to develop a natural, straightforward and simple solution to an otherwise difficult problem Dr. Clincy - Lecture

3 Example - Recursive Methods
Two well known use of recursion: N-factorial (n!) Algorithm Fibonacci-series Problem N-factorial: mathematical expression for the number of ways n items of a data set can be arranged – permutations. Fibonacci-series: a series of numbers where every number after the first two numbers is the sum There are many different scientific and mathematical processes and phenomena that can be modeled using a Fibonacci series Dr. Clincy - Lecture

4 2! 3! N-Factorial (n!) Computing Factorial
N-factorial: mathematical expression for the number of ways n items of a data set can be arranged – permutations. 2! 3! Computing Factorial n! = n * (n-1)! 0! = 1 factorial(0) = 1; factorial(n) = n*factorial(n-1); Needed to eventually “stop” the recursion Why is 0!=1 ? Because there is one way to arrange a data set of “nothing”. Also, 1!=1 Dr. Clincy - Lecture

5 Computing Factorial Import needed User enter integer
Factorial method call Factorial method If 0, n!=1 n! = n x (n-1)! (n-1)! Is needed to eventually stop the recursion Function calling itself Enter a nonnegative integer: 4 [ENTER] Factorial of 4 is 24 Factorial of 10 is Dr. Clincy - Lecture

6 Computing Factorial factorial(4) factorial(0) = 1;
animation Computing Factorial factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(4) Dr. Clincy - Lecture

7 Example - Computing Factorial
animation Example - Computing Factorial factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(4) = 4 * factorial(3) Dr. Clincy - Lecture

8 Computing Factorial factorial(4) = 4 * factorial(3)
animation Computing Factorial factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(4) = 4 * factorial(3) = 4 * 3 * factorial(2) Dr. Clincy - Lecture

9 Computing Factorial factorial(4) = 4 * factorial(3)
animation Computing Factorial factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(4) = 4 * factorial(3) = 4 * 3 * factorial(2) = 4 * 3 * (2 * factorial(1)) Dr. Clincy - Lecture

10 Computing Factorial factorial(4) = 4 * factorial(3)
animation Computing Factorial factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(4) = 4 * factorial(3) = 4 * 3 * factorial(2) = 4 * 3 * (2 * factorial(1)) = 4 * 3 * ( 2 * (1 * factorial(0))) Dr. Clincy - Lecture

11 Computing Factorial factorial(4) = 4 * factorial(3)
animation Computing Factorial factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(4) = 4 * factorial(3) = 4 * 3 * factorial(2) = 4 * 3 * (2 * factorial(1)) = 4 * 3 * ( 2 * (1 * factorial(0))) = 4 * 3 * ( 2 * ( 1 * 1))) Dr. Clincy - Lecture

12 Computing Factorial factorial(4) = 4 * factorial(3)
animation Computing Factorial factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(4) = 4 * factorial(3) = 4 * 3 * factorial(2) = 4 * 3 * (2 * factorial(1)) = 4 * 3 * ( 2 * (1 * factorial(0))) = 4 * 3 * ( 2 * ( 1 * 1))) = 4 * 3 * ( 2 * 1) Dr. Clincy - Lecture

13 Computing Factorial factorial(4) = 4 * factorial(3)
animation Computing Factorial factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(4) = 4 * factorial(3) = 4 * 3 * factorial(2) = 4 * 3 * (2 * factorial(1)) = 4 * 3 * ( 2 * (1 * factorial(0))) = 4 * 3 * ( 2 * ( 1 * 1))) = 4 * 3 * ( 2 * 1) = 4 * 3 * 2 Dr. Clincy - Lecture

14 Computing Factorial factorial(4) = 4 * factorial(3)
animation Computing Factorial factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(4) = 4 * factorial(3) = 4 * (3 * factorial(2)) = 4 * (3 * (2 * factorial(1))) = 4 * (3 * ( 2 * (1 * factorial(0)))) = 4 * (3 * ( 2 * ( 1 * 1)))) = 4 * (3 * ( 2 * 1)) = 4 * (3 * 2) = 4 * (6) Dr. Clincy - Lecture

15 Computing Factorial factorial(4) = 4 * factorial(3)
animation Computing Factorial factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(4) = 4 * factorial(3) = 4 * (3 * factorial(2)) = 4 * (3 * (2 * factorial(1))) = 4 * (3 * ( 2 * (1 * factorial(0)))) = 4 * (3 * ( 2 * ( 1 * 1)))) = 4 * (3 * ( 2 * 1)) = 4 * (3 * 2) = 4 * (6) = 24 Dr. Clincy - Lecture

16 Trace Recursive factorial
animation Trace Recursive factorial Executes factorial(4) Dr. Clincy - Lecture

17 Trace Recursive factorial
animation Trace Recursive factorial Executes factorial(3) Dr. Clincy - Lecture

18 Trace Recursive factorial
animation Trace Recursive factorial Executes factorial(2) Dr. Clincy - Lecture

19 Trace Recursive factorial
animation Trace Recursive factorial Executes factorial(1) Dr. Clincy - Lecture

20 Trace Recursive factorial
animation Trace Recursive factorial Executes factorial(0) Dr. Clincy - Lecture

21 Trace Recursive factorial
animation Trace Recursive factorial returns 1 Dr. Clincy - Lecture

22 Trace Recursive factorial
animation Trace Recursive factorial returns factorial(0) Dr. Clincy - Lecture

23 Trace Recursive factorial
animation Trace Recursive factorial returns factorial(1) Dr. Clincy - Lecture

24 Trace Recursive factorial
animation Trace Recursive factorial returns factorial(2) Dr. Clincy - Lecture

25 Trace Recursive factorial
animation Trace Recursive factorial returns factorial(3) Dr. Clincy - Lecture

26 Trace Recursive factorial
animation Trace Recursive factorial returns factorial(4) Dr. Clincy - Lecture

27 factorial(4) Stack Trace
Dr. Clincy - Lecture

28 Example - Fibonacci Numbers
Fibonacci-series: a series of numbers where every number after the first two numbers is the sum Fibonacci series: … indices: fib(0) = 0; fib(1) = 1; fib(index) = fib(index -1) + fib(index -2); index >=2 fib(3) = fib(2) + fib(1) = (fib(1) + fib(0)) + fib(1) = (1 + 0) +fib(1) = 1 + fib(1) = = 2 Dr. Clincy - Lecture

29 Fibonacci Numbers fib(0) = 0; fib(1) = 1;
fib(index) = fib(index -1) + fib(index -2); index >=2 Allow user to enter desired index Initial method call to determine f-number at index Fibonacci method If index 0, f-number is 0 If index 1, f-number is 1 Recursion: method calling itself twice – implementing from left to right Enter an index for Fibonacci number: 1 [ENTER] The Fibonacci number at index 1 is 1 Enter an index for Fibonacci number: 6 [ENTER] The Fibonacci number at index 6 is 8 Enter an index for Fibonacci number: 7 [ENTER] The Fibonacci number at index 7 is 13 Dr. Clincy - Lecture

30 Example - Fibonnaci Number for Index =4
Left then Right Dr. Clincy - Lecture

31 Characteristics of Recursion
All recursive methods have the following characteristics: One or more base cases (the simplest case) are used to stop recursion. Every recursive call reduces the original problem, bringing it increasingly closer to a base case until it becomes that case. In general, to solve a problem using recursion, you break it into subproblems. If a subproblem resembles the original problem, you can apply the same approach to solve the subproblem recursively. This subproblem is almost the same as the original problem in nature with a smaller size. Dr. Clincy - Lecture

32 Recursion Disadvantage
Recursion Advantage Recursion is good for solving the problems that are inherently recursive. Recursion Disadvantage Recursion bears substantial overhead. Each time the program calls a method, the system must assign space for all of the method’s local variables and parameters. This can consume considerable memory and requires extra time to manage the additional space. Dr. Clincy - Lecture

33 Your chapter 18 has other examples of recursive methods implemented
Cover Lab 14 Dr. Clincy - Lecture


Download ppt "Important Notes Remaining Schedule: recall the 2 bad weather days (Jan 8th, Jan 17th) we will need to make up. In providing the 2 weeks needed for the."

Similar presentations


Ads by Google