29-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:

Slides:



Advertisements
Similar presentations
Basics of Recursion Programming with Recursion
Advertisements

Recursion CS 367 – Introduction to Data Structures.
Factorial Recursion stack Binary Search Towers of Hanoi
Sorting. “Sorting” When we just say “sorting,” we mean in ascending order (smallest to largest) The algorithms are trivial to modify if we want to sort.
Analysis of Algorithms. Time and space To analyze an algorithm means: –developing a formula for predicting how fast an algorithm is, based on the size.
16-Jun-15 Recognizers. 2 Parsers and recognizers Given a grammar (say, in BNF) and a string, A recognizer will tell whether the string belongs to the.
16-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Programming with Recursion
Quicksort. Quicksort I To sort a[left...right] : 1. if left < right: 1.1. Partition a[left...right] such that: all a[left...p-1] are less than a[p], and.
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.
Quicksort.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
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.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Data Structures Review Session 1
28-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
28-Jun-15 Recognizers. 2 Parsers and recognizers Given a grammar (say, in BNF) and a string, A recognizer will tell whether the string belongs to the.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Unit 7 1 Unit 7: Recursion H Recursion is a fundamental programming technique that can provide an elegant solution to a certain kinds of problems H In.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
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.
analysis, plug ‘n’ chug, & induction
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
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.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
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.
CPS120: Introduction to Computer Science Functions.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Halting Problem Introduction to Computing Science and Programming I.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
Function Definition by Cases and Recursion Lecture 2, Programmeringsteknik del A.
LECTURE 20: RECURSION CSC 212 – Data Structures. Humorous Asides.
Chapter 7 Programming with Recursion. What Is Recursion? Recursive call A method call in which the method being called is the same as the one.
27-Jan-16 Analysis of Algorithms. 2 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based.
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
Question of the Day  What three letter word completes the first word and starts the second one: DON???CAR.
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
Recursion Powerful Tool
Recursion 4-Jun-18.
Java 4/4/2017 Recursion.
Java Software Structures: John Lewis & Joseph Chase
Programming with Recursion
Recursion 12-Nov-18.
Stacks.
slides created by Ethan Apter
Recursion 2-Dec-18.
Recursion 2-Dec-18.
Data Structures Review Session
Recursion 29-Dec-18.
slides created by Ethan Apter
Stacks.
Basics of Recursion Programming with Recursion
Recursion 10-Apr-19.
Recursion 23-Apr-19.
slides created by Ethan Apter and Marty Stepp
Lecture 6 - Recursion.
Presentation transcript:

29-Jun-15 Recursion

2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: An atom is a name or a number A list consists of: An open parenthesis, "(" Zero or more atoms or lists, and A close parenthesis, ")"

3 Definitions II Indirect recursion is when a thing is defined in terms of other things, but those other things are defined in terms of the first thing Example: A list is: An open parenthesis, Zero or more S-expressions, and A close parenthesis An S-expression is an atom or a list

4 Recursive functions...er, methods The mathematical definition of factorial is: We can define this in Java as: long factorial(long n) { if (n <= 1) return 1; else return n * factorial(n – 1); } This is a recursive function because it calls itself Recursive functions are completely legal in Java 1, if n <= 1 n * factorial(n-1) otherwise factorial(n) is

5 Anatomy of a recursion long factorial(long n) { if (n <= 1) return 1; else return n * factorial(n – 1); } Base case: does some work without making a recursive call Recursive case: recurs with a simpler parameter Extra work to convert the result of the recursive call into the result of this call

