Recursion and Iteration Kavita Math 231 We use

  • Slides: 31
Download presentation
Recursion and Iteration Kavita Math 231

Recursion and Iteration Kavita Math 231

We use Recursion when we have to perform a complex task that can be

We use Recursion when we have to perform a complex task that can be broken into the several subtasks. Recursion is implemented as a method that calls itself to solve subtasks. It often appears in functional-style definitions or divide-and-conquer explanations of an algorithm. To understand recursion, we need to understand recurrence A recurrence relation for a sequence a 0 , a 1 , a 2, …is a formula that relates each term ak to certain of it predecessors ak-1, ak-2 , …, ak-i where i is a fixed integer and k is any integer greater than or equal to I. The initial conditions for recurrence relation specify the values of a 0 , a 1 , a 2, …ai-1. For example a 0 , a 1 , a 2, …can be specified as. The advantage of defining a sequence by such an explicit formula is that each term of the sequence is uniquely determined and can be computed in a fixed, finite number of steps. Calculate a 0 and a 1. Another way of defining the terms of a sequence is recursion which requires an equation called recurrence relation that relates later terms in the sequence to earlier terms and a specification, called initial conditions. For example a sequence b 0 , b 1 , b 2 can be defined recursively as follows recurrence relation Kavita Math 231 initial condition Compute b 2 , b 3 , b 4

A function is said to be recursively defined if the function definition refers to

A function is said to be recursively defined if the function definition refers to itself. In order for the definition to be not circular, it must have the following two properties: 1. There must be certain arguments, called the base values, for which the function does not refer to itself. 2. Each time the function does refer to itself, the argument of the function must be closer to a base value. A recursive function with these two properties is also said to be well defined. Examples: • Factorial(n) a. IF n is 0 b. Return 1 c. ELSE d. Return n* Factorial(n-1) Kavita Math 231

Factorial(4) N 4 N 3 N 2 N 1 N 0 Kavita Math 231

Factorial(4) N 4 N 3 N 2 N 1 N 0 Kavita Math 231

 • exponential (POWER) function XN POWER(x, n) IF n=1 return x ELSE return

• exponential (POWER) function XN POWER(x, n) IF n=1 return x ELSE return x* POWER(x, n - 1) POWER(2, 3) x n 2 3 x n 2 2 x n 2 1 Kavita Math 231

Towers of Hanoi: When the game begins, we’ve three pegs and all the circles

Towers of Hanoi: When the game begins, we’ve three pegs and all the circles are on the first peg in order by size, the biggest on the bottom and smallest on top. Object of the game: Is to move the circles one at a time to the third peg, the catch being that the order of size has to be maintained. The middle peg can be used as a helper, a go between but should be empty at the beginning and at the end of the game. And the algorithm for such a fancy schmancy game is: Get N circles moved from peg 1 to peg 3 Get n-1 circles removed from peg 1 and placed in peg 2 Move n’th circle from peg 1 to peg 3 Get n-1 circles moved from peg 2 to peg 3 Kavita Math 231

 • the FIBONACCI sequence: Fib(N) = Fib(N-1) + Fib(N-2) for n, and Fib(0)

• the FIBONACCI sequence: Fib(N) = Fib(N-1) + Fib(N-2) for n, and Fib(0) = Fib(1) = 1 1. If n=0 or n =1, then Fn= 1. 2. If n > 1, then Fn= Fn-2 + Fn-1 example: Fib(10) = 1, 1, 2, 3, 5, 8, 13, 21, 33, 54. . . . • Compound interest: Amount accumulated at Compound Interest on P at rate r over n intervals Amt. C. I(P, r, n) 1. IF n = 1, A 0 = P(1+ r) 2. IF n > 1, Ak = Ak-1(1+ r) Kavita Math 231

Example: Counting Strings • Addition rules • Let = {0, 1}. • Let ak

Example: Counting Strings • Addition rules • Let = {0, 1}. • Let ak be the number of strings in * of length k that do not contain 11. – a 0 = 1, – a 1 = 2, – a 2 = 3. Kavita Math 231

Example: Counting Strings • Consider strings of length k, for some k 2, that

Example: Counting Strings • Consider strings of length k, for some k 2, that do not contain 11. • If the first character is 0, then the remainder of the string is a string of length k – 1 which does not contain 11. • If the first character is 1, then the next character must be 0 and the remainder is a string that does not contain 11. Kavita Math 231

