Recursion Recursive Thinking Recursive Programming

Slides:



Advertisements
Similar presentations
Basics of Recursion Programming with Recursion
Advertisements

Recursion.
Recursion CS /02/05 L7: Files Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Iteration.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 16: Recursion.
1 Recursion  Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems  Chapter 11 of the book.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Csci1300 Introduction to Programming Recursion Dan Feng.
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.
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.
1 Recursion Recursive Thinking Recursive Programming Recursion versus Iteration Direct versus Indirect Recursion More on Project 2 Reading L&C 10.1 – 10.4.
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.
Chapter 11 Recursion. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Recursion Recursion is a fundamental programming technique that can provide.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Recursion Review.
Programming in C++ Language ( ) Lecture 6: Functions-Part2 Dr. Lubna Badri.
M180: Data Structures & Algorithms in Java
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Functions and an Introduction to Recursion.  Recursive function ◦ A function that calls itself, either directly, or indirectly (through another function)
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Lecture 12 Recursion part 1 Richard Gesick. Recursion A recursive method is a method that calls itself. A recursive method is capable of solving only.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter 8 Recursion Modified.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
1 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
CSE 501N Fall ‘09 12: Recursion and Recursive Algorithms 8 October 2009 Nick Leidenfrost.
11-1 Recursive Thinking A recursive definition is one which uses the word or concept being defined in the definition itself When defining an English word,
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.
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 =
Recursion A function that calls itself. Recursion A function which calls itself is said to be recursive. Recursion is a technique which will allow us.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
Chapter 6: Repetition Continued. 2 Validity Checks What’s weak about the following code ? do { s1 = JOptionPane.showInputDialog (“Enter a number: ”);
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
1 Recursion Recursive Thinking Recursive Programming Recursion versus Iteration Direct versus Indirect Recursion Reading L&C 3 rd : 7.1 – nd :
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
Chapter 15: Recursion. Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
Chapter 4 Repetition Statements (loops)
Recursion DRILL: Please take out your notes on Recursion
Abdulmotaleb El Saddik University of Ottawa
RECURSION.
Chapter 3: Decisions and Loops
Recursion, Tail Recursion
Recursion Recursive Thinking Recursive Programming
Lecture 17 Recursion part 1 Richard Gesick.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Recursion (part 1) October 24, 2007 ComS 207: Programming I (in Java)
Unit 3 Test: Friday.
Chapter 11 Recursion.
11 Recursion Software Solutions Lewis & Loftus java 5TH EDITION
Lecture 12 Recursion part 1 CSE /26/2018.
Review of Previous Lesson
Java Software Solutions Foundations of Program Design Sixth Edition
Recursion.
Recursion (part 1) October 25, 2006 ComS 207: Programming I (in Java)
Presentation transcript:

Recursion Recursive Thinking Recursive Programming Recursion versus Iteration Direct versus Indirect Recursion More on Project 2 Reading L&C 7.1 – 7.2

Recursive Thinking Many common problems can be stated in terms of a “base case” and an “inferred sequence of steps” to develop all examples of the problem statement from the base case Let’s look at one possible definition of a comma separated values (.csv) list: A list can contain one item (the base case) A list can contain one item, a comma, and a list (the inferred sequence of steps)

Recursive Thinking The above definition of a list is recursive because the second portion of the definition depends on there already being a definition for a list The second portion sounds like a circular definition that is not useful, but it is useful as long as there is a defined base case The base case gives us a mechanism for ending the circular action of the second portion of the definition

Recursive Thinking Using the recursive definition of a list: A list is a: number A list is a: number comma list Leads us to conclude 24, 88, 40, 37 is a list number comma list 24 , 88, 40, 37 88 , 40, 37 40 , 37 number 37

Recursive Thinking Note that we keep applying the recursive second portion of the definition until we reach a situation that meets the first portion of the definition (the base case) Then we apply the base case definition What would have happened if we did not have a base case defined?

Infinite Recursion If there is no base case, use of a recursive definition becomes infinitely long and any program based on that recursive definition will never terminate and produce a result This is similar to having an inappropriate or no condition statement to end a “for”, “while”, or “do … while” loop

Recursion in Math One of the most obvious math definitions that can be stated in a recursive manner is the definition of integer factorial The factorial of a positive integer N (N!) is defined as the product of all integers from 1 to the integer N (inclusive) That definition can be restated recursively 1! = 1 (the base case) N! = N * (N – 1)! (the recursion)

Recursion in Math Using that recursive definition to get 5! 5! = 5 * (5-1)! 5! = 5 * 4 * (4-1)! 5! = 5 * 4 * 3 * (3-1)! 5! = 5 * 4 * 3 * 2 * (2-1)! 5! = 5 * 4 * 3 * 2 * 1! (the base case) 5! = 5 * 4 * 3 * 2 * 1 5! = 120

Recursive Programming Recursive programming is an alternative way to program loops without using “for”, “while”, or “do … while” statements A Java method can call itself A method that calls itself must choose to continue using either the recursive definition or the base case definition The sequence of recursive calls must make progress toward meeting the definition of the base case

Recursion versus Iteration We can calculate 5! using a loop int fiveFactorial = 1; for (int i = 1; i <= 5; i++) fiveFactorial *= i; Or we can calculate 5! using recursion int fiveFactorial = factorial(5); . . . private int factorial(int n) { return n == 1? 1 : n * factorial(n – 1); }

Recursion versus Iteration return 120 main return 24 factorial factorial(5) return 6 factorial factorial(4) return 2 factorial factorial(3) return 1 factorial factorial(2) factorial factorial(1)

Recursion versus Iteration Note that in the “for” loop calculation, there is only one variable containing the factorial value in the process of being calculated In the recursive calculation, a new variable n is created on the system stack each time the method factorial calls itself As factorial calls itself proceeding toward the base case, it pushes the current value of n-1 As factorial returns after the base case, the system pops the now irrelevant value of n-1

Recursion versus Iteration Note that in the “for” loop calculation, there is only one addition (i++) and a comparison (i<=5) needed to complete each loop In the recursive calculation, there is a comparison (n==1) and a subtraction (n - 1), but there is also a method call/return needed to complete each loop Typically, a recursive solution uses both more memory and more processing time than an iterative solution

Direct versus Indirect Recursion Direct recursion is when a method calls itself Indirect recursion is when a method calls a second method (and/or perhaps subsequent methods) that can call the first method again method1 method2 method3 method1 method2 method3 method1 method2 method3

Calling main( ) Recursively  Any Java method can call itself Even main() can call itself as long as there is a base case to end the recursion You are restricted to using a String [] as the parameter list for main() The JVM requires the main method of a class to have that specific parameter list

Calling main( ) Recursively  public class RecursiveMain { public static void main(String[] args) if (args.length > 1) { String [] newargs = new String[args.length - 1]; for (int i = 0; i < newargs.length; i++) newargs[i] = args[i + 1]; main(newargs); // main calls itself with a new args array } System.out.println(args[0]); return; java RecursiveMain computer science is fun fun is science computer

More on Project 2 The Solve2 class for project 2 needs a recursive recurse() method You don’t need an explicit stack to backtrack When recurse calls itself: the context of all local variables (path, current, and end) is left on the system stack a new context for all local variables is created on the top of the system stack The return from recurse() pops the previous context off the system stack

More on Project 2 The context variables consist of: The Stack<Bird> path that gets passed each time recurse is called. In my suggested solution strategy, you push Bird references on this stack in reverse order along the path when you reach the end Bird and start returning true The Bird current that gets used to check for the end and get the next Bird in its direction The Bird end that is used to check against the current Bird to determine if solved or not

Calling Sequence / System Stack recurse(path, B2.0, B2.6) main recurse(path, B1.1, B2.6) recurse recurse(path, B1.0, B2.6) recurse recurse(path, B2.0, B2.6) recurse recurse(path, B0.2, B2.6) recurse recurse System Stack (Context for Each Instance of Recurse Above) path[empty] curr. = B2.0 end = B2.6 path[empty] curr. = B1.1 end = B2.6 path[empty] curr. = B1.0 end = B2.6 path[empty] curr. = B2.0 end = B2.6 path[empty] curr. = B0.2 end = B2.6 * * *

Calling Sequence / System Stack From B3.3 recurse(path, B1.2, B2.6)  First time recurse recurse(path, B3.0, B2.6) recurse false recurse(path, null, B2.6) recurse false recurse(path, B0.2, B2.6) Second Time  recurse recurse false System Stack (Context for Each Instance of Recurse Above) path[empty] curr. = B2.2 end = B2.6 path[empty] curr. = B1.2 end = B2.6 path[empty] curr. = B3.0 end = B2.6 path[empty] curr. = null end = B2.6 path[empty] curr. = B2.2 end = B2.6 * * * path[empty] curr. = B2.2 end = B2.6 path[empty] curr. = B0.2 end = B2.6 * * *

Returning Sequence / System Stack recurse true recurse true recurse true recurse true recurse true System Stack (Context for Each Instance of Recurse Above) path[B1.6, B1.5, B4,5, B4.4, B2.6] curr. = B1.6 end = B2.6 path[B1.5, B4.5, B4.4, B2.6] curr. = B1.5 end = B2.6 path[B4.5, B4.4, B2.6] curr. = B4.5 end = B2.6 path[B4.4, B2.6] curr. = B4.4 end = B2.6 path[B2.6] curr. = B2.6 end = B2.6 * * *