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

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

Solving Recursive Sequences by Iteration

The Critical-Path Algorithm

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

The Traveling Salesman Problem Brute Force Method

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

Binary Tree Applications

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

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

Street-Routing Problems

Displaying Distributions - Quantitative Variables

The Traveling Salesman Problem Nearest-Neighbor Algorithm

Scheduling and Digraphs

The Graphics Pipeline

The Pairwise-Comparison Method

LR Parsing - Conflicts

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

Implementing Linked Lists

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

Recursive Linked Lists

Nondeterministic Programming in C++

Building the Abstract Syntax Trees

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

Density Curves Sections

The Graphics Pipeline

Friends and Unary Operators

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

Function Definition Syntax Tree

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

The CYK Parsing Algorithm

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

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

Scope and Parameter Passing

The Coefficient of Determination

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

Magnification and Minification

Scope and Parameter Passing

The Traveling Salesman Problem Cheapest-Link Algorithm

Recognition of Tokens

The Normal Distribution

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

LR Parsing - The Items

Programming Languages

Programming Languages

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

PHP Queries and HTML Forms Lecture 23

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

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

Inheritance: The Fundamental Functions

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

Stacks and their Applications

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

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

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

Induction and Recursion. CMPS/MATH 2170: Discrete Mathematics

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

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

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

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

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

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

Introduction to Databases

Functional Dependencies and Normal Forms

Functional Dependencies and Normal Forms

Basic PHP Lecture 17

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

MySQL Creating a Database Lecture 3

Basic CSS Lecture 17

XQuery FLOWR Expressions Lecture 35

Binary Tree Implementation

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

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

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

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

JFlex Regular Expressions

Lecture 29 Section Wed, Apr 1, 2009

The Mesh Class. Lecture 23. Robb T. Koether. Hampden-Sydney College. Wed, Oct 18, 2017

Transcription:

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

1 Recursive Sequences The Fibonacci Sequence Choosing Subsets The Catalan Numbers The Towers of Hanoi 2 Assignment Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 2 / 26

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

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 26, 2014 4 / 26

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

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 26, 2014 6 / 26

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

Choosing Subsets In how many ways can 2 elements be selected from a set of n elements? Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 8 / 26

Choosing Subsets In how many ways can 2 elements be selected from a set of n elements? Let a n be the number of such ways, for n 2. Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 8 / 26

Choosing Subsets In how many ways can 2 elements be selected from a set of n elements? Let a n be the number of such ways, for n 2. Clearly, a 2 = 1. Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 8 / 26

Choosing Subsets In how many ways can 2 elements be selected from a set of n elements? Let a n be the number of such ways, for n 2. Clearly, a 2 = 1. It is easy to see that a 3 = 3. Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 8 / 26

Choosing Subsets n elements Consider a set of n elements Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 9 / 26

Choosing Subsets n - 1 elements Remove one element Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 9 / 26

Choosing Subsets n - 1 elements There are a n 1 ways to choose 2 elements not using the removed element Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 9 / 26

Choosing Subsets n - 1 elements There are n 1 ways to choose 2 elements using the removed element Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 9 / 26

Choosing Subsets Thus, a n = a n 1 + (n 1). Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 10 / 26

Choosing Subsets Thus, a n = a n 1 + (n 1). It follows that a n = 1 + 2 + 3 + + (n 1) = (n 1)n. 2 Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 10 / 26

Choosing Subsets In how many ways can 3 elements be selected from a set of n elements? Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 11 / 26

Choosing Subsets In how many ways can 3 elements be selected from a set of n elements? Let b n be the number of such ways, for n 3. Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 11 / 26

Choosing Subsets In how many ways can 3 elements be selected from a set of n elements? Let b n be the number of such ways, for n 3. Clearly, b 3 = 1. Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 11 / 26

Choosing Subsets In how many ways can 3 elements be selected from a set of n elements? Let b n be the number of such ways, for n 3. Clearly, b 3 = 1. How is b n related to b n 1? Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 11 / 26

Outline 1 Recursive Sequences The Fibonacci Sequence Choosing Subsets The Catalan Numbers The Towers of Hanoi 2 Assignment Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 12 / 26

Binary Trees How many distinct binary trees are there with exactly n vertices? Let a n be the number of distinct binary trees with exactly n vertices. Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 13 / 26

Binary Trees Binary trees with 1 vertex a 1 = 1 Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 14 / 26

Binary Trees Binary trees with 2 vertices a 2 = 2 Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 14 / 26

Binary Trees Binary trees with 3 vertices a 3 = 5 Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 14 / 26

Binary Trees Binary trees with 4 vertices a 4 = 14 Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 14 / 26

The Catalan Numbers The Catalan numbers can be defined recursively as C 0 = 1 and C n = C 0 C n 1 + C 1 C n 2 + + C n 1 C 0, for all n 1. The first few terms are 1, 1, 2, 5, 14, 42, 132. Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 15 / 26

The Catalan Numbers ( ) They can also be defined recursively C n = 2 2n 1 n+1 C n 1 for all n 1. They can also be defined nonrecursively as C n = 1 ( ) 2n. n + 1 n Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 16 / 26

Routes Through a Graph 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 26, 2014 17 / 26

Routes Through a Graph B A Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 17 / 26

Routes Through a Graph B A Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 17 / 26

Routes Through a Graph B A Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 17 / 26

Routes Through a Graph B A Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 17 / 26

Routes Through a Graph B A Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 17 / 26

Routes Through a Graph B A Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 17 / 26

Routes Through a Graph B A Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 17 / 26

Routes Through a Graph B A Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 17 / 26

Routes Through a Graph B A Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 17 / 26

Routes Through a Graph B A Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 17 / 26

Routes Through a Graph B A Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 17 / 26

Routes Through a Graph B A Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 17 / 26

Routes Through a Graph B A Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 17 / 26

Routes Through a Graph B A Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 17 / 26

Outline 1 Recursive Sequences The Fibonacci Sequence Choosing Subsets The Catalan Numbers The Towers of Hanoi 2 Assignment Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 18 / 26

The Towers of Hanoi 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 26, 2014 19 / 26

The Towers of Hanoi Peg 1 Peg 2 Peg 3 All 8 disks on Peg 1 Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 20 / 26

The Towers of Hanoi Peg 1 Peg 2 Peg 3 Reassemble 7 disks on Peg 2 Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 20 / 26

The Towers of Hanoi Peg 1 Peg 2 Peg 3 Move largest disk to Peg 3 Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 20 / 26

The Towers of Hanoi Peg 1 Peg 2 Peg 3 Reassemble 7 disks on Peg 3 Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 20 / 26

The Towers of Hanoi 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 26, 2014 21 / 26

The Towers of Hanoi 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 26, 2014 22 / 26

The Towers of Hanoi Peg 1 Peg 2 Peg 3 Reassemble 6 disks on Peg 3 Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 22 / 26

The Towers of Hanoi Peg 1 Peg 2 Peg 3 Move Disk 7 to Peg 2 Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 22 / 26

The Towers of Hanoi Peg 1 Peg 2 Peg 3 Reassemble 6 disks on Peg 2 Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 22 / 26

The Towers of Hanoi Peg 1 Peg 2 Peg 3 Move Disk 8 to Peg 3 Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 22 / 26

The Towers of Hanoi Peg 1 Peg 2 Peg 3 Reassemble 6 disks on Peg 1 Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 22 / 26

The Towers of Hanoi Peg 1 Peg 2 Peg 3 Move Disk 7 to Peg 3 Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 22 / 26

The Towers of Hanoi Peg 1 Peg 2 Peg 3 Reassemble 6 disks on Peg 3 Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 22 / 26

The Towers of Hanoi 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 26, 2014 23 / 26

The Towers of Hanoi We can show by induction that m n = 2 n 1. Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 24 / 26

Outline 1 Recursive Sequences The Fibonacci Sequence Choosing Subsets The Catalan Numbers The Towers of Hanoi 2 Assignment Robb T. Koether (Hampden-Sydney College) Recursive Sequences Wed, Feb 26, 2014 25 / 26

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 26, 2014 26 / 26