Lecture Notes for Algorithm Design
Statements, 30 November 1999
These are provisional lecture notes, expect changes.
- overview - state transformers
- going from the initial state to the final state
- statements as state transformers
- each statement is a small program with initial and final states
- assignment statements
- working forward - establish a truth in the initial state and use the
assignment to establish it
- {a[0..i + 1] <= n} i <- i + 1 {a[0..i] <= n}
- work backward from the final to initial state
- {P[x <- E]} x <- E {P}
- choice statements
- Find S to establish
{x = X and y = Y}
S
{x = X and y = Y and (z = X or Z = Y) and z >= X and z >= Y}
- there are two cases to consider:
- X >= Y, in which case
{x = X and y = Y and X >= Y}
z <- x
{x = X and y = Y and (z = X or z = Y) and z >= X and z >= Y}
does the trick
- X <= Y, in which case
{x = X and y = Y and X < Y}
z <- y
{x = X and y = Y and (z = X or z = Y) and z >= X and z >= Y}
does the trick
- but which case holds on any particular execution?
- The alternative (or choice or if) statement:
{x = X and y = Y}
if x >= y
{x = X and y = Y and X >= Y}
z <- x
{x = X and y = Y and (z = X or z = Y) and z >= X and z >= Y}
else
{x = X and y = Y and X < Y}
z <- y
{x = X and y = Y and (z = X or z = Y) and z >= X and z >= Y}
fi
- In general
{ I }
if B
{ I and B}
S1
{ F }
else
{ I and not B}
S2
{ F }
fi
{ F }
- eliminate the else if P and not B can establish Q
- repetitive statements
- In general
{ I }
do B
{ I and B}
S
{ I }
od
{ I and not B }
{ F }
- partial correctness - no termination
- termination function - a function decreasing on every iteration
- creating the invariant
- the tip-off is a for-all statement in the final state
- create the invariant by weakening the final state
- the do guard becomes the part removed from the final state to get
the invariant
- example
- given predicate B(), find the smallest natural number x
such that B(x) is true.
- the initial state is true
- the final state is for all i such that 0 <= i < x :
not B(i) and B(x)
- one way to weaken the final state is to drop part of it; the
dropped part becomes the guard to the do.
- sequencing statements
- { I1 } S1 { F1 } ; { I2 } S2 { F2 }
- the relation between F1 and I2
- simple - F1 = I2
- more complicated - manipulate F1 to I2; stronger and weaker states
- working backward, working forward, inside out, from both ends in
- for example, swap
{ x = X and y = Y }
S
{ x = Y and y = X }
This page last modified on 14 December 1999.