Example: Counting Strings • Therefore, – ak = ak – 1 + ak –

Example: Counting Strings • Therefore, – ak = ak – 1 + ak – 2, for all k 2. • The first few terms – a 3 = a 2 + a 1 = 5, – a 4 = a 3 + a 2 = 8, – a 5 = a 4 + a 3 = 13. • k = 3: {000, 001, 010, 101} Kavita Math 231

Example: Counting r-Partitions • • An r-partition of a set is a partition of

Example: Counting r-Partitions • • An r-partition of a set is a partition of the set into r nonempty subsets. Let A be a set of size n. Let an, r be the number of distinct r-partitions of A. Special cases – an, n = 1 for all n 1. – an, 1 = 1 for all n 1. Kavita Math 231

Example: Counting r-Partitions • Let A = {a, b, c, d}. • a 4,

Example: Counting r-Partitions • Let A = {a, b, c, d}. • a 4, 2 = 7 since the 2 -partitions are – {{a}, {b, c, d}} – {{b}, {a, c, d}} – {{c}, {a, b, d}} – {{d}, {a, b, c}} – {{a, b}, {c, d}} – {{a, c}, {b, d}} – {{a, d}, {b, c}} Kavita Math 231

Example: Counting r-Partitions • • • Consider an r-partition of a set A. Let

Example: Counting r-Partitions • • • Consider an r-partition of a set A. Let x A. Either x is in a set {x} by itself or it isn’t. If it is, then the remaining sets form an (r – 1)-partition of A – {x}. If it isn’t, then if we remove x, we have an r-partition of A – {x}. Kavita Math 231

Example: Counting r-Partitions • In fact, we get the same r-partition of A –

Example: Counting r-Partitions • In fact, we get the same r-partition of A – {x} that we would get had x been a member of any other set in the partition. • Thus, each r-partition of A – {x} gives rise to r r-partitions of A. • Therefore, an, r = an – 1, r – 1 + r an – 1, r, for all n 1 and for all r, 1 < r < n. Kavita Math 231

Example: Counting r-Partitions • Compute a 4, 2. and a 5, 2. – a

Example: Counting r-Partitions • Compute a 4, 2. and a 5, 2. – a 4, 2 = a 3, 1 + 2 a 3, 2 = 1 + 2(a 2, 1 + 2 a 2, 2) = 1 + 2(1 + 2 1) = 7. – a 5, 2 = a 4, 1 + 2 a 4, 2 =1+2 7 = 15. Kavita Math 231

Section 8. 2 Solving Recurrence Relations by Iteration Kavita Math 231

Section 8. 2 Solving Recurrence Relations by Iteration Kavita Math 231

Guessing the Answer • Write out the first several terms. • Look for a

Guessing the Answer • Write out the first several terms. • Look for a pattern. • Two strategies – Do the arithmetic. – Postpone the arithmetic. Kavita Math 231

Example: Do the Arithmetic • Define {an} by – a 1 = 2, –

Example: Do the Arithmetic • Define {an} by – a 1 = 2, – an = 2 an – 1, for all n 2. • Find a formula for an. • First few terms: 2, 3, 5, 9, 17, 33, 65. • Compare to: 1, 2, 4, 8, 16, 32, 64. • Guess that an = 2 n – 1 + 1. Kavita Math 231

Example: Postpone the Arithmetic • Define {an} by – a 1 = 1, –

Example: Postpone the Arithmetic • Define {an} by – a 1 = 1, – an = 5 an – 1 + 2, for all n 2. • Find a formula for an. • First few terms: 1, 7, 37, 187, 937. • What is an? Kavita Math 231

Example: Postpone the Arithmetic • Calculate a few terms – a 1 = 1.

Example: Postpone the Arithmetic • Calculate a few terms – a 1 = 1. – a 2 = 5 1 + 2. – a 3 = 52 1 + 5 2 + 2. – a 4 = 53 1 + 52 2 + 5 2 + 2. – a 5 = 54 1 + 53 2 + 52 2 + 5 2 + 2. • It appears that, in general, – an = 5 n – 1 + (5 n – 2 + 5 n – 3 + … + 1) 2. Kavita Math 231

Arithmetic sequence A sequence in which each term is obtained by adding a fixed

