Recursive Functions The Fibonacci function shown previously is recursive, that is, it calls itself Each call to a recursive method results in a separate.

Slides:



Advertisements
Similar presentations
Recursion –a programming strategy for solving large problems –Think “divide and conquer” –Solve large problem by splitting into smaller problems of same.
Advertisements

COMPSCI 105 S Principles of Computer Science Recursion 1.
Chapter 10 Recursion Instructor: alkar/demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe.
CS102 Algorithms and Programming II1 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Unit 191 Recursion General Algorithm for Recursion When to use and not use Recursion Recursion Removal Examples Comparison of the Iterative and Recursive.
Ch. 3: Recursion. Recursive Solutions Recursion –An extremely powerful problem-solving technique –Breaks a problem into smaller identical problems –An.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
1 Executing Method Calls This lecture tells you precisely how method calls are executed (a few details will have to wait until we get to classes and objects).
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. Recursive Solutions Recursion breaks a problem into smaller identical problems – mirror images so to speak. By continuing to do this, eventually.
© 2006 Pearson Addison-Wesley. All rights reserved3-1 Chapter 3 Recursion: The Mirrors.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
Recursion!. Can a method call another method? YES.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
CENG 7071 Recursion. CENG 7072 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive function.
Recursion.  Identify recursive algorithms  Write simple recursive algorithms  Understand recursive function calling  With reference to the call stack.
Introduction to Programming (in C++) Recursion Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
Chapter 2 Recursion: The Mirrors CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation.
Recursion. Definition Recursion is a function calling on itself over and over until reaching an end state. One such example is factorial. 10! = 10 * 9.
RECURSION Lecture 7 CS2110 – Fall Overview references to sections in text 2  Note: We’ve covered everything in JavaSummary.pptx!  What is recursion?
Recursion.
COSC 2006 Data Structures I Recursion II
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
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. 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.
Functions-Recall 1. 2 Parameter passing & return void main() { int x=10, y=5; printf (“M1: x = %d, y = %d\n”, x, y); interchange (x, y); printf (“M2:
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.
CMPT 225 Recursion. Objectives Understand how the Fibonacci series is generated Recursive Algorithms  Write simple recursive algorithms  Analyze simple.
CS111 Computer Programming Department of Computer Science Wellesley College Classic Recursion Examples Plus Class Methods and Applications Tuesday, October.
Ramamurthy Recursion: The Mirrors B. Ramamurthy CS114A,B.
1 Recursion. 2 A process by which a function calls itself repeatedly  Either directly. X calls X  Or cyclically in a chain. X calls Y, and Y calls X.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
1 Recursion and induction We teach these early, instead of new object- oriented ideas, so that those who are new to Java can have a chance to catch up.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
Recursion.
CS212: Data Structures and Algorithms
CSC 205 Programming II Lecture 8 Recursion.
Recursion CENG 707.
Recursion CENG 707.
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Recursion: The Mirrors
Hassan Khosravi / Geoffrey Tien
Review: recursion Tree traversals
Recursion: The Mirrors
CS201: Data Structures and Discrete Mathematics I
Module 1-10: Recursion.
Java Programming: Chapter 9: Recursion Second Edition
Recursion: The Mirrors
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Recursion: The Mirrors
Presentation transcript:

Recursive Functions The Fibonacci function shown previously is recursive, that is, it calls itself Each call to a recursive method results in a separate instance (invocation) of the method, with its own input and own local variables

Function Analysis for call fib(5) fib(5) fib(4) fib(3) fib(2) fib(1)fib(0)fib(2) fib(1)fib(0) fib(1) fib(2) fib(1)fib(0) fib(1) public static int fib(int n) if (n == 0 || n == 1) return n else return fib(n-1) + fib(n-2)

Recursive Function Calls on the Stack When a method is called it is pushed onto the call stack Subsequent recursive invocations are also pushed onto the call stack Whenever a recursive invocation is made execution is switched to that method invocation The call stack keeps track of the line number of the previous method where the call was made from Once execution of one method invocation is finished it is removed from the call stack, and execution returns to the previous invocation

Anatomy of a Recursive Function A recursive function consists of two types of cases A base case(s) and A recursive case The base case is a small problem The solution to this problem should not be recursive, so that the function is guaranteed to terminate There can be more than one base case The recursive case defines the problem in terms of a smaller problem of the same type The recursive case includes a recursive function call There can be more than one recursive case

Finding Recursive Solutions Define the problem in terms of a smaller problem of the same type and The recursive part e.g. return fib(n-1) + fib(n-2); A small problem where the solution can be easily calculated This solution should not be recursive The “base case” e.g. if (n == 0 || n == 1) return n;

Steps Leading to Recursive Solutions How can the problem be defined in terms of smaller problems of the same type? By how much does each recursive call reduce the problem size? What is the base case that can be solved without recursion? Will the base case be reached as the problem size is reduced?

Designing a recursive solution: Writing a String Backward Problem: Given a string of characters, write it in reverse order Recursive solution: How can the problem be defined in terms of smaller problems of the same type? We could write the last character of the string and then solve the problem of writing first n-1 characters backward We could write the last character of the string and then solve the problem of writing first n-1 characters backward By how much does each recursive call reduce the problem size? Each recursive step of the solution diminishes by 1 the length of the string to be written backward Each recursive step of the solution diminishes by 1 the length of the string to be written backward What is the base case that can be solved without recursion? Base case: Write the empty string backward = Do nothing. Base case: Write the empty string backward = Do nothing. Will the base case be reached as the problem size is reduced? Yes. Yes.

Designing a recursive solution: Writing a String Backward public static void writeBackward(String s, int size) { // // Writes a character string backward. // Precondition: The string s contains size // characters, where size >= 0. // Postcondition: s is written backward, but remains // unchanged. // if (size > 0) { // write the last character System.out.println(s.substring(size-1, size)); // write the rest of the string backward writeBackward(s, size-1); // Point A } // end if // size == 0 is the base case - do nothing } // end writeBackward

Designing a recursive solution: Writing a String Backward Execution of recursive method like writeBackward() can be traced using the box trace: Useful for debugging and understanding recursive methods. Simulates the work of computer. A sequence of boxes: each box corresponds to an activation record = a record put on the call stack when a function is called containing values of parameters and local variables. All recursive calls in the body of the recursive method are labeled with different letters (the label is then used to indicate which recursive call caused creation of a new box in the sequence.

Box trace of writeBackward(“cat”, 3) The initial call is made, and the method begins execution: s = “cat” size = 3 s = “cat” size = 3 s = “cat” size = 2 s = “cat” size = 1 s = “cat” size = 2 s = “cat” size = 3 AA A Outputline: ta Point A is reached, and the recursive call is made. The new invocation begins execution: Outputline: t Point A ( writeBackward(s, size-1)) is reached, and the recursive call is made. The new invocation begins execution:

Box trace of writeBackward(“cat”, 3) Output line: tac Point A is reached, and the recursive call is made. The new evocation begins execution: s = “cat” size = 3 s = “cat” size = 2 s = “cat” size = 1 s = “cat” size = 0 s = “cat” size = 0 s = “cat” size = 1 s = “cat” size = 2 s = “cat” size = 3 A AAA AA This is the base case, so this invocation completes. Control returns to the calling box, which continues execution:

Box trace of writeBackward(“cat”, 3) This invocation completes. Control returns to the calling box, which continues execution: s = “cat” size = 3 s = “cat” size = 2 s = “cat” size = 1 s = “cat” size = 0 s = “cat” size = 0 s = “cat” size = 1 s = “cat” size = 2 s = “cat” size = 3 A AAA AA This invocation completes. Control returns to the calling box, which continues execution: This invocation completes. Control returns to the statement following the initial call.

A recursive definition of factorial n! fact(n) = n*(n-1)*(n-2)* … *1 1 if n=0 fact(n) = n * fact(n-1) if n>0 public static int fact (int n) { if (n==0) { return 1; } else { return n * fact(n-1); // Point A }

return 2*fact(1) 2* ? Figure 2.2 fact(3) System.out.println(fact(3));? return 3*fact(2) 3*? return 1*fact(0) 1* ? return

Figure 2.3 A box

Figure 2.4 The beginning of the box trace

Figure 2.5a Box trace of fact(3)

Figure 2.5b Box trace of fact(3)

Figure 2.5c Box trace of fact(3)