Lecture Notes for Algorithm Design
Algorithms and States, 23 November 1999
These are provisional lecture notes, expect changes.
- a variable represents a value
- the state of a variable is given by the variable's value
- characterizations about a variable's state are presented as
characterizations about its value
- the variable x is positive
- the (value of the) variable x is between 1 and 5
- variable state characterizations can be abstract
- the (value of the) variable x is an integer
- the (value of the) variable x is undefined
- it is easy to confuse a variable's value with its state
- variable state represents many values
- variable value represents a single entity
- a variable's state may or may not be true at any particular time
- confusing the two at this level is not dangerous, but should be
avoided
- an algorithm is a collection of variables (among other things)
- the state of an algorithm is the state of its variables
- not all variables need make up an algorithm's state
- two important algorithm states are the initial state and the
final state
- the initial state describes what is true before the algorithm
starts executing
- the final state describes what is true after the algorithm
finishes executing
- the initial state is conserned with input variables; the final state
is conserned with output variables
- the initial state is the precondition; the final state is the
post condition
- example - swap(x, y)
- initial state - x = X and y = Y
- final state - x = Y and y = X
- example - square root
- initial state - x = X
- final state - x = sqrt(X)
- does this work always - no, not for integer x
- strong and weak states
- the description of a variable's state may represent several values -
e.g. the value of the variable x is positive
- the more values a state description represents, the weaker that
state description is - e.g. the value of x is greater than 5 vs. the value
of x is positive
- a weak state description is less restrictive, it allows more
values
- the fewer values a state description represents, the stronger that
state description is - e.g. the value of x is an integer vs. the value
of x is positive
- a strong state description is more restrictive, it allows fewer
values
- one of the main stratagies for manipulating state descriptions is to
strengthen them - that is, make them more restrictive - and to weaken them
- that is, to make them less restrictive
- the choice between strengthening and weakening is sometimes a
matter of taste and sometimes a matter of good policy
- example - square root again
- initial state - x = X is too weak; it admits too many values, some
of which may not have a solution
- how to strengthen the initial state - x = X2
- now the post condition is x = X
- is this correct - traditionally, no because X could be negative;
the initial state is still too weak
- the intial and final states are a functional specification for
the algorithm
- an algorithm that begins when the initial state is true should end
with the final state being true too
- if the initial state is false, what the algorithm does is undefined
- as a matter of good policy - initial states should be weak and final
states should be strong
- a functional specification clarifies what needs to be done, and
makes sure it's possible to do what needs to be done
- example - sorting
- initial state - a = A
- final state a = A' and A' = permutation(A) and for all i, j such
that 0 <= i <= j < |a|, a[i] <= a[j]
This page last modified on 3 December 1999.