Can someone explain mathematical induction to prove a recursive method? I am a freshmen computer science student and I have not yet taken Calculus (I have had up through Trig). I kind of understand it but I have trouble when asked to write out an induction proof for a recursive method.
views:
879answers:
3First, you need a base case. Then you need an inductive step that holds true for some step n. In your inductive step, you will need an inductive hypothesis. That hypothesis is the assumption that you needed to have made. Finally, use that assumption to prove step n+1
Here is a explanation by example:
Let's say you have the following formula that you want to prove:
sum(i | i <- [1, n]) = n * (n + 1) / 2
This formula provides a closed form for the sum of all integers between 1
and n
.
We will start by proving the formula for the simple base case of n = 1
. In this case, both sides of the formula reduce to 1
. This in turn means that the formula holds for n = 1
.
Next, we will prove that if the formula holds for a value n
, then it holds for the next value of n
(or n + 1
). In other words, if the following is true:
sum(i | i <- [1, n]) = n * (n + 1) / 2
Then the following is also true:
sum(i | i <- [1, n + 1]) = (n + 1) * (n + 2) / 2
To do so, let's start with the first side of the last formula:
s1 = sum(i | i <- [1, n + 1]) = sum(i | i <- [1, n]) + (n + 1)
That is, the sum of all integers between 1
and n + 1
is equal to the sum of integers between 1
and n
, plus the last term n + 1
.
Since we are basing this proof on the condition that the formula holds for n
, we can write:
s1 = n * (n + 1) / 2 + (n + 1) = (n + 1) * (n + 2) / 2 = s2
As you can see, we have arrived at the second side of the formula we are trying to prove, which means that the formula does indeed hold.
This finishes the inductive proof, but what does it actually mean?
- The formula is correct for n = 0.
- If the formula is correct for
n
, then it is correct forn + 1
.
From 1 and 2, we can say: if the formula is correct for n = 0, then it is correct for 0 + 1 = 1
. Since we proved the case of n = 0
, then the case of n = 1
is indeed correct.
We can repeat this above process again. The case of n = 1
is correct, then the case of n = 2
is correct. This reasoning can go ad infinitum; the formula is correct for all integer values of n >= 1.
induction != Calc!!!
I can get N guys drunk with 10*N beers.
Base Case: 1 guy
I can get one guy drunk with 10 beers
Inductive step, given p(n) prove p(n + 1)
I can get i guys drunk with 10 * i beers, if I add another guy, I can get him drunk with 10 more beers. Therefore, I can get i + 1 guys drunk with 10 * (i + 1) beers.
p(1) -> p(i + 1) -> p(i + 2) ... p(inf)
Discrete Math is easy!