E N D
Recursion and Iteration Kavita Math231
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 a0 ,a1 ,a2,…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 a0 ,a1 ,a2,…ai-1. For examplea0 ,a1 ,a2,…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 a0 and a1 . 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 b0 ,b1 ,b2 can be defined recursively as follows recurrence relation initial condition Compute b2 ,b3 ,b4 Kavita Math231
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: • There must be certain arguments,called the base values, for which the function does not refer to itself. • 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) • IF n is 0 • Return 1 • ELSE • Return n* Factorial(n-1) Kavita Math231
N 4 N 3 N 2 N 1 N 0 Factorial(4) Kavita Math231
x n 2 3 x n 2 2 x n 2 1 • exponential (POWER) function XN • POWER(x,n) • IF n=1 • return x • ELSE • return x* POWER(x,n - 1) • POWER(2,3) Kavita Math231
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 Math231
the FIBONACCI sequence: • Fib(N) = Fib(N-1) + Fib(N-2) for n, and Fib(0) = Fib(1) = 1 • If n=0 or n =1, then Fn= 1. • 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) • IF n = 1, A0 = P(1+ r) • IF n > 1, Ak = Ak-1(1+ r) Kavita Math231
Example: Counting Strings • Addition rules • Let = {0, 1}. • Let ak be the number of strings in * of length k that do not contain 11. • a0 = 1, • a1 = 2, • a2 = 3. Kavita Math231
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 Math231
Example: Counting Strings • Therefore, • ak = ak – 1 + ak – 2, for all k 2. • The first few terms • a3 = a2 + a1 = 5, • a4 = a3 + a2 = 8, • a5 = a4 + a3 = 13. • k = 3: {000, 001, 010, 100, 101} Kavita Math231
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 Math231
Example: Counting r-Partitions • Let A = {a, b, c, d}. • a4, 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 Math231
Example: Counting r-Partitions • Consider an r-partition of a set A. • Let xA. • 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 Math231
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 rr-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 Math231
Example: Counting r-Partitions • Compute a4, 2. and a5, 2. • a4, 2 = a3, 1 + 2a3, 2 = 1 + 2(a2, 1 + 2a2, 2) = 1 + 2(1 + 2 1) = 7. • a5, 2 = a4, 1 + 2a4, 2 = 1 + 2 7 = 15. Kavita Math231
Section 8.2 Solving Recurrence Relations by Iteration Kavita Math231
Guessing the Answer • Write out the first several terms. • Look for a pattern. • Two strategies • Do the arithmetic. • Postpone the arithmetic. Kavita Math231
Example: Do the Arithmetic • Define {an} by • a1 = 2, • an = 2an – 1 – 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 = 2n – 1 + 1. Kavita Math231
Example: Postpone the Arithmetic • Define {an} by • a1 = 1, • an = 5an – 1 + 2, for all n 2. • Find a formula for an. • First few terms: 1, 7, 37, 187, 937. • What is an? Kavita Math231
Example: Postpone the Arithmetic • Calculate a few terms • a1 = 1. • a2 = 5 1 + 2. • a3 = 52 1 + 5 2 + 2. • a4 = 53 1 + 52 2 + 5 2 + 2. • a5 = 54 1 + 53 2 + 52 2 + 5 2 + 2. • It appears that, in general, • an = 5n – 1 + (5n – 2 + 5n – 3 + … + 1) 2. Kavita Math231
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 477example 8.2.2, 8.2.3, 8.2.4, 8.2.5 Page 452, Page 485 1-c, 2-b,c, 4, 17 Kavita Math231
Example: Postpone the Arithmetic • an = 5n – 1 + (5n – 2 + 5n – 3 + … + 1) 2 = 5n – 1 + (5n – 1 – 1)/(5 – 1) 2 = 5n – 1 + (5n – 1 – 1)/2 = (3 5n – 1 – 1)/2. Kavita Math231
Example: Future Value of an Annuity • Define {an} by • a0 = d, • an = (1 + r)an – 1 + d, for all n 1. • Find a formula for an. • a1 = (1 + r)d + d. • a2 = (1 + r)2d + (1 + r)d + d. • a3 = (1 + r)3d + (1 + r)2d + (1 + r)d + d. Kavita Math231
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 Math231
Verifying the Guess • Use mathematical induction to verify the guess. Kavita Math231
Verify the Guess • Define {an} by • a1 = 1, • an = 5an – 1 + 2, for all n 2. • Verify, by induction, the formula an = (3 5n – 1 – 1)/2. Kavita Math231
Verify the Guess • Basic Step (n = 1) • a1 = 1 • (3 51 – 1 – 1)/2 = (3 50 – 1)/2 = (3 – 1)/2 = 1. Kavita Math231
Verify the Guess • Inductive Step • Suppose ak = (3 5k – 1 – 1)/2 for some k 1. • Then ak + 1 = 5[(3 5k – 1 – 1)/2] + 2 = (3 5k – 5)/2 + 2 = (3 5k – 1)/2. • Therefore, it is true for all n 1. Kavita Math231
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 Math231
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 a0 ,a1 ,a2,… 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 Math231
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 Math231