Recursion. Lecture 26 Sections , Robb T. Koether. Hampden-Sydney College. Mon, Apr 6, 2015

Similar documents
Recursion. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 17, 2018

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

The Pairwise-Comparison Method

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

The Critical-Path Algorithm

Minimal Spanning Trees

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Scope and Parameter Passing

Solving Recursive Sequences by Iteration

Recursive Descent Parsers

Recursive Linked Lists

Scheduling and Digraphs

The Decreasing-Time Algorithm

Total Orders. Lecture 41 Section 8.5. Robb T. Koether. Hampden-Sydney College. Mon, Apr 8, 2013

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

Scope and Parameter Passing

Binary Tree Applications

Binary Tree Implementation

Friends and Unary Operators

LR Parsing - Conflicts

The Traveling Salesman Problem Nearest-Neighbor Algorithm

Programming Languages

Webpage Navigation. Lecture 27. Robb T. Koether. Hampden-Sydney College. Mon, Apr 2, 2018

Recognition of Tokens

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

List Iterator Implementation

Magnification and Minification

The Traveling Salesman Problem Brute Force Method

DTDs and XML Attributes

Stacks and their Applications

Binary Tree Implementation

Density Curves Sections

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

Sampling Distribution Examples Sections 15.4, 15.5

Nondeterministic Programming in C++

Rotations and Translations

XQuery FLOWR Expressions Lecture 35

Integer Overflow. Lecture 8 Section 2.5. Robb T. Koether. Hampden-Sydney College. Mon, Jan 27, 2014

The Coefficient of Determination

The x86 Instruction Set

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

The Plurality-with-Elimination Method

Fundamental Data Types

Implementing Linked Lists

Programming Languages

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Mon, Feb 22, 2016

The Class Construct Part 1

Shading Triangles. Lecture 37. Robb T. Koether. Hampden-Sydney College. Mon, Nov 30, 2015

The Constructors. Lecture 7 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 1, 2017

The Graphics Pipeline

Function Usage. Lecture 15 Sections 6.3, 6.4. Robb T. Koether. Hampden-Sydney College. Mon, Oct 1, 2018

Function Definition Syntax Tree

The Graphics Pipeline

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

XML Attributes. Lecture 33. Robb T. Koether. Hampden-Sydney College. Wed, Apr 25, 2018

The Class Construct Part 2

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Fri, Feb 16, 2018

Street-Routing Problems

Boxplots. Lecture 17 Section Robb T. Koether. Hampden-Sydney College. Wed, Feb 10, 2010

XPath. Lecture 36. Robb T. Koether. Wed, Apr 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, / 28

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

Regular Expressions. Lecture 10 Sections Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2016

Building the Abstract Syntax Trees

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

Ambient and Diffuse Light

The Constructors. Lecture 6 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 26, 2018

Introduction to Compiler Design

Basic PHP. Lecture 19. Robb T. Koether. Hampden-Sydney College. Mon, Feb 26, 2108

Inheritance: The Fundamental Functions

Lecture 29 Section Wed, Apr 1, 2009

List Iterators. Lecture 27 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

Basic PHP Lecture 17

Dynamic Allocation of Memory

jquery Lecture 34 Robb T. Koether Wed, Apr 10, 2013 Hampden-Sydney College Robb T. Koether (Hampden-Sydney College) jquery Wed, Apr 10, / 29

PHP Queries and HTML Forms Lecture 23

The CYK Parsing Algorithm

LR Parsing - The Items

CS 310 Advanced Data Structures and Algorithms

List Iterators. Lecture 34 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 24, 2013

Functional Dependencies and Normal Forms

Functional Dependencies and Normal Forms

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

The Normal Distribution

Abstract Syntax Trees Synthetic and Inherited Attributes

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

Standard Version of Starting Out with C++, 4th Edition. Chapter 19 Recursion. Copyright 2003 Scott/Jones Publishing

Displaying Distributions - Quantitative Variables

Aggregation. Lecture 7 Section Robb T. Koether. Hampden-Sydney College. Wed, Jan 29, 2014

The x86 Architecture

Recursive Functions. Biostatistics 615 Lecture 5

Inheritance: The Fundamental Functions

PHP Arrays. Lecture 18. Robb T. Koether. Hampden-Sydney College. Mon, Mar 4, 2013

Introduction to Databases

XSLT. Lecture 38. Robb T. Koether. Mon, Apr 21, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XSLT Mon, Apr 21, / 26

11/2/2017 RECURSION. Chapter 5. Recursive Thinking. Section 5.1

Algorithm. An algorithm is a computational process for solving a problem. computational: a computer must be able to perform the steps of the process

Abstract Data Types. Lecture 23 Section 7.1. Robb T. Koether. Hampden-Sydney College. Wed, Oct 24, 2012

Dynamic Allocation of Memory

Transcription:

Recursion Lecture 26 Sections 14.1-14.5, 14.7 Robb T. Koether Hampden-Sydney College Mon, Apr 6, 2015 Robb T. Koether (Hampden-Sydney College) Recursion Mon, Apr 6, 2015 1 / 18

1 Recursion 2 Advantages and Disadvantages 3 Examples 4 Assignment Robb T. Koether (Hampden-Sydney College) Recursion Mon, Apr 6, 2015 2 / 18

Outline 1 Recursion 2 Advantages and Disadvantages 3 Examples 4 Assignment Robb T. Koether (Hampden-Sydney College) Recursion Mon, Apr 6, 2015 3 / 18

Recursion Definition (Recursive Function) A recursive function is a function that calls on itself. A recursive function must contain a decision structure that divides the code into two cases. The recursive (general) case. The nonrecursive (base) case. The code must guarantee that eventually the nonrecursive case is called. Robb T. Koether (Hampden-Sydney College) Recursion Mon, Apr 6, 2015 4 / 18

