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.

Slides:



Advertisements
Similar presentations
Recursion Recursive Thinking Recursive Programming
Advertisements

Proof Techniques and Recursion. Proof Techniques Proof by induction –Step 1: Prove the base case –Step 2: Inductive hypothesis, assume theorem is true.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Revision Using Recursion1 Recursion. Revision Using Recursion2 Recall the Recursion Pattern Recursion: when a method calls itself Classic example--the.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
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.
Unit 181 Recursion Definition Recursive Methods Constructing Recursion Benefits and Usage Infinite Recursion Recursion Removal Examples Exercises.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Introduction to Recursion by Dr. Bun Yue Professor of Computer Science 2013
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
1 Decrease-and-Conquer Approach Lecture 06 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.
Introduction to Recursion CSCI 3333 Data Structures.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
Chapter 3 (Part 3): Mathematical Reasoning, Induction & Recursion  Recursive Algorithms (3.5)  Program Correctness (3.6)
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 4 (Part 3): Mathematical Reasoning, Induction.
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.
M180: Data Structures & Algorithms in Java
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
RecursionRecursion Recursion You should be able to identify the base case(s) and the general case in a recursive definition To be able to write a recursive.
Chapter 15 Recursion INTRODUCTION Recursion is a program-solving technique that expresses the solution of a problem in terms of the solutions of.
Recursion Self-Referencing Functions. Problem # 1 Write a function that, given n, computes n! n! == (n-1) n n! == 1  2 ...  (n-1)  nExample:
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
1 Chapter 3. Recursion Lecture 6. In functions and data structures.
CSE 501N Fall ‘09 12: Recursion and Recursive Algorithms 8 October 2009 Nick Leidenfrost.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
Recursion, pt. 1 The Foundations. What is Recursion? Recursion is the idea of solving a problem in terms of itself. – For some problems, it may not possible.
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 Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
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.
W1-1 University of Washington Computer Programming I Recursion © 2000 UW CSE.
Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout
Concepts of Algorithms CSC-244 Unit 5 and 6 Recursion Shahid Iqbal Lone Computer College Qassim University K.S.A.
Welcome to Recursion! Say what?!? Recursion is… the process of solving large problems by simplifying them into smaller ones. similar to processing using.
Ms N Nashandi Dr SH Nggada Week 08 – Recursion. Outline We shall be covering the following What is a recursion? Iteration versus Recursion. Recursion.
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
Chapter Topics Chapter 16 discusses the following main topics:
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.
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
RECURSION.
Recursion DRILL: Please take out your notes on Recursion
Decrease-and-Conquer Approach
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Applied Algorithms (Lecture 17) Recursion Fall-23
Algorithm Analysis (for Divide-and-Conquer problems)
Recursion (part 1) October 24, 2007 ComS 207: Programming I (in Java)
Unit 3 Test: Friday.
Applied Discrete Mathematics Week 9: Integer Properties
Module 1-10: Recursion.
Computer Science
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:
Self-Referencing Functions
Recursion.
Recursive Thinking.
Recursion (part 1) October 25, 2006 ComS 207: Programming I (in Java)
Recursion.
Presentation transcript:

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 to solve certain problems that would be difficult to solve in other ways.

Recursion To illustrate recursion, let's consider the mathematical concept of factorial (the expression n! is read "n factorial"). Here are two ways to define the factorial of a non-negative integer n:

Recursion Note that 0! is defined to be 1. Note also that in the recursive definition, factorial is defined in terms of itself!

Recursion Using the iterative definition, we would calculate 5! as

Recursion Using the iterative definition, we would calculate 5! as 5! = 1 * 2 * 3 * 4 * 5 = 120

Recursion Using the iterative definition, we would calculate 5! as 5! = 1 * 2 * 3 * 4 * 5 = 120 Using the recursive definition, 5! is calculated as 5! = 5 * 4! = 5 * 4 * 3! = 5 * 4 * 3 * 2! = 5 * 4 * 3 * 2 * 1! = 5 * 4 * 3 * 2 * 1 * 0! = 5 * 4 * 3 * 2 * 1 * 1 = 120

Recursion Here is a C++ function that implements the iterative definition of factorial. Note that it uses a loop to do the iteration: int Factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; }

