












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!













Introduction Recursion is a function invoking itself, either directly or indirectly. Many algorithms and mathematical properties are naturally expressed in a recursive manner. It can be used as an alternative to iteration. Recursion is an important and powerful tool in problem solving and programming. It is a programming technique that naturally implements the divide-and-conquer problem solving methodology. Some programming languages are inherently recursive (Lisp, Scheme). In its simplest form the idea of recursion is straightforward: Example 1: Simple countdown timer
Example 2: Multiplication of natural numbers. a * b = a added to itself b times. (iterative definition) a * b = a if b= (recursive definition) _a b = a * (b-1) + a_* if b > 1 e.g. 6 * 3 = 6 * 2 + 6 = 6 * 1 + 6 + 6 = 6 + 6 + 6 = 18 Example 3: Factorial Function
The Nature of Recursion One or more simple cases of the problem (called the stopping cases ) have a simple non-recursive solution. The other cases of the problem can be reduced ( using recursion ) to problems that are closer to stopping cases. Eventually the problem can be reduced to stopping cases only, which are relatively easy to solve. In general: if (stopping case) solve it else reduce the problem using recursion Tracing a Recursive Function Computer uses a stack to keep track of function calls. Whenever a new function is called, all its parameters and local variables are pushed onto the stack along with the memory address of the calling statement (this gives the computer the return point after execution of the function).
Example 1: Tracing the function multiply
Example 3: Trace of palindrome: for input abc
Example 4: Fibonacci Sequence Fibonacci numbers are named in honor of Leonardo Pisano (Leonardo of Pisa), the son of Bonaccio, (which in Latin is Filius Bonaccii) who discovered the series in 1202. The original problem that Fibonacci investigated (in the year 1202) was about how fast rabbits could breed in ideal circumstances. Suppose a newly-born pair of rabbits, one male, one female, are put in a field. Rabbits are able to mate at the age of one month so that at the end of its second month a female can produce another pair of rabbits. Suppose that our rabbits never die and that the female always produces one new pair (one male, one female) every month from the second month on. The puzzle that Fibonacci posed was...How many pairs will there be in one year? (answer appears on last page of the notes)
Proof by Induction Induction proofs are most often used to establish theorems that hold for positive integers. That is, you can establish the validity of a conjecture such as: n 0, and n integer numbers, it is true that (^)
n i 0 i 2 n( n 1 ) by showing that the conjecture is true for one or more base values of n (generally, n = 0 is sufficient – though not always). Once this base case is proven true, you assume that the conjecture is true for all values of n from the base case through m , where m is an arbitrary integer greater than or equal to the largest value of n covered in the base case; this is the inductive hypothesis. Finally, using the assumption of the inductive hypothesis you prove that the conjecture is true for the next value of n (i.e., m + 1). This final step is the induction step. To summarize: An induction proof consists of three parts:
n i 1 i 2 n( n 1 ) Basis : n=1, by definition (^)
n i 1 i (^)
1 i 1 i (^) 1, by substitution 1 i 1 i 2 1 ( 1 1 ) = 2 2 = 1 So the base case is true!
Inductive Hypothesis : n=k, assume n i 1 i 2 k( k 1 ) is true. Inductive Step : prove conjecture is true for n = k+ Must prove that: k 1 i 1 i 2 (k 1 )[(k 1 ) 1 ] = 2 (k 1 )(k 2 ) Note that k 1 i 1 i (^) k i 1 i (^) + (k +1) = 2 k( k 1 )
2 k
2 2 k
2 2 2 k^2
2 k
2 2 k
2 2 = 2 k^2 3 k 2 = 2 (k 1 )(k 2 ) Thus the proof is completed and our conjecture is true for all integer numbers. At first glance, a proof by induction appears to be a circular proof, in that you establish a result by assuming that it is correct. However, an induction proof is not a circular proof for the same reason that a recursive definition is not circular. A correct proof by induction has an induction base that is similar to the base case of a recursive definition. The induction step proves the correctness using the correctness for smaller values of n. Repeated application of the induction step reduces the proof to one that is solely in terms of the base. Common Errors with Recursion
Problem Solving with Recursion The Towers of Hanoi Problem : involves moving a specified number of disks (N) that are all different sizes from one tower to another. The goal is to move all disks from tower A to C subject to the following rules:
Towers of Hanoi: Solution
Test: tower(3,‘A’,’C’, ’B’); Output: Move from A to C Move from A to B Move from C to B Move from A to C Move from B to A Move from B to C Move from A to C Practice Problems
Answer to Fibonacci’s Rabbit Puzzle