Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.

Slides:



Advertisements
Similar presentations
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Advertisements

Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
CS 106 Introduction to Computer Science I 11 / 09 / 2007 Instructor: Michael Eckmann.
Computer Science II Recursion Professor: Evan Korth New York University.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Introduction to Computers and Programming Lecture 5 New York University.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
Recursion Road Map Introduction to Recursion Recursion Example #1: World’s Simplest Recursion Program Visualizing Recursion –Using Stacks Recursion Example.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.12Recursion 3.13Example Using Recursion: The Fibonacci Series 3.14Recursion.
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
Fall 2009ACS-1805 Ron McFadyen1 Ch 8 Recursion Recursion occurs when a method (or function) calls itself.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 – Recursive Funtions From Deitel’s “C” Book 5.13Recursion 5.14Example Using Recursion: The Fibonacci.
Recursion Examples Fundamentals of CS Case 1: Code /* Recursion: Case 1 */ #include void count (int index); main () { count (0); getchar(); } void count.
20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Programming in C++ Language ( ) Lecture 6: Functions-Part2 Dr. Lubna Badri.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Lecture6 Recursion function © by Pearson Education, Inc. All Rights Reserved. 1.
Chapter 7 Recursion Recursive methods Recursion in two- dimensional grids Recursive helper method Analysis of recursive algorithms.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
1 CS1120: Recursion. 2 What is Recursion? A method is said to be recursive if the method definition contains a call to itself A recursive method is a.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Recursion) Outline 5.13Recursion 5.14Example.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
1 Wright State University, College of Engineering Dr. T. Doom, Computer Science & Engineering CS 241 Computer Programming II CS 241 – Computer Programming.
 2007 Pearson Education, Inc. All rights reserved C Functions -Continue…-
