0

I have an algorithm with a for loop that goes from i=n-1 to 1. I'm attempting to prove it's correctness.

Am I allowed to use induction like so

step 1 -- base case i = n

step 2 -- induction hypothesis, assume it works for i = n + 1

step 3 -- prove (using induction hypothesis) it works for i <= n + 1

  • Your step 3 isn't what people usually call induction. It looks more like a full proof without induction. – Tunococ Oct 12 '15 at 20:02
  • 1
    Base case: true for n. Assumption: true for i. Prove it is true for i -1. You may need to make assumptions true for n and n > 0. Assume true for i > 0 then prove for i-1 is i-1 > 0. – fleablood Oct 12 '15 at 20:18
  • You should probably edit your question so that step 1 says "base case i = 1 (or some constant), Induction Hypothesis says "assume it works for i <= n, and step 3 says "prove, using IH, that it works for i = n + 1. – BrianO Oct 13 '15 at 05:29
  • I don't think I really gave enough for an answer. Just advise. I'm also not sure of what your problem is. If you are trying to see if your loop will work of any number of n. You should start with i = 1 loop and work your way up. Backwards induction is possible but its not as common as forward. – fleablood Oct 13 '15 at 05:58
  • Once I wanted to show that between prime p and 2p there is a second prime q s.t. p < q < 2p. I figured if that were not so then it would also not be so for the last prime smaller than p. (I was mistaken.). Then by induction it would be true for the last prime smaller than that all the way down to the first case of p = 2 which I could show directly that 2<3<2$*$2 thus all my previous assumptions were wrong. So that was induction that worked backwards. (Or would have been had I been able to show it. The idea of downward induction was sound, although my math wasn't.) – fleablood Oct 13 '15 at 06:04

1 Answers1

1

As you haven't said anything about the nature of your algorithm, it's difficult to answer in a way you'll definitely find useful. But I'll attempt it, at this level of generality.

One tool you may find useful is a downward form of induction, as suggested by Nex:

For $n \in \mathbb{N}$ and statements $S_i, 1 \le i \le n$, if $S_n$ holds and so does the implication $S_{i+1} \to S_i$ for $1 < i \le n$, then $S_i$ holds for all $i \in \{1, \dots ,n\}$.

Proof: If not, then there is a greatest $i_{max} \le n$ where $S_{i_0}$ is false. We must have $i_0 < n$, as $S_n$ is true; so $i_0 + 1 \le n$. Because $i_0$ is greatest, $S_{i_0 + 1}$ is true. But $S_{i_0 + 1} \to S_{i_0}$, so $S_{i_0}$ after all.

When trying to prove a program correct, the usual approach a la Dijkstra is to attach assertions to points before and after statements or blocks of statements (preconditions and postconditions), such that the assertions are true for every execution path through the program, and such that every exit point is covered by an assertion.

When proving correctness of a loop, you want to find a precondition and a postcondition for the entire loop, as well as loop invariants – a precondition and a postcondition for the body the loop – such that at the end of the last iteration, the body postcondition implies (or just is the same thing as) the loop postcondition. For your situation, you might set this up schematically as follows (for simplicity I've change your loop limit from n-1 to n; assertions are shown within curly braces):

{loop-precond}  # assertions about initial state of in & out variables, etc. 
for i = n to 1:
    {S(i+1, n) holds}
    *body of loop*
    {S(i, n) holds} 
{S(i, n) holds for 1 ≤ i ≤ n}

The loop-invariant assertions need n as a parameter: for example, the body of the loop might process an array a[1:n] in reverse order, and S(i, n) might assert something about what has been done with a[i:n]. Assume that S(n+1, n) holds, probably trivially/vacuously.

You want to prove that the loop postcondition {S(i, n) holds for 1 ≤ i ≤ n} holds after the loop has executed. To do this, show that for any i ≤ n, if S(i+1, n) holds before the body executes, then S(i, n) holds after it executes. Because S(n+1, n) is vacuous, you may also have to show as a special case that S(n, n) holds after the first time through the loop. Now you can conclude from to the "downward induction" principle stated above that the loop postcondition holds – i.e. that the loop is "correct", whatever correctness means for your purposes.

BrianO
  • 16,579
  • -1 because....? – BrianO Oct 12 '15 at 20:29
  • It is not wrong, it's correct. If $n =1$ then the loop body won't execute, and it's "correct". If $n = 2$, then the loop body will run once; you have to prove that it does so when $i = n-1 = 1$; having done that, you can conclude from the IH that it's correct when $n = 2$. When $n=3$, proof of the induction step will show that the body of the loop is correct for $i = n-1 = 2$, and then by the IH that the entire loop is true for $n$. This is how you reason about loops with decreasing numeric loop variables. – BrianO Oct 13 '15 at 04:11
  • Example - naively reverse a string in, result in out. For simpicity let's also just use n not n-1 . Unfortunately can't indent in comments, so: out = ''; n = len(in); for i in range(n): out = in[i] + out. The loop invariant is: out == s[i+1:n] reversed. Without seeing just what the algorithm is, I'll grant that maybe my answer oversimplifies; but then, so does the question. – BrianO Oct 13 '15 at 04:24
  • I am sorry I didn't read carefully enough. However part of your answer is slightly wrong. It is possible to do an induction type argument "nearly" as described above. Precisely: For $n \in \mathbb{N}$ and some statements $S_i$ , $1\leq i \leq n$, if $S_n$ holds and so does the implication $S_{i+1} \Rightarrow S_i$ for $1<i \leq n$, then $S_i$ holds for all $i \in {1,..,n}$. Perhaps you could edit the first part of your answer to something like "Although it is not impossible to make precise what you are suggesting a more common pattern is as follows" so I can remove the downvote. – Nex Oct 13 '15 at 05:21
  • @Nex Sure, more can and should be said. the "downward induction" you mention belongs in the answer, as does some of what I said in my previous comment. Stay tuned... – BrianO Oct 13 '15 at 05:25
  • @Nex See if that's less sketchy. I redid the answer. – BrianO Oct 13 '15 at 06:41