Recursion Here is a C++ function that implements the recursive definition of factorial. Note that it contains no loop: int RecursiveFactorial(int n) { if (n == 0) { return 1; }else { return n * RecursiveFactorial(n - 1); }

Recursion RecursiveFactorial illustrates the structure that is common to all recursive functions:

Recursion RecursiveFactorial illustrates the structure that is common to all recursive functions: –First, a stopping condition is checked.

Recursion RecursiveFactorial illustrates the structure that is common to all recursive functions: –First, a stopping condition is checked. –This is called the base case.

Recursion RecursiveFactorial illustrates the structure that is common to all recursive functions: –First, a stopping condition is checked. –This is called the base case. In the case of RecursiveFactorial, the base case is n == 0.

Recursion RecursiveFactorial illustrates the structure that is common to all recursive functions: –First, a stopping condition is checked. –This is called the base case. In the case of RecursiveFactorial, the base case is n == 0. –If the base case test evaluates to true, recursion ends with the return of the value 1.

Recursion RecursiveFactorial illustrates the structure that is common to all recursive functions: –First, a stopping condition is checked. –This is called the base case. In the case of RecursiveFactorial, the base case is n == 0. –If the base case test evaluates to true, recursion ends with the return of the value 1. –If the base case condition is false, an expression involving a recursive call is returned.

Recursion RecursiveFactorial illustrates the structure that is common to all recursive functions: –First, a stopping condition is checked. –This is called the base case. In the case of RecursiveFactorial, the base case is n == 0. –If the base case test evaluates to true, recursion ends with the return of the value 1. –If the base case condition is false, an expression involving a recursive call is returned. In the case of RecursiveFactorial, the returned expression is n * RecursiveFactorial(n - 1).

The figure below illustrates how RecursiveFactorial calculates the value of 3!:

Recursion By the way, recursion is not the technique to use for calculating the factorial of n. –The iterative factorial function is much more efficient and should be used instead. –This example was used because its recursive calculation is easy to implement and understand. As you continue your study of computer science you will see some very elegant recursive solutions to problems.

As a second example of a recursive function, let’s look at the following recursive definition for a n :

This definition is implemented as the Power function, which returns an integral power of a number: double Power(double base, int expo){ if (0 == expo) { return 1.0; // x 0 = 1 } else { double semi = Power(base,expo/2); if (expo % 2 == 0) { // even exponent return semi * semi; } else { // odd exponent return base*semi*semi; }

Recursion Note again that, like all "good" recursive functions, Power begins by testing a stopping condition.

Recursion Note again that, like all "good" recursive functions, Power begins by testing a stopping condition. –Only if the stopping condition is false does Power call itself recursively.

Here are general rules for writing recursive functions: 1. Identify a base case that does not make any recursive calls. – Each call should make progress toward reaching the base case; – this ensures termination since the function will end.

Here are general rules for writing recursive functions: 1. Identify a base case that does not make any recursive calls. – Each call should make progress toward reaching the base case; – this ensures termination since the function will end. 2. Solve the problem by making recursive calls that are similar but simpler (i.e., that move toward the base case). –The similarity ensures that the recursion works: –you'll be solving a similar problem.

Here are general rules for writing recursive functions: 1. Identify a base case that does not make any recursive calls. – Each call should make progress toward reaching the base case; – this ensures termination since the function will end. 2. Solve the problem by making recursive calls that are similar but simpler (i.e., that move toward the base case). –The similarity ensures that the recursion works: –you'll be solving a similar problem. Note that RecursiveFactorial and Power satisfy both rules.

Okay, let's see how well you understand recursion: Determine the values returned by each of the following function calls by hand-executing the functions: int Problem1(int n) { if (n <= 1) return 0; else return 1 + Problem1(n - 2); }

Okay, let's see how well you understand recursion: int Problem1(int n) { if (n <= 1) return 0; else return 1 + Problem1(n - 2); } What value is returned from the calls a. Problem1(8) b. Problem1(13) c. Problem1(x), where x is a nonnegative integer

Problem1(8) int Problem1(int n) { if (n <= 1) return 0; else return 1 + Problem1(n-2); } Problem1(8) = 1 + Problem1(6) = Problem1(4) = Problem1(2) = Problem1(0) = = 4

Okay, let's see how well you understand recursion: int Problem1(int n) { if (n <= 1) return 0; else return 1 + Problem1(n - 2); } What value is returned from the calls a. Problem1(8) = 4 b. Problem1(13) c. Problem1(x), where x is a nonnegative integer

Problem1(13) int Problem1(int n) { if (n <= 1) return 0; else return 1 + Problem1(n-2); } Problem1(13) = 1 + Problem1(11) = Problem1(9) = Problem1(7) = Problem1(5) = Problem1(3) = Problem1(1) = = 6

Okay, let's see how well you understand recursion: int Problem1(int n) { if (n <= 1) return 0; else return 1 + Problem1(n - 2); } What value is returned from the calls a. Problem1(8) = 4 b. Problem1(13) = 6 c. Problem1(x), where x is a nonnegative integer

Okay, let's see how well you understand recursion: int Problem1(int n) { if (n <= 1) return 0; else return 1 + Problem1(n - 2); } What value is returned from the calls a. Problem1(8) = 4 b. Problem1(13) = 6 c. Problem1(x), where x is a nonnegative integer = x/2

Greatest Common Divisor int Problem2(int m, int n) { if (m == n) return m; else if (m < n) return Problem2(n - m, m); else return Problem2(m - n, n) }

Greatest Common Divisor int Problem2(int m, int n) { if (m == n) return m; else if (m < n) return Problem2(n - m, m); else return Problem2(m - n, n) } What value is returned from the calls a. Problem2(12, 8) = Problem2(4, 8) = Problem2(4, 4) = 4

Greatest Common Divisor int Problem2(int m, int n) { if (m == n) return m; else if (m < n) return Problem2(n - m, m); else return Problem2(m - n, n) } What value is returned from the calls b. Problem2(18, 24) = Problem2(6, 18) = Problem2(6, 12) = Problem2(6, 6) = 6

Greatest Common Divisor int Problem2(int m, int n) { if (m == n) return m; else if (m < n) return Problem2(n - m, m); else return Problem2(m - n, n) } What value is returned from the calls c. Problem2(x, y), where x and y are positive integers, returns the greatest common divisor of x and y