Presentation is loading. Please wait.

Presentation is loading. Please wait.

Proofs, Recursion, and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs,

Similar presentations


Presentation on theme: "Proofs, Recursion, and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs,"— Presentation transcript:

1 Proofs, Recursion, and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs, Recursion, and Analysis of Algorithms

2 Section 2.3More on Proof of Correctness1 Loop Rule Suppose that s i 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} s i {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.

3 Section 2.3More on Proof of Correctness2 Euclidean Algorithm 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.

4 Section 2.3More on Proof of Correctness3 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

5 Section 2.3More on Proof of Correctness4 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(i n, j n ) = gcd(a, b) for all n  0. Q(0) is gcd(i 0, j 0 ) = 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(i k, j k ) = gcd(a, b). Show Q(k + 1): gcd(i k + 1, j k + 1 ) = gcd(a, b).

6 Section 2.3More on Proof of Correctness5 Euclidean Algorithm Proof By the assignment statements within the loop body, we know that i k + 1 = j k j k + 1 = r k Then, by the additional fact on the previous slide: gcd(i k + 1, j k + 1 ) = gcd(j k, r k ) = gcd(i k, j k ) 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.


Download ppt "Proofs, Recursion, and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs,"

Similar presentations


Ads by Google