Correctness of Algorithms
CS 30 : Discrete Mathematics for Computer Science First Semester, AY 2011-2012
https://sites.google.com/a/dcs.upd.edu.ph/nhsh_classes/cs 30
sablay-logo
Nestine Hope S. Hernandez Algorithms and Complexity Laboratory Department of Computer Science University of the Philippines, Diliman nshernandez@dcs.upd.edu.ph updilseal
Day 6
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
Correctness of Algorithms
Programming reliably must be an activity of an undeniably mathematical nature ... You see, mathematics is about thinking, and doing mathematics is always trying to think as well as possible.
sablay-logo
Edsger W. Dijkstra (1981)
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
Correctness of Algorithms
Programming reliably must be an activity of an undeniably mathematical nature ... You see, mathematics is about thinking, and doing mathematics is always trying to think as well as possible.
sablay-logo
Edsger W. Dijkstra (1981)
...it is not only the programmer's task to produce a correct program but also to demonstrate its correctness in a convincing manner. Edsger W. Dijkstra updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
Correctness of Algorithms
Programming reliably must be an activity of an undeniably mathematical nature ... You see, mathematics is about thinking, and doing mathematics is always trying to think as well as possible.
sablay-logo
Edsger W. Dijkstra (1981)
...it is not only the programmer's task to produce a correct program but also to demonstrate its correctness in a convincing manner. Edsger W. Dijkstra
... a program and its proof should be developed hand-in-hand, with the proof usually leading the way. David Gries
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
Correctness of Algorithms
Mathematical Logic
sablay-logo
1
Correctness of Algorithms
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
Correctness of Algorithms
Hmmm ... is the algorithm correct?
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
Correctness of Algorithms
Hmmm ... is the algorithm correct?
Consider an algorithm that is designed to produce a certain nal state from a certain initial state. Both the initial and nal states can be expressed as predicates involving the input and output variables.
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
Correctness of Algorithms
Hmmm ... is the algorithm correct?
Consider an algorithm that is designed to produce a certain nal state from a certain initial state. Both the initial and nal states can be expressed as predicates involving the input and output variables.
pre-condition
Often the predicate describing the initial state is called the , and the predicate describing the nal state is called the .
for the algorithm post-condition for the algorithm
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
Correctness of Algorithms
Pre-condition and Post-condition
Algorithm to compute the product of a nonnegative integer and a real number
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
Correctness of Algorithms
Pre-condition and Post-condition
Algorithm to compute the product of a nonnegative integer and a real number Pre-condition: The input variables
m is a nonnegative integer and x is a real number.
Post-condition: The output variable
p equals mx .
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
Correctness of Algorithms
Pre-condition and Post-condition
Algorithm to nd quotient and remainder of the division of one positive integer by another
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
Correctness of Algorithms
Pre-condition and Post-condition
Algorithm to nd quotient and remainder of the division of one positive integer by another Pre-condition: The input variables
a and d are positive integers.
Post-condition: The output variables 0â&#x2030;¤ < .
r d
sablay-logo
q and r are integers such that a = dq + r and updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
Correctness of Algorithms
Pre-condition and Post-condition
Algorithm to sort a one-dimensional array of real numbers
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
Correctness of Algorithms
Pre-condition and Post-condition
Algorithm to sort a one-dimensional array of real numbers Pre-condition: The input variable [1], [2], . . . , [ ] is a one-dimensional array of real numbers.
A A
An
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
Correctness of Algorithms
Pre-condition and Post-condition
Algorithm to sort a one-dimensional array of real numbers Pre-condition: The input variable [1], [2], . . . , [ ] is a one-dimensional array of real numbers.
A A
An
sablay-logo
Post-condition: The output variable [1], [2], . . . , [ ] is a one-dimensional array of real numbers with same elements as [1], [2], . . . , [ ] but with the property that [ ] â&#x2030;¤ [ ] whenever â&#x2030;¤ .
Bi Bj
B B Bn A A i j
An
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
Correctness of Algorithms
Pre-condition and Post-condition
A proof of algorithm correctness consists of showing that if the pre-condition for the algorithm is true for a collection of values for the input variables and if the statements of the algorithms are executed, then the post-condition is also true.
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
Correctness of Algorithms
Loop Invariants
The method of loop invariants is used to prove correctness of a loop with respect to certain pre- and post-conditions. It is based on the principle of mathematical induction.
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
Correctness of Algorithms
Loop Invariants
The method of loop invariants is used to prove correctness of a loop with respect to certain pre- and post-conditions. It is based on the principle of mathematical induction. Suppose that an algorithm contains a while loop and that entry to this loop is restricted by a condition G , called the guard. Suppose also that assertions describing the current states of algorithm variables have been placed immediately preceding and immediately following the loop. The assertion just preceding the loop is called the pre-condition for the loop and the one just following is called the post-condition for the loop.
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
Correctness of Algorithms
Loop
{Pre-condition for the loop} (G) {Statements in the body of the loop. None contain branching statements that lead outside the loop.} {Post-condition for the loop} while
sablay-logo
end while
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
Correctness of Algorithms
Loop Invariants
Establishing the correctness of a loop uses the concept of loop invariant.
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
Correctness of Algorithms
Loop Invariants
Establishing the correctness of a loop uses the concept of loop invariant. A loop invariant is a predicate with domain a set of integers, which satis es the condition: For each iteration of the loop, if the predicate is true before the iteration, then it is true after the iteration.
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
Correctness of Algorithms
Loop Invariant Theorem
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
Correctness of Algorithms
Correctness of the Division Algorithm
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
Correctness of Algorithms
Correctness of the Division Algorithm
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
Correctness of Algorithms
Correctness of the Division Algorithm
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
Correctness of Algorithms
Correctness of the Division Algorithm
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
Correctness of Algorithms
Correctness of the Division Algorithm
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
Correctness of Algorithms
Correctness of the Division Algorithm
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
Correctness of Algorithms
Correctness of the Product Algorithm
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
Correctness of Algorithms
Correctness of the Product Algorithm
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
Correctness of Algorithms
Correctness of the Product Algorithm
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
Correctness of Algorithms
Correctness of the Product Algorithm
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
Correctness of Algorithms
Correctness of the Product Algorithm
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
Correctness of Algorithms
Correctness of the Product Algorithm
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
Correctness of Algorithms
Correctness of the Product Algorithm
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
Correctness of Algorithms
sablay-logo
Questions? See you next meeting!
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30