Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.

Slides:



Advertisements
Similar presentations
Introduction to Recursion and Recursive Algorithms
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.
Recursion Rosen 5 th ed., § Recursion Sometimes, defining an object explicitly might be difficult.Sometimes, defining an object explicitly might.
Recursion. Binary search example postponed to end of lecture.
Recursion CS 308 – Data Structures. What is recursion? smaller version Sometimes, the best way to solve a problem is by solving a smaller version of the.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Programming with Recursion
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 7 Recursion. 2 What Is Recursion? l Recursive call A method call in which the method being called is the same as the one making the call l Direct.
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.
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.
Prof. S.M. Lee Department of Computer Science. Answer:
Chapter 3: Recursive Algorithms
Recursion CS Goals Discuss recursion as another form of repetition Do the following tasks, given a recursive routine Determine whether the routine.
CS Discrete Mathematical Structures Mehdi Ghayoumi MSB rm 132 Ofc hr: Thur, 9:30-11:30a.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
Prof. S.M. Lee Department of Computer Science.
Recursion CS 3358 – Data Structures. Big Picture Our objective is to write a recursive program and convince ourselves it is correct with the minimum amount.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Modified from the slides by Sylvia Sorkin, Community College of Baltimore County.
Computer Science and Software Engineering University of Wisconsin - Platteville 9. Recursion Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++
Recursion CS 302 – Data Structures Chapter 7. What is recursion? smaller A technique that solves problem by solving smaller versions of the same problem!
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Recursion.
Lecture 7. Solution by Substitution Method T(n) = 2 T(n/2) + n Substitute n/2 into the main equation 2T(n/2) = 2(2(T(n/4)) + n/2) = 4T(n/4) + n And T(n)
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
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.
Recursion. What is recursion? smaller version Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first.
Chapter 15: Recursion. Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
Lecture #3 Analysis of Recursive Algorithms
12-CRS-0106 REVISED 8 FEB 2013 KUG1C3 Dasar Algoritma dan Pemrograman.
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! =
Data Structures I (CPCS-204) Week # 5: Recursion Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
递归算法的效率分析. When a function is called... A transfer of control occurs from the calling block to the code of the function --It is necessary that there be.
Recursion Powerful Tool
Programming with Recursion
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 19: Recursion.
Recursion Version 1.0.
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.
Recursion Topic 5.
Chapter 15 Recursion.
RECURSION.
Chapter 10 Recursion Instructor: Yuksel / Demirer.
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Computer Science 4 Mr. Gerb
Chapter 15 Recursion.
Java 4/4/2017 Recursion.
Chapter 14: Recursion Starting Out with C++ Early Objects
Programming with Recursion
Chapter 14: Recursion Starting Out with C++ Early Objects
Applied Algorithms (Lecture 17) Recursion Fall-23
Algorithm design and Analysis
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion Data Structures.
Basics of Recursion Programming with Recursion
Yan Shi CS/SE 2630 Lecture Notes
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Recursion.
7.2 Recursive Definitions of Mathematical Formulas
Presentation transcript:

Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking

CS 221 - Computer Science II What is Recursion? Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first. Recursion is a technique that solves a problem by solving a smaller problem of the same type first. CS 221 - Computer Science II

CS 221 - Computer Science II Recursion More than programming technique: A way of describing, defining, or specifying things. A way of designing solutions to problems (divide and conquer). CS 221 - Computer Science II

CS 221 - Computer Science II Basic Recursion 1. Base cases: Always have at least one case that can be solved without using recursion. Indicates an end to the recursion. 2. Make progress: Any recursive call must make progress toward a base case. CS 221 - Computer Science II

Mathematical Examples There are recursive definitions for many mathematical problems: Power Function Fibonacci Sequence Factorial Function CS 221 - Computer Science II

CS 221 - Computer Science II Power Function The Power function yx : raise the number y to the xth power. Assume x is a non-negative integer: CS 221 - Computer Science II

CS 221 - Computer Science II Power Function The Power function yx : raise the number y to the xth power. Assume x is a non-negative integer: yx = 1 if x is 0 // base case yx = y*y(x-1) otherwise // make progress CS 221 - Computer Science II

