RECURSION.

Slides:



Advertisements
Similar presentations
7 7 Quiz Function call stack Passing values to functions Recursion.
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.12Recursion 3.13Example Using Recursion: The Fibonacci Series 3.14Recursion.
8 8 Function call stack Passing values to functions Recursion.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 – Recursive Funtions From Deitel’s “C” Book 5.13Recursion 5.14Example Using Recursion: The Fibonacci.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Recursion Review.
Programming in C++ Language ( ) Lecture 6: Functions-Part2 Dr. Lubna Badri.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
計算機程式 第五單元 Function II 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 本課程指定教材為 C++ How to Program, 7/e,
 Pearson Education, Inc. All rights reserved Recursion.
 2008 Pearson Education, Inc. All rights reserved Function Call Stack and Activation Records Data structure: collection of related data items.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Recursion.  A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for n>1 n!=1 for n=1 int factorial (int n) { if (n == 0)
 2006 Pearson Education, Inc. All rights reserved Functions and an Introduction to Recursion.
Lecture6 Recursion function © by Pearson Education, Inc. All Rights Reserved. 1.
Functions and an Introduction to Recursion.  Recursive function ◦ A function that calls itself, either directly, or indirectly (through another function)
Lecture 12 Recursion part 1 Richard Gesick. Recursion A recursive method is a method that calls itself. A recursive method is capable of solving only.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Recursion) Outline 5.13Recursion 5.14Example.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Lecture-13 Instructor Name: Muhammad Safyan Programming Fundamental.
Principles of Programming Chapter 11: Recursive Function  In this chapter, you will learn about  Recursion function 1 NI S1 2009/10.
 2007 Pearson Education, Inc. All rights reserved C Functions -Continue…-
 2005 Pearson Education, Inc. All rights reserved Recursion.
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
1 Recursion Recursive method –Calls itself (directly or indirectly) through another method –Method knows how to solve only a base case –Method divides.
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.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
 Prentice Hall. All rights reserved. 1 Recursion (Section 7.13 & Exercise7.40 from ed.3) (Sections 6.15, 6.16 from ed.1) Many slides modified.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
A Brief Introduction to Recursion. Recursion Recursive methods … –methods that call themselves! –They can only solve a base case –So, you divide a problem.
C++ Programming Lecture 12 Functions – Part IV
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
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.
Java How to Program, 10/e Late Objects Version © Copyright by Pearson Education, Inc. All Rights Reserved.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
 2006 Pearson Education, Inc. All rights reserved Functions and an Introduction to 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.
5 C Functions.
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSC113: Computer Programming (Theory = 03, Lab = 01)
15 Recursion.
Functions.
6.11 Function Call Stack and Activation Records
Chapter 5 - Functions Outline 5.1 Introduction
MSIS 655 Advanced Business Applications Programming
Recursion Chapter 11.
Lecture 17 Recursion part 1 Richard Gesick.
JavaScript: Functions Part II
Functions and an Introduction to Recursion
Functions Recursion CSCI 230
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 17: Recursion.
Recursion Taken from notes by Dr. Neil Moore
Lecture 12 Recursion part 1 CSE /26/2018.
Fundaments of Game Design
Recursive Algorithms 1 Building a Ruler: drawRuler()
Programming Fundamentals Lecture #7 Functions
Recursion.
Programming Fundamental
Presentation transcript:

RECURSION

Recursion Recursive function Recursion A function that calls itself, either directly, or indirectly (through another function) Recursion Base case(s) The simplest case(s), which the function knows how to handle For all other cases, the function typically divides the problem into two conceptual pieces A piece that the function knows how to do A piece that it does not know how to do Slightly simpler or smaller version of the original problem

Recursion Recursion (Cont.) Recursive call (also called the recursion step) The function launches (calls) a fresh copy of itself to work on the smaller problem Can result in many more recursive calls, as the function keeps dividing each new problem into two conceptual pieces This sequence of smaller and smaller problems must eventually converge on the base case Otherwise the recursion will continue forever

Recursion Factorial The factorial of a nonnegative integer n, written n! (and pronounced “n factorial”), is the product n · (n – 1) · (n – 2) · … · 1 Recursive definition of the factorial function n! = n · (n – 1)! Example 5! = 5 · 4 · 3 · 2 · 1 5! = 5 · ( 4 · 3 · 2 · 1) 5! = 5 · ( 4! )

Fig. 6.28 | Recursive evaluation of 5!.

First call to factorial function Outline fig06_29.cpp (1 of 2) First call to factorial function

Outline Base cases simply return 1 (2 of 2) fig06_29.cpp (2 of 2) Recursive call to factorial function with a slightly smaller problem

Common Programming Error 6.24 Either omitting the base case, or writing the recursion step incorrectly so that it does not converge on the base case, causes “infinite” recursion, eventually exhausting memory. This is analogous to the problem of an infinite loop in an iterative (nonrecursive) solution.

Example Using Recursion: Fibonacci Series The Fibonacci series 0, 1, 1, 2, 3, 5, 8, 13, 21, … Begins with 0 and 1 Each subsequent Fibonacci number is the sum of the previous two Fibonacci numbers can be defined recursively as follows: fibonacci(0) = 0 fibonacci(1) = 1 fibonacci(n) = fibonacci(n – 1) + fibonacci(n – 2)

Outline fig06_30.cpp (1 of 2)

Recursive calls to fibonacci function Outline Base cases fig06_30.cpp (2 of 2) Recursive calls to fibonacci function

Fig. 6.31 | Set of recursive calls to function fibonacci.

Example Using Recursion: Fibonacci Series Caution about recursive programs Each level of recursion in function fibonacci has a doubling effect on the number of function calls i.e., the number of recursive calls that are required to calculate the nth Fibonacci number is on the order of 2n 20th Fibonacci number would require on the order of 220 or about a million calls 30th Fibonacci number would require on the order of 230 or about a billion calls. Exponential complexity Can humble even the world’s most powerful computers

Performance Tip 6.8 Avoid Fibonacci-style recursive programs that result in an exponential “explosion” of calls.

Recursion vs. Iteration Both are based on a control statement Iteration – repetition structure Recursion – selection structure Both involve repetition Iteration – explicitly uses repetition structure Recursion – repeated function calls Both involve a termination test Iteration – loop-termination test Recursion – base case

Recursion vs. Iteration Both gradually approach termination Iteration modifies counter until loop-termination test fails Recursion produces progressively simpler versions of problem Both can occur infinitely Iteration – if loop-continuation condition never fails Recursion – if recursion step does not simplify the problem

Outline fig06_32.cpp (1 of 2)

Iterative approach to finding a factorial Outline Iterative approach to finding a factorial fig06_32.cpp (2 of 2)

Recursion vs. Iteration Negatives of recursion Overhead of repeated function calls Can be expensive in both processor time and memory space Each recursive call causes another copy of the function (actually only the function’s variables) to be created Can consume considerable memory Iteration Normally occurs within a function Overhead of repeated function calls and extra memory assignment is omitted

Software Engineering Observation 6.18 Any problem that can be solved recursively can also be solved iteratively (nonrecursively). A recursive approach is normally chosen in preference to an iterative approach when the recursive approach more naturally mirrors the problem and results in a program that is easier to understand and debug.

Software Engineering Observation Another reason to choose a recursive solution is that an iterative solution is not apparent. Avoid using recursion in performance situations. Recursive calls take time and consume additional memory.

// This method returns the sum of 1 to n public int sum (int n) { int result; if (n == 1) result = 1; else result = n + sum (n-1); return result; }

start temp end