Recursively Defined Sequences Lecture 40 Section 8.1 Wed, Apr 11, 2007.

Slides:



Advertisements
Similar presentations
Discrete Math Recurrence Relations 1.
Advertisements

Tower of Hanoi Tower of Hanoi is a mathematical puzzle invented by a French Mathematician Edouard Lucas in The game starts by having few discs stacked.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
Week 6 - Wednesday CS322.
The Algorithmic problems?
Chapter Recurrence Relations
Chapter 6 Advanced Counting 6.1 Recurrence Relations.
Recursion Lecture 18: Nov 18.
CS 2210 (22C:19) Discrete Structures Advanced Counting
Chapter 16 Recursion: Another Control Mechanism. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. a.
Kavita Math231 Recursion and Iteration. Kavita Math231 We use Recursion when we have to perform a complex task that can be broken into the several subtasks.
Recursion. 2 CMPS 12B, UC Santa Cruz Solving problems by recursion How can you solve a complex problem? Devise a complex solution Break the complex problem.
Recursion Gordon College CPS212
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Recursion.
1 Section 6.1 Recurrence Relations. 2 Recursive definition of a sequence Specify one or more initial terms Specify rule for obtaining subsequent terms.
Recursion Lecture 17: Nov 11. Quiz int hello(int n) { if (n==0) return 0; else printf(“Hello World %d\n”,n); hello(n-1); } 1.What would the program do.
Chapter 8 With Question/Answer Animations 1. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
Introduction to Programming (in C++) Recursion Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Recursion Apan Qasem Texas State University Spring 2011.
Copyright © Cengage Learning. All rights reserved.
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
Recursively Defined Sequences Lecture 35 Section 8.1 Wed, Mar 23, 2005.
Advanced Counting Techniques
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
Chapter 8. Section 8. 1 Section Summary Introduction Modeling with Recurrence Relations Fibonacci Numbers The Tower of Hanoi Counting Problems Algorithms.
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
Data Structures and Abstractions with Java, 4e Frank Carrano
13.2 Recursive Definitions Objective 1) Provide the recursive definition for sequences; 2) Identify the type of a sequence from a recursive definition.
CSE 2813 Discrete Structures Recurrence Relations Section 6.1.
Module #20 - Recurrences 1 Ch. 7 Recurrence Relations Rosen 6 th ed., §§7.1.
R. Johnsonbaugh Discrete Mathematics 7 th edition, 2009 Chapter 7 Recurrence Relations Instructor Tianping Shuai.
15.1 CompSci 102© Michael Frank Today’s topics Recurrence relationsRecurrence relations –Stating recurrences –LiHoReCoCo Reading: Sections Reading:
Solving Second-Order Recursive Relations Lecture 36 ½ Section 8.3 Wed, Apr 19, 2006.
Mathematical Induction II Lecture 21 Section 4.3 Mon, Feb 27, 2006.
Section 2.4. Section Summary Sequences. Examples: Geometric Progression, Arithmetic Progression Recurrence Relations Example: Fibonacci Sequence Summations.
Module #1 - Logic 1 Based on Rosen, Discrete Mathematics & Its Applications. Prepared by (c) , Michael P. Frank and Modified By Mingwu Chen Recurrence.
Chapter 8 With Question/Answer Animations. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
Data Structures & Algorithms Recursion and Trees Richard Newman.
after UCI ICS/Math 6A, Summer AdvancedCounting -1 Recurrence Relations (RRs) A “Recurrence Relation”
Chapter 6.1: Recurrence Relations Discrete Mathematical Structures: Theory and Applications.
Lecture - 8 On Stacks, Recursion. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Lecture Outline Quick sort Algorithm Recursion –Calculate n factorial –Fibonacci.
Discrete Mathematics Recurrence Relations Chapter 5 R. Johnsonbaugh
22C:19 Discrete Math Advanced Counting Fall 2010 Sukumar Ghosh.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
Discrete Mathematics Lecture # 22 Recursion.  First of all instead of giving the definition of Recursion we give you an example, you already know the.
Chapter 9 Recursion © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
1 In this puzzle, the player begins with n disks of decreasing diameter placed one on top of the other on one of three pegs of the game board. The player.
Tower of Hanoi Tower of Hanoi is a mathematical puzzle invented by a French Mathematician Edouard Lucas in The game starts by having few discs stacked.
Aumm-e-hani munir Maria umer Rakhshanda batool Faiza.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Tower of Hanoi problem: Move the pile of rings from one peg to another
A0=0 a1=2 an a2=6 an-1 a3=12 a4=20 a2 a1 a0.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
More Recursion.
Java Software Structures: John Lewis & Joseph Chase
Programming Language Principles
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Data Structures & Algorithms
Tower of Hanoi problem: Move the pile of rings from one peg to another
Copyright © Cengage Learning. All rights reserved.
CS 2210 Discrete Structures Advanced Counting
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Tower of Hanoi problem: Move the pile of rings from one peg to another
Recursively Defined Sequences
Algorithms CSCI 235, Spring 2019 Lecture 8 Recurrences III
Presentation transcript:

Recursively Defined Sequences Lecture 40 Section 8.1 Wed, Apr 11, 2007

Recursive Sequences A recurrence relation for a sequence {a n } is an equation that defines each term of the sequence as a function of previous terms, from some point on. The initial conditions are equations that specify the values of the first several terms a 0, …, a n – 1.

Recursive Sequence Define a sequence {a k } by a 0 = 2, a 1 = 3, a k = a k – 1 + 2a k – 2, for all k  2. The next few terms are a 2 = 7, a 3 = 13, a 4 = 27.

The Towers of Hanoi The Towers of Hanoi puzzle has A game board, Three pegs (Peg 1, Peg 2, Peg 3), 10 disks, of 10 different sizes. Initially, the 10 disks are stacked on Peg 1, each disk smaller than the disk below it.

The Towers of Hanoi By moving one disk at a time from peg to peg, the object of the puzzle is to reassemble the disks on Peg 3 in the original order. At no point may a larger disk be placed on a smaller disk.

The Towers of Hanoi 123 Start

The Towers of Hanoi 123 Finish

The Towers of Hanoi There is a very simple recursive solution. Reassemble the top 9 disks on Peg 2. Move Disk 10 from Peg 1 to Peg 3. Reassemble the top 9 disks on Peg 3.

The Towers of Hanoi 123

123

123

123

But how does one reassemble the top 9 disks on Peg 2? It is very simple to reassemble the top 9 disks on Peg 2. Reassemble the top 8 disks on Peg 3. Move Disk 9 from Peg 1 to Peg 2. Reassemble the top 8 disks on Peg 2.

The Towers of Hanoi 123

123

123

123

But how does one reassemble the top 8 disks on Peg 3? It is very simple to reassemble the top 8 disks on Peg 3. Reassemble the top 7 disks on Peg 2. Move Disk 8 from Peg 1 to Peg 3. Reassemble the top 7 disks on Peg 3.

The Towers of Hanoi 123

123

123

123

But how does one reassemble the top 7 disks on Peg 2? And so on…

The Towers of Hanoi Ultimately, the question becomes, how does one reassemble the top 1 disk on Peg 2? Hmmm….

The Towers of Hanoi 123

123

How many moves will it take? Let m n be the number of moves to reassemble n disks. Then m 1 = 1. m n = 2m n – 1 + 1, for all n  2.

Counting Strings Let b k be the number of binary strings of length k that do not contain 11. b 0 = 1, {  } b 1 = 2, {0, 1} b 2 = 3, {00, 01, 10} b 3 = 5, {000, 001, 010, 100, 101} What is the pattern?

Counting Strings Consider strings of length k, for some k  2, that do not contain 11. If the first character is 0, then the remainder of the string is a string of length k – 1 which does not contain 11. If the first character is 1, then the next character must be 0 and the remainder is a string that does not contain 11.

Counting Strings k = 1: {0, 1}

Counting Strings k = 1: {0, 1} k = 2: {00, 01, 10}

Counting Strings k = 1: {0, 1} k = 2: {00, 01, 10} k = 3: {000, 001, 010}  {100, 101}

Counting Strings k = 1: {0, 1} k = 2: {00, 01, 10} k = 3: {000, 001, 010, 100, 101}

Counting Strings k = 1: {0, 1} k = 2: {00, 01, 10} k = 3: {000, 001, 010, 100, 101} k = 4: {0000, 0001, 0010, 0100, 0101}  {1000, 1001, 1010}

Counting Strings k = 1: {0, 1} k = 2: {00, 01, 10} k = 3: {000, 001, 010, 100, 101} k = 4: {0000, 0001, 0010, 0100, 0101, 1000, 1001, 1010}

Counting Strings Therefore, b k = b k – 1 + b k – 2, for all k  2. The next few terms are b 3 = b 2 + b 1 = 5, b 4 = b 3 + b 2 = 8, b 5 = b 4 + b 3 = 13.

