Algo - Lec3 - Verifying Correctness of Algorithm PDF
Algo - Lec3 - Verifying Correctness of Algorithm PDF
Lecture 3:
Verification of the correctness of algorithms
Tauseef Iftikhar
Algorithm Analysis
Basic Notions
Precondition and Postcondition
Algorithm State
Assertions and Annotation
Algorithm Analysis
Basic Notions
Precondition and Postcondition
Algorithm State
Assertions and Annotation
Algorithm Analysis
Basic Notions
Precondition and Postcondition
Algorithm State
Assertions and Annotation
Algorithm 8 Solve ax = b
1: procedure MyProcedure(real a, b)
2: real x . a = a0 , b = b0 , x = undefined
3: x b/a . a = a0 , b = b0 , x = b0 /a0
4: return x
Todays Agenda
Algorithm Analysis
Basic Notions
Precondition and Postcondition
Algorithm State
Assertions and Annotation
Algorithm 13 Minimum1
1: procedure minimum(real a, b, c) . a <> b, b <> c, c <> a
2: if a < b then .a<b
3: if a < c then
4: m a . a < b, a < c, m = a, m = min(a, b, c)
5: else
6: m c . a < b, c < a, m = c, m = min(a, b, c)
7: else .b<a
8: if b < c then
9: mb . b < a, b < c, m = b m = min(a, b, c)
10: else
11: mc . b < a, c < b, m = c m = min(a, b, c)
return m
Example: Assertions and Annotation
Algorithm 15 Minimum2
1: procedure minimum(real a, b, c) . a <> b, b <> c, c <> a
2: ma .m=a
3: if m > b then
4: mb . m a, m b
5: if m > c then
6: mc . m a, m b, m c
7:
return m
Basic steps in correctness verification
I P: the precondition
I Q: the postcondition
I A: the algorithm
The triple (P, A, Q) denote a correct algorithm if for input data
which satisfy the preconditions P the algorithm will:
I lead to postconditions Q
I stop after a finite number of processing steps
A
P
Q
Rules for correctness verification
Algorithm Analysis
Basic Notions
Precondition and Postcondition
Algorithm State
Assertions and Annotation
Structure
A:
{P0 }
A1
{P1 }
..
.
Sequential Statements Rule
Let we have algorithm A with sequence of actions A1 , A2 , . . . , An
Structure
A:
{P0 }
A1
{P1 }
..
.
{Pi1 }
Ai
{Pi }
..
.
Sequential Statements Rule
Let we have algorithm A with sequence of actions A1 , A2 , . . . , An
Structure
A:
{P0 }
A1
{P1 }
..
.
{Pi1 }
Ai
{Pi }
..
.
{Pn1 }
An
{Pn }
Sequential Statements Rule
Let we have algorithm A with sequence of actions A1 , A2 , . . . , An
Structure
A:
Rule
{P0 }
If
A1
{P1 } P P0
..
.
{Pi1 }
Ai
{Pi }
..
.
{Pn1 }
An
{Pn }
Sequential Statements Rule
Let we have algorithm A with sequence of actions A1 , A2 , . . . , An
Structure
A:
Rule
{P0 }
If
A1
{P1 } P P0
.. Ai
. Pi1 Pi ,
{Pi1 } wherei = 1, . . . , n
Ai
{Pi }
..
.
{Pn1 }
An
{Pn }
Sequential Statements Rule
Let we have algorithm A with sequence of actions A1 , A2 , . . . , An
Structure
A:
Rule
{P0 }
If
A1
{P1 } P P0
.. Ai
. Pi1 Pi ,
{Pi1 } wherei = 1, . . . , n
Ai Pn Q
{Pi }
.. then
.
{Pn1 }
An
{Pn }
Sequential Statements Rule
Let we have algorithm A with sequence of actions A1 , A2 , . . . , An
Explanation
Structure If
A: I the precondition
Rule
{P0 } implies the initial
If
A1 assertion,
{P1 } P P0
..
I each action
. Ai
Pi1 Pi , implies the next
{Pi1 } wherei = 1, . . . , n assertion
Ai Pn Q I the final
{Pi } assertion implies
.. then
. the
{Pn1 } A post-condition
P Q
An then
{Pn } the sequence is
correct
Example of Sequential Statement Rules
variant 1
{x = a, y = b, aux = undefined}
Example of Sequential Statement Rules
variant 1
{x = a, y = b, aux = undefined}
aux x
Example of Sequential Statement Rules
variant 1
{x = a, y = b, aux = undefined}
aux x
{x = a, y = b, aux = a}
x y
Example of Sequential Statement Rules
variant 1
{x = a, y = b, aux = undefined}
aux x
{x = a, y = b, aux = a}
x y
{x = b, y = b, aux = a}
y aux
Example of Sequential Statement Rules
variant 1
{x = a, y = b, aux = undefined}
aux x
{x = a, y = b, aux = a}
x y
{x = b, y = b, aux = a}
y aux
{x = b, y = a, aux = a} Q
Example of Sequential Statement Rules
variant 1 variant 2
{x = a, y = b, aux = undefined} {x = a, y = b}
aux x x x +y
{x = a, y = b, aux = a} {x = a + b, y = b}
x y y x y
{x = b, y = b, aux = a} {x = a + b, y = a}
y aux x x y
{x = b, y = a, aux = a} Q {x = b, y = a} Q
Todays Agenda
Algorithm Analysis
Basic Notions
Precondition and Postcondition
Algorithm State
Assertions and Annotation
Structure
A:
{P0 }
If c then
{c, P0 }
A1
{P1 }
Conditional Statement Rule
Structure
A:
{P0 }
If c then
{c, P0 }
A1
{P1 }
Else
{NOTc, P0 }
A2
{P2 }
EndIf
Conditional Statement Rule
Rule
If
Structure
A: I c is well defined
{P0 } I c AND
If c then A1
P0 P1
{c, P0 } I P1 Q
A1
{P1 } OR
Else
{NOTc, P0 }
A2
{P2 }
EndIf
Conditional Statement Rule
Rule
If
Structure
A: I c is well defined
{P0 } I c AND
If c then A1
P0 P1
{c, P0 } I P1 Q
A1
{P1 } OR
Else I NOT c AND
A2
{NOTc, P0 } P0 P2
A2 I P2 Q
{P2 }
then
EndIf
A
P
Q
Conditional Statement Rule
Rule
If
Structure
A: I c is well defined
{P0 } I c AND
A1 Explanation
If c then P0 P1
{c, P0 } I P1 Q
I The condition c
A1 can be evaluated
{P1 } OR I Both branches
Else I NOT c AND lead to the
A2
{NOTc, P0 } P0 P2 postconditions
A2 I P2 Q
{P2 }
then
EndIf
A
P
Q
Example Conditional Statement Rules
Algorithm Analysis
Basic Notions
Precondition and Postcondition
Algorithm State
Assertions and Annotation
{P P0 }
While c Do
{c, P0 }
A
{P1 }
EndWhile
{NOTc, P1 } Q
Loop Statement Rules
Algorithm 17 Minimum
1: m x[1]
2: i 2 .
m = min{x[j]|j = 1 . . . i 1}
3: while i n do .i n
4: if x[i] < m then m x[i]
. {m = minx[j]|j = 1 . . . i}
5: i i +1 .
{m = minx[j]|j = 1 . . . i 1}
Loop Invariant
Example: Find the minimum, m, of a non-empty array, x[1..n]
P: n 1
Q: m = min{x[i]|1 i n}
Loop Invariant:
m = min{x[j]|j = 1 . . . i 1}
Algorithm 18 Minimum
1: m x[1]
2: i 2 .
m = min{x[j]|j = 1 . . . i 1}
3: while i n do .i n
4: if x[i] < m then m x[i]
. {m = minx[j]|j = 1 . . . i}
5: i i +1 .
{m = minx[j]|j = 1 . . . i 1}
Loop Invariant
Example: Find the minimum, m, of a non-empty array, x[1..n]
P: n 1
Q: m = min{x[i]|1 i n}
Loop Invariant:
m = min{x[j]|j = 1 . . . i 1}
Why?
Algorithm 19 Minimum
1: m x[1]
2: i 2 .
m = min{x[j]|j = 1 . . . i 1}
3: while i n do .i n
4: if x[i] < m then m x[i]
. {m = minx[j]|j = 1 . . . i}
5: i i +1 .
{m = minx[j]|j = 1 . . . i 1}
Loop Invariant
Example: Find the minimum, m, of a non-empty array, x[1..n]
P: n 1
Q: m = min{x[i]|1 i n}
Loop Invariant:
m = min{x[j]|j = 1 . . . i 1}
Why? Because..
Algorithm 20 Minimum
I when i = 2 and m = x[1] it
1: m x[1]
2: i 2 . holds
m = min{x[j]|j = 1 . . . i 1} I while i n after the
5: i i +1 . implies
{m = minx[j]|j = 1 . . . i 1} m = min{x[j]|j = 1 . . . n}
which is exactly the
postcondition
Loop Invariant
Example: Find the minimum, m, of a non-empty array, x[1..n]
P: n 1
Q: m = min{x[i]|1 i n}
Algorithm 21 Minimum
1: i 1
2: m x[1] .
{m = minx[j]|j = 1 . . . i}
3: while i < n do .i <n
4: i i +1 .
{m = minx[j]|j = 1 . . . i 1}
5: if x[i] < m then
6: m x[i] .
{m = minx[j]|j = 1 . . . i}
Loop Invariant
Example: Find the minimum, m, of a non-empty array, x[1..n]
P: n 1
Q: m = min{x[i]|1 i n}
Loop Invariant:
m = min{x[j]|j = 1 . . . i}
Algorithm 22 Minimum
1: i 1
2: m x[1] .
{m = minx[j]|j = 1 . . . i}
3: while i < n do .i <n
4: i i +1 .
{m = minx[j]|j = 1 . . . i 1}
5: if x[i] < m then
6: m x[i] .
{m = minx[j]|j = 1 . . . i}
Loop Invariant
Example: Find the minimum, m, of a non-empty array, x[1..n]
P: n 1
Q: m = min{x[i]|1 i n}
Loop Invariant:
m = min{x[j]|j = 1 . . . i} Why?
Algorithm 23 Minimum
1: i 1
2: m x[1] .
{m = minx[j]|j = 1 . . . i}
3: while i < n do .i <n
4: i i +1 .
{m = minx[j]|j = 1 . . . i 1}
5: if x[i] < m then
6: m x[i] .
{m = minx[j]|j = 1 . . . i}
Loop Invariant
Example: Find the minimum, m, of a non-empty array, x[1..n]
P: n 1
Q: m = min{x[i]|1 i n}
Loop Invariant:
m = min{x[j]|j = 1 . . . i} Why?
Algorithm 24 Minimum Because..
1: i 1
I when i = 1 and m = x[1]
2: m x[1] .
the invariant is true
{m = minx[j]|j = 1 . . . i}
3: while i < n do .i <n I while i < n after the
Algorithm 27 Mini-
mum
1: i 1 .
{x[j] <> x0 , j = 1 . . . i 1}
3: i i +1 .
1 . . . i 1}
Loop Invariant
Example:Let x[1..n]be an array which contains x0 . Find the
smallest index for which x[i] = x0
P: n 1 and there exist 1 k n such thta x[k] = x0
Q: x[i] = x0 and x[j] 6= x0 for j = 1 . . . i 1
Loop Invariant:
x[j] 6= x0 for j = 1 . . . i 1
Algorithm 28 Mini-
mum
1: i 1 .
{x[j] <> x0 , j = 1 . . . i 1}
3: i i +1 .
1 . . . i 1}
Loop Invariant
Example:Let x[1..n]be an array which contains x0 . Find the
smallest index for which x[i] = x0
P: n 1 and there exist 1 k n such thta x[k] = x0
Q: x[i] = x0 and x[j] 6= x0 for j = 1 . . . i 1
Loop Invariant:
x[j] 6= x0 for j = 1 . . . i 1 Why?
Algorithm 29 Mini-
mum
1: i 1 .
{x[j] <> x0 , j = 1 . . . i 1}
3: i i +1 .
1 . . . i 1}
Loop Invariant
Example:Let x[1..n]be an array which contains x0 . Find the
smallest index for which x[i] = x0
P: n 1 and there exist 1 k n such thta x[k] = x0
Q: x[i] = x0 and x[j] 6= x0 for j = 1 . . . i 1
Loop Invariant:
x[j] 6= x0 for j = 1 . . . i 1 Why? Because..
Algorithm 30 Mini-
mum I when i = 1 the range j = 1 . . . 0 is
1: i 1 . empty thus the assertion is satisfied
{x[j] <> x0 , j = 1 . . . i 1}
3: i i +1 .
1 . . . i 1}
Loop Invariant
Example:Let x[1..n]be an array which contains x0 . Find the
smallest index for which x[i] = x0
P: n 1 and there exist 1 k n such thta x[k] = x0
Q: x[i] = x0 and x[j] 6= x0 for j = 1 . . . i 1
Loop Invariant:
x[j] 6= x0 for j = 1 . . . i 1 Why? Because..
Algorithm 31 Mini-
mum I when i = 1 the range j = 1 . . . 0 is
1: i 1 . empty thus the assertion is satisfied
{x[j] <> x0 , j = 1 . . . i 1} I Let us suppose that x[i] <> x0 and
2: while x[i] <> x0 the invariant is true. Then x[j] <> x0
do . {x[i] <> x0 , x[j] <> for j = 1 . . . i
x0 forj = 1 . . . i}
3: i i +1 .
1 . . . i 1}
Loop Invariant
Example:Let x[1..n]be an array which contains x0 . Find the
smallest index for which x[i] = x0
P: n 1 and there exist 1 k n such thta x[k] = x0
Q: x[i] = x0 and x[j] 6= x0 for j = 1 . . . i 1
Loop Invariant:
x[j] 6= x0 for j = 1 . . . i 1 Why? Because..
Algorithm 32 Mini-
mum I when i = 1 the range j = 1 . . . 0 is
1: i 1 . empty thus the assertion is satisfied
{x[j] <> x0 , j = 1 . . . i 1} I Let us suppose that x[i] <> x0 and
2: while x[i] <> x0 the invariant is true. Then x[j] <> x0
do . {x[i] <> x0 , x[j] <> for j = 1 . . . i
x0 forj = 1 . . . i}
I After i = i + 1 we obtain again
3: i i +1 .
x[j] <> x0 for j = 1 . . . i which is
{x[i] <> x0 , x[j] <> x0 forj =
exactly the postcondition
1 . . . i 1}
Loop Invariant
Example:Let x[1..n]be an array which contains x0 . Find the
smallest index for which x[i] = x0
P: n 1 and there exist 1 k n such thta x[k] = x0
Q: x[i] = x0 and x[j] 6= x0 for j = 1 . . . i 1
Loop Invariant:
x[j] 6= x0 for j = 1 . . . i 1 Why? Because..
Algorithm 33 Mini-
mum I when i = 1 the range j = 1 . . . 0 is
1: i 1 . empty thus the assertion is satisfied
{x[j] <> x0 , j = 1 . . . i 1} I Let us suppose that x[i] <> x0 and
2: while x[i] <> x0 the invariant is true. Then x[j] <> x0
do . {x[i] <> x0 , x[j] <> for j = 1 . . . i
x0 forj = 1 . . . i}
I After i = i + 1 we obtain again
3: i i +1 .
x[j] <> x0 for j = 1 . . . i which is
{x[i] <> x0 , x[j] <> x0 forj =
exactly the postcondition
1 . . . i 1}
I Finally, when x[i] = x0 we obtain Q
Termination Function
To prove that a loop finishes after a finite number of iterations it
suffices to find a termination function
Termination Function
To prove that a loop finishes after a finite number of iterations it
suffices to find a termination function
Definition:
A function F : N N is a termination function if it satisfies the
following properties:
1. F is strictly decreasing
2. if c is true then F (p) > 0 and if F (p) = 0 then c is false
Termination Function
To prove that a loop finishes after a finite number of iterations it
suffices to find a termination function
Definition:
A function F : N N is a termination function if it satisfies the
following properties:
1. F is strictly decreasing
2. if c is true then F (p) > 0 and if F (p) = 0 then c is false
Remarks:
I F depends on the loop counter p (at the first execution of the
loop body p is 1, at the second it is 2 and so on )
I The loop counter is not necessarily an explicit variable (it can
be just a formal variable useful to analyze the loop
correctness)
I F reaches 0 because it is strictly decreasing; when F becomes
0 then c becomes false, thus the loop finishes.
Termination Function
Example:S = 1 + 2 + . . . + n
Variant 1:
F (p) = n ip
Termination Function
Example:S = 1 + 2 + . . . + n
Variant 1:
F (p) = n ip
F (p) = n ip1 1
Termination Function
Example:S = 1 + 2 + . . . + n
Variant 1:
F (p) = n ip
F (p) = n ip1 1
F (p) = F (p 1) 1
Termination Function
Example:S = 1 + 2 + . . . + n
Variant 1:
F (p) = n ip
F (p) = n ip1 1
F (p) = F (p 1) 1
F (p) < F (p 1)
Termination Function
Example:S = 1 + 2 + . . . + n
Variant 1:
F (p) = n ip
F (p) = n ip1 1
F (p) = F (p 1) 1
F (p) < F (p 1)
ip < n F (p) > 0
Termination Function
Example:S = 1 + 2 + . . . + n
Variant 1:
F (p) = n ip
F (p) = n ip1 1
F (p) = F (p 1) 1
F (p) < F (p 1)
ip < n F (p) > 0
F (p) = 0 ip = n
Termination Function
Example:S = 1 + 2 + . . . + n
Variant 1: Variant 2:
F (p) = n ip
F (p) = n ip1 1
F (p) = F (p 1) 1
F (p) < F (p 1)
ip < n F (p) > 0
F (p) = 0 ip = n
Termination Function
Example:S = 1 + 2 + . . . + n
Variant 1: Variant 2:
F (p) = n ip F (p) = n + 1 ip
F (p) = n ip1 1
F (p) = F (p 1) 1
F (p) < F (p 1)
ip < n F (p) > 0
F (p) = 0 ip = n
Termination Function
Example:S = 1 + 2 + . . . + n
Variant 1: Variant 2:
F (p) = n ip F (p) = n + 1 ip
F (p) = n ip1 1 F (p) = n + 1 ip1 1
F (p) = F (p 1) 1
F (p) < F (p 1)
ip < n F (p) > 0
F (p) = 0 ip = n
Termination Function
Example:S = 1 + 2 + . . . + n
Variant 1: Variant 2:
F (p) = n ip F (p) = n + 1 ip
F (p) = n ip1 1 F (p) = n + 1 ip1 1
F (p) = F (p 1) 1 F (p) = F (p 1) 1
F (p) < F (p 1)
ip < n F (p) > 0
F (p) = 0 ip = n
Termination Function
Example:S = 1 + 2 + . . . + n
Variant 1: Variant 2:
F (p) = n ip F (p) = n + 1 ip
F (p) = n ip1 1 F (p) = n + 1 ip1 1
F (p) = F (p 1) 1 F (p) = F (p 1) 1
F (p) < F (p 1) F (p) < F (p 1)
ip < n F (p) > 0
F (p) = 0 ip = n
Termination Function
Example:S = 1 + 2 + . . . + n
Variant 1: Variant 2:
F (p) = n ip F (p) = n + 1 ip
F (p) = n ip1 1 F (p) = n + 1 ip1 1
F (p) = F (p 1) 1 F (p) = F (p 1) 1
F (p) < F (p 1) F (p) < F (p 1)
ip < n F (p) > 0 i < n F (p) > 0
F (p) = 0 ip = n
Termination Function
Example:S = 1 + 2 + . . . + n
Variant 1: Variant 2:
F (p) = n ip F (p) = n + 1 ip
F (p) = n ip1 1 F (p) = n + 1 ip1 1
F (p) = F (p 1) 1 F (p) = F (p 1) 1
F (p) < F (p 1) F (p) < F (p 1)
ip < n F (p) > 0 i < n F (p) > 0
F (p) = 0 ip = n F (p) = 0 ip = n + 1
Termination Function