Recursion 10-Apr-19.

Slides:



Advertisements
Similar presentations
Basics of Recursion Programming with Recursion
Advertisements

Recursion CS 367 – Introduction to Data Structures.
Recursion in Scala. 2 Definitions A recursive method is a method that calls itself A method is indirectly recursive if it calls a method that calls a.
Factorial Recursion stack Binary Search Towers of Hanoi
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
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:
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.
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
Stacks. 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.
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:
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:
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.
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.
1 CS 177 Week 16 Recitation Recursion. 2 Objective To understand and be able to program recursively by breaking down a problem into sub problems and joining.
1 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert Moyer, Montgomery County Community.
Recursion Concepts Implementation Data Structures and Algorithms in Java, Third EditionCh05 – 1.
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.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
ITEC 109 Lecture 11 While loops. while loops Review Choices –1 st –2 nd to ?th –Last What happens if you only use ifs? Can you have just an else by itself?
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.
October 3, 2001CSE 373, Autumn Mathematical Background Exponents X A X B = X A+B X A / X B = X A-B (X A ) B = X AB X N +X N = 2X N 2 N +2 N = 2 N+1.
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.
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
W1-1 University of Washington Computer Programming I Recursion © 2000 UW CSE.
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.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
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.
Recursion Powerful Tool
Programming with Recursion
Recursion.
4. Java language basics: Function
Recursion DRILL: Please take out your notes on Recursion
Recursion 4-Jun-18.
Recursion notes Chapter 8.
Java 4/4/2017 Recursion.
Recursion CSE 2320 – Algorithms and Data Structures
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Java Software Structures: John Lewis & Joseph Chase
Programming with Recursion
Building Java Programs
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Quicksort 1.
Recursion 12-Nov-18.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Algorithm design and Analysis
Stacks.
Recursion 2-Dec-18.
Recursion 2-Dec-18.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Recursion 29-Dec-18.
Stacks.
Basics of Recursion Programming with Recursion
Analysis of Algorithms
Recursion Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves,
When a function is called...
Recursion notes Chapter 8.
Recursion 23-Apr-19.
Building Java Programs
Chapter 18 Recursion.
Analysis of Algorithms
ITEC324 Principle of CS III
Analysis of Algorithms
Lecture 6 - Recursion.
Presentation transcript:

Recursion 10-Apr-19

Definitions A recursive method is a method that calls itself A method is indirectly recursive if it calls a method that calls a method ... that calls the original method Mutually recursive methods are methods that call each other

Recursive functions...er, methods The mathematical definition of factorial is: 1, if n <= 1 n * factorial(n-1) otherwise factorial(n) 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 legal in almost all languages

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

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

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]

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

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: Write a “façade” 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

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 == 1) return 1; if (n % 2 == 0) return collatz(n / 2); else return collatz(3 * n - 1); }

Example: member // A façade method to test whether x occurs in a 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 }

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!

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

The End