Arithmetic sequence A sequence in which each term is obtained by adding a fixed factor to the preceding term. Example if Geometric sequence A sequence in which each term is obtained by multiplying a fixed factor to the preceding term. Example if Sum of geometric sequence is given as Page 442, Page 477 example 8. 2. 2, 8. 2. 3, 8. 2. 4, 8. 2. 5 Page 452, Page 485 1 -c, 2 -b, c, 4, 17 Kavita Math 231

Example: Postpone the Arithmetic • an = 5 n – 1 + (5 n

Example: Postpone the Arithmetic • an = 5 n – 1 + (5 n – 2 + 5 n – 3 + … + 1) 2 = 5 n – 1 + (5 n – 1)/(5 – 1) 2 = 5 n – 1 + (5 n – 1)/2 = (3 5 n – 1)/2. Kavita Math 231

Example: Future Value of an Annuity • Define {an} by – a 0 =

Example: Future Value of an Annuity • Define {an} by – a 0 = d, – an = (1 + r)an – 1 + d, for all n 1. • Find a formula for an. – a 1 = (1 + r)d + d. – a 2 = (1 + r)2 d + (1 + r)d + d. – a 3 = (1 + r)3 d + (1 + r)2 d + (1 + r)d + d. Kavita Math 231

Example: Future Value of an Annuity • It appears that, in general, an =

Example: Future Value of an Annuity • It appears that, in general, an = (1 + r)nd + … + (1 + r)d + d = d((1 + r)n + 1 – 1)/((1 + r) – 1) = d((1 + r)n + 1 – 1)/r. Kavita Math 231

Verifying the Guess • Use mathematical induction to verify the guess. Kavita Math 231

Verifying the Guess • Use mathematical induction to verify the guess. Kavita Math 231

Verify the Guess • Define {an} by – a 1 = 1, – an

Verify the Guess • Define {an} by – a 1 = 1, – an = 5 an – 1 + 2, for all n 2. • Verify, by induction, the formula an = (3 5 n – 1)/2. Kavita Math 231

Verify the Guess • Basic Step (n = 1) – a 1 = 1

Verify the Guess • Basic Step (n = 1) – a 1 = 1 – (3 51 – 1)/2 = (3 50 – 1)/2 = (3 – 1)/2 = 1. Kavita Math 231

Verify the Guess • Inductive Step – Suppose ak = (3 5 k –

Verify the Guess • Inductive Step – Suppose ak = (3 5 k – 1)/2 for some k 1. – Then ak + 1 = 5[(3 5 k – 1)/2] + 2 = (3 5 k – 5)/2 + 2 = (3 5 k – 1)/2. • Therefore, it is true for all n 1. Kavita Math 231

Page 438, Page 472 #2, 4, 6 Examples of recursively defined sequences. To solve

Page 438, Page 472 #2, 4, 6 Examples of recursively defined sequences. To solve a problem recursively, find a way to break it down into smaller sub problems each having the same sub form as the original problem. Also put a terminating condition called the base condition. Have you seen this methodology anywhere before? Page 438, Page 472 #17, 22, (24), 28, (23), 29, (34), Kavita Math 231

ITERATION Suppose I want to print my name backwards. How will I do it

ITERATION Suppose I want to print my name backwards. How will I do it recursively? Is there any other way I can achieve the same objective? For that I need to have a knowledge of another method called the iterative method. • The iterative method converts the recurrence into a summation within some limits called bounds. • Given a sequence a 0 , a 1 , a 2, … defined by a recursive relation and initial condition, you start from the initial conditions and calculate successive terms till you see a pattern developing. At that point you guess an explicit formula. Page 442, example 8. 2. 1 • You can check the correctness of the formula that you’ve derived by mathematical induction. Kavita Math 231

Recursion and iteration are different ways to cause the evaluation of an expression to

Recursion and iteration are different ways to cause the evaluation of an expression to re-occur as needed. Recursion often appears in functional-style definitions or divide-and-conqure explanations of an algorithm. Programming with recursion involves the idea of "vertical" self-reference, as in a data structure that has substructures similar to itself, or a function which invokes itself. In contrast, iteration simply involves the notion of "horizontal" repetition of some concept, such as an expression to be evaluated repeatedly or a data structure which contains a sequence of identically-structured components. In theory, recursion and iteration are equivalent; anything we can do with one, we can do with the other. That may be true, but each is the best tool for some purposes. Kavita Math 231