Java 4/4/2017 Recursion.

Slides:



Advertisements
Similar presentations
Recursion Chapter 11. Objectives become familiar with the idea of recursion learn to use recursion as a programming tool become familiar with the binary.
Advertisements

Recursion.
Computer Science II Recursion Professor: Evan Korth New York University.
Recursion. Binary search example postponed to end of lecture.
Imperative programming public int factorial (int N){ int F = 1; for(X=N; X>1; X-- ){ F= F*X; } return F; } Functional programming (defun factorial(N) (cond.
James Tam Recursion You will learn the definition of recursion as well as seeing how simple recursive programs work.
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
Recursion.
James Tam Recursion You will learn what recursion is as well as how simple recursive programs work.
James Tam Recursion You will learn what is recursion as well as how simple recursive programs work.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Recursion You will learn the definition of recursion as well as seeing how simple recursive programs work.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Prof. S.M. Lee Department of Computer Science. Answer:
J. Michael Moore Recursion CSCE 110 From James Tam’s material.
James Tam Recursion You will learn the definition of recursion as well as seeing how simple recursive programs work.
Recursion.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Recursion.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Recursion. Basic problem solving technique is to divide a problem into smaller sub problems These sub problems may also be divided into smaller sub problems.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
Recursion.  A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for n>1 n!=1 for n=1 int factorial (int n) { if (n == 0)
M180: Data Structures & Algorithms in Java
Lecture 10 Recursion CSE225: Data Structures. 2 A Look Back at Functions #include double distance(double x1, double y1, double x2, double y2) { double.
RECURSION. Objectives: become familiar with the idea of recursion Examine examples to show the wide variety of situations to which recursion can be applied.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
LECTURE 20: RECURSION CSC 212 – Data Structures. Humorous Asides.
W1-1 University of Washington Computer Programming I Recursion © 2000 UW CSE.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Welcome to Recursion! Say what?!? Recursion is… the process of solving large problems by simplifying them into smaller ones. similar to processing using.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
Recursion Chapter 10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Recursion Chapter 10 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
James Tam Recursion You will learn the definition of recursion as well as seeing how simple recursive programs work.
Recursion Powerful Tool
Recursion.
Recursion.
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
Recursion The programs discussed so far have been structured as functions that invoke one another in a disciplined manner For some problems it is useful.
Chapter 15 Recursion.
Introduction to Recursion
Recursion You will learn the definition of recursion as well as seeing how simple recursive programs work Recursion in Pascal 1.
Chapter 15 Recursion.
Recursion Chapter 10.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Recursion You will learn what recursion is as well as how simple recursive programs work Recursion in Pascal.
Applied Algorithms (Lecture 17) Recursion Fall-23
Recursion You will learn the definition of recursion as well as seeing how simple recursive programs work Recursion in Pascal 1.
Recursion Chapter 11.
Recursion Data Structures.
Module 1-10: Recursion.
Java Programming: Chapter 9: Recursion Second Edition
Yan Shi CS/SE 2630 Lecture Notes
Computer Science 2 Take out a piece of paper for the following dry runs. Label It Recursive Dry Runs 4/12/2018. It will be turned in when completed
Computer Science
Main() { int fact; fact = Factorial(4); } main fact.
Recursion.
Recursive Thinking.
Presentation transcript:

Java 4/4/2017 Recursion

Learning Objectives Understand how recursion works and be able to read and write a program that uses recursion. See Learning Objectives

Dry Run

Recursion quotes. "In order to understand recursion, one must first understand recursion." Definition Recursion If you still don't get it, See: "Recursion".

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

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.

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.

Drawbacks Of Recursion Function/procedure calls can be costly • Uses up memory • Uses up time

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”)

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

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

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

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

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

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!

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; }

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 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 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; }

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; }

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; }

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; }

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; }

public static int factorial(int 3) { int fact; if (n > 1) fact = 2 * 3; else fact = 1; return 6; }

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

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

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

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) 4 * factorial(2) 3 * factorial(1)->1 2

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) 4 * factorial(2)->2 3

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

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

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; }

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

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; }

Execution Trace (decomposition) fibonacci(4) fibonacci(3) fibonacci(2)

Execution Trace (decomposition) fibonacci(4) fibonacci(3) fibonacci(2) fibonacci(2) fibonacci(1)

Execution Trace (composition) fibonacci(4) + fibonacci(3) fibonacci(2) + fibonacci(2)->1 fibonacci(1)->0

Execution Trace (composition) fibonacci(4) + fibonacci(3)->1 fibonacci(2)->1

Execution Trace (composition) fibonacci(4)->2

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

Template … method(…) { if ( … )// base case } else // decomposition & composition return … ; // if not void method

Template (only one base case) … method(…) { … result = … ;//base case if ( … ) // not base case { //decomposition & composition result = … } return result;

What Happens Here? public static int factorial(int n) { int fact=1; if (n > 1) fact = factorial(n) * n; return fact; }

What Happens Here? public static int factorial(int n) { return factorial(n – 1) * n; }

Sample AP Question public int change(int value) { if(value < 3) return value % 3; else return value % 3 + 10 * change(value/3); } What will be returned by the call change(45)?

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