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

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

The Critical-Path Algorithm

Solving Recursive Sequences by Iteration

Minimal Spanning Trees

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

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

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

Rotations and Translations

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

The Decreasing-Time Algorithm

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

Recursive Descent Parsers

Sampling Distribution Examples Sections 15.4, 15.5

Displaying Distributions - Quantitative Variables

Binary Tree Applications

The Traveling Salesman Problem Brute Force Method

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

Recursive Linked Lists

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

Street-Routing Problems

The Traveling Salesman Problem Nearest-Neighbor Algorithm

Scheduling and Digraphs

The Pairwise-Comparison Method

The Graphics Pipeline

LR Parsing - Conflicts

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

Implementing Linked Lists

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

The Coefficient of Determination

Building the Abstract Syntax Trees

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

Magnification and Minification

Density Curves Sections

The Graphics Pipeline

Nondeterministic Programming in C++

Friends and Unary Operators

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

Function Definition Syntax Tree

The Normal Distribution

The CYK Parsing Algorithm

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

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

LR Parsing - The Items

Scope and Parameter Passing

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

The Plurality-with-Elimination Method

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

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

The Class Construct Part 1

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

Inheritance: The Fundamental Functions

Scope and Parameter Passing

The Traveling Salesman Problem Cheapest-Link Algorithm

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

The x86 Instruction Set

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

Fundamental Data Types

Introduction to Compiler Design

Programming Languages

Programming Languages

Shader Programs. Lecture 30 Subsections 2.8.2, Robb T. Koether. Hampden-Sydney College. Wed, Nov 16, 2011

The Projection Matrix

Mipmaps. Lecture 35. Robb T. Koether. Hampden-Sydney College. Wed, Nov 18, 2015

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

Triggers. Lecture 14. Robb T. Koether. Hampden-Sydney College. Wed, Feb 14, 2018

Specular Reflection. Lecture 19. Robb T. Koether. Hampden-Sydney College. Wed, Oct 4, 2017

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

PHP Queries and HTML Forms Lecture 23

Recognition of Tokens

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

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

Lecture 29 Section Wed, Apr 1, 2009

Stacks and their Applications

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

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

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

List Iterator Implementation

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

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

The x86 Architecture

Form Validation. Lecture 25. Robb T. Koether. Hampden-Sydney College. Wed, Mar 23, 2018

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

Recursion. Chapter 7. Copyright 2012 by Pearson Education, Inc. All rights reserved

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

Introduction to Databases

Basic PHP Lecture 17

The Model Stack. Lecture 8. Robb T. Koether. Hampden-Sydney College. Wed, Sep 6, 2017

Basic CSS Lecture 17

XQuery FLOWR Expressions Lecture 35

Binary Tree Implementation

Binary Tree Implementation

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

The Mesh Class. Lecture 26. Robb T. Koether. Wed, Oct 28, Hampden-Sydney College

Abstract Syntax Trees Synthetic and Inherited Attributes

CUP. Lecture 18 CUP User s Manual (online) Robb T. Koether. Hampden-Sydney College. Fri, Feb 27, 2015

Ambient and Diffuse Light

Dynamic Allocation of Memory

Inheritance: The Fundamental Functions

JFlex Regular Expressions

Induction and Recursion. CMPS/MATH 2170: Discrete Mathematics

Transcription:

Recursive Sequences Lecture 24 Section 5.6 Robb T. Koether Hampden-Sydney College Wed, Feb 27, 2013 Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 1 / 21

1 Recursive Sequences The Fibonacci Sequence The Catalan Numbers The Towers of Hanoi 2 Assignment Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 2 / 21

Outline 1 Recursive Sequences The Fibonacci Sequence The Catalan Numbers The Towers of Hanoi 2 Assignment Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 3 / 21

Recursive Sequences Definition (Recursive Sequence) A sequence a 0, a 1, a 2,... is recursive if, for all i k for some integer k > 0, each term a i is defined in terms of certain terms a j with j < i. The initial conditions specify the values of a 0, a 1,..., a k 1. In most examples, we specify the first one or two terms and then define all subsequent terms in terms of the previous one or two terms. Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 4 / 21

Outline 1 Recursive Sequences The Fibonacci Sequence The Catalan Numbers The Towers of Hanoi 2 Assignment Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 5 / 21

The Fibonacci Sequence The Fibonacci sequence is a well known example. Define F 0 = 0 and F 1 = 1. Define recursively F n = F n 1 + F n 2 for all n 2. The first few terms are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34. Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 6 / 21

