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.
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).
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.
What technique is often used to prove the correctness of a recursive function? Mathematical induction.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With