Chapter 14 Recursion Java Programming Program Design Including
- Slides: 31
Chapter 14: Recursion Java Programming: Program Design Including Data Structures
Chapter Objectives s Learn about recursive definitions s Explore the base case and the general case of a recursive definition s Learn about recursive algorithms s Learn about recursive methods s Become aware of direct and indirect recursion s Explore how to use recursive methods to implement recursive algorithms Java Programming: Program Design Including Data Structures 2
Recursion or Iteration? s Many problem can be solved by building an iterative control structures using a looping structure to repeat a set of statements: s while, for, or do … while s different approach? a recursion s Designing a recursive method s Using an iterative control structure s In addition, a selection control structure is used to control the repeated calls in recursion. Java Programming: Program Design Including Data Structures 3
Recursion or Iteration? s Tradeoffs between two options: s Sometimes recursive solution is easier s Recursive solution is often slower s No simple answer! consider the nature of the problem and system efficiency. Java Programming: Program Design Including Data Structures 4
Recursive Definitions s Recursion: s Process of solving a problem by reducing it to smaller versions of itself s Recursive definition: s Definition in which a problem is expressed in terms of a smaller version of itself s Has one or more base cases Java Programming: Program Design Including Data Structures 5
Tracing a Recursive Method s Recursive method: s Has unlimited copies of itself s Every recursive call has its own: s Code s Set of parameters s Set of local variables Every recursive call requires that the system allocate memory space for its parameters and local variables Java Programming: Program Design Including Data Structures 6
Tracing a Recursive Method s After completing a recursive call: s Control goes back to the calling environment s Recursive call must execute completely before control goes back to previous call s Execution in previous call begins from point immediately following recursive call Java Programming: Program Design Including Data Structures 7
Recursive Algorithm s Algorithm that finds the solution to a given problem by reducing the problem to smaller versions of itself s Has one or more base cases s In base case, the solution is obtained directly and stops the recursion. s Implemented using recursive methods Java Programming: Program Design Including Data Structures 8
Recursive Definitions (continued) s General solution: s Breaks problem into smaller versions of itself s General case: s Case in recursive definition in which a smaller version of itself is called s Must eventually be reduced to a base case Java Programming: Program Design Including Data Structures 9
Designing Recursive Methods s Understand problem nature and requirements s Determine limiting conditions s Identify base cases s Provide direct solution to each base case s Identify general (recursive) cases s Provide solutions to general (recursive) cases in terms of smaller versions of general cases Java Programming: Program Design Including Data Structures 10
Recursive Factorial Method public static int fact(int num) { if (num = = 0) return 1; else return num * fact(num – 1); } Java Programming: Program Design Including Data Structures 11
Recursive Factorial Method (continued) Java Programming: Program Design Including Data Structures 12
Largest Value in Array public static int largest(int[] list, int lower. Index, int upper. Index) { int max; if (lower. Index == upper. Index) return list[lower. Index]; else { max = largest(list, lower. Index + 1, upper. Index); if (list[lower. Index] >= max) return list[lower. Index]; else return max; } } Java Programming: Program Design Including Data Structures 13
Largest Value in Array (continued) list = {5, 10, 12, 8} Java Programming: Program Design Including Data Structures 14
Recursive Fibonacci where a and b: the first two numbers of the Fibonacci sequence n: the desired nth Fibonacci number Java Programming: Program Design Including Data Structures 15
Recursive Fibonacci (continued) public static int r. Fib. Num(int a, int b, int n) { if(n == 1) return a; else if (n == 2) return b; else return r. Fib. Num(a, b, n - 1) + r. Fib. Num(a, b, n - 2); } Java Programming: Program Design Including Data Structures 16
r. Fib. Num(2, 3, 5) Java Programming: Program Design Including Data Structures 17
Towers of Hanoi: Three Disk Problem s Rules s Only one disk can be moved at a time s The removed disk must be placed on one of the needles s A larger disk cannot be placed on top of a smaller disk Java Programming: Program Design Including Data Structures 18
Towers of Hanoi: Three Disk Solution Java Programming: Program Design Including Data Structures 19
Towers of Hanoi: Three Disk Solution Java Programming: Program Design Including Data Structures 20
Tower of Hanoi: Recursive Algorithm s Identify base cases s When n = 1 : move the disk from needle 1 to needle 3 s Identify general (recursive) cases s Provide solutions to general cases in terms of smaller versions of general cases Java Programming: Program Design Including Data Structures 21
Recursive Solution // Base solution: // Move a single disk from needle 1 to needle 3 // General solution: // 1. Move top (count-1) disks from needle 1 to needle 2 // using intermediate needle 3 // 2. Move disk count from needle 1 to needle 3 // 3. Move the top (count-1) disks from needle 2 // to needle 3 using intermediate needle 1 public static void move. Disks(int count, int needle 1, int needle 3, int needle 2){ if (count > 0) { move. Disks(count - 1, needle 2, needle 3); System. out. println("Move disk " + count + " from needle " + needle 1 + " to needle " + needle 3 + ". "); move. Disks(count - 1, needle 2, needle 3, needle 1); } } Java Programming: Program Design Including Data Structures 22
Programming Example: Decimal to Binary public static void dec. To. Bin(int num, int base) { if (num > 0) { dec. To. Bin(num / base, base); System. out. print(num % base); } } Java Programming: Program Design Including Data Structures 23
Java Programming: Program Design Including Data Structures 24
Sierpinski Gasket as a Fractal s A fractal is a geometric shape in which certain patterns repeat at a different scale and orientation. s Sierpinski gasket is a special type of fractal Sierpinski gasket of order 1 Sierpinski gasket of order 2 Sierpinski gasket of order 3 Sierpinski gasket of order 4 Java Programming: Program Design Including Data Structures 25
Programming Example: Sierpinski gasket (continued) s Input: Non-negative integer that indicates level of Sierpinski gasket s Output: Triangle shape that displays a Sierpinski gasket of the given order s Solution includes: s Recursive method draw. Sierpinski s Method to find midpoint of two points Java Programming: Program Design Including Data Structures 26
Sierpinski Gasket s Base case: If the level is 1, draw the first triangle s Recursive case: If level > 1, for each triangle, find the midpoints of the sides and draw lines through those points. private Point mid. Point(Point p. One, Point p. Two) { Point mid = new Point((p. One. x + p. Two. x)/2, (p. One. y + p. Two. y)/2); return mid; } Java Programming: Program Design Including Data Structures 27
Sierpinski Gasket private void draw. Sierpinski(Graphics g, int lev, Point p 1, Point p 2, Point p 3) { Point mid. P 1 P 2; Point mid. P 2 P 3; Point mid. P 3 P 1; if (lev > 0) { g. draw. Line(p 1. x, p 1. y, p 2. x, p 2. y); g. draw. Line(p 2. x, p 2. y, p 3. x, p 3. y); g. draw. Line(p 3. x, p 3. y, p 1. x, p 1. y); mid. P 1 P 2 = mid. Point(p 1, p 2); mid. P 2 P 3 = mid. Point(p 2, p 3); mid. P 3 P 1 = mid. Point(p 3, p 1); draw. Sierpinski(g, lev - 1, p 1, mid. P 1 P 2, mid. P 3 P 1); draw. Sierpinski(g, lev - 1, p 2, mid. P 2 P 3, mid. P 1 P 2); draw. Sierpinski(g, lev - 1, p 3, mid. P 3 P 1, mid. P 2 P 3); } } Java Programming: Program Design Including Data Structures 28
Sierpinski Gasket (continued) Java Programming: Program Design Including Data Structures 29
Chapter Summary s Recursive definitions s Recursive algorithms s Recursive methods s Base cases s General cases Java Programming: Program Design Including Data Structures 30
Chapter Summary (continued) s Tracing recursive methods s Designing recursive methods s Varieties of recursive methods s Recursion vs. iteration s Various recursive functions Java Programming: Program Design Including Data Structures 31
- To understand recursion you must understand recursion
- Recursive thinking definition
- Dynamic programming recursion example
- Dynamic programming recursion example
- Tail recursive
- How recursion works in java
- Recursion in java
- Tail recursion
- Tail recursion
- Handshake problem formula
- What is recursion
- Recursion java
- Recursion java
- Perbedaan linear programming dan integer programming
- Greedy programming vs dynamic programming
- System programming vs application programming
- Integer programming vs linear programming
- Definisi linear
- Java client server tutorial
- Parallel programming in java
- Java object oriented exercises
- Java introduction to problem solving and programming
- Event driven programming in java
- Elementary programming in java
- Java asynchronous programming
- Contoh pemrograman terstruktur
- Arne kutzner
- Khan academy programming
- Event driven programming in java
- Defensive programming java
- Java programming refresher
- Java games programming