chap14
TRANSCRIPT
![Page 1: Chap14](https://reader036.vdocument.in/reader036/viewer/2022081400/555c3f9bd8b42a0b038b4d60/html5/thumbnails/1.jpg)
Java Programming: From Problem Analysis to Program Design, 3e
Chapter 14
Recursion
![Page 2: Chap14](https://reader036.vdocument.in/reader036/viewer/2022081400/555c3f9bd8b42a0b038b4d60/html5/thumbnails/2.jpg)
Java Programming: From Problem Analysis to Program Design, 3e 2
Chapter Objectives
• Learn about recursive definitions
• Explore the base case and the general case of a recursive definition
• Learn about recursive algorithms
![Page 3: Chap14](https://reader036.vdocument.in/reader036/viewer/2022081400/555c3f9bd8b42a0b038b4d60/html5/thumbnails/3.jpg)
Java Programming: From Problem Analysis to Program Design, 3e 3
Chapter Objectives (continued)
• Learn about recursive methods
• Become aware of direct and indirect recursion
• Explore how to use recursive methods to implement recursive algorithms
![Page 4: Chap14](https://reader036.vdocument.in/reader036/viewer/2022081400/555c3f9bd8b42a0b038b4d60/html5/thumbnails/4.jpg)
Java Programming: From Problem Analysis to Program Design, 3e 4
Recursive Definitions
• Recursion– Process of solving a problem by reducing it to
smaller versions of itself
• Recursive definition– Definition in which a problem is expressed in
terms of a smaller version of itself– Has one or more base cases
![Page 5: Chap14](https://reader036.vdocument.in/reader036/viewer/2022081400/555c3f9bd8b42a0b038b4d60/html5/thumbnails/5.jpg)
Java Programming: From Problem Analysis to Program Design, 3e 5
Recursive Definitions (continued)
![Page 6: Chap14](https://reader036.vdocument.in/reader036/viewer/2022081400/555c3f9bd8b42a0b038b4d60/html5/thumbnails/6.jpg)
Java Programming: From Problem Analysis to Program Design, 3e 6
Recursive Definitions (continued)• Recursive algorithm
– Algorithm that finds the solution to a given problem by reducing the problem to smaller versions of itself
– Has one or more base cases– Implemented using recursive methods
• Recursive method– Method that calls itself
• Base case– Case in recursive definition in which the solution is
obtained directly – Stops the recursion
![Page 7: Chap14](https://reader036.vdocument.in/reader036/viewer/2022081400/555c3f9bd8b42a0b038b4d60/html5/thumbnails/7.jpg)
Java Programming: From Problem Analysis to Program Design, 3e 7
Recursive Definitions (continued)
• General solution– Breaks problem into smaller versions of itself
• General case– Case in recursive definition in which a smaller
version of itself is called– Must eventually be reduced to a base case
![Page 8: Chap14](https://reader036.vdocument.in/reader036/viewer/2022081400/555c3f9bd8b42a0b038b4d60/html5/thumbnails/8.jpg)
Java Programming: From Problem Analysis to Program Design, 3e 8
Tracing a Recursive Method
• Recursive method– Logically, you can think of a recursive method
having unlimited copies of itself– Every recursive call has its own
• Code• Set of parameters• Set of local variables
![Page 9: Chap14](https://reader036.vdocument.in/reader036/viewer/2022081400/555c3f9bd8b42a0b038b4d60/html5/thumbnails/9.jpg)
Java Programming: From Problem Analysis to Program Design, 3e 9
Tracing a Recursive Method (continued)
• After completing a recursive call– Control goes back to the calling environment– Recursive call must execute completely before
control goes back to previous call– Execution in previous call begins from point
immediately following recursive call
![Page 10: Chap14](https://reader036.vdocument.in/reader036/viewer/2022081400/555c3f9bd8b42a0b038b4d60/html5/thumbnails/10.jpg)
Java Programming: From Problem Analysis to Program Design, 3e 10
Recursive Definitions
• Directly recursive: a method that calls itself• Indirectly recursive: a method that calls another
method and eventually results in the original method call
• Tail recursive method: recursive method in which the last statement executed is the recursive call
• Infinite recursion: the case where every recursive call results in another recursive call
![Page 11: Chap14](https://reader036.vdocument.in/reader036/viewer/2022081400/555c3f9bd8b42a0b038b4d60/html5/thumbnails/11.jpg)
Java Programming: From Problem Analysis to Program Design, 3e 11
Designing Recursive Methods
• Understand problem requirements
• Determine limiting conditions
• Identify base cases
![Page 12: Chap14](https://reader036.vdocument.in/reader036/viewer/2022081400/555c3f9bd8b42a0b038b4d60/html5/thumbnails/12.jpg)
Java Programming: From Problem Analysis to Program Design, 3e 12
Designing Recursive Methods (continued)
• Provide direct solution to each base case
• Identify general case(s)
• Provide solutions to general cases in terms of smaller versions of general cases
![Page 13: Chap14](https://reader036.vdocument.in/reader036/viewer/2022081400/555c3f9bd8b42a0b038b4d60/html5/thumbnails/13.jpg)
Java Programming: From Problem Analysis to Program Design, 3e 13
Recursive Factorial Method
public static int fact(int num){ if (num = = 0) return 1; else return num * fact(num – 1);}
![Page 14: Chap14](https://reader036.vdocument.in/reader036/viewer/2022081400/555c3f9bd8b42a0b038b4d60/html5/thumbnails/14.jpg)
Java Programming: From Problem Analysis to Program Design, 3e 14
Recursive Factorial Method (continued)
![Page 15: Chap14](https://reader036.vdocument.in/reader036/viewer/2022081400/555c3f9bd8b42a0b038b4d60/html5/thumbnails/15.jpg)
Java Programming: From Problem Analysis to Program Design, 3e 15
Largest Value in Array
![Page 16: Chap14](https://reader036.vdocument.in/reader036/viewer/2022081400/555c3f9bd8b42a0b038b4d60/html5/thumbnails/16.jpg)
Java Programming: From Problem Analysis to Program Design, 3e 16
• if the size of the list is 1 - the largest element in the list is the only element in the list
• else - to find the largest element in list[a]...list[b] a. find the largest element in list[a + 1]...list[b]
and call it max b. compare list[a] and max
• if (list[a] >= max) the largest element in list[a]...list[b] is list[a]• else the largest element in list[a]...list[b] is max
Largest Value in Array (continued)
![Page 17: Chap14](https://reader036.vdocument.in/reader036/viewer/2022081400/555c3f9bd8b42a0b038b4d60/html5/thumbnails/17.jpg)
Java Programming: From Problem Analysis to Program Design, 3e 17
public static int largest(int[] list, int lowerIndex, int upperIndex){ int max; if (lowerIndex == upperIndex) return list[lowerIndex]; else { max = largest(list, lowerIndex + 1, upperIndex); if (list[lowerIndex] >= max) return list[lowerIndex]; else return max; }}
Largest Value in Array (continued)
![Page 18: Chap14](https://reader036.vdocument.in/reader036/viewer/2022081400/555c3f9bd8b42a0b038b4d60/html5/thumbnails/18.jpg)
Java Programming: From Problem Analysis to Program Design, 3e 18
Execution of largest(list, 0, 3)
![Page 19: Chap14](https://reader036.vdocument.in/reader036/viewer/2022081400/555c3f9bd8b42a0b038b4d60/html5/thumbnails/19.jpg)
Java Programming: From Problem Analysis to Program Design, 3e 19
Execution of largest(list, 0, 3)
![Page 20: Chap14](https://reader036.vdocument.in/reader036/viewer/2022081400/555c3f9bd8b42a0b038b4d60/html5/thumbnails/20.jpg)
Java Programming: From Problem Analysis to Program Design, 3e 20
Recursive Fibonacci
![Page 21: Chap14](https://reader036.vdocument.in/reader036/viewer/2022081400/555c3f9bd8b42a0b038b4d60/html5/thumbnails/21.jpg)
Java Programming: From Problem Analysis to Program Design, 3e 21
Recursive Fibonacci (continued)
public static int rFibNum(int a, int b, int n){ if (n == 1) return a; else if (n == 2) return b; else return rFibNum(a, b, n -1) + rFibNum(a, b, n - 2);}
![Page 22: Chap14](https://reader036.vdocument.in/reader036/viewer/2022081400/555c3f9bd8b42a0b038b4d60/html5/thumbnails/22.jpg)
Java Programming: From Problem Analysis to Program Design, 3e 22
Recursive Fibonacci (continued)
![Page 23: Chap14](https://reader036.vdocument.in/reader036/viewer/2022081400/555c3f9bd8b42a0b038b4d60/html5/thumbnails/23.jpg)
Java Programming: From Problem Analysis to Program Design, 3e 23
Towers of Hanoi Problem with Three Disks
![Page 24: Chap14](https://reader036.vdocument.in/reader036/viewer/2022081400/555c3f9bd8b42a0b038b4d60/html5/thumbnails/24.jpg)
Java Programming: From Problem Analysis to Program Design, 3e 24
Towers of Hanoi: Three Disk Solution
![Page 25: Chap14](https://reader036.vdocument.in/reader036/viewer/2022081400/555c3f9bd8b42a0b038b4d60/html5/thumbnails/25.jpg)
Java Programming: From Problem Analysis to Program Design, 3e 25
Towers of Hanoi: Three Disk Solution (continued)
![Page 26: Chap14](https://reader036.vdocument.in/reader036/viewer/2022081400/555c3f9bd8b42a0b038b4d60/html5/thumbnails/26.jpg)
Java Programming: From Problem Analysis to Program Design, 3e 26
Towers of Hanoi: Recursive Algorithmpublic static void moveDisks(int count, int needle1, int needle3, int needle2){ if (count > 0) { moveDisks(count - 1, needle1, needle2, needle3); System.out.println("Move disk " + count + " from needle " + needle1 + " to needle " + needle3 + ". "); moveDisks(count - 1, needle2, needle3, needle1); }}
![Page 27: Chap14](https://reader036.vdocument.in/reader036/viewer/2022081400/555c3f9bd8b42a0b038b4d60/html5/thumbnails/27.jpg)
Java Programming: From Problem Analysis to Program Design, 3e 27
Recursion or Iteration?
• Two ways to solve particular problem– Iteration– Recursion
• Iterative control structures: use looping to repeat a set of statements
• Tradeoffs between two options– Sometimes recursive solution is easier– Recursive solution is often slower
![Page 28: Chap14](https://reader036.vdocument.in/reader036/viewer/2022081400/555c3f9bd8b42a0b038b4d60/html5/thumbnails/28.jpg)
Java Programming: From Problem Analysis to Program Design, 3e 28
Programming Example: Decimal to Binary
public static void decToBin(int num, int base){ if (num > 0) { decToBin(num / base, base); System.out.print(num % base); }}
![Page 29: Chap14](https://reader036.vdocument.in/reader036/viewer/2022081400/555c3f9bd8b42a0b038b4d60/html5/thumbnails/29.jpg)
Java Programming: From Problem Analysis to Program Design, 3e 29
Execution of decToBin(13, 2)
![Page 30: Chap14](https://reader036.vdocument.in/reader036/viewer/2022081400/555c3f9bd8b42a0b038b4d60/html5/thumbnails/30.jpg)
Java Programming: From Problem Analysis to Program Design, 3e 30
Sierpinski Gaskets of Various Orders
![Page 31: Chap14](https://reader036.vdocument.in/reader036/viewer/2022081400/555c3f9bd8b42a0b038b4d60/html5/thumbnails/31.jpg)
Java Programming: From Problem Analysis to Program Design, 3e 31
Programming Example:Sierpinski Gasket
• Input: non-negative integer indicating level of Sierpinski gasket
• Output: triangle shape displaying a Sierpinski gasket of the given order
• Solution includes:– Recursive method drawSierpinski– Method to find midpoint of two points
![Page 32: Chap14](https://reader036.vdocument.in/reader036/viewer/2022081400/555c3f9bd8b42a0b038b4d60/html5/thumbnails/32.jpg)
Java Programming: From Problem Analysis to Program Design, 3e 32
private void drawSierpinski(Graphics g, int lev, Point p1, Point p2, Point p3){ Point midP1P2; Point midP2P3; Point midP3P1; if (lev > 0) { g.drawLine(p1.x, p1.y, p2.x, p2.y); g.drawLine(p2.x, p2.y, p3.x, p3.y); g.drawLine(p3.x, p3.y, p1.x, p1.y); midP1P2 = midPoint(p1, p2); midP2P3 = midPoint(p2, p3); midP3P1 = midPoint(p3, p1); drawSierpinski(g, lev - 1, p1, midP1P2, midP3P1); drawSierpinski(g, lev - 1, p2, midP2P3, midP1P2); drawSierpinski(g, lev - 1, p3, midP3P1, midP2P3); }}
Programming Example:Sierpinski Gasket (continued)
![Page 33: Chap14](https://reader036.vdocument.in/reader036/viewer/2022081400/555c3f9bd8b42a0b038b4d60/html5/thumbnails/33.jpg)
Java Programming: From Problem Analysis to Program Design, 3e 33
Programming Example: Sierpinski Gasket Input
![Page 34: Chap14](https://reader036.vdocument.in/reader036/viewer/2022081400/555c3f9bd8b42a0b038b4d60/html5/thumbnails/34.jpg)
Java Programming: From Problem Analysis to Program Design, 3e 34
Chapter Summary
• Recursive definitions
• Recursive algorithms
• Recursive methods
• Base cases
• General cases
![Page 35: Chap14](https://reader036.vdocument.in/reader036/viewer/2022081400/555c3f9bd8b42a0b038b4d60/html5/thumbnails/35.jpg)
Java Programming: From Problem Analysis to Program Design, 3e 35
Chapter Summary (continued)
• Tracing recursive methods
• Designing recursive methods
• Varieties of recursive methods
• Recursion vs. Iteration
• Various recursive functions explored