Proofs, Recursion and Analysis of Algorithms

Download Report

Transcript Proofs, Recursion and Analysis of Algorithms

Proofs, Recursion, and Analysis of
Algorithms
Mathematical Structures
for Computer Science
Chapter 2
Copyright © 2006 W.H. Freeman & Co.
MSCS Slides
Proofs, Recursion, and Analysis of
Loop Rule

Suppose that si is a loop statement in the form:




while condition B do
P
end while
where B is a condition that is either true or false and P is a
program segment.
The precondition Q holds before the loop is entered and after it
terminates.
The Loop Rule of inference states that we can derive
{Q} si {Q Λ B} from {Q Λ B} P {Q}
Because it may not be known exactly when the loop will
terminate, Q must remain true after each iteration through the
loop.
Q represents a predicate, or relation, among the values of the
program variables.
The loop invariant is the relation among these variables
unaffected by the action of the loop iteration.




Section 2.3
More on Proof of Correctness
1
Euclidean Algorithm





Section 2.3
The Euclidean algorithm finds the greatest common
divisor of two positive integers a and b.
The greatest common divisor of a and b, denoted by
gcd(a, b), is the largest integer n such that n|a and n|b.
For example, gcd(12, 18) = 6 and gcd(420, 66) = 6.
The Euclidean algorithm works by a succession of
divisions.
To find gcd(a, b), assuming that a >= b, you first
divide a by b, getting a quotient and a remainder.
Next, you divide the divisor, b, by the remainder and
keep doing this until the remainder is 0, at which point
the greatest common divisor is the last divisor used.
More on Proof of Correctness
2
Euclidean Algorithm
ALGORITHM: Euclidean algorithm
GCD(positive integer a; positive integer b)
//a  b
Local variables:
integers i, j
i=a
j=b
while j != 0 do
compute i = qj + r, 0  r < j
i=j
j=r
end while
//i now has the value gcd(a, b)
return i;
end function GCD
Section 2.3
More on Proof of Correctness
3
Euclidean Algorithm Proof




To prove the correctness of this function, we need one
additional fact:
(" integers a, b, q, r)[(a = qb + r) (gcd(a, b) = gcd(b, r))]
Using function GCD, we will prove the loop invariant Q:
gcd(i, j) = gcd(a, b) and evaluate Q when the loop
terminates.
We use induction to prove:
Q(n): gcd(in, jn) = gcd(a, b) for all n  0.
Q(0) is gcd(i0, j0) = gcd(a, b) is true because when we first
get to the loop statement, i and j have the values a and b.
Assume Q(k): gcd(ik, jk) = gcd(a, b).
Show Q(k + 1): gcd(ik + 1, jk + 1) = gcd(a, b).




Section 2.3
More on Proof of Correctness
4
Euclidean Algorithm Proof

By the assignment statements within the loop body, we
know that
ik + 1 = jk
j k + 1 = rk
Then, by the additional fact on the previous slide:
gcd(ik + 1, jk + 1) = gcd(jk, rk) = gcd(ik, jk)
By the inductive hypothesis, the above is equal to
gcd(a, b)
Q is therefore a loop invariant.
At loop termination, gcd(i, j) = gcd(a, b) and j = 0, so
gcd(i, 0) = gcd(a, b).
But gcd(i, 0) is i, so i = gcd(a, b). Therefore, function
GCD is correct.







Section 2.3
More on Proof of Correctness
5