1st-Order Recursion Example (The Factorial Function) int factorial(int n) { if (n == 0 n == 1) return 1; else return n*factorial(n - 1); } Robb T. Koether (Hampden-Sydney College) Recursion Mon, Apr 6, 2015 5 / 18

An Analogy Level 1 Level 2 Level 3 Level 4 caption Robb T. Koether (Hampden-Sydney College) Recursion Mon, Apr 6, 2015 6 / 18

An Analogy f(4) Level 1 Level 2 Level 3 Level 4 caption Robb T. Koether (Hampden-Sydney College) Recursion Mon, Apr 6, 2015 6 / 18

An Analogy 4 f(3) Level 1 Level 2 Level 3 Level 4 caption Robb T. Koether (Hampden-Sydney College) Recursion Mon, Apr 6, 2015 6 / 18

An Analogy 4 3 f(2) Level 1 Level 2 Level 3 Level 4 caption Robb T. Koether (Hampden-Sydney College) Recursion Mon, Apr 6, 2015 6 / 18

An Analogy 4 3 2 f(1) Level 1 Level 2 Level 3 Level 4 caption Robb T. Koether (Hampden-Sydney College) Recursion Mon, Apr 6, 2015 6 / 18

An Analogy 4 3 2 1 Level 1 Level 2 Level 3 Level 4 caption Robb T. Koether (Hampden-Sydney College) Recursion Mon, Apr 6, 2015 6 / 18

An Analogy 4 3 2 * 1 = 2 Level 1 Level 2 Level 3 Level 4 caption Robb T. Koether (Hampden-Sydney College) Recursion Mon, Apr 6, 2015 6 / 18

An Analogy 4 3 2 Level 1 Level 2 Level 3 Level 4 caption Robb T. Koether (Hampden-Sydney College) Recursion Mon, Apr 6, 2015 6 / 18

An Analogy 4 3 * 2 = 6 Level 1 Level 2 Level 3 Level 4 caption Robb T. Koether (Hampden-Sydney College) Recursion Mon, Apr 6, 2015 6 / 18

An Analogy 4 6 Level 1 Level 2 Level 3 Level 4 caption Robb T. Koether (Hampden-Sydney College) Recursion Mon, Apr 6, 2015 6 / 18

An Analogy 4 * 6 = 24 Level 1 Level 2 Level 3 Level 4 caption Robb T. Koether (Hampden-Sydney College) Recursion Mon, Apr 6, 2015 6 / 18

An Analogy 24 Level 1 Level 2 Level 3 Level 4 caption Robb T. Koether (Hampden-Sydney College) Recursion Mon, Apr 6, 2015 6 / 18

2nd-Order Recursion Example (The Fibonacci Sequence) int fibon(int n) { if (n == 0 n == 1) return n; else return fibon(n - 1) + fibon(n - 2); } Robb T. Koether (Hampden-Sydney College) Recursion Mon, Apr 6, 2015 7 / 18

Greatest Common Divisor Example (Greatest Common Divisor) int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } Robb T. Koether (Hampden-Sydney College) Recursion Mon, Apr 6, 2015 8 / 18

Binomial Coefficients Example (Binomial Coefficients) int binom(int n, int r) { if (r == 0 r == n) return 1; else return binom(n - 1, r) + binom(n - 1, r - 1); } Robb T. Koether (Hampden-Sydney College) Recursion Mon, Apr 6, 2015 9 / 18

Outline 1 Recursion 2 Advantages and Disadvantages 3 Examples 4 Assignment Robb T. Koether (Hampden-Sydney College) Recursion Mon, Apr 6, 2015 10 / 18

Advantages of Recursion Advantages The code may be much easier to write. Some situations are naturally recursive. Robb T. Koether (Hampden-Sydney College) Recursion Mon, Apr 6, 2015 11 / 18

Advantages of Recursion Naturally recursive data structures: Linked lists. Binary trees. Naturally recursive problems: Traversing linked lists. Traversing binary trees. Evaluating expressions. Differentiating functions. The Triangle puzzle. Robb T. Koether (Hampden-Sydney College) Recursion Mon, Apr 6, 2015 12 / 18

Disadvantages of Recursion Disadvantages Recursive functions are generally slower than nonrecursive functions. Excessive recursion may overflow the run-time stack. One must be very careful when writing recursive functions; they can be tricky. Robb T. Koether (Hampden-Sydney College) Recursion Mon, Apr 6, 2015 13 / 18

Disadvantages of Recursion There is shallow recursion and there is deep recursion. Shallow recursion will not overflow the stack, but it may take an excessively long time to execute. Deep recursion is generally much faster, but it may overflow the stack. Sometimes each function call generates two or more recursive calls at that level. This has the potential to consume an enormous amount of time. Robb T. Koether (Hampden-Sydney College) Recursion Mon, Apr 6, 2015 14 / 18

Outline 1 Recursion 2 Advantages and Disadvantages 3 Examples 4 Assignment Robb T. Koether (Hampden-Sydney College) Recursion Mon, Apr 6, 2015 15 / 18

Examples of Recursive Functions GCD.cpp. BinomialCoefficients.cpp. TowersOfHanoi.cpp. Robb T. Koether (Hampden-Sydney College) Recursion Mon, Apr 6, 2015 16 / 18

Outline 1 Recursion 2 Advantages and Disadvantages 3 Examples 4 Assignment Robb T. Koether (Hampden-Sydney College) Recursion Mon, Apr 6, 2015 17 / 18

Assignment Assignment Read Sections 14.1-14.5, 14.7. Robb T. Koether (Hampden-Sydney College) Recursion Mon, Apr 6, 2015 18 / 18