Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Lecture 3:
Verification of the correctness of algorithms
Tauseef Iftikhar
Department of Computer Science
Government College University, Lahore.
Todays Agenda
Algorithm Analysis
Basic Notions
Precondition and Postcondition
Algorithm State
Assertions and Annotation
Basic Steps in Correctness Verification
Rule for Correctness Verification
Sequential Statements Rule
Conditional Statements Rule
Loop Statements Rule
Loop Invariant
Termination Function
Algorithm Analysis
Algorithm Analysis
Algorithm Analysis
Algorithm Correctness
How to verify that algorithm is correct?
Algorithm Correctness
How to verify that algorithm is correct?
I
Algorithm Correctness
How to verify that algorithm is correct?
I
Simple
Easy to apply
Algorithm Correctness
How to verify that algorithm is correct?
I
Simple
Easy to apply
Disadvantage:
I
Algorithm Correctness
How to verify that algorithm is correct?
I
Disadvantage:
I
Simple
Easy to apply
It doesnt guarantee the correctness
Algorithm Correctness
How to verify that algorithm is correct?
I
Disadvantage:
I
Simple
Easy to apply
It doesnt guarantee the correctness
Algorithm Correctness
How to verify that algorithm is correct?
I
Disadvantage:
I
Simple
Easy to apply
Disadvantage:
I
I
Difficult
Not possible for complex algorithms
Basic Notions
Algorithm State
Assertions
Annotation
Todays Agenda
Algorithm Analysis
Basic Notions
Precondition and Postcondition
Algorithm State
Assertions and Annotation
Basic Steps in Correctness Verification
Rule for Correctness Verification
Sequential Statements Rule
Conditional Statements Rule
Loop Statements Rule
Loop Invariant
Termination Function
Todays Agenda
Algorithm Analysis
Basic Notions
Precondition and Postcondition
Algorithm State
Assertions and Annotation
Basic Steps in Correctness Verification
Rule for Correctness Verification
Sequential Statements Rule
Conditional Statements Rule
Loop Statements Rule
Loop Invariant
Termination Function
Algorithm State
I
Algorithm State
I
Algorithm State
I
Algorithm State
I
Algorithm State
I
Algorithm State
I
Algorithm State
I
Algorithm State
I
2:
Todays Agenda
Algorithm Analysis
Basic Notions
Precondition and Postcondition
Algorithm State
Assertions and Annotation
Basic Steps in Correctness Verification
Rule for Correctness Verification
Sequential Statements Rule
Conditional Statements Rule
Loop Statements Rule
Loop Invariant
Termination Function
I
I
correctness verification
documentation tool
procedure minimum(real a, b, c)
ma
if m > b then
mb
if m > c then
mc
return m
I
I
Some Notation
P: the precondition
Q: the postcondition
A: the algorithm
lead to postconditions Q
P
Q
Sequential statement
Conditional statement
Loop statement
Todays Agenda
Algorithm Analysis
Basic Notions
Precondition and Postcondition
Algorithm State
Assertions and Annotation
Basic Steps in Correctness Verification
Rule for Correctness Verification
Sequential Statements Rule
Conditional Statements Rule
Loop Statements Rule
Loop Invariant
Termination Function
Rule
If
P P0
Rule
If
P P0
Ai
Pi1
Pi ,
wherei = 1, . . . , n
Rule
If
P P0
Ai
Pi1
Pi ,
wherei = 1, . . . , n
Pn Q
then
= undefined}
= a}
= a}
= a} Q
= undefined}
= a}
= a}
= a} Q
variant 2
{x = a, y = b}
x x +y
{x = a + b, y = b}
y x y
{x = a + b, y = a}
x x y
{x = b, y = a} Q
Todays Agenda
Algorithm Analysis
Basic Notions
Precondition and Postcondition
Algorithm State
Assertions and Annotation
Basic Steps in Correctness Verification
Rule for Correctness Verification
Sequential Statements Rule
Conditional Statements Rule
Loop Statements Rule
Loop Invariant
Termination Function
Structure
A:
{P0 }
If c then
{c, P0 }
A1
{P1 }
Structure
A:
{P0 }
If c then
{c, P0 }
A1
{P1 }
Else
{NOTc, P0 }
A2
{P2 }
EndIf
Structure
A:
{P0 }
If c then
{c, P0 }
A1
{P1 }
Else
{NOTc, P0 }
A2
{P2 }
EndIf
Rule
If
I
c is well defined
c AND
A1
P0
P1
P1 Q
OR
Structure
A:
{P0 }
If c then
{c, P0 }
A1
{P1 }
Else
{NOTc, P0 }
A2
{P2 }
EndIf
Rule
If
I
c is well defined
c AND
A1
P0
P1
P1 Q
OR
I
NOT c AND
A2
P0
P2
P2 Q
then
A
P
Q
Structure
A:
{P0 }
If c then
{c, P0 }
A1
{P1 }
Else
{NOTc, P0 }
A2
{P2 }
EndIf
Rule
If
I
c is well defined
c AND
A1
P0
P1
P1 Q
OR
I
NOT c AND
A2
P0
P2
P2 Q
then
A
P
Q
Explanation
I
The condition c
can be evaluated
Both branches
lead to the
postconditions
. {a <> b}
. {a < b}
. {a < b, m = a} Q
. {b < a}
. {b < a, m = b} Q
Todays Agenda
Algorithm Analysis
Basic Notions
Precondition and Postcondition
Algorithm State
Assertions and Annotation
Basic Steps in Correctness Verification
Rule for Correctness Verification
Sequential Statements Rule
Conditional Statements Rule
Loop Statements Rule
Loop Invariant
Termination Function
Definition:
A loop invariant is an assertion
which satisfies:
Definition:
A loop invariant is an assertion
which satisfies:
1. It is true at the beginning of
the loop
Definition:
A loop invariant is an assertion
which satisfies:
1. It is true at the beginning of
the loop
2. As long as c is true it
remains true after each
execution of the loop body
Definition:
A loop invariant is an assertion
which satisfies:
1. It is true at the beginning of
the loop
2. As long as c is true it
remains true after each
execution of the loop body
3. When c is false it implies the
postconditions
Definition:
A loop invariant is an assertion
which satisfies:
1. It is true at the beginning of
the loop
2. As long as c is true it
remains true after each
execution of the loop body
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 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]
holds
2: i 2
.
I while i n after the
m = min{x[j]|j = 1 . . . i 1}
execution of the loop body
3: while i n do
.i n
it still holds
4:
if x[i] < m then m x[i]
. {m = minx[j]|j = 1 . . . i}
I finally, when i = n + 1 it
implies
5:
i i +1
.
m = min{x[j]|j = 1 . . . n}
{m = minx[j]|j = 1 . . . i 1}
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}
I while i < n after the
3: while i < n do
.i <n
execution of the loop body
4:
i i +1
.
it still holds
{m = minx[j]|j = 1 . . . i 1}
I finally, when i = n it implies
5:
if x[i] < m then
6:
m x[i]
.
m = min{x[j]|j = 1 . . . n}
{m = minx[j]|j = 1 . . . i}
which is exactly the
postcondition
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
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
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
Algorithm
mum
1: i 1
27 Mini.
{x[j] <> x0 , j = 1 . . . i 1}
2:
3:
i 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 Minimum
1: i 1
.
{x[j] <> x0 , j = 1 . . . i 1}
2:
3:
i 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 Minimum
1: i 1
.
{x[j] <> x0 , j = 1 . . . i 1}
2:
3:
i 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 MiniI when i = 1 the range j = 1 . . . 0 is
mum
empty thus the assertion is satisfied
1: i 1
.
{x[j] <> x0 , j = 1 . . . i 1}
2:
3:
i 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 MiniI when i = 1 the range j = 1 . . . 0 is
mum
empty thus the assertion is satisfied
1: i 1
.
{x[j] <> x0 , j = 1 . . . i 1}
2:
3:
i 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 MiniI when i = 1 the range j = 1 . . . 0 is
mum
empty thus the assertion is satisfied
1: i 1
.
{x[j] <> x0 , j = 1 . . . i 1}
2:
x0 forj = 1 . . . i}
3:
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 MiniI when i = 1 the range j = 1 . . . 0 is
mum
empty thus the assertion is satisfied
1: i 1
.
{x[j] <> x0 , j = 1 . . . i 1}
2:
x0 forj = 1 . . . i}
3:
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
Termination Function
Example:S = 1 + 2 + . . . + n
Variant 1:
Algorithm 34 Sum of Series
1: i 1
2: S 1
3: while i < n do
4:
i i + 1 . ip = ip1 + 1
5:
S i +1
Termination Function
Example:S = 1 + 2 + . . . + n
Variant 1:
Algorithm 36 Sum of Series
1: i 1
2: S 1
3: while i < n do
4:
i i + 1 . ip = ip1 + 1
5:
S i +1
F (p) = n ip
Termination Function
Example:S = 1 + 2 + . . . + n
Variant 1:
Algorithm 38 Sum of Series
1: i 1
2: S 1
3: while i < n do
4:
i i + 1 . ip = ip1 + 1
5:
S i +1
F (p) = n ip
F (p) = n ip1 1
Termination Function
Example:S = 1 + 2 + . . . + n
Variant 1:
Algorithm 40 Sum of Series
1: i 1
2: S 1
3: while i < n do
4:
i i + 1 . ip = ip1 + 1
5:
S i +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:
Algorithm 42 Sum of Series
1: i 1
2: S 1
3: while i < n do
4:
i i + 1 . ip = ip1 + 1
5:
S i +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:
Algorithm 44 Sum of Series
1: i 1
2: S 1
3: while i < n do
4:
i i + 1 . ip = ip1 + 1
5:
S i +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:
Algorithm 46 Sum of Series
1: i 1
2: S 1
3: while i < n do
4:
i i + 1 . ip = ip1 + 1
5:
S i +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 ip1 1
F (p) = F (p 1) 1
F (p) < F (p 1)
ip < n F (p) > 0
F (p) = 0 ip = n
F (p) = n + 1 ip
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
F (p) = n + 1 ip
F (p) = n + 1 ip1 1
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
F (p) = n + 1 ip
F (p) = n + 1 ip1 1
F (p) = F (p 1) 1
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
F (p) = n + 1 ip
F (p) = n + 1 ip1 1
F (p) = F (p 1) 1
F (p) < F (p 1)
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
F (p) = n + 1 ip
F (p) = n + 1 ip1 1
F (p) = F (p 1) 1
F (p) < F (p 1)
i < n F (p) > 0
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
F (p) = n + 1 ip
F (p) = n + 1 ip1 1
F (p) = F (p 1) 1
F (p) < F (p 1)
i < n F (p) > 0
F (p) = 0 ip = n + 1
Termination Function
Termination Function
Termination Function
Termination Function
Termination Function
Termination Function