6 Another dumb example The following fills an array with the numbers 0 through n-1 void run() { int[ ] a = new int[10]; fill(a, a.length - 1); System.out.println(Arrays.toString(a)); } void fill(int[ ] a, int n) { if (n < 0) return; else { a[n] = n; fill(a, n - 1); } } [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

7 Parts of the dumb example void fill(int[ ] a, int n) { if (n < 0) return; else { a[n] = n; fill(a, n - 1); } } Base case: does some work without making a recursive call Recursive case: recurs with a simpler parameter Extra work to convert the result of the recursive call into the result of this call

8 Improving the dumb example The line fill(a, a.length - 1); just seems ugly Why should we have ask the array how big it is, then tell the method? Why can’t the method itself ask the array? Solution: Put a “front end” on the method, like so: void fill(int[ ] a) { fill(a, a.length - 1); } Now in our run method we can just say fill(a); We can, if we want, “hide” the two-parameter version by making it private

9 The four rules Do the base cases first Recur only with simpler cases Don't modify and use non-local variables You can modify them or use them, just not both Remember, parameters count as local variables, but if a parameter is a reference to an object, only the reference is local—not the referenced object Don't look down

10 Base cases and recursive cases Every valid recursive definition consists of two parts: One or more base cases, where you compute the answer directly, without recursion One or more recursive cases, where you do part of the work, and recur with a simpler problem

11 Do the base cases first Every recursive function must have some things it can do without recursion These are the simple, or base, cases Test for these cases, and do them first The important part here is testing before you recur; the actual work can be done in any order long factorial(long n) { if (n > 1) return n * factorial(n – 1); else return 1; } However, it’s usually better style to do the base cases first This is just writing ordinary, nonrecursive code

12 Recur only with a simpler case If the problem isn't simple enough to be a base case, break it into two parts: A simpler problem of the same kind (for example, a smaller number, or a shorter list) Extra work not solved by the simpler problem Combine the results of the recursion and the extra work into a complete solution “Simpler” means “more like a base case”

13 Infinite recursion The following is the recursive equivalent of an infinite loop: int toInfinityAndBeyond(int x) { return toInfinityAndBeyond(x); } This happened because we recurred with the same case! While this is obviously foolish, infinite recursions can happen by accident in more complex methods int collatz(int n) { if (n % 2 == 0) return collatz(n / 2); else return collatz(3 * n - 1); }

14 Don’t modify and use non-local variables Consider the following code fragment: int n = 10;... int factorial() { if (n <= 1) return 1; else { n = n – 1; return (n + 1) * factorial(); } } It is very difficult to determine (without trying it) whether this method works The problem is keeping track of the value of n at all the various levels of the recursion

15 Modifying or using global variables When we change the value of a “global” variable, we change it for all levels of the recursion Hence, we cannot understand a single level in isolation It’s okay to modify a global variable if we don’t also use it For example, we might update a variable count as we step through a list It’s okay to use (read) a global variable if we don’t also try to change it As far as our code is concerned, it’s just a constant The problem comes when we try to both modify a global variable and use it in the recursion

16 Using non-local variables int total = 0; int[ ] b = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int sum(int n) { if (n < 0) return total; else { total += b[n]; sum(n - 1); return total; } } The global array b is being used, but not changed The global variable total is being changed, but not used (at least, not in any way that affects program execution) This program works, and can be understood

17 Style The previous method works, but it’s terrible style! int sum(int n) { if (n < 0) return total; else { total += b[n]; sum(n - 1); return total; } } What’s b ? What’s total ? Where do these come from? The method just isn’t very “self-contained” It would be more acceptable if b and total were instance variables describing the state of this object—but that seems unlikely Some programmers prefer using getters and setters for all instance variables

18 It's OK to modify local variables A function has its own copy of local variables parameters passed by value (which are effectively local variables) Each level of a recursive function has its own copy of these variables and parameters Changing them at one level does not change them at other levels One level can't interfere with another level

19 It's bad to modify objects There is (typically) only one copy of a given object If a parameter is passed by reference, there is only one copy of it If such a variable is changed by a recursive function, it’s changed at all levels Hence, it’s acting like a global variable (one accessible to all parts of the program) The various levels interfere with one another This can get very confusing Don’t let this happen to you!

20 Don't look down When you write or debug a recursive function, think about this level only Wherever there is a recursive call, assume that it works correctly If you can get this level correct, you will automatically get all levels correct You really can't understand more than one level at a time, so don’t even try

21 We have small heads* It's hard enough to understand one level of one function at a time It's almost impossible to keep track of many levels of the same function all at once But you can understand one level of one function at a time......and that's all you need to understand in order to use recursion well *According to Edsger Dijkstra

22 Example: member // Just a front end boolean member(int x, int[ ] a) { return member(x, a, a.length - 1); } boolean member(int x, int[ ] a, int n) { if (a[n] == x) return true; // one base case if (n < 0) return false; // another base case return member(x, a, n - 1); // recursive case }

23 Proving that member is correct boolean member(int x, int[] a, int n) { This is supposed to test if x is one of the elements 0..n of the array a if (a[n] == x) return true; This says: If x is in location n of the array, then it’s in the array This is obviously true if (n < 0) return false; This says: If we’ve gone off the left end of the array, then x isn’t in the array This is true if: We started with the rightmost element of the array (true because of the front end), and We looked at every element (true because we decrease n by 1 each time) return member(x, a, n - 1); This says: If x isn’t in location n, then x is one of the elements 0..n if and only if x is one of the elements 0..n-1 } Did we cover all possible cases? Did we recur only with simpler cases? Did we change any non-local variables? We’re done!

24 Reprise Do the base cases first Recur only with a simpler case Don't modify and use nonlocal variables Don't look down

25 The End