- Base case: Show the statement holds for a value of n (usually n=1 or n=0).
- Inductive step: Assuming the statement holds for any value of n then show it also holds for a value of n+1
Base case: Show the statement holds for n=0
0=0(0+1)/2This equation clearly holds.
Inductive step: Assuming the statement holds for any value of n then show it will also hold for n+1.
0+1+2...n+(n+1) = ((n+1)((n+1)+1)/2If we assume that the statement holds for n then we can reduce the expression.
(n(n+1))/2 + (n+1) = ((n+1)((n+1)+1)/2Using some basic algebra we can reduce both sides and discover that the inductive step holds.
Now we can apply same (perhaps slightly tweaked) rules to writing a recursive algorithm for finding the length of a linked-list (a recursive data-structure).
Let's start out with the function signature.
int length(Node *node) { // Do stuff here }Base case: Write the algorithm for n=0 (the linked-list with 0 nodes).
int length(Node *node) { if (NULL == node) { return 0; } // Do more stuff here }Congratulations! If you've make it this far you're above average. You've solved the base case without dumping core.
Now onto the inductive step: Assume that your length function will work for lists of length n now write code to make it work for lists of length n + 1.
int length(Node *node) { if (NULL == node) { return 0; } else { return 1 + length(node->next); } }Notice that if we assume that "length(...)" works for lists of length n then for lists of length n + 1 all we need to do is add 1 to the length. You should also notice that we passed node->next to the "length(...)" function not just node because rule 2.5 states that the inductive step must reduce the problem space and just passing node would not reduce the problem space. Congratulations! If you've make it this far you're now well above average. Now not only will your algorithm not core dump, but it won't hang either.
So let's review what we've done. We've used the rules for inductive proofs to guide us in writing a recursive algorithm. Does this mean we've proved that this algorithm is correct? The answer is, not really or at least, not formally. Even though we haven't proved this algorithm is correct we can be much more confident that it will work.
If we were to write unit-tests for this algorithm (which you should *always* do before writing the algorithm) we could use the rules of induction to guide our unit tests. We could write a test for testing the base case and another test for testing the inductive case.
Since math has been around longer than computers, it makes sense that we could borrow a few ideas from it to make our jobs easier.
This comment has been removed by a blog administrator.
ReplyDelete