RECURSION Go back, Jack, and do it again.. Recursion 2  Recursion occurs whenever "something" is defined in terms of itself.  Structural recursion:

Slides:



Advertisements
Similar presentations
Recursive methods. Recursion A recursive method is a method that contains a call to itself Often used as an alternative to iteration when iteration is.
Advertisements

Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Chapter 10 Recursion Instructor: alkar/demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe.
1 Recursion Overview l Introduction to recursion and recursive methods l Simple popular recursive algorithms l Writing recursive methods l Preview: Parameter.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 14: Recursion by.
Unit 191 Recursion General Algorithm for Recursion When to use and not use Recursion Recursion Removal Examples Comparison of the Iterative and Recursive.
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Chapter 19 Recursion.
1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such.
CS 202 Computer Science II Lab Fall 2009 October 29.
CSC 2300 Data Structures & Algorithms January 30, 2007 Chapter 2. Algorithm Analysis.
1 Recursion Overview l Introduction to recursion and recursive methods l Simple popular recursive algorithms l Writing recursive methods l Preview: 1-D.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 14 Recursion.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
Unit 181 Recursion Definition Recursive Methods Constructing Recursion Benefits and Usage Infinite Recursion Recursion Removal Examples Exercises.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 19 Recursion.
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
1 Decrease-and-Conquer Approach Lecture 06 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.
CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
Programming Principles II Lecture Notes 5 Recursion Andreas Savva.
Recursive. 2 Recursive Definitions In a recursive definition, an object is defined in terms of itself. We can recursively define sequences, functions.
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-
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.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
CP104 Introduction to Programming Recursion 2 Lecture 29 __ 1 Recursive Function for gcd Recursive formula for the greatest common divisor of m and n,
1 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
1 Recursion n what is it? n how to build recursive algorithms n recursion analysis n tracing simple recursive functions n hands on attempts at writing.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Starting Out with C++, 3 rd Edition Chapter 19 Recursion.
Recursion COMP x1 Sedgewick Chapter 5. Recursive Functions problems can sometimes be expressed in terms of a simpler instance of the same problem.
Recursion. What is Recursion? Method of solving problems Alternative to iteration All recursive solutions can be implemented iteratively Characteristic...
Maitrayee Mukerji. Factorial For any positive integer n, its factorial is n! is: n! = 1 * 2 * 3 * 4* ….* (n-1) * n 0! = 1 1 ! = 1 2! = 1 * 2 = 2 5! =
Program Development and Design Using C++, Third Edition
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.
Chapter 9 Recursion. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe recursive function is –a.
CS212: Data Structures and Algorithms
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 19: Recursion.
Recursion The programs discussed so far have been structured as functions that invoke one another in a disciplined manner For some problems it is useful.
Topic 6 Recursion.
Data Structures with Java © Rick Mercer
Chapter 15 Recursion.
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Decrease-and-Conquer Approach
Recursion Chapter 12.
Chapter 15 Recursion.
Chapter 14: Recursion Starting Out with C++ Early Objects
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion UW CSE 160 Winter 2017
CS201: Data Structures and Discrete Mathematics I
7.Recursion Recursion is the name given for expression anything in terms of itself. Recursive function is a function which calls itself until a particular.
Recursion: The Mirrors
Java Programming: Chapter 9: Recursion Second Edition
Recursion.
Algorithms An algorithm is a set of instructions used to solve a specific problem In order to be useful, an algorithm must have the following properties:
Presentation transcript:

RECURSION Go back, Jack, and do it again.

Recursion 2  Recursion occurs whenever "something" is defined in terms of itself.  Structural recursion: when the data itself is self- referential. A class contains an instance of that class.  Functional recursion: when a procedure is self- referential. A method calls itself.

Example 3  Problem: Write a method to compute the sum of the numbers 1 through N where N is a non-negative integer parameter. Iterative Solution 1 int sum(int n) { int sum = 0; for(int i=1; i<=n; i++) { sum = sum + i; } return sum; } Iterative Solution 2 int sum(int n) { int sum = 0; while(n > 0) { sum += n; n--; } return sum; }