CS 221 - Computer Science II Power Function 23 = 2*22 CS 221 - Computer Science II

CS 221 - Computer Science II Power Function 23 = 2 * 22 22 = CS 221 - Computer Science II

CS 221 - Computer Science II Power Function 23 = 2 * 22 22 = 2 * 21 21 = CS 221 - Computer Science II

CS 221 - Computer Science II Power Function 23 = 2 * 22 22 = 2 * 21 21 = 2 * 20 20 = CS 221 - Computer Science II

CS 221 - Computer Science II Power Function 23 = 2 * 22 22 = 2 * 21 21 = 2 * 20 20 = 1 CS 221 - Computer Science II

CS 221 - Computer Science II Power Function 23 = 2 * 22 22 = 2 * 21 21 = 2 * 20 = 2 * 1 = 2 20 = 1 CS 221 - Computer Science II

CS 221 - Computer Science II Power Function 23 = 2 * 22 22 = 2 * 21 = 2 * 2 = 4 21 = 2 * 20 = 2 * 1 = 2 20 = 1 CS 221 - Computer Science II

CS 221 - Computer Science II Power Function 23 = 2 * 22 = 2 * 4 = 8 22 = 2 * 21 = 2 * 2 = 4 21 = 2 * 20 = 2 * 1 = 2 20 = 1 CS 221 - Computer Science II

CS 221 - Computer Science II Fibonacci Sequence Fibonacci Sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,… Fibonacci Function: Fib(0) = 1 // base case Fib(1) = 1 // base case Fib(n) = Fib(n-1) + Fib(n-2) // n>1 Unlike most recursive algorithms: Two base cases, not just one. Two recursive calls, not just one. CS 221 - Computer Science II

CS 221 - Computer Science II Factorial Factorial Function factorial(0) = 1 // base case factorial(n) = n * factorial(n-1) // n > 0 Compute factorial(3): CS 221 - Computer Science II

CS 221 - Computer Science II Factorial Factorial Function factorial(0) = 1 // base case factorial(n) = n * factorial(n-1) // n > 0 Compute factorial(3): factorial(3) = 3 * factorial(2) = 3 * ( 2 * factorial(1) ) = 3 * ( 2 * ( 1 * factorial(0) ) = 3 * ( 2 * ( 1 * 1 ) )) = 6 CS 221 - Computer Science II

