ppt - People Server at UNCW

Download Report

Transcript ppt - People Server at UNCW

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.

n a n

n

 1 – 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 b 2 = b = 1, b 2-1 + b 1 2-2 = 3 (initial conditions) = b 1 + b 0 = 3 + 1 = 4 b b 5 3 = b = b 3-1 5-1 + b + b 3-2 5-2 = b = b 2 4 + b + b 1 3 = 4 + 3 = 7 = 11 + 7 = 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-2 + 1 (recurrence relation) 2. c 0 = 1 and c 1 = 2 (initial conditions) c 2 = c 2-1 + 2*c 2-2 + 1 = c 1 + 2*c 0 + 1 = 2 + 2*1 + 1 = 5 c 3 = c 3-1 + 3*c 3-2 + 1 = c 2 + 3*c 1 + 1 = 5 + 3*2 + 1 = 12 c 4 = c 4-1 + 4*c 4-2 + 1 = c 3 + 4*c 2 + 1 = 10 + 4*12 + 1 = 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 • starting at k = 0: s 0+1 = 3s 0 – 1, s 1+1 = 3s 1 – 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 (k≥0) s k+1 =3s k • same as the second form.

– 1

Example

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

k-1 (k-1)! defined above) = (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.

3.

Move the bottom disk from pole A to pole C.

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

min moves to transfer a tower of k disks from A to C =

Towers of Hanoi

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 n≥1, 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-1 + 1 + m k-1 = 2m k-1 + 1, integers k≥2 initial condition: m 1 = [move one disk to another pole] = 1

Towers of Hanoi

1. m k = 2m k-1 + 1 (recurrence relation) 2. m 1 = 1 (initial condition) m 2 = 2m 1 + 1 = 3 m 3 = 2m 2 + 1 = 7 m 4 = 2m 3 + 1 = 15 … m 6 = 2m 5 + 1 = 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 alive at month k-1

+

+

num of pairs born at end of month k

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 F k = 1 = F k-1 + F k-2 Fibonacci Sequence 1. F k 2. F 0 F 2 F 3 = F = F 1 2 F 4 … = F 3 = F k-1 = 1, F 1 + F k-2 (recurrence relation) = 1 (initial condition) + F + F 1 0 = 1+1 = 2 = 2+1 = 3 + F 2 = 3+2 = 5 F 12 = F 11 + F 10 = 144 + 89 = 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 = A k-1 + (0.04)*A = (1.04)*A k-1 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 = 0.0075 (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 0 = P k-1 (1+0.0075) = P k-1 (1.0075), integers k≥1 = 10,000 P 1 P 2 = 1.0075*P = 10,150.56

0 =1.0075*10,000 =10,075.00

P 3 P 4 = 10,226.69

= 10,303.39