Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

General proof strategies to show correctness of recursive functions?

I'm wondering if there exists any rule/scheme of proceeding with proving algorithm correctness? For example we have a function $F$ defined on the natural numbers and defined below:

function F(n,k)
begin
  if k=0 then return 1
  else if (n mod 2 = 0) and (k mod 2 = 1) then return 0
  else return F(n div 2, k div 2);
end;

where $n \ \text{div}\ 2 = \left\lfloor\frac{n}{2}\right\rfloor$

the task is to prove that $F(n,k)= \begin{cases} 1 \Leftrightarrow {n \choose k} \ \text{mod} \ 2 = 1 \ 0 \text{ otherwise } \end{cases} $

It does not look very complicated (am I wrong?), but I don't know how does this kind of proof should be structured. I would be very grateful for help.

like image 803
xan Avatar asked Mar 02 '12 18:03

xan


People also ask

How do you prove the correctness of recursive algorithms?

To prove the correctness of a recursive algorithm we use mathematical induction. In a mathematical induction we want to prove a statement P(n) for all natural numbers n (possibly starting at an n0, but lets assume for simplicity we are proving the statement for all n≥1).

How do you write proof of correctness?

One way to prove the correctness of the algorithm is to check the condition before (precondition) and after (postcondition) the execution of each step. The algorithm is correct only if the precondition is true, and then the postcondition must also be true.

Is often used to prove the correctness of a recursive function?

What technique is often used to prove the correctness of a recursive function? Mathematical induction.

How do you prove a function is recursive?

Basis Step: Prove that the statement holds for all elements specified in the basis step of the set definition. Recursive Step: Prove that if the statement is true for each of the elements used to construct elements in the recursive step of the set definition, then the result holds for these new elements.


2 Answers

Correctness of recursive algorithms is often proven by mathematical induction. This method consists of two parts: first, you establish the basis, and then you use an inductive step.

In your case, the basis is all cases when k=0, or when k is odd but n is even.

The inductive step requires proving that when f(n,k) is correct, f(2*n,2*k), f(2*n+1,2*k), f(2*n,2*k+1) and f(2*n+1,2*k+1) are all correct.

like image 183
Sergey Kalinichenko Avatar answered Sep 22 '22 16:09

Sergey Kalinichenko


Outside of proving your logic mathematically (example: inductive proof), there are some results in computing science related to this.

You can start here for an outline of the subject: Correctness
For your particular case, you'd be interested in partial correctness to show that the answer is the intended one. Then total correctness to show that the program terminates.

Hoare logic can solve your partial correctness.

As for the termination for this particular problem:

If (n%2==0 and k%1==1) or (k==0) the program terminates, otherwise it recurses to the n/2, k/2 case.
Using strong induction on k, we can show that the program always reaches one of the terminal nodes where k==0. (It may terminate earlier on the first clause but we only needed to show that it terminates at all, which this does)

So I've left the proof of partial correctness to you (because I don't know that stuff)

like image 42
Jean-Bernard Pellerin Avatar answered Sep 19 '22 16:09

Jean-Bernard Pellerin