3-Correctness of algorithms
3-Correctness of algorithms
3-Correctness of algorithms
Design
(Algorithm Correctness)
Algorithm Correctness
When an algorithm is designed it should be analyzed from the following two aspects:
1. Correctness : to verify if the algorithm leads to the solution of the problem or it always
produces the expected output for the range of inputs and it eventually terminates.
2. Efficiency: to analyze the performance of the algorithm
• Failure to find a counter example to a given algorithm does not mean “ it is oblivious
that the algorithm is correct. A proof or demonstration of correctness is needed . To
proof the correctness we can use mathematical induction is the method of choice.
• Mathematical induction is usually the right way to verify the correctness of recursive
and incremental algorithms
• For recursive algorithm we are using method of induction directly to prove the
correctness
• For iterative algorithm we are using loop variants and method of induction to prove
the correctness.
Algorithm Correctness
Correctness of recursive algorithms:-
To prove correctness of recursive algorithm:
• Prove it by induction on the size of the problem being solved
• Base of recursion is the base of induction
• Need to prove that recursive call are given subproblems that is no infinite recursion.
• Inductive step: assume that the recursive calls work correctly and use this
assumption to prove that the current call works correctly.
Question to prove
Example-1
The Fibonacci numbers are a sequence of integers in which the first two elements are 0
& 1, and each following elements are the sum of the two preceding elements:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ..., 233
Algorithm Correctness
Recursive Fibonacci number:
F0 =0 , F1= 1 and for all n >= 2 , Required to prove:
Fn = Fn-2 + Fn-1 fib(m+1) returns Fm+1
function fib(n) What does fib(m+1) returns ?
//comment returns Fn fib(m+1) returns
1. if n = = 0 then return 0; fib(m+1-1) + fib(m+1-2)
= fib(m) + fib(m-1)
2. else return(fib(n-1)+fib(n-2))
= Fm + Fm-1 by induction hypothesis
Claim: For all n >= 0, fib(n) return Fn
= Fm+1
Base : for n=0 fib(n) returns 0 F0 as claimed
Hence by applying induction we are
for n= 1 fib(n) returns 1 F1 as
claimed verified the correctness.
Induction Hypothesis :
suppose that n>= 2 and for all 0<=m <n
fib(m) returns Fm
Algorithm Correctness
Example-2 Let n=4 A[ 10, 12, 13 , 5,]
Maximum(4)
Recursive Maximum max(maximum(3),A[4})
function maximum(n)
//comment return max of A[1……n] maximum(3)--------max( maximum(2), A[3])
Claim:
For all n >=1, maximum(n) returns max{A[1],………..A[n]}
Base: for n=1, maximum(n) returns A[1] as claimed
Induction hypothesis: suppose that n >=1 and maximum(n) returns max{A[1],…….,A[n]}
Required to prove
maximum(n+1)-----returns max{A[1],………..,A[n+1]}
Algorithm Correctness suppose that n >=1 and isum(A,n) returns sum{A[1],…….,A[n]}
n+1 -------- isum(A,n+1) -- sum(A[1],A[2]….A[n+1])
isum(A,n+1)=sum(A[n+1]+isum(A,n))
What does maximum(n+1) returns ? =sum(A[n+1],sum(A[1]…A[n])
= sum(A[n+1],A[n]…..A[1])
max(maximum(n),A[n+1])
= max( max{A[1],……,A[n]},A[n+1]) by induction hypothesis
= max{A[1],A[2],……,A[n+1]} proved
function sum(n)
// Input: a non negative integer n
In this example:
//output: the sum 1+2+3………+n 1. The loop invariant holds initially since sum=0 and i=0 , if no
sum:=0 element sum is zero
i:=0 i =1 sum =0+1
i=2 sum =1+2
while(i<=n) i= n sum=1+2……+n
// invariant :sum 1+2 …+i-1 2. Invariant holds before ith iteration and also after this iteration
sum := sum + i since the loop adds i to the sum and increments i by 1
3. When loop terminates sum= 1+2+……+n
i=i+1
return sum
Algorithm Correctness
Question:-
function max(A)
//input :an array A storing n integers
//output: the largest element in A(max of A[1… n]
m=A[1]; i:=2 Consider an array A{7, 5, 3, 10, 2, 6} with 6
while i<=n do elements and we have to find maximum element
max in the array.
if A[i]>m then m:=A[i] In the above example after the 3rd iteration of
i=i+1 the loop max value is 7, which holds true for the
first 3 elements of array A. Here, the loop
return(m) invariant condition is that max is always
maximum among the first i elements of array A.
Algorithm Correctness
Example: Iterative Fibonacci Numbers
function fibo(n) Let n=5 then fib(5) should return F5=5
F2=f0+f1 j=1
//comment return Fn F3=f1+f2 j=2 n=0 0 1 2 3 4 5 6
1. if n==0 then return 0 Fj=Fj-1 +Fj Fn=0 0 1 1 2 3 5 8
Fj+1=Fj+Fj+1
2. else a:=0; b:=1;i:=2; F0 F1 F2 F3 F4 F5 F6
3. while(i<=n) do j =1 j=2 j=3 j=4 j=5
4. c:= a + b; a :=b; b:=c; i=i+1; Iteration0 Iteration1 Iteration2 Iteration3 Iteration4
5. return(b) j=0 j=1 j=2 j=3 j=4
Proof by induction.
Basis Step: n = 1. When n = 1, that is when the loop is entered the first time, F1 = F0* 1 = 1 and i1 = i0 + 1 = 2.
Induction Hypothesis: For an arbitrary value m of n, Fm = m! and im = m + 1 hold after going through the loop m times.
Inductive Step: for (m + 1) time, Fm+1 = (m+1)! and im+1 = (m+2)
Now, when the algorithm stops, i = n + 1. Hence the loop will have been entered n times. Thus F = n! is returned. Hence the algorithm is