415 lecture note #4
procedure foo(n) 1. if n satisfy some condition then // base case 2. return (possibly a value) 3. else 4. (possibly do some processing) 5. return (possibly some processing using) 6. foo(n-1) // recursive call
1! = 1 2! = 1 * 2 = 1! * 2 3! = 1 * 2 * 3 = 2! * 3 n! = 1 * 2 * 3 * .. * (n-1) * n = (n-1)! * n
Input: A positive integer n >= 0
Output: n!procedure factorial(n) 1. if n = 0 then 2. return 1 // base case, 0! = 1 3. return n * factorial(n-1) // recursive call
Trace of procedure calls when n = 3
factorial(3)
factorial(2)
factorial(1)
factorial(0)
return 1
return 1 * 1
= 1
return 2 * 1 = 2
return 3 * 2 = 6
With an initial amount of $100 and 22 % annual interest, the amount at the end of the nth year, An, is expressed recursively as:
procedure compound_interest(n, a, r) 1. if (n == 0) then 2. return a 3. return ((1 + r) * compound_interest(n-1, a, r)) end compound_interest
gcd(a, b) = gcd(b, a mod b), b > 0
Recursive algorithm for gcd:
Input: Integers a, b
Output: gcd(a, b)
procedure recursive_gcd(a, b) 1. if a < b then 2. swap(a, b) 3. if b = 0 then // base case 4. return a 5. return recursive_gcd(b, a mod b) // recursive call
Proof of Correctness using Induction
procedure factorial(n) 1. if n = 0 then 2. return 1 // base case, 0! = 1 3. return n * factorial(n-1) // recursive call
Theorem: The algorithm factorial(n) outputs the value of n!, where n >= 0.
Proof:
Basic Step (n = 0): In line 2, 0! = 1 is correctly outputted.
Inductive Step: Assume the algorithm correctly computes (n-1)! for factorial(n-1). Then for factorial(n), line 3 computes n * (n-1)! = n!, which is the correct value for factorial(n).
NOTE: The proof above uses "strong mathematical induction", which is defined as follows.
Principle of Strong Mathematical Induction: Let P(n) be a predicate that is defined for integers n, and let a and b be fixed integers with a <= b. Suppose the following two statements are true:
- P(a), P(a+1),..,P(b) are all true.
- For all integers k >= b, if P(i) is true for all integers i with a <= i < k, then P(k) is true.
Then the statement "For all integers n >= a, P(n)" is true.
Notice here
- k >= b >= a. So when b != a, the inductive step starts from some number bigger than a.
- P(k) -- not P(k+1) -- depends on all of P(a), P(a+1),.., P(b), P(b+1),..P(k-1). In other words, the inductive hypothesis assumes PREVIOUS terms.
More examples of recursive algorithms
f1 1 f2 2 f3 f2 + f1 = f4 f3 + f2 = f5 f4 + f3 =
Recursive algorithm for Fibonocci:
Input: A positive integer n >= 1
Output: fn
procedure fibonocci(n)
There are 3 pegs, and n number of disks of various size are stacked in a peg, from the smallest disk at the top to the largest disk at the bottom. How many moves are required to transfer those disks to another peg, if a disk can only be moved one at a time and a smaller disk can only be placed on a bigger disk???
!! Web link to a Tower of Hanoi Puzzle !!
Cn, the number of moves required to transfer n disks is expressed by a recursive relation:
, where
1) Cn-1 moves to transfer the top n-1 disks to the intermediate peg (say peg 2);
2) 1 move to transfer the bottom disk to the destination peg (say peg 3);
3) Cn-1 moves to transfer n-1 disks from the intermediate peg to the destination peg.