Chapter 8 Recursion. 8.1 Recursively Defined Sequences.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms II
Advertisements

Chapter 3 Mathematics of Finance
Discrete Math Recurrence Relations 1.
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.
Week 6 - Wednesday CS322.
Advanced Counting Techniques
Chapter Recurrence Relations
Introduction to Algorithms
By Nicole, Karen, Arthur, Nico
AE1APS Algorithmic Problem Solving John Drake
Starting Out with Java: From Control Structures through Objects
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
MAT 4 – Kompleks Funktionsteori MATEMATIK 4 INDUKTION OG REKURSION MM 1.4 MM 1.4: Induktion og Rekursion Topics: Mathematical induction Example of Towers.
Recursion Lecture 18: Nov 18.
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.
The Tower of Hanoi
1 Section 6.1 Recurrence Relations. 2 Recursive definition of a sequence Specify one or more initial terms Specify rule for obtaining subsequent terms.
COMPSCI 105 S Principles of Computer Science Recursion 3.
Copyright © Cengage Learning. All rights reserved. CHAPTER 5 SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION.
CENG 7071 Recursion. CENG 7072 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive function.
13.2: Arithmetic and Geometric Sequences Recursive Definitions.
Chapter 8 With Question/Answer Animations 1. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
Copyright © Cengage Learning. All rights reserved.
Advanced Counting Techniques
Week 6 - Monday CS322.
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
© 2006 Pearson Addison-Wesley. All rights reserved 3-1 Chapter 3 Recursion: The Mirrors.
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.
Chapter 3 Recursion: The Mirrors. © 2004 Pearson Addison-Wesley. All rights reserved 3-2 Recursive Solutions Recursion –An extremely powerful problem-solving.
Chapter 8. Section 8. 1 Section Summary Introduction Modeling with Recurrence Relations Fibonacci Numbers The Tower of Hanoi Counting Problems Algorithms.
13.2 Recursive Definitions Objective 1) Provide the recursive definition for sequences; 2) Identify the type of a sequence from a recursive definition.
Chapter 5: Sequences, Mathematical Induction, and Recursion 5.6 Defining Sequences Recursively 1 So, Nat’ralists observe, a Flea/Hath smaller Fleas on.
Fall 2015 COMP 2300 Discrete Structures for Computation Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Lecture#16 Discrete Mathematics. Recursion Now, 1 is an odd positive integer by the definition base. With k = 1, = 3, so 3 is an odd positive integer.
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 5 th edition, 2001 Chapter 5 Recurrence Relations.
Section 2.4. Section Summary Sequences. Examples: Geometric Progression, Arithmetic Progression Recurrence Relations Example: Fibonacci Sequence Summations.
Chapter 8 With Question/Answer Animations. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
Recurrence Relation Models
1 Topics Recursion sections 8.1 – Recursion A recursively defined sequence –First, certain initial values are specified –Later terms of the sequence.
Lecture - 8 On Stacks, Recursion. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Lecture Outline Quick sort Algorithm Recursion –Calculate n factorial –Fibonacci.
Leonardo Pisano Fibonacci
Discrete Mathematics Recurrence Relations Chapter 5 R. Johnsonbaugh
Agenda Lecture Content:  Recurrence Relations  Solving Recurrence Relations  Iteration  Linear homogenous recurrence relation of order k with constant.
RECURRENCE Sequence Recursively defined sequence
Discrete Mathematics Lecture # 22 Recursion.  First of all instead of giving the definition of Recursion we give you an example, you already know the.
1 RECURRENCE 1. Sequence 2. Recursively defined sequence 3. Finding an explicit formula for recurrence relation.
1 CS1120: Recursion. 2 What is Recursion? A method is said to be recursive if the method definition contains a call to itself A recursive method is a.
Recursion Chapter What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar.
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
Discrete Math For Computing II. Contact Information B. Prabhakaran Department of Computer Science University of Texas at Dallas Mail Station EC 31, PO.
Recursion CENG 707.
Recursion: The Mirrors
Patterns.
Introduction to Recurrence Relations
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Chapter 14: Recursion Starting Out with C++ Early Objects
Copyright © Cengage Learning. All rights reserved.
Chapter 14: Recursion Starting Out with C++ Early Objects
CS1120: Recursion.
Copyright © Cengage Learning. All rights reserved.
Recursion: The Mirrors
1A Recursively Defined Functions
Recursion: The Mirrors
Presentation transcript:

Chapter 8 Recursion

8.1 Recursively Defined Sequences

Recursion A Sequence can be defined as: – informally be providing a few terms to demonstrate the pattern, i.e. 3, 5, 7, …. – give an explicit formula for it nth term, i.e. – or, by recursion which requires a recurrence relation.

Defining Recursion Recursion requires recurrence relation relates later terms in the sequence to earlier terms and initial conditions, values of the first few terms of the sequence. – Example b 0, b 1, b 2, … For all integers k>= 2, 1.b k = b k-1 + b k-2 (recurrence relation) 2.b 0 = 1, b 1 = 3 (initial conditions) b 2 = b b 2-2 = b 1 + b 0 = = 4 b 3 = b b 3-2 = b 2 + b 1 = = 7 b 5 = b b 5-2 = b 4 + b 3 = = 18

Recursion Definition: – A recurrence relation for a sequence a 0, a 1, a 2,… is a formula that relates each term a k to ceratin of its predecessors a k-1, a k-2, …, a k-I, where I is an integer and k is any integer greater than or equal to i. – The initial conditions for such a recurrence relation specify the values of a 0, a 1, a 2,…,a i-1, if I is a fixed integer, or a 0, a 1, a 2,…,a m, where m is an integer with m>=0, if i depends on k.

