Download presentation
Presentation is loading. Please wait.
1
Kavita Math231 Recursion and Iteration
2
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. T o 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 a k to certain of it predecessors a k-1, a k-2,…, a k-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,…a i-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 initial conditionCompute b 2,b 3,b 4
3
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: 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 Return 1 ELSE Return n* Factorial(n-1)
4
Kavita Math231 Factorial(4) N4N4 N3N3 N2N2 N1N1 N0N0
5
Kavita Math231 exponential (POWER) function X N 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 x n 2 1
6
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
7
Kavita Math231 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 F n = 1. 2.If n > 1, then F n = F n-2 + F n-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, A k = A k-1 (1+ r)
8
Kavita Math231 Example: Counting Strings Addition rules Let = {0, 1}. Let a k be the number of strings in * of length k that do not contain 11. –a 0 = 1, –a 1 = 2, –a 2 = 3.
9
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.
10
Kavita Math231 Example: Counting Strings Therefore, –a k = a k – 1 + a k – 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, 100, 101}
11
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 a n, r be the number of distinct r-partitions of A. Special cases –a n, n = 1 for all n 1. –a n, 1 = 1 for all n 1.
12
Kavita Math231 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}}
13
Kavita Math231 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}.
14
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 r r-partitions of A. Therefore, a n, r = a n – 1, r – 1 + r a n – 1, r, for all n 1 and for all r, 1 < r < n.
15
Kavita Math231 Example: Counting r-Partitions Compute a 4, 2. and a 5, 2. –a 4, 2 = a 3, 1 + 2a 3, 2 = 1 + 2(a 2, 1 + 2a 2, 2 ) = 1 + 2(1 + 2 1) = 7. –a 5, 2 = a 4, 1 + 2a 4, 2 = 1 + 2 7 = 15.
16
Kavita Math231 Section 8.2 Solving Recurrence Relations by Iteration
17
Kavita Math231 Guessing the Answer Write out the first several terms. Look for a pattern. Two strategies –Do the arithmetic. –Postpone the arithmetic.
18
Kavita Math231 Example: Do the Arithmetic Define {a n } by –a 1 = 2, –a n = 2a n – 1 – 1, for all n 2. Find a formula for a n. First few terms: 2, 3, 5, 9, 17, 33, 65. Compare to: 1, 2, 4, 8, 16, 32, 64. Guess that a n = 2 n – 1 + 1.
19
Kavita Math231 Example: Postpone the Arithmetic Define {a n } by –a 1 = 1, –a n = 5a n – 1 + 2, for all n 2. Find a formula for a n. First few terms: 1, 7, 37, 187, 937. What is a n ?
20
Kavita Math231 Example: Postpone the Arithmetic Calculate a few terms –a 1 = 1. –a 2 = 5 1 + 2. –a 3 = 5 2 1 + 5 2 + 2. –a 4 = 5 3 1 + 5 2 2 + 5 2 + 2. –a 5 = 5 4 1 + 5 3 2 + 5 2 2 + 5 2 + 2. It appears that, in general, –a n = 5 n – 1 + (5 n – 2 + 5 n – 3 + … + 1) 2.
21
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 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
22
Kavita Math231 Example: Postpone the Arithmetic a n = 5 n – 1 + (5 n – 2 + 5 n – 3 + … + 1) 2 = 5 n – 1 + (5 n – 1 – 1)/(5 – 1) 2 = 5 n – 1 + (5 n – 1 – 1)/2 = (3 5 n – 1 – 1)/2.
23
Kavita Math231 Example: Future Value of an Annuity Define {a n } by –a 0 = d, –a n = (1 + r)a n – 1 + d, for all n 1. Find a formula for a n. –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.
24
Kavita Math231 Example: Future Value of an Annuity It appears that, in general, a n = (1 + r) n d + … + (1 + r)d + d = d((1 + r) n + 1 – 1)/((1 + r) – 1) = d((1 + r) n + 1 – 1)/r.
25
Kavita Math231 Verifying the Guess Use mathematical induction to verify the guess.
26
Kavita Math231 Verify the Guess Define {a n } by –a 1 = 1, –a n = 5a n – 1 + 2, for all n 2. Verify, by induction, the formula a n = (3 5 n – 1 – 1)/2.
27
Kavita Math231 Verify the Guess Basic Step (n = 1) –a 1 = 1 –(3 5 1 – 1 – 1)/2 = (3 5 0 – 1)/2 = (3 – 1)/2 = 1.
28
Kavita Math231 Verify the Guess Inductive Step –Suppose a k = (3 5 k – 1 – 1)/2 for some k 1. –Then a k + 1 = 5[(3 5 k – 1 – 1)/2] + 2 = (3 5 k – 5)/2 + 2 = (3 5 k – 1)/2. Therefore, it is true for all n 1.
29
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),
30
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 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.
31
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.