Advanced Computer Architecture and Parallel Processing Rabie A. Ramadan http:
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
1 Recursion Recursive method –Calls itself (directly or indirectly) through another method –Method knows how to solve only a base case –Method divides.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
 Prentice Hall. All rights reserved. 1 CS 1120 – Computer Science II Recursion (Ch.15) Many slides modified by Prof. L. Lilien (even many without.
 Prentice Hall. All rights reserved. 1 Recursion (Section 7.13 & Exercise7.40 from ed.3) (Sections 6.15, 6.16 from ed.1) Many slides modified.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
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.
A Brief Introduction to Recursion. Recursion Recursive methods … –methods that call themselves! –They can only solve a base case –So, you divide a problem.
C++ Programming Lecture 12 Functions – Part IV
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
 Prentice Hall. All rights reserved. 1 CS 1120 – Computer Science II Recursion (Ch.15) Many slides modified by Prof. L. Lilien (even many without.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Welcome to Recursion! Say what?!? Recursion is… the process of solving large problems by simplifying them into smaller ones. similar to processing using.
Recursion.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Recursion.
Chapter 15 Recursion.
Introduction to Recursion
Introduction to Recursion
5.13 Recursion Recursive functions Functions that call themselves
Chapter 15 Recursion.
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
Java 4/4/2017 Recursion.
Java Software Structures: John Lewis & Joseph Chase
CMSC201 Computer Science I for Majors Lecture 16 – Recursion
MSIS 655 Advanced Business Applications Programming
Chapter 12 Recursion (methods calling themselves)
Recursion Chapter 11.
Recursion Data Structures.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Functions Recursion CSCI 230
Chapter 12 Supplement: Recursion with Java 1.5
Recursion Chapter 18.
Java Programming: Chapter 9: Recursion Second Edition
Presentation transcript:

Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University

Road Map Introduction to Recursion Recursion Example #1: World’s Simplest Recursion Program Visualizing Recursion –Using Stacks Recursion Example #2 Computing Factorials –Iterative Approach Computing Factorials –Recursive Approach Reading, Chapter 4, 4.10

Introduction to Recursion

So far, we have seen methods that call other functions. –For example, the main() method calls the square () function. Recursive Method: –A recursive method is a method that calls itself. main() square() compute()

Why use Recursive Methods? In computer science, some problems are more easily solved by using recursive functions. If you go on to take a computer science algorithms course, you will see lots of examples of this. For example: –Traversing through a directory or file system. –Traversing through a tree of search results. For today, we will focus on the basic structure of using recursive methods.

World’s Simplest Recursion Program

public class Recursion { public static void main (String args[]) { count(0); System.out.println(); } public static void count (int index) { System.out.print(index); if (index < 2) count(index+1); } This program simply counts from 0-2: 012 This is where the recursion occurs. You can see that the count() function calls itself.

Visualizing Recursion To understand how recursion works, it helps to visualize what’s going on. To help visualize, we will use a common concept called the Stack. A stack basically operates like a container of trays in a cafeteria. It has only two operations: –Push: you can push something onto the stack. –Pop: you can pop something off the top of the stack. Let’s see an example stack in action.

Stacks Time: 0 Empty Stack Time 1: Push “2” 2 Time 2: Push “8” 2 8 Time 3: Pop: Gets 8 2 The diagram below shows a stack over time. We perform two pushes and one pop. Time 4: Pop: Gets 2

Stacks and Methods When you run a program, the computer creates a stack for you. Each time you invoke a method, the method is placed on top of the stack. When the method returns or exits, the method is popped off the stack. The diagram on the next page shows a sample stack for a simple Java program.

Stacks and Methods Time: 0 Empty Stack Time 1: Push: main() main() Time 2: Push: square() main() square() Time 3: Pop: square() returns a value. method exits. main() Time 4: Pop: main() returns a value. method exits.

Stacks and Recursion Each time a method is called, you push the method on the stack. Each time the method returns or exits, you pop the method off the stack. If a method calls itself recursively, you just push another copy of the method onto the stack. We therefore have a simple way to visualize how recursion really works.

Back to the Simple Recursion Program Here’s the code again. Now, that we understand stacks, we can visualize the recursion. public class Recursion1V0 { public static void main (String args[]) { count(0); System.out.println(); } public static void count (int index) { System.out.print(index); if (index < 2) count(index+1); }

Stacks and Recursion in Action Time: 0 Empty Stack Time 1: Push: main() main() Time 2: Push: count(0) main() count(0) Time 3: Push: count(1) Inside count(0): print (index);  0 if (index < 2) count(index+1); Inside count(1): print (index);  1 if (index < 2) count(index+1); main() count(0) count(1) Time 4: Push: count(2) main() count(0) count(1) Inside count(2): print (index);  2 if (index < 2) count(index+1); This condition now fails! Hence, recursion stops, and we proceed to pop all functions off the stack. count(2) Times 5-8: Pop everything …

Recursion, Variation 1 What will the following program do? public class Recursion1V1 { public static void main (String args[]) { count(3); System.out.println(); } public static void count (int index) { System.out.print(index); if (index < 2) count(index+1); }

Recursion, Variation 2 What will the following program do? public class Recursion1V2 { public static void main (String args[]) { count(0); System.out.println(); } public static void count (int index) { if (index < 2) count(index+1); System.out.print(index); } Note that the print statement has been moved to the end of the method.

Recursion Example #2

public class Recursion2 { public static void main (String args[]) { upAndDown(1); System.out.println(); } public static void upAndDown (int n) { System.out.print ("\nLevel: " + n); if (n < 4) upAndDown (n+1); System.out.print ("\nLEVEL: " + n); } Recursion occurs here.

Determining the Output Suppose you were given this problem on the final exam, and your task is to “determine the output.” How do you figure out the output? Answer: Use Stacks to Help Visualize

Stack Short-Hand Rather than draw each stack like we did last time, you can try using a short-hand notation. timestackoutput time 0:empty stack time 1:f(1)Level: 1 time 2:f(1), f(2)Level: 2 time 3:f(1), f(2), f(3)Level: 3 time 4:f(1), f(2), f(3), f(4)Level: 4 time 5:f(1), f(2), f(3)LEVEL: 4 time 6:f(1), f(2)LEVEL: 3 time 7:f(1)LEVEL: 2 time 8:emptyLEVEL: 1

Computing Factorials

Factorials Computing factorials are a classic problem for examining recursion. A factorial is defined as follows: n! = n * (n-1) * (n-2) …. * 1; For example: 1! = 1 (Base Case) 2! = 2 * 1 = 2 3! = 3 * 2 * 1 = 6 4! = 4 * 3 * 2 * 1 = 24 5! = 5 * 4 * 3 * 2 * 1 = 120

Iterative Approach import javax.swing.JOptionPane; public class FindFactorialIterative { public static void main (String args[]) { int answer, n; String nAsString = JOptionPane.showInputDialog("Enter a number: "); n = Integer.parseInt (nAsString); answer = findFactorial (n); System.out.println ("Answer: " + answer); System.exit(0); } public static int findFactorial (int n) { int i, factorial = n; for (i = n - 1; i >= 1; i--) factorial = factorial * i; return factorial; } This is an iterative solution to finding a factorial. It’s iterative because we have a simple for loop. Note that the for loop goes from n-1 to 1.

Factorials Computing factorials are a classic problem for examining recursion. A factorial is defined as follows: n! = n * (n-1) * (n-2) …. * 1; For example: 1! = 1 (Base Case) 2! = 2 * 1 = 2 3! = 3 * 2 * 1 = 6 4! = 4 * 3 * 2 * 1 = 24 5! = 5 * 4 * 3 * 2 * 1 = 120 If you study this table closely, you will start to see a pattern.

Seeing the Pattern Seeing the pattern in the factorial example is difficult at first. But, once you see the pattern, you can apply this pattern to create a recursive solution to the problem. Divide a problem up into: –What it can do (usually a base case) –What it cannot do What it cannot do resembles original problem The function launches a new copy of itself (recursion step) to solve what it cannot do.

Factorials Computing factorials are a classic problem for examining recursion. A factorial is defined as follows: n! = n * (n-1) * (n-2) …. * 1; For example: 1! = 1 (Base Case) 2! = 2 * 1 = 2 3! = 3 * 2 * 1 = 6 4! = 4 * 3 * 2 * 1 = 24 5! = 5 * 4 * 3 * 2 * 1 = 120 If you study this table closely, you will start to see a pattern. The pattern is as follows: You can compute the factorial of any number (n) by taking n and multiplying it by the factorial of (n-1). For example: 5! = 5 * 4! (which translates to 5! = 5 * 24 = 120)

Recursive Solution public class FindFactorialRecursive { public static void main (String args[]) { for (int i = 1; i < 10; i++) System.out.println ( i + "! = " + findFactorial(i)); } public static int findFactorial (int number) { if ( (number == 1) || (number == 0) ) return 1; else return (number * findFactorial (number-1)); } Base Case.

Finding the factorial of 3 Time 2: Push: fact(3) main() fact(3) Time 3: Push: fact(2) Inside findFactorial(3): if (number <= 1) return 1; else return (3 * factorial (2)); main() fact(3) fact(2) Time 4: Push: fact(1) Inside findFactorial(2): if (number <= 1) return 1; else return (2 * factorial (1)); main() fact(3) fact(2) fact(1) Inside findFactorial(1): if (number <= 1) return 1; else return (1 * factorial (0)); Time 5: Pop: fact(1) returns 1. main() fact(3) fact(2) 1 Time 6: Pop: fact(2) returns 2. main() fact(3) 2 Time 7: Pop: fact(3) returns 6. main() 6

 2003 Prentice Hall, Inc. All rights reserved. 29 Example Using Recursion: The Fibonacci Series Fibonacci series –Each number in the series is sum of two previous numbers e.g., 0, 1, 1, 2, 3, 5, 8, 13, 21… fibonacci(0) = 0 fibonacci(1) = 1 fibonacci(n) = fibonacci(n - 1) + fibonacci( n – 2 ) fibonacci(0) and fibonacci(1) are base cases –Golden ratio (golden mean)

 2003 Prentice Hall, Inc. All rights reserved. 30 import javax.swing.JOptionPane; public class FibonacciTest { public static void main (String args[]) { long number, fibonacciValue; String numberAsString; numberAsString = JOptionPane.showInputDialog("What Fib value do you want?"); number = Long.parseLong( numberAsString ); fibonacciValue = fibonacci( number ); System.out.println (fibonacciValue); System.exit (0); } // recursive declaration of method fibonacci public static long fibonacci( long n ) { if ( n == 0 || n == 1 ) return n; else return fibonacci( n - 1 ) + fibonacci( n - 2 ); } // end method fibonacci } // end class FibonacciTest

 2003 Prentice Hall, Inc. All rights reserved. 31 Recursion vs. Iteration Iteration –Uses repetition structures ( for, while or do…while ) –Repetition through explicitly use of repetition structure –Terminates when loop-continuation condition fails –Controls repetition by using a counter Recursion –Uses selection structures ( if, if…else or switch ) –Repetition through repeated method calls –Terminates when base case is satisfied –Controls repetition by dividing problem into simpler one

 2003 Prentice Hall, Inc. All rights reserved. 32 Recursion vs. Iteration (cont.) Recursion –More overhead than iteration –More memory intensive than iteration –Can also be solved iteratively –Often can be implemented with only a few lines of code