Table of Contents | Proof Methods | Glossary
Induction and recursion are very closely related and understanding one can help with understanding the other. This most often arises in proofs of correctness for recursive algorithms. (e.g. Quicksort) We start by illustrating this with a simpler recursive algorithm: Factorial.
As you probably remember from a long ago math class, n! of some integer n can be characterized as:
n! = 1 for n = 0; otherwise
or in other words
n! = n (n - 1)! (since n (n - 1)! = (n [(n - 1) (n - 2)...(1)] )
Now you can devise a recursive algorithm to compute this recurrence.
An Algorithm for n!
Fact (N)
if (N == 0) then
return 1
else
return (Fact <= N * Fact (N-1))
But what if you had to prove that your recursive algorithm worked? One way of checking this is to do an example and plug in some numbers.
Algorithm |
| Fact(10) |
| 10 * Fact(9) |
| | 9 * Fact (8) |
| | | 8 * Fact (7) |
| | | | | .............. |
| | | | | | | | 2 * Fact(1) |
| | | | | | | | | 1 * Fact(0) |
| | | | | | | | | | | 1 * 1 |
| | | | | | | | | | 2 * 1 * 1 |
| | | | | | | | | ............... |
| | | 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 * 1 |
| | 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 * 1 |
| 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 * 1 |
Why is the recurrence returning the correct answer? Well we know that the following is true by definition.
We also know that:
Then:
and
Fact(3) = 3 * Fact(2) = 3* (2) = 6 = 3!
Because the level preceding 3! was correct, we don't need to check the full multiplication again when we calculate the 4th level, since 4 * Fact(3) = 4 * 3! = 4! and we know the 3rd level correctly calculates 3!.
In
fact, we know that this repetition will be true all the way up to the 10th
level.
But now you want to establish that this recursion works, not only for numbers 1..10 but for any positive integer n. This is important because it would be annoying to find out that your algorithm actually breaks at the number 5,730,526. By showing it true for all n, you can gurantee this won't happen.
This is where our intuition about how a recursive function like Factorial works, comes in handy. By tracing the function, we have learned how it builds an answer up. This is very similar to building the row of dominoes needed for the inductive proof. What the trace allows us to see is that the m + 1th case is just a simple function of the mth case:Fact(m+1) = (m+1) Fact(m) (Look at the trace if this isn't clear.)
Substituting the inductive hypothesis gives us
Fact(m+1) = (m+1) m! = (m+1)!
5. Therefore Fact(n) returns n!.
Top of Page | Table of Contents | Proof Methods | Glossary1