Outline 1 Recursive Sequences The Fibonacci Sequence The Catalan Numbers The Towers of Hanoi 2 Assignment Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 7 / 21

The Catalan Numbers Another example is the Catalan numbers. Define C 0 = 1. Define recursively C n = 2 ( 2n 1 n+1 The first few terms are ) C n 1 for all n 1. 1, 1, 2, 5, 14, 42, 132. Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 8 / 21

The Catalan Numbers The Catalan numbers can also be defined nonrecursively as C n = 1 ( ) 2n. n + 1 n They can also be defined recursively as C 0 = 1 and for all n 1. C n = C 0 C n 1 + C 1 C n 2 + + C n 1 C 0, Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 9 / 21

The Catalan Numbers How many distinct binary trees are there with exactly n vertices. Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 10 / 21

Binary trees with 1 vertex Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 11 / 21

Binary trees with 2 vertices Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 11 / 21

Binary trees with 3 vertices Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 11 / 21

Binary trees with 4 vertices Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 11 / 21

B A How many paths are there from A to B, moving north and east? Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 12 / 21

B A Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 12 / 21

B A Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 12 / 21

B A Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 12 / 21

B A Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 12 / 21

B A Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 12 / 21

B A Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 12 / 21

B A Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 12 / 21

B A Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 12 / 21

B A Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 12 / 21

B A Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 12 / 21

B A Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 12 / 21

B A Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 12 / 21

B A Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 12 / 21

B A Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 12 / 21

Outline 1 Recursive Sequences The Fibonacci Sequence The Catalan Numbers The Towers of Hanoi 2 Assignment Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 13 / 21

The Towers of Hanoi puzzle has three pegs (Peg 1, Peg 2, Peg 3) with n disks stacked on Peg 1. A legal move is to move a single disk to another peg without placing a larger disk on a smaller disk. The goal is to assemble all n disks on Peg 3. How many moves will it take? Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 14 / 21

Peg 1 Peg 2 Peg 3 All 8 disks on Peg 1 Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 15 / 21

Peg 1 Peg 2 Peg 3 Reassemble 7 disks on Peg 2 Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 15 / 21

Peg 1 Peg 2 Peg 3 Move largest disk to Peg 3 Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 15 / 21

Peg 1 Peg 2 Peg 3 Reassemble 7 disks on Peg 3 Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 15 / 21

We see that we can solve the puzzle for 8 disks if we can solve it for 7 disks. So we can solve it for 7 disks if we can solve it for 6 disks. And so on, down to 2 disks: we can solve it for 2 disks if we can solve it for 1 disk. How do we solve it for 1 disk? Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 16 / 21

Peg 1 Peg 2 Peg 3 The solution for 8 disks using the solution for 6 disks Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 17 / 21

Peg 1 Peg 2 Peg 3 Reassemble 6 disks on Peg 3 Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 17 / 21

Peg 1 Peg 2 Peg 3 Move Disk 7 to Peg 2 Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 17 / 21

Peg 1 Peg 2 Peg 3 Reassemble 6 disks on Peg 2 Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 17 / 21

Peg 1 Peg 2 Peg 3 Move Disk 8 to Peg 3 Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 17 / 21

Peg 1 Peg 2 Peg 3 Reassemble 6 disks on Peg 1 Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 17 / 21

Peg 1 Peg 2 Peg 3 Move Disk 7 to Peg 3 Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 17 / 21

Peg 1 Peg 2 Peg 3 Reassemble 6 disks on Peg 3 Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 17 / 21

How many moves are required to solve the puzzle for n disks? Let m n be the number of moves required when there are n disks. When n = 1: m 1 = 1. When n = 2: m 1 + 1 + m 1 = 3. When n = 3: m 2 + 1 + m 2 = 7. In general, m n = 2m n 1 + 1. Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 18 / 21

We can show by induction that m n = 2 n 1. Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 19 / 21

Outline 1 Recursive Sequences The Fibonacci Sequence The Catalan Numbers The Towers of Hanoi 2 Assignment Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 20 / 21

Assignment Assignment Read Section 5.6, pages 290-301. Exercises 2, 5, 11, 14, 22, 23, 27, 28, 33, page 302. Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 21 / 21

Collected Homework 6 Collected Homework 6 Exercises 28, 44, page 242. Exercises 7, 14, page 256. Exercises 18, 34, page 266. Exercises 6, 8, page 277. Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 27, 2013 22 / 21