Coding the Factorial Function Recursive Implementation int factorial(int n) { if (n==0) // base case return 1; else return n * factorial(n-1); } CS 221 - Computer Science II

CS 221 - Computer Science II Recursive Call Stack CS 221 - Computer Science II

Implementing Recursion What happens when a function gets called? // a method int b(int x) { int z,y;  // other statements z = a(x) + y; return z; } // another method int a(int w) return w+w;   CS 221 - Computer Science II

When a Function is Called Stop executing function b. To return to function b later, need to store everything about function b. Create activation record. Includes values of variables x, y, z. The place to start executing when return. Push activation record onto the call stack. Then, a is bound to w from b. Control is transferred to function a. CS 221 - Computer Science II

When a Function is Called After function a is executed, the activation record is popped out off call stack. Values of the parameters and variables in function b are restored. Return value of function a replaces a(x) in the assignment statement. CS 221 - Computer Science II

Recursion vs. Iteration Recursion calls the same function over and over. Recursive algorithms use a branching structure. Iteration `jumps back' to the start of the loop. Iterative algorithms use a looping construct. Iteration can be used in place of recursion Recursive solutions are usually less efficient. In terms of both time and space. Usually exponential in both. Recursion may simplify the solution. Shorter, more easily understood source code. CS 221 - Computer Science II

CS 221 - Computer Science II Recursion Overhead Space Every invocation of a function call requires: Space for parameters and local variables. Space for return address. Thus, a recursive algorithm needs space proportional to the number of nested calls to the same function. CS 221 - Computer Science II

CS 221 - Computer Science II Recursion Overhead Time Calling a function involves: Allocating, and later releasing, local memory. Copying values into the local memory for the parameters. Branching to/returning from the function All contribute to the time overhead, which is generally exponential. CS 221 - Computer Science II

Coding Factorial Function Iterative implementation: int factorial(int n) { int fact = 1;   for(int count = 2; count <= n; count++) fact = fact * count; return fact; } CS 221 - Computer Science II

Other Recursive Examples Combinations Euclid’s Algorithm Binary Search CS 221 - Computer Science II

Combinations: n choose k Given n things, how many different sets of size k can be chosen? n n-1 n-1 = + , 1 < k < n (recursive solution) k k k-1 n n! = , 1 < k < n (closed-form solution) k k!(n-k)! with base cases: n n = n (k = 1), = 1 (k = n) 1 n Pascal’s Triangle CS 221 - Computer Science II

Combinations: n choose k int combinations(int n, int k) { if(k == 1) // base case 1 return n; else if (n == k) // base case 2 return 1; else return(combinations(n-1, k) + combinations(n-1, k-1)); } CS 221 - Computer Science II

Combinations

CS 221 - Computer Science II Euclid's Algorithm About 300 BC, Euclid wrote an algorithm to calculate the greatest common divisor (GCD) of two numbers x and y, where (x < y). This can be stated as: Divide y by x with remainder r. Replace y by x, and x with r. Repeat step 1 until r is zero. When this algorithm terminates, y is the highest common factor. CS 221 - Computer Science II

CS 221 - Computer Science II GCD(34017, 16966) Euclid's algorithm works as follows: 34,017/16,966 produces a remainder 85 16,966/85 produces a remainder 51 85/51 produces a remainder 34 51/34 produces a remainder 17 34/17 produces a remainder 0 The highest common divisor of 34,017 and 16,966 is 17. CS 221 - Computer Science II

Writing a Recursive Function Determine the base case(s) . Can solve without recursion. Determine the general case(s) . Express solution as a smaller version of itself. Verify the algorithm. Use the "Three-Question-Method." CS 221 - Computer Science II

Three-Question Method 1. The Base-Case Question: Is there a non-recursive way out of the function, and does the routine work correctly for this "base" case?  2. The Smaller-Caller Question: Does each recursive call to the function involve a smaller case of the original problem, leading inescapably to the base case?  3. The General-Case Question: Assuming that the recursive call(s) work correctly, does the whole function work correctly? CS 221 - Computer Science II

CS 221 - Computer Science II Binary Search Search algorithm Finds a target value within a sorted list. Compares target value to the middle element. If the two are equal, done. If target less than middle element, search lower half of list. Otherwise, search upper half of list. Continue dividing list in half until find target or run out of list to search. Efficiency: Runs in at worst logarithmic O(log n) time. Takes up linear O(n) space. CS 221 - Computer Science II

Recursive Binary Search What are the base cases? If first > last, return false If item==info[midPoint], return true What is the general case? if item < info[midPoint] // search the first half if item > info[midPoint], //search the second half CS 221 - Computer Science II

Recursive Binary Search boolean binarySearch(Item info[], Item item, int first, int last) { int midPoint;   if(first > last) // base case 1 return false; else midPoint = (first + last)/2; if (item == info[midPoint]) // base case 2 item = info[midPoint]; return true; } else if(item < info[midPoint]) return binarySearch(info, item, first, midPoint-1); return binarySearch(info, item, midPoint+1, last); CS 221 - Computer Science II

CS 221 - Computer Science II When to Use Recursion When the depth of recursive calls is relatively "shallow." The recursive version does about the same amount of work as the non-recursive version. The recursive version is shorter and simpler than the non-recursive solution. CS 221 - Computer Science II

CS 221 - Computer Science II Benefits of Recursion Recursive functions are clearer, simpler, shorter, and easier to understand than their non-recursive counterparts. The program directly reflects the abstract solution strategy (algorithm). Reduces the cost of maintaining the software. CS 221 - Computer Science II

Disadvantages of Recursion Makes it easier to write simple and elegant programs, but it also makes it easier to write inefficient ones. Use recursion to ensure correctness, not efficiency. Simple, elegant recursive algorithms are inherently inefficient. CS 221 - Computer Science II

CS 221 - Computer Science II