Example Computing terms – Recursive sequence c k, for all integers k >=2, find c 2, c 3, c 4 1.c k = c k-1 + k c k (recurrence relation) 2.c 0 = 1 and c 1 = 2 (initial conditions) c 2 = c *c = c 1 + 2*c = 2 + 2*1 + 1 = 5 c 3 = c *c = c 2 + 3*c = 5 + 3*2 + 1 = 12 c 4 = c *c = c 3 + 4*c = * = 59

Equivalent Recursion There is more than one way to setup a recursive sequence! – for all k>=1, s k = 3s k-1 – 1 – for all k>=0, s k+1 = 3s k – 1 Are the two sequences equivalent? – show the results of the sequence for terms: starting at k = 1: s 1 = 3s 0 – 1, s 2 = 3s 1 – 1, s 3 = 3s 2 – 1 starting at k = 0: s 0+1 = 3s 0 – 1, s 1+1 = 3s 1 – 1, … – change first term to second by adjusting first k for all k>=1, s k = 3s k-1 – 1 => k start at 0 (k0) s k+1 =3s k – 1 same as the second form.

Example Show that sequence given satisfies a recurrence relation: – 1, -1!, 2!, -3!, 4!, …, (-1) n n!, …, for n0 is equivalent to s k = (-k)s k-1 for k1 – General term of the seq s n starting with s 0 = 1, then s n = (- 1) n n! for n0 substitute for k and k-1: s k = (-1) k k!, s k-1 = (-1) k-1 (k-1)! (-k)s k-1 = (-k)[(-1) k-1 (k-1)!] (s k defined above) = (k)(-1)(-1) k-1 (k-1)! = (k)(-1) k (k-1)! = (-1) k (k)(k-1)! = (-1) k k! = s k

Solving Recursive Problems To solve a problem recursively means to find a way to break it down into smaller subproblems each having the same form as the original problem.

Towers of Hanoi Problem statement: eight disks with holes in the center that are stacked from largest diameter to smallest on the first of three poles. Move the stacked disk from one pole to another. Rules: a larger disk cannot be placed on top of a smaller disk at any time.

Towers of Hanoi Recursive Solution 1.Transfer the top k-1 disks from pole A to pole B. (Note: k>2 requires a number of moves.) 2.Move the bottom disk from pole A to pole C. 3.Transfer the top k-1 disks from pole B to pole C. (Again, if k>2, execution of this step will require more than one move.)

Towers of Hanoi min moves to transfer a tower of k disks from A to C = min num of moves to go from position a to position b + min num of moves to go from position b to position c + min num of moves to go from position c to position d For each integer n1, m n = min num of moves to transfer a tower of n disks from one pole to another. position a to position b = m k-1, position b to c = 1 move, position c to position d = m k-1 moves. m k = m k m k-1 = 2m k-1 + 1, integers k2 initial condition: m 1 = [move one disk to another pole] = 1

Towers of Hanoi 1.m k = 2m k (recurrence relation) 2.m 1 = 1 (initial condition) m 2 = 2m = 3 m 3 = 2m = 7 m 4 = 2m = 15 … m 6 = 2m = 63

Fibonacci Leonardo of Pisa was the greatest mathematician of the 13 th century. Proposed the following problem: – A single pair of rabbits (male/female) is born at the beginning of a year. Assuming the following conditions: 1.Rabbit pairs are not fertile during their first month of life but thereafter give birth to one new male/female pair at the end of every month. 2.No rabbits die – How many rabbits will there be at the end of the year?

Fibonacci To solve the problem you can hand compute for each of the 12 months. – m1 = 1, m2 = 1, m3 = 2, m4 = 3, m5 = 5, … m11 = 144, m12 = 233 num of pairs alive at end of month k = num of pairs alive at end of month k-1 + num of pairs born at end of month k = num of pairs alive at month k-1 + num of pairs alive at month k-2

Fibonacci – for n 1, F n = num of pairs alive at month n – F 0 = the initial number of pairs – F 0 = 1 – F k = F k-1 + F k-2 Fibonacci Sequence 1.F k = F k-1 + F k-2 (recurrence relation) 2.F 0 = 1, F 1 = 1 (initial condition) F 2 = F 1 + F 0 = 1+1 = 2 F 3 = F 2 + F 1 = 2+1 = 3 F 4 = F 3 + F 2 = 3+2 = 5 … F 12 = F 11 + F 10 = = 233

Compound Interest Compute compound interest on principle value using recursive sequence. $100,000 principle, earning 4% per year, how much would you have in 21 years. amt in account end of a year = amt in account at end of previous year + interest earned on account during year = amt in account at end of previous year + (0.04)* amt in account previous year A n = amt in account at end of year n A 0 = initial amount (principle) A k = A k-1 + (0.04)*A k-1 A k = (1.04)*A k-1

Compound Interests Compounding multiple times a year – interest is broken up of over the year based on the number of compounding periods – example 3% compounded quarterly 3%/4 =.03/4 = (interest rate per period) interest earned during kth period = P k-1 (i/m) P k = P k-1 + P k-1 (i/m) = P k-1 (1 + i/m)

Example Given $10,000, How much will the account be work at the end of one year with a interest rate of 3% compounded quarterly? – P k = P k-1 ( ) = P k-1 (1.0075), integers k1 – P 0 = 10,000 – P 1 = *P 0 =1.0075*10,000 =10, – P 2 = 10, – P 3 = 10, – P 4 = 10,303.39