Download presentation
Presentation is loading. Please wait.
1
Java 4/4/2017 Recursion
2
Learning Objectives Understand how recursion works and be able to read and write a program that uses recursion. See Learning Objectives
3
Dry Run
4
Recursion quotes. "In order to understand recursion, one must first understand recursion." Definition Recursion If you still don't get it, See: "Recursion".
5
Recursion Recursion: When a subroutine calls itself.
When designing a recursive function it is imperative to include the following four elements: a well-defined BASE CASE, a RECURSIVE CALL within the body of the function, a guaranteed SMALLER PROBLEM as a result of the recursive call, and a guarantee that the BASE CASE IS REACHABLE
6
When To Use Recursion • When a problem can be divided into steps.
• The result of one step can be used in a previous step. • There is scenario when you can stop sub-dividing the problem into steps and return to previous steps. • All of the results together solve the problem.
7
When To Consider Alternatives To Recursion
• When a loop will solve the problem just as well • Types of recursion: • Tail recursion — A recursive call is the last statement in the recursive module. — This form of recursion can easily be replaced with a loop. • Non-tail recursion — A statement which is not a recursive call to the module comprises the last statement in the recursive module. —This form of recursion is very difficult to replace with a loop.
8
Drawbacks Of Recursion
Function/procedure calls can be costly • Uses up memory • Uses up time
9
Benefits Of Using Recursion
• Simpler solution that’s more elegant (for some problems) • Easier to visualize solutions (for some people and certain classes of problems – typically require either: non-tail recursion to be implemented or some form of “backtracking”)
10
Common Pitfalls When Using Recursion
•These three pitfalls can result in a segmentation fault (using up too much memory) occurring • No base case • No progress towards the base case • Using up too many resources (e.g., variable declarations) for each function call
11
Recursion: a definition in terms of itself.
Overview Recursion: a definition in terms of itself. Recursion in algorithms: Natural approach to some (not all) problems A recursive algorithm uses itself to solve one or more smaller identical problems Recursion in Java: Recursive methods implement recursive algorithms A recursive method includes a call to itself
12
Recursive Methods Must Eventually Terminate
A recursive method must have at least one base, or stopping, case. A base case does not execute a recursive call stops the recursion Each successive call to itself must be a "smaller version of itself” an argument that describes a smaller problem a base case is eventually reached
13
Key Components of a Recursive Algorithm Design
What is a smaller identical problem(s)? Decomposition How are the answers to smaller problems combined to form the answer to the larger problem? Composition Which is the smallest problem that can be solved easily (without further decomposition)? Base/stopping case
14
Examples in Recursion Usually quite confusing the first time
Start with some simple examples recursive algorithms might not be best Later with inherently recursive algorithms harder to implement otherwise
15
Factorial (N!) N! = (N-1)! * N [for N > 1] 1! = 1 3! = 2! * 3
= 2! * 3 = (1! * 2) * 3 = 1 * 2 * 3 Recursive design: Decomposition: (N-1)! Composition: * N Base case: 1!
16
factorial Method public static int factorial(int n) { int fact;
if (n > 1) // recursive case (decomposition) fact = factorial(n – 1) * n; // composition else // base case fact = 1; return fact; }
17
public static int factorial(int 3)
{ int fact; if (n > 1) fact = factorial(2) * 3; else fact = 1; return fact; }
18
public static int factorial(int 3)
{ int fact; if (n > 1) fact = factorial(2) * 3; else fact = 1; return fact; } public static int factorial(int 2) { int fact; if (n > 1) fact = factorial(1) * 2; else fact = 1; return fact; }
19
public static int factorial(int 3)
{ int fact; if (n > 1) fact = factorial(2) * 3; else fact = 1; return fact; } public static int factorial(int 2) { int fact; if (n > 1) fact = factorial(1) * 2; else fact = 1; return fact; } public static int factorial(int 1) { int fact; if (n > 1) fact = factorial(n - 1) * n; else fact = 1; return fact; }
20
public static int factorial(int 3)
{ int fact; if (n > 1) fact = factorial(2) * 3; else fact = 1; return fact; } public static int factorial(int 2) { int fact; if (n > 1) fact = factorial(1) * 2; else fact = 1; return fact; } public static int factorial(int 1) { int fact; if (n > 1) fact = factorial(n - 1) * n; else fact = 1; return 1; }
21
public static int factorial(int 3)
{ int fact; if (n > 1) fact = factorial(2) * 3; else fact = 1; return fact; } public static int factorial(int 2) { int fact; if (n > 1) fact = 1 * 2; else fact = 1; return fact; } public static int factorial(int 1) { int fact; if (n > 1) fact = factorial(n - 1) * n; else fact = 1; return 1; }
22
public static int factorial(int 3)
{ int fact; if (n > 1) fact = factorial(2) * 3; else fact = 1; return fact; } public static int factorial(int 2) { int fact; if (n > 1) fact = 1 * 2; else fact = 1; return 2; }
23
public static int factorial(int 3)
{ int fact; if (n > 1) fact = 2 * 3; else fact = 1; return fact; } public static int factorial(int 2) { int fact; if (n > 1) fact = 1 * 2; else fact = 1; return 2; }
24
public static int factorial(int 3)
{ int fact; if (n > 1) fact = 2 * 3; else fact = 1; return 6; }
25
Execution Trace (decomposition)
public static int factorial(int n) { int fact; if (n > 1) // recursive case (decomposition) fact = factorial(n – 1) * n; (composition) else // base case fact = 1; return fact; } Execution Trace (decomposition) factorial(4) factorial(3) 4
26
Execution Trace (decomposition)
public static int factorial(int n) { int fact; if (n > 1) // recursive case (decomposition) fact = factorial(n – 1) * n; (composition) else // base case fact = 1; return fact; } Execution Trace (decomposition) factorial(4) factorial(3) 4 factorial(2) 3
27
Execution Trace (decomposition)
public static int factorial(int n) { int fact; if (n > 1) // recursive case (decomposition) fact = factorial(n – 1) * n; (composition) else // base case fact = 1; return fact; } Execution Trace (decomposition) factorial(4) factorial(3) 4 factorial(2) 3 factorial(1) 2
28
Execution Trace (composition)
public static int factorial(int n) { int fact; if (n > 1) // recursive case (decomposition) fact = factorial(n – 1) * n; (composition) else // base case fact = 1; return fact; } Execution Trace (composition) factorial(4) * factorial(3) * factorial(2) * factorial(1)->1 2
29
Execution Trace (composition)
public static int factorial(int n) { int fact; if (n > 1) // recursive case (decomposition) fact = factorial(n – 1) * n; (composition) else // base case fact = 1; return fact; } Execution Trace (composition) factorial(4) * factorial(3) * factorial(2)->2 3
30
Execution Trace (composition)
public static int factorial(int n) { int fact; if (n > 1) // recursive case (decomposition) fact = factorial(n – 1) * n; (composition) else // base case fact = 1; return fact; } Execution Trace (composition) factorial(4) * factorial(3)->6 4
31
Execution Trace (composition)
public static int factorial(int n) { int fact; if (n > 1) // recursive case (decomposition) fact = factorial(n – 1) * n; (composition) else // base case fact = 1; return fact; } Execution Trace (composition) factorial(4)->24
32
Improved factorial Method
public static int factorial(int n) { int fact=1; // base case value if (n > 1) // recursive case (decomposition) fact = factorial(n – 1) * n; // composition // else do nothing; base case return fact; }
33
Fibonacci Numbers The Nth Fibonacci number is the sum of the previous two Fibonacci numbers 0, 1, 1, 2, 3, 5, 8, 13, … Recursive Design: Decomposition & Composition fibonacci(n) = fibonacci(n-1) + fibonacci(n-2) Base case: fibonacci(1) = 0 fibonacci(2) = 1
34
fibonacci Method public static int fibonacci(int n) { int fib;
if (n > 2) fib = fibonacci(n-1) + fibonacci(n-2); else if (n == 2) fib = 1; else fib = 0; return fib; }
35
Execution Trace (decomposition)
fibonacci(4) fibonacci(3) fibonacci(2)
36
Execution Trace (decomposition)
fibonacci(4) fibonacci(3) fibonacci(2) fibonacci(2) fibonacci(1)
37
Execution Trace (composition)
fibonacci(4) + fibonacci(3) fibonacci(2) + fibonacci(2)->1 fibonacci(1)->0
38
Execution Trace (composition)
fibonacci(4) + fibonacci(3)->1 fibonacci(2)->1
39
Execution Trace (composition)
fibonacci(4)->2
40
Remember: Key to Successful Recursion
if-else statement (or some other branching statement) Some branches: recursive call "smaller" arguments or solve "smaller" versions of the same task (decomposition) Combine the results (composition) [if necessary] Other branches: no recursive calls stopping cases or base cases
41
Template … method(…) { if ( … )// base case }
else // decomposition & composition return … ; // if not void method
42
Template (only one base case)
… method(…) { … result = … ;//base case if ( … ) // not base case { //decomposition & composition result = … } return result;
43
What Happens Here? public static int factorial(int n) { int fact=1;
if (n > 1) fact = factorial(n) * n; return fact; }
44
What Happens Here? public static int factorial(int n) {
return factorial(n – 1) * n; }
45
Sample AP Question public int change(int value) { if(value < 3) return value % 3; else return value % * change(value/3); } What will be returned by the call change(45)?
46
Recursion Assignment Recursion Practice Exam
In the assignments folder 13 Problems. Show work as appropriate on your paper Variable names/ Screen/… Record your answer on your paper We will check answers in class
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.