Download presentation
Presentation is loading. Please wait.
Published byOctavia Stephanie Dorsey Modified over 9 years ago
1
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion
2
Java Programming: Guided Learning with Early Objects2 Objectives Learn about recursive definitions Determine the base case and general case of a recursive definition Learn about recursive algorithms
3
Java Programming: Guided Learning with Early Objects3 Objectives (continued) Learn about recursive methods Become familiar with direct and indirect recursion Learn how to use recursive methods to implement recursive algorithms
4
Java Programming: Guided Learning with Early Objects4 Recursive Definitions Recursion: reducing a problem to successively smaller versions of itself –Powerful way to solve problems for which the solution is otherwise complicated
5
Java Programming: Guided Learning with Early Objects5 Recursive Definitions (continued) Factorial: –0! = 1equation 11-1 –n! = n ( n – 1)! if n > 0equation 11-2 Equation 11-1 is the base case Equation 11-2 is the general (recursive) case
6
Java Programming: Guided Learning with Early Objects6 Recursive Definitions (continued) Recursion definition: defined in terms of a smaller version of itself Every recursive definition must have at least one base case
7
Java Programming: Guided Learning with Early Objects7 Recursive Definitions (continued) General case eventually must be reduced to a base case Base case stops the recursion Recursive algorithm: finds solution by reducing problems to smaller versions of itself
8
Java Programming: Guided Learning with Early Objects8 Recursive Definitions (continued) Recursive method: method that calls itself –Body contains a statement that calls same method before completing the current call –Must have one or more base cases –General solution eventually must reduce to base case Recursive algorithms implemented with recursive methods
9
Java Programming: Guided Learning with Early Objects9 Recursive Definitions (continued) Factorial definition: public static int fact(int num) { if (num == 0) return 1; else return num * fact(num -1); }
10
Java Programming: Guided Learning with Early Objects10 Figure 11-1 Execution of fact(4)
11
Java Programming: Guided Learning with Early Objects11 Recursive Definitions (continued) Think of a recursive method as having unlimited copies of itself Every recursive call has its own code, parameters, and local variables
12
Java Programming: Guided Learning with Early Objects12 Recursive Definitions (continued) After completing a recursive call, control goes back to previous call Current call must execute completely Execution in previous call begins from point immediately following recursive call
13
Java Programming: Guided Learning with Early Objects13 Direct and Indirect Recursion Directly recursive method calls itself Indirectly recursive method calls another method –Eventually original method is called –Involves several methods –Can be elusive; take extra care in design
14
Java Programming: Guided Learning with Early Objects14 Infinite Recursion If every recursive call results in another recursive call, method is infinitely recursive –Base case never executes Every recursive call allocates memory –System saves information to transfer control back to caller
15
Java Programming: Guided Learning with Early Objects15 Infinite Recursion (continued) Computer memory is finite Infinitely recursive method continues until system runs out of memory
16
Java Programming: Guided Learning with Early Objects16 Designing Recursive Algorithms and Methods Determine limiting conditions Identify base cases –Provide direct solution to each base case Identify general cases –Provide solution to each general case in terms of smaller version of itself
17
Java Programming: Guided Learning with Early Objects17 Problem Solving Using Recursion Largest element in an array –list is name of array containing list elements –If list has length 1, single element is the largest –Find largest element by: max(list[a],largest(list[a+1]…list[b]))
18
Java Programming: Guided Learning with Early Objects18 Figure 11-2 List with six elements
19
Java Programming: Guided Learning with Early Objects19 Figure 11-3 List with four elements
20
Java Programming: Guided Learning with Early Objects20 Figure 11-4 Execution of largest(list, 0, 3)
21
Java Programming: Guided Learning with Early Objects21 Fibonacci Numbers Recall Chapter 5 designed a program to determine a Fibonacci number –Each Fibonacci number is the sum of the previous two
22
Java Programming: Guided Learning with Early Objects22 Fibonacci Numbers (continued)
23
Java Programming: Guided Learning with Early Objects23 Fibonacci Numbers (continued) public static int Fib(int a, int b, int n){ if (n==1) return a; else if (n == 2) return b else return Fib(a,b,n-1) + Fib(a,b,n-2) }
24
Java Programming: Guided Learning with Early Objects24 Figure 11-5 Execution of rFibNum(2,3,5)
25
Java Programming: Guided Learning with Early Objects25 Towers of Hanoi At creation of universe, priests in the temple of Brahma given three diamond needles One needle contained 64 golden disks Each disk slightly smaller than disks below it Task: move all 64 disks from first needle to third
26
Java Programming: Guided Learning with Early Objects26 Towers of Hanoi (continued) Rules: –Only one disk moved at a time –Removed disk must be placed on one of the other two needles –Larger disk cannot be placed on smaller disk Once all disks moved from first needle to third, universe comes to an end
27
Java Programming: Guided Learning with Early Objects27 Figure 11-5 Towers of Hanoi with three disks
28
Java Programming: Guided Learning with Early Objects28 Towers of Hanoi (continued) One disk: –Base case –Move disk from needle one to needle three
29
Java Programming: Guided Learning with Early Objects29 Towers of Hanoi (continued) Two disks: –First disk moves to second needle –Second disk moves to third needle –First disk moves to third needle
30
Java Programming: Guided Learning with Early Objects30 Towers of Hanoi (continued) Three disks: –Two problems of moving two disks 64 disks: –Two problems of moving 63 disks n disks: –Two problems of moving n-1 disks
31
Java Programming: Guided Learning with Early Objects31 Figure 11-6 Solution to Towers of Hanoi with three disks
32
Java Programming: Guided Learning with Early Objects32 Towers of Hanoi (continued) public static void moveDisks(int count, int needle1, int needle3, int needle2) { if (count > 0) { moveDisks (count-1, needle1, needle2,needle3); moveDisks (count-1, needle2, needle3, needle1); }
33
Java Programming: Guided Learning with Early Objects33 Towers of Hanoi: Analysis Needle 1 contains 64 disks –Number of moves to needle 3: 2 64 -1 ≈ 1.6 x 10 19 Number of seconds in one year: 3.2 x 10 7
34
Java Programming: Guided Learning with Early Objects34 Towers of Hanoi: Analysis (continued) Priests move one disk per second without resting: 5 x 10 11 years Estimated age of universe: 1.5 x 10 10 years Computer: 1 billion moves per second, finishes in 500 years
35
Java Programming: Guided Learning with Early Objects35 Recursive Binary Search Recall binary search from Chapter 9 Find middle element Compare sought element with middle Repeat on half of list –Use method call
36
Java Programming: Guided Learning with Early Objects36 Recursive Binary Search (continued) public static int rBin(int[] list, int first, int last, int srchItm ) { int mid; int location = 0; if (first <= last) { mid = (first + last)/2; if (list[mid] == srchItm) location = mid;
37
Java Programming: Guided Learning with Early Objects37 Recursive Binary Search (continued) else if (list[mid] > srchItm) location = rBin(list, first, mid – 1, srchItm); else location = rBin(list, mid + 1, last, srchItm); }// end if first <= last if (first > location || last < location) location = -1; return location; }//end rBin
38
Java Programming: Guided Learning with Early Objects38 Figure 11-8 A sorted list
39
Java Programming: Guided Learning with Early Objects39 Figure 11-9 Tracing the recursive binary search algorithm
40
Java Programming: Guided Learning with Early Objects40 Recursion or Iteration? Often two ways to solve a problem: –Recursion –Iteration Iterative algorithm often seems simpler Iterative control structure: uses a looping structure to repeat a set of statements
41
Java Programming: Guided Learning with Early Objects41 Recursion or Iteration? (continued) No general answer to which is better Guidelines: –Nature of the solution –Efficiency of solution
42
Java Programming: Guided Learning with Early Objects42 Recursion or Iteration? (continued) Every recursive call has its own parameters and local variables –Requires system to allocate space when method is called –Memory deallocated when method terminates Recursive calls have overhead in memory and execution time
43
Java Programming: Guided Learning with Early Objects43 Recursion or Iteration? (continued) Efficiency of programmer’s time also important consideration –Balance with execution efficiency Choice may be a matter of personal preference Any program that can be written recursively can be written iteratively If iterative solution is at least as obvious and easy as recursive solution, choose iterative
44
Java Programming: Guided Learning with Early Objects44 Summary Recursion: solving a problem by reducing it to smaller versions of itself Recursive definition defines problem in terms of smaller versions of itself Every recursive definition has one or more base cases Recursive algorithm solves a problem by reducing it to smaller versions of itself
45
Java Programming: Guided Learning with Early Objects45 Summary (continued) Solution to a problem in a base case obtained directly Recursive method calls itself Recursive algorithms implemented as recursive methods Recursive method must have one or more base cases
46
Java Programming: Guided Learning with Early Objects46 Summary (continued) General solution breaks problem into smaller versions of itself General case eventually reduced to a base case Base case stops the recursion
47
Java Programming: Guided Learning with Early Objects47 Summary (continued) Tracing a recursive method: –Think of recursive method as having unlimited copies of itself –Every call to recursive method executes the code with its own set of parameters and variables
48
Java Programming: Guided Learning with Early Objects48 Summary (continued) Tracing a recursive method (continued): –After completing recursive call, control goes back to calling environment –Current call executes completely before control returns –Execution in previous call continues from point following recursive call
49
Java Programming: Guided Learning with Early Objects49 Summary (continued) Method is directly recursive if it calls itself Method is indirectly recursive if it: –Calls another method –Eventually results in call to itself
50
Java Programming: Guided Learning with Early Objects50 Summary (continued) Design a recursive method: –Understand problem requirements –Determine limiting conditions –Identify base cases Provide direct solution to base cases –Identify general cases Provide recursive solution to each general case
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.