Recursive Solution  The goal is to compute: (N-1) + N  We can define sum(n) as Recursive Solution int sum(int n) { if(n == 0) { return 0; } else { return sum(n-1)+n; } Base Case Reduction step (makes the input smaller)

Variable Scope and Lifetime 5  Class variables belong to Classes  Always exist  Instance variables belong to Objects  Are “born” when the object is created and “die” when the object is destroyed by the garbage collector.  Local variables belong to methods  Are “born” when the method is called and “die” when the method ends.  The time between the “birth” and “death” of a variable is called the “lifetime” of a variable

Basic Approach  Establish a base case for which the solution is trivial.  Execute a reduction step which makes the solution converge towards the base case.  The reduction step reduces the input size (makes the problem simpler)  The reduced problem is identical in nature to the larger problem.  The results of solving this reduced problem can then be used to solve the larger problem

Greatest Common Divisor 7  Goal: Find the largest number D that evenly divides M and N. otherwise if n = 0 gcd(n, m%n) m gcd(m,n) = Base Case Reduction step (makes the input smaller) gcd(1440, 408) = gcd(408, 216) gcd(408, 216) = gcd(216, 192) gcd(216,192) = gcd(192, 24) gcd(192, 24) = gcd(24, 0) gcd(24, 0) = 24 Example Mr. Euclid of Alexandria 325 BC to 265 BC

Greatest Common Divisor 8 otherwise if n = 0 gcd(n, m%n) m gcd(m,n) = Base Case Reduction step (makes the input smaller) Implementation int gcd(int m, int n) { if(n == 0) { return m; } else { return gcd(n, m%n); } Mr. Euclid

Greatest Common Divisor 9  When a method is invoked, an activation of that method is created.  Examine what occurs when executing “int z = gcd(1440, 408)” Activation 1 int gcd(int m=1440, int n=408) { if(n == 0) { return m; } else { return gcd(n, m%n); } Activation 2 int gcd(int m=408, int n=216) { if(n == 0) { return m; } else { return gcd(n, m%n); } Activation 3 int gcd(int m=216, int n=192) { if(n == 0) { return m; } else { return gcd(n, m%n); } Activation 4 int gcd(int m=192, int n=24) { if(n == 0) { return m; } else { return gcd(n, m%n); } Activation 5 int gcd(int m=24, int n=0) { if(n == 0) { return m; } else { return gcd(n, m%n); }

Conversion to Binary 10 Goal: Print the binary representation of the integer N. Implementation void printBinary(int n) { if(n == 0) { return; } printBinary(n/2); System.out.print(n%2); } Algorithm to print the binary representation of N Stop if N = 0 Write a ‘1’ if N is odd and a ‘0’ if N is even Move the pen to the left one digit Print the binary representation of the integer N/2 CallOutput print(43)1 print(21)11 print(10)011 print(5)1011 print(2)01011 print(1) print(0)101011

Watch Out! 11 Mr. Fibonacci 1170 to 1250 Goal: Compute the Nth fibonacci number if n = 11 otherwise if n = 0 fib(n-1)+fib(n-2) 0 fib(n) = Recursive Implementation int fib(int n) { if(n == 0 || n == 1) { return n; } else { return fib(n-2) + fib(n-1); }

Watch Out! 12 Mr. Fibonacci Goal: Compute the Nth fibonacci number Recursive Implementation int fib(int n) { if(n == 0 || n == 1) { return n; } else { return fib(n-2) + fib(n-1); } Why is this so terribly slow?

Reverse Printing 13 Goal: Print a string backwards! Let S be a string consisting of the characters [C 0, C 1, C 2, C 3, …, C n-2, C n-1 ] Note that Reverse(S) = [C n-1, C n-2, …, C 3, C 2, C 1, C 0 ] Reverse(C 0, C n-2 ) Reverse(S) = [C n-1, Reverse(C 0, C n-2 )] Recursive Implementation void printBackwards(String s, int k) { System.out.println(s.charAt(k)); if(k > 0) { printBackwards(s, k-1); }

Reversing an Array 14 Goal: Reverse the elements in an array. Let A be an array consisting of the elements [X 0, X 1, X 2, X 3, …, X n-2, X n-1 ] Note that Reverse(A) = [X n-1, X n-2, …, X 3, X 2, X 1, X 0 ] Reverse(A, X 1, X n-2 ) Reverse(A, f, l) = [X l, Reverse(A, X f+1, X l-1 ), X f ] Recursive Implementation void arrayReverse(int[] data, int first, int last) { if(first < last) { int temp = data[first]; data[first] = data[last]; data[last] = temp; arrayReverse(data, first+1, last-1); }

Pop Quiz! 15  Write a single recursive method to print the numbers N to 0.  Make the signature be “void print(int n)”. Feeble Attempt 1 void print(int n) { System.out.println(n); print(n-1); } Feeble Attempt 2 void print(int n) { if(n > 0) return; System.out.println(n); print(n-1); } 3 rd Times the Charm void print(int n) { if(n < 0) return; System.out.println(n); print(n-1); }

MineSweeper Example 16 public void select(int x, int y) { if coordinate (x,y) is off of the game-board or coordinate (x,y) is already selected then return mark (x,y) as selected if coordinate (x,y) is not a bomb AND not adjacent to a bomb then select(x-1, y-1) select(x, y-1) select(x+1, y-1) select(x-1, y) select(x+1, y) select(x-1, y+1) select(x, y+1) select(x+1, y+1) } 1111B 1B B B B1 Initially – all cells are UNSELECTED Now, select cell (3,1)