Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion.

Similar presentations


Presentation on theme: "Recursion."— Presentation transcript:

1 Recursion

2 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 for n≥2

3 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

4 Move disks from left pole to right pole
Tower of Hanoi: Move disks from left pole to right pole 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 Pole A Pole B Pole C Pole A Pole B Pole C

5

6 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 for n≥2

7 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 *an-1 = 1.03*an-1 for n≥1

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

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

10 Finding an explicit formula for a recursively defined sequence
Examples (cont.) Recall the recursive formula for the Hanoi tower example: a1 = 1 an = 2an 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)


Download ppt "Recursion."

Similar presentations


Ads by Google