##### Document information

Recursion

Docsity.com

Recursively defined sequences To define a sequence recursively: • give initial conditions, i.e., the values of the first

few terms explicitly; • give a recurrence relation, i.e., an equation that

relates later terms in the sequence to earlier terms. Example: • Define the following sequence recursively: 1, 4, 7, 10, 13, … • Solution: a1 = 1, an = an-1 + 3 for n≥2

Docsity.com

Recursion is one of the central ideas of computer science

To solve a problem recursively • Break it down into smaller subproblems each

having the same form as the original problem; • When the process is repeated many times, the

last of the subproblems are small and easy to solve;

• The solutions of the subproblems can be woven together to form a solution to the original problem.

• Example: The tower of Hanoi

Docsity.com

**
RULES:
You may only move one disk at a time.
A disk may only be moved to one of the three columns.
You must never place a larger disk on top of a smaller disk.
**

**INITIAL STATE GOAL STATE
**

Tower of Hanoi: Move disks from left pole to right pole

Pole A Pole B Pole C Pole A Pole B Pole C Docsity.com

Docsity.com

The Tower of Hanoi • How to generalize the procedure to n disks? • How many moves are required? • Recursive procedure:

– Transfer the top n-1 disks from pole A to pole B – Move the bottom disk from pole A to pole C – Transfer the top n-1 disks from pole B to pole C

• Let ak denote the number of moves needed to transfer a tower of n disks from one pole to another using the above procedure

• Then we have the following recursive formula for counting the moves:

a1 = 1 an = 2an-1 + 1 for n≥2

Docsity.com

Recursive formula for Compound Interest

• Suppose $10K is deposited in an account paying 3%

interest compounded annually. • For each positive integer n, let a0 = the initial amount deposited; an = the amount on deposit at the end of year n. • Find a recursive relation for a0 , a1 , a2 ,… assuming no additional deposits or withdrawals. • We have the following recursive formula: a0 = 10,000 an = an-1 + 0.03*an-1 = 1.03*an-1 for n≥1

Docsity.com

Finding an explicit formula for a recursively defined sequence

It is often helpful to know an explicit formula for the sequence, especially if you need – to compute terms with very large subscripts; – to examine general properties of the sequence.

Examples • Recall the recursive formula for the compound interest

example:
a0 = 10,000
an = 1.03*an-1 for n≥1
The explicit formula is
an = 10000*(1.03)n for n≥0
*Note*: this formula can be generalized to any geometric

sequence. Docsity.com

Finding an explicit formula for a recursively defined sequence

Examples (cont.) • Suppose the sequence is given by the following

recursive relation: a0 = 3 an = an-1 + 4 for n≥1 Then the explicit formula is an = 3 + 4*n for n≥0

• *Note*: this formula can be generalized to any arithmetic

sequence.

Docsity.com

Finding an explicit formula for a recursively defined sequence

Examples (cont.) • Recall the recursive formula for the Hanoi tower

example: a1 = 1 an = 2an-1 + 1 for n≥2 • How to get explicit formula? • Compute the first few terms of this sequence: a1 = 1, a2 = 3, a3 = 7, a4 = 15, a5 = 31, a6 = 63 • Based on the pattern, an = 2n – 1 for n≥1 • But need a proof! • The proof by induction (blackboard) Docsity.com