Download presentation

Presentation is loading. Please wait.

Published byAndy Cordingley Modified over 2 years ago

1
Chapter 8 Recursion

2
8.1 Recursively Defined Sequences

3
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.

4
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

5
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.

6
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

7
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.

8
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

9
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.

10
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.

11
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.)

12
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

13
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

14
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?

15
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

16
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

17
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

18
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)

19
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

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google