340 likes | 412 Views
CS1022 Computer Programming & Principles. Lecture 2.2 Logic and Proof (2). Plan of lecture. Motivation: why proof is important to computing Methods of proof Mathematical induction Correctness of algorithms. Why proof is important to Computing.
E N D
CS1022Computer Programming & Principles Lecture 2.2 Logic and Proof (2)
Plan of lecture • Motivation: why proof is important to computing • Methods of proof • Mathematical induction • Correctness of algorithms CS1022
Why proof is important to Computing • We need to provide guarantees to our programs • Guarantee should be given as a proof • No amount of “sales talk” would do it • Core functionalities and where “the buck stops” • We need to check if a program has guarantees CS1022
Methods of proof (1) • Logical arguments as proofs of theorems • Common proof: establish the truth of (P Q) • If P is true then Q follows • Every situation in which P is true, Q is also true • There are standard methods of proof: • Direct argument • Contrapositive argument • Proof by contradiction CS1022
Direct Argument (1) • Assume P is true and show that Q is true • This rules out situations where P is true and Q is false • Remember truth table of (P Q) • “in all those situations where P is true, Q is also true” CS1022
Direct Argument (2) Example: show that if x and y are odd integers then xy is also an odd integer Solution • If x is an odd integer then it can be written as x = 2m + 1, for some integer m • Likewise, y = 2n + 1, for some integer n • Then xy = (2m + 1)(2n + 1) = 4mn + 2m + 2n + 1 = 2(2mn + m + n) + 1 = 2p + 1 • 2p + 1 is an odd integer CS1022
Contrapositive Argument (1) • Assume Q is false and show that P is false • It shows that ((notQ) (notP)) is true • This is the same as showing that (P Q) is true ((notQ) (notP)) (P Q) CS1022
Contrapositive Argument (2) Example: Let n be a positive integer. Using the contrapositive argument, prove that if n2 is odd, then n is odd. Solution • “n2 is odd” is P; “n is odd” is Q • notP(negation of “n2 is odd”) is “n2 is even” • not Q (negation of “n is odd”) is “n is even” • Prove “if not Q then notP”, that is, ((not Q) (notP)) or “if n is even then n2 is even” • Since n is even, then n = 2mfor some integer m • Then n2 = (2m)2 = 4m2 = 2(2m2) which is even CS1022
Proof by Contradiction (1) • Assume P is true and Q is false and derive a contradiction • This rules out the situation where P is true and Q is false which is the only case where (P Q) is false CS1022
Proof by Contradiction (2) Example: prove that if p2 is even (P), then p is even (Q) Solution • Assume P is true (p2 is even) and Q is false (p is not even) • If p is not even, then it is odd, that is, p = 2n + 1, and we have p2 = (2n + 1)2 = 4n2 + 4n + 1. • But 4n2 + 4n = 2 (2n2 + 2n) = 2m is even, so 4n2 + 4n + 1 is odd. • This means p2 is not even, that is, (not P) is true – a contradiction since we assumed P is true • Therefore our assumption that p is not even must be wrong, i.e. p is even CS1022
Mathematical induction (1) • Algorithm correctness: • Guarantee that output/behaviour meets requirements • E.g., a correct algorithm to find highest value in a list • What about testing algorithms/programs? • Testing illustrates correct/incorrect output/behaviour • Testing and profiling are very important in software engineering • There are important lessons to learn when testing and improving a program/algorithm • However, unless testing is exhaustive, it merely increases trust (subjective) but it does not provide guarantees • Not getting an error does not mean that there are no errors, but just that your testing did not catch the error(s) CS1022
Mathematical induction (2) • Proof of correctness – formal ways to prove that “for any input values, the output values are correct” • Most (all?) non-trivial algorithms have loops • Proving the correctness of algorithms with loops requires a powerful method of proof called mathematical induction CS1022
Mathematical induction (3) • Example: algorithm to find maximum element in a list of positive integers CS1022
Mathematical induction (4) • Sample run: assume input a1 = 4, a2 = 7, a3 = 3 , a4 = 8 CS1022
Mathematical induction (5) • Output (M = 8) is correct • After each execution of the loop, M is the maximum element of the list so far considered • Does the algorithm work for all lists of any length n? CS1022
Mathematical induction (6) • Consider an input list a1, a2, ...,an of size n • Mk is the value of M after k iterations of the loop Reasoning: • For an input list a1 of size 1, the loop is executed once and M is assigned a1, the correct output • If after k executions of the loop Mk is the maximum element of the list a1, a2, ...,akthen after one more loop Mk + 1 is assigned max(Mk, ak+ 1) which will then be the maximum element of the list a1, a2, ...,ak+ 1 CS1022
Mathematical induction (7) • By condition 1 the algorithm works for lists of size 1. • By condition 2 it also works for any lists of size 2. • By condition 2 it also works for any lists of size 3. • And so on... Hence the algorithm works for any list of any size • And so it is correct!! CS1022
Mathematical induction (8) The Principle of Mathematical Induction: CS1022
Correctness of algorithms (1) • An algorithm is correct if it does what it claims • To prove correctness of algorithms • We need to check any claims about variables before, during and after the execution of the algorithm • These claims (or assertions) are predicates • Predicates are properties withparameters(e.g., odd(X)) CS1022
Correctness of algorithms (2) • Let P be a predicate which is true for the input values of an algorithm A • Let Q be a predicate describing conditions that the output variables should satisfy • The statement {P} A {Q} means “if the execution of A starts with P true then it will terminate with Q true” • P is called the precondition • Q is called the postcondition • {P}A{Q} is itself a predicate which we must prove in order to establish the correctness of A • Proof of correctness is straightforward for simple algorithms CS1022
Correctness of algorithms (3) Prove that algorithm Difference is correct CS1022
Correctness of algorithms (4) Prove that algorithm Difference is correct Solution: CS1022
Correctness of algorithms (5) • When a sequence of statements occur in an algorithm A, • We break the sequence into suitable sub-parts A1,A2,..., An and • Prove a chain of assertions of the form {P1}A1{Q1}, {Q1}A2{Q2}, ...,{Qn–1}An {Qn} • The post-condition for any part becomes the pre-condition of the following part CS1022
Correctness of algorithms (6) Prove that the algorithm Quadratic is correct CS1022
Correctness of algorithms (7) Solution CS1022
Correctness of algorithms (8) • Proof techniques also work in algorithms with conditional statements (if-then-else) • Pre- and post-conditions must reflect alternative paths in the execution of the algorithm CS1022
Correctness of algorithms (9) • More specifically: CS1022
Correctness of algorithms (10) Prove that the algorithm Absolute is correct CS1022
Correctness of algorithms (11) Solution: CS1022
Correctness of algorithms (12) • When iterative statements (for, while and repeat loops) appear, the use of pre- and post-conditions becomes impractical • We then use mathematical induction (as seen) CS1022
Correctness of algorithms (13) Proof by induction that algorithm Square is correct CS1022
Correctness of algorithms (14) Solution • Let P(n) be sq = n2after n executions of the loop • Let sqk be the value of sq after k executions of the loop We prove that • sq1 = 12 • if sqk = k2 then sqk+1 = (k + 1)2 Proof: • After one execution of the loop, sq1 = 12, so cond. 1 holds • If after k executions of the loop sqk = k2 then after one more loop, we have (N.B.: sq := sq + 2i – 1) sqk+1 = sqk + 2(k + 1) – 1 = k2 + 2k + 1 = (k + 1)2 Hence, 2 holds By condition 1, P(1) is true. By condition 2, for all k 1 (P(k) P(k + 1)) is true. Therefore, by induction, P(n) is true for all n 1 CS1022
Summary You should now know: • Proof by direct argument • Proof by contrapositive argument • Proof by contradiction • Mathematical induction • How to prove correctness of simple algorithms CS1022
Further reading • R. Haggarty. “Discrete Mathematics for Computing”. Pearson Education Ltd. 2002. (Chapter 2) CS1022