Correctness of Algorithm
Correctness of Algorithm
Algorithm
Decide on : algorithm
design techniques etc.
Design an algorithm
Prove correctness
Prove the
correctness of
the algorithm
PRE-CONDITIONS
The predicate describing the initial state is
called the pre-condition of the algorithm.
It indicates what must be true before the
algorithm start.
Precondition: a predicate I on the input data
POST-CONDITIONS
The predicate describing the final state is
called the post-condition of the algorithm.
It indicates what will be true when the
algorithm finishes its work.
Post condition: a predicate O on the output
data
Department of Computer Science
Pre conditions and Post conditions
Precondition: and y = b
x = a and y = b 2. temp := x => temp
Postcondition:
=a
3. x : = y => x = b
x = b and y = a
4. y := temp => y = a
5. x = b and y = a is
Department of Computer Science
the Postcondition
Proving correctness
Correctness of
Algorithm
a: 19 5 12 7 a: 5 12 7 19
0 1 2 3 0 1 2 3
a: 5 19 12 7 a: 5 7 12 19
0 1 2 3 0 1 2 3
a: 5 12 19 7 a: 5 7 12 19
0 1 2 3 0 1 2 3
a: 5 12 7 19 a: 5 7 12 19
0 1 2 3 0 1 2 3
a: 5 12 7 19 a: 5 7 12 19
Department of Computer Science 0 1 30 2 3
0 1 2 3
Example 3: Bubble
Sort
| 89 45 68 90 29 34 17
17 | 45 68 90 29 34 89
17 29 | 68 90 45 34 89
17 29 34 45 | 90 68 89
17 29 34 45 68 | 90 89
17 29 34 45 68 89 | 90
In selection sort algorithm we find the minimum element
from the unsorted part and put it at the beginning.
| 89 45 68 90 29 34 17
17 | 45 68 90 29 34 89
| 89 45 68 90 29 34 17
17 | 45 68 90 29 34 89
89 | 45 68 90 29 34 17
45 89 | 68 90 29 34 17
45 68 89 | 90 29 34 17
45 68 89 90 | 29 34 17
29 45 68 89 90 | 34 17
29 34 45 68 89 90 | 17
17 29 34 45 68 89 90
89 | 45 68 90 29 34 17
45 89 | 68 90 29 34 17
45 68 89 | 90 29 34 17
45 68 89 90 | 29 34 17
29 45 68 89 90 | 34 17
29 34 45 68 89 90 | 17
17 29 34 45 68 89 90
Invariant: at the start of each for loop, A[1…j-1] consists of
elements originally in A[1…j-1] but in sorted order
Department of Computer Science
Example 6: Quick Sort:
Invariant:
1) All entries in A[p . . i] are ≤ pivot.
2) All entries in A[i + 1 . . j − 1] are > pivot.
3) A[r] = pivot.
Invariant:
If m is the integer represented by array b[1..k],
then n=t*2k+m
= (ik +1) y
= (ik+1).y
So, P(K+1) is true
Department of Computer Science
Induction Examples (4/4)Example 1: Multiplication
3.3 Mathematical Induct
1. Initialization:
In this step we must show that invariant is
true prior to the first iteration of the loop.
Algorithm
Proof: the list of actions
Sum_of_N_numbers applied to the
precondition imply the
Input: a, an array of N postcondition
numbers BUT: we cannot enumerate
Output: s, the sum of the N all the actions in case of
numbers in a a repetitive algorithm !
How to prove
s:=0; repetitive
k:=0; algorithms?
While (k<N) do We use techniques based
k:=k+1; on loop invariants and
s:=s+a[k]; induction
end Iterative algorithms: use
Loop invariants
Recursive algorithms: use
Department of Computer Science induction using as
Example 4
Loop invariant = induction
Algorithm hypothesis: At step k, S holds the
Sum_of_N_numbers
sum of the first k numbers
Input: a, an array of N 1. Initialization: The
numbers
hypothesis is true at
Output: s, the sum of
the N numbers in a the beginning of the
loop:
s:=0; Before the first iteration:
k:=0;
While (k<N) do s=0, k=0.
k:=k+1; The first 0 numbers have
s:=s+a[k]; sum zero (there are no
end
numbers)
=> hypothesis true before
entering the loop
Department of Computer Science
Example 4
DONE
a: 19 5 12 7 a: 5 12 7 19
0 1 2 3 0 1 2 3
a: 5 19 12 7 a: 5 7 12 19
0 1 2 3 0 1 2 3
a: 5 12 19 7 a: 5 7 12 19
0 1 2 3 0 1 2 3
a: 5 12 7 19 a: 5 7 12 19
0 1 2 3 0 1 2 3
a: 5 12 7 19 a: 5 7 12 19
Department of Computer Science 0 1 71 2 3
0 1 2 3
Example 7: Bubble
Sort
Proof
Basis step: When n =1, the list contains just one element and
hence is clearly sorted, So P(1) is true
Induction Step: Assume P(k) is true; that is, the algorithm sorts
correctly every list of k ( ≥ 1)
To show that P(k+1) is true, consider a list X = [x1, x2 x3, x4…. xk,
xk+1]
Since k+1 ≥ 2 , the for loop in line 1 is entered. When i =0, j
runs from 1 through n-2. Statements in line are executed: the
consecutive elements xj and xj+1 are compared and swapped if
out of order. The inner for loop places the largest of the
elements x1, x2 x3, x4…. xk, xk+1 in position k+1. This leaves a sub
list of k elements, [x1, x2 x3, x4…. xk]. By the inductive hypothesis,
the algorithm correctly sort it. It follows that the algorithm
correctly sorts the
Department of Computer entire list X; that is, P(k+1) is true. Thus, by
Science
Conclusion
Reasoning
Since a loop iterates of Proofof
a finite number
times, the form of mathematical
induction used here proves that a finite
sequence of statement is true, rather
than an infinite sequence of
statements.
Whether the sequence of statements is
finite or infinite, the steps needed for
the proof by the mathematical
induction are the same.
Review of
Mathemati
cal
Induction
In other n, (P(n)
[P(1) words, P(n+1))]
we showed n, P(n)
that:
Sk = 2 + 4 + 6 + 8 + ...
+ 2k = k(k + 1) Assumption
Example continues
Department of Computer Science 93
Example 1
Sk + 1 = 2 + 4 + 6 + 8 + ...
+ 2k + [2(k + 1)]
=2+4+6+8+ ...
+ 2k + (2k + 2)
= k2 + k + 2k + 2 Simplify.
= k2 + 3k + 2
= (k + 1)(k + 2)
= (k + 1)((k + 1)+1)
The formula Sn = n(n + 1) is valid for all positive integer
values of n.
Department of Computer Science 94
Quick survey
( 1)(2(1) 1) 1(2)(2 1) 6
11
S1 1 True
6 6 6
k (k 1)(2k 1)
S k 12 22 32 42 k 2 Assumptio
6 n
S k 1 12 22 32 42 k 2 (k 1)2
S k (k 1) 2
S k k 2 2k 1 Group terms to
form Sk.
k (k 1)(2k 1) Replace Sk by k(k
k 2 2k 1
6 + 1).
Department of Computer Science
Example continues.
96
Example 2
3 2 2
2k 3k k 6k 12k 6 Simplify.
6 6
3 2
2k 9k 13k 6
6
(k 2 3k 2)(2k 3)
6
(k 1)(k 2)(2k 3)
6
(k 1)[(k 1) 1][2(k 1) 1]
6
The formula S n(n 1)(2n 1) is valid for all positive
n
6
integer values of n.
Self
reading
of Computer Science
Department
Source : Times of india, July 2001
Significance and importance of algorithm
correctness
There are many situations where safety
to human life is involved, like in medical
applications, or industrial safety systems,
atomic power plants, manned space
flights, and so on.
In these applications, program/algorithm
correctness has to be guaranteed.
Imagine what will happen if the computer
inside Ghouri missile has an unreliable
program due to which the missile goes hay
wire – it may turn around and strike its own
city rather than the enemy site!
Correctness of
Algorithm
PRE-CONDITIONS
The predicate describing the initial state is
called the pre-condition of the algorithm.
It indicates what must be true before the
algorithm start.
Precondition: a predicate I on the input data
POST-CONDITIONS
The predicate describing the final state is
called the post-condition of the algorithm.
It indicates what will be true when the
algorithm finishes its work.
Post condition: a predicate O on the output
data
Department of Computer Science
Precondition/Postcondition
Types
PartialCorrectness
Total Correctness
To prove
input
partial correctness
we associate a number of assertions
(statements about the state of the
execution) with specific checkpoints in
the algorithm.
e.g., A[1], …, A[k] form an increasing
sequence
Department of Computer Science
Total Correctness
Conditional Statement
Case Statement
Repetitive statement
Examples
How to ensure correct termination of program/algorithm
Examples
Department of Computer Science
Checking the correctness of the
algorithm
Once an algorithm has been specified,
you have to prove its correctness which
is one of the difficult, but essential task
in algorithm development.
The correctness of algorithm means the
correctness of the logic/steps of the
algorithm.
Generally, algorithm will have many steps,
branching and loops, which are included in
different modules. One has to verify whether
each and every module is working for every
legitimate input in a finite amount of time.
The second way writing can be taken to mean “implication via S. This is a program specification with the
following meaning
(p B){S}q
(p B) q
-------------------------------------
p{if condition then S}q
(p B){S1}q
(p B){S2}q
----------------------------------------------
p{if condition then S1 else S2}q
(p condition){S}p
----------------------------------------------
p{while condition S}(condition p)
DYS
Here S2 is given by