Counting Binary Trees How many binary trees are there with n nodes? Let t n be the number binary trees with n nodes. Clearly, t 0 = 1, t 1 = 1, and t 2 = 2. n = 1 n = 2

Counting Binary Trees When n = 2, we have 5 trees:

Counting Binary Trees What is the recursive relation?

Counting Binary Trees What is the recursive relation? t 2 = 2    2 = 5

Counting Binary Trees When n = 2, we have 5 trees: 2 on left, 0 on right 0 on left, 2 on right 1 on left, 1 on right

Counting Binary Trees Following this pattern, we compute t 4 = 5     5 = 14. t 5 = 14      14 = 42. t 6 = 42       42 = 132. and so on.

Counting Paths In the following figure, how many paths are there from the lower left corner to the upper right corner?

Counting Paths Let p n be the number of paths when the bottom has length n. Then clearly, p 0 = 1, p 1 = 1, and p 2 = 2.

Counting Paths When n = 3, …

Counting Paths When n = 3, …

Counting Paths When n = 3, …

Counting Paths When n = 3, …

Counting Paths When n = 3, …

Counting Paths When n = 3, …

Counting Paths When n = 3, p 3 = 5.

Counting Paths Are there 14 paths when n = 4? What is the connection with binary trees?

Counting Parenthesized Expressions How many possible legitimate arrangements are there of n left parentheses and n right parentheses? Let a n be the number of legitimate arrangements. When n = 0, a 0 = 1. When n = 1, a 1 = 1: (). When n = 2, a 2 = 2: ()() and (()).

Counting Parenthesized Expressions When n = 3, a 3 = 5: ()()(), ()(()), (())(), (()()), and ((())). What is the connection with binary trees and the paths?

Counting r-Partitions An r-partition of a set is a partition of the set into exactly r nonempty subsets. Let A be a set of size n. Let a n, r be the number of distinct r- partitions of A. Special cases a n, n = 1 for all n  1. a n, 1 = 1 for all n  1.

Counting r-Partitions Let A = {a, b, c, d}. a 4, 3 = 6 since the 3-partitions are {{a}, {b}, {c, d}} {{a}, {c}, {b, d}} {{a}, {d}, {b, c}} {{b}, {c}, {a, d}} {{b}, {d}, {a, c}} {{c}, {d}, {a, b}}

Counting r-Partitions Consider an r-partition of a set A. Let x  A. Either x appears as a set {x} by itself or it doesn’t. If it does, then the remaining sets form an (r – 1)-partition of A – {x}. If it doesn’t, then if we remove x, we have an r-partition of A – {x}.

Counting r-Partitions The 3-partitions that contain { a }. {{a}, {b}, {c, d}} {{a}, {c}, {b, d}} {{a}, {d}, {b, c}} The 3-partitions that do not contain { a }. {{b}, {c}, {a, d}} {{b}, {d}, {a, c}} {{c}, {d}, {a, b}}

Counting r-Partitions The 3-partitions that contain { a }. {{a}, {b}, {c, d}} {{a}, {c}, {b, d}} {{a}, {d}, {b, c}} The 3-partitions that do not contain { a }. {{b}, {c}, {a, d}} {{b}, {d}, {a, c}} {{c}, {d}, {a, b}} Distinct 2-partitions of {b, c, d}

Counting r-Partitions The 3-partitions that contain { a }. {{a}, {b}, {c, d}} {{a}, {c}, {b, d}} {{a}, {d}, {b, c}} The 3-partitions that do not contain { a }. {{b}, {c}, {a, d}} {{b}, {d}, {a, c}} {{c}, {d}, {a, b}} Three copies of same 3-partition of {b, c, d}

Counting r-Partitions Therefore, a n, r = a n – 1, r – 1 + r  a n – 1, r, for all n  1 and for all r, 1 < r < n.

Counting r-Partitions Compute a 4, 3. a 4, 3 = a 3, 2 + 3a 3, 3 = (a 2, 1 + 2a 2, 2 ) + 3(1) = (1+2(1)) + 3 = 6.

Counting r-Partitions Compute a 5, 3. a 5, 3 = a 4, 2 + 3a 4, 3 = (a 3, 1 + 2a 3, 2 ) + 3(6) = (1 + 2(a 2, 1 + 2a 2, 2 )) + 18 = (1 + 2(1 + 2)) + 18 = 25.