Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Reading and Writing Mathematical Proofs Spring 2016 Lecture 1: Basic Proving Techniques I Organization The required stuff… Organization Lecturer: Arthur van Goethem MSc, MF 4.144, [email protected] Web page: Part of Data Structures http://www.win.tue.nl/aga/teaching/2IL50/ Book: Daniel Solow. How to Read and Do Proofs (5th edition) not mandatory Schedule Only 4 lectures Prerequisites Not covered Logical inferences Logical derivations (flag proofs) In other words: Logic and Set Theory (2IT60) Covered Mathematical proofs in common English Common proof techniques (proof by contradiction, induction, etc.) Common errors Many proof examples Today on: Reading and Writing Mathematical Proofs Today on: Basic Proving Techniques I The proving cookbook… Today: “How to properly slap your sidekick.” “Really poorly drawn circles!” “Proving my hamsters are strange.” What is a proof? Why explanations are not proofs… What is a proof? A method for establishing truth What establishes truth depends on context Physics Sufficient experimental evidence Courtroom Admissible evidence and witness testimony Mathematical proof Not a doubt possible! What is a proof? A form of communication Proof must convince reader (not the writer!) of correctness Proofs must be: Clearly written Should be easy to follow Very different from “proving process” Very precise No ambiguities! Leaving no doubts What is a proof? Clearly written “We turn the solution into an array. We invert the graph and then it look like the array. Because the solution can be an inverted graph and the array is input, the solution exists.” Very precise “We use Depth-First-Search to find some nodes that are too deep and somehow form the solution.” Leaving no doubts “In most cases it is the case that this conversion works. As we only have the best cases we likely can always perform this conversion.” Definition Mathematical proof A convincing argument for the reader to establish the correctness of a mathematical statement without any doubt. Definition Mathematical proof A convincing argument for the reader to establish the correctness of a mathematical statement without any doubt. Statement must be true or false 3+6 3+6=9 Definition Mathematical proof A convincing argument for the reader to establish the correctness of a mathematical statement without any doubt. In what format should a proof be? Logical derivation Good Very systematic Hard to make mistakes Bad Not convenient for statements not stated in logical formulas Emphasis on logical reasoning → detract from crux argument Hard to read Cumbersome Common English Theorem If x is odd, then x2 is odd Proof Since x is odd, there exists a k ϵ ℤ such that x = 2k + 1. Then, x2 = (2k + 1)2 = 4k2 + 4k + 1 = 2(2k2 + 2k) + 1 = 2m+1. As there exists a m ϵ ℤ such that x2 = 2m + 1, x2 is odd. □ This is the kind of proof we expect in Data Structures! Good Short and to the point Easy to read Bad Logical reasoning somewhat hidden Natural language can be ambiguous Two Proof Formats Theorem If x is odd, then x2 is odd Proof Since x is odd, there exists a k ϵ ℤ such that x = 2k + 1. Then, x2 = (2k + 1)2 = 4k2 + 4k + 1 = 2(2k2 + 2k) + 1 = 2m+1. As there exists a m ϵ ℤ such that x2 = 2m + 1, x2 is odd. □ This is what you should write down This should be in the back of your mind Two Proof Formats Theorem If x is odd, then x2 is odd Proof Since x is odd, there exists a k ϵ ℤ such that x = 2k + 1. Then, x2 = (2k + 1)2 = 4k2 + 4k + 1 = 2(2k2 + 2k) + 1 = 2m+1. As there exists a m ϵ ℤ such that x2 = 2m + 1, x2 is odd. □ Must be able to translate: Logic ↔ English Logic and Ambiguities The stuff that is kind of doing that thing… Logic? Logic? Logic? Logic? Logic? Logic vs. English Logic English P⋀Q P and Q Both P and Q P⋁Q P or Q careful…. ¬P ¬(P ⋁ Q) not P doesn’t hold, cannot neither P nor Q Logic vs. English Logic English P⇒Q P implies Q If P, then Q P⇔Q P is equivalent to Q P if and only if Q P iff Q For P ⇒ Q: ¬Q ⇒ ¬P (equivalent) Q ⇒ P (not equivalent!) contrapositive converse Ambiguities Do you want beer or water? Do you want beer or water or both? (or) OR Do you want either beer or water? (exclusive-or) If you can solve any problem we pose, then you ace the course If you can solve some problem we pose, then you ace the course OR If you can solve all problems we pose, then you ace the course Always avoid ambiguities! Detail What to describe and what not Definition Mathematical proof A convincing argument for the reader to establish the correctness of a mathematical statement without any doubt. How detailed should a proof be? A Simple Proof Theorem Two distinct circles can have at most two intersection points. A Simple Proof Theorem Two distinct circles can have at most two intersection points. Proof For the sake of contradiction, assume that two distinct circles C1 and C2 have (at least) three intersection points p1, p2, and p3. Note that p1, p2, and p3 must lie on both C1 and C2. But, three points on a circle uniquely define the circle. Thus, C1 = C2. Contradiction! □ A Simple Proof Theorem Two distinct circles can have at most two intersection points. Proof For the sake of contradiction, assume that two distinct circles C1 and C2 have (at least) three intersection points p1, p2, and p3. Note that p1, p2, and p3 must lie on both C1 and C2. But, three points on a circle uniquely define the circle. Thus, C1 = C2. Contradiction! □ A Simple Proof Theorem Two distinct circles can have at most two intersection points. Proof For the sake of contradiction, assume that two distinct circles C1 and C2 have (at least) three intersection points p1, p2, and p3. Note that p1, p2, and p3 must lie on both C1 and C2. But then the center of C1 (and C2) must be at the intersection of the bisector of p1 and p2, and the bisector of p2 and p3. Since the bisectors are lines, and two lines intersect only once, this uniquely defines the center (and radius) of both C1 and C2.Thus, C1 = C2. Contradiction! □ A Simple Proof Theorem Two distinct circles can have at most two intersection points. Proof For the sake of contradiction, assume that two distinct circles C1 and C2 have (at least) three intersection points p1, p2, and p3. Note that p1, p2, and p3 must lie on both C1 and C2. But then the center of C1 (and C2) must be at the intersection of the bisector of p1 and p2, and the bisector of p2 and p3. Since the bisectors are lines, and two lines intersect only once, this uniquely defines the center (and radius) of both C1 and C2.Thus, C1 = C2. Contradiction! □ Proof Detail When is a proof detailed enough? What will the reader accept as true statements More detail results in “more obvious” statements… … but is also much longer A good proof must find the right balance Axiomatic System Axiom A simple statement assumed to be true Which axioms are you allowed to use? ‘Anything’ from high school math (middelbare school) Anything covered in class If you are unsure, just ask! Exception: If you are asked to prove a statement, the statement itself can never be used as an axiom Axioms in this Course Exception: If you are asked to prove a statement, the statement itself can never be used as an axiom Practice What are the axioms? Theorem If x is odd, then x2 is odd Proof Since x is odd, there exists a k ϵ ℤ such that x = 2k + 1. Then, x2 = (2k + 1)2 = 4k2 + 4k + 1 = 2(2k2 + 2k) + 1. Thus, x2 is odd. □ Analogy Proving process Analogy Written proof Analogy The Maze Analogy entrance/premise Proving process Find path between entrance and exit Erratic, creative Written proof Give instructions for others to find path Stick to essentials exit/goal Proving Tips Tips for writing proofs 1. State the proof techniques you’re using (see next lecture) 2. Keep a linear flow Proving process different from written proof 3. Describe every step clearly in words 4. Don’t use complicated notation 5. Make sure your axioms are actually “obvious” What is obvious to you may not be obvious to the reader 6. Finish your proof Connect everything with what you were trying to prove Practice Practice Proofs Theorem (handshaking lemma) For any graph G=(V, E) the number of vertices with odd degree is even. Practice Proofs Theorem (handshaking lemma) Consider a group of people shaking hands with each other (but not necessarily with everyone). The number of people that shook hands with an odd number of people is even. Practice Proofs Theorem (handshaking lemma) For any graph G=(V, E) the number of vertices with odd degree is even. Proof Consider the sum of the degrees of all vertices: Σv d(v). This sum counts every edge exactly twice. Thus the sum is 2*|E|, which is even. Vertices with an even degree do not affect the parity of this sum. Hence, if V’ is the set of vertices with odd degree, then Σv ϵ V’ d(v) must also be even. Since the sum of an odd number of odd numbers is odd, the sum Σv ϵ V’ d(v) is even if and only if |V’| is even. □ Practice Proofs Theorem (Pythagoras) For a right triangle T with side lengths x, y, and z (x ≤ y ≤ z), it holds that x2 + y2 = z2. Practice Proofs Theorem (Pythagoras) For a right triangle T with side lengths x, y, and z (x ≤ y ≤ z), it holds that x2 + y2 = z2. Proof Seems tricky… Let’s try to prove something simpler. What if x = y? Then the area of the triangle is x2/2. We need to prove that 2x2 = z2. So 4 triangles should together have area z2. How about this configuration? z z x y z z z Practice Proofs Theorem (Pythagoras) For a right triangle T with side lengths x, y, and z (x ≤ y ≤ z), it holds that x2 + y2 = z2. Proof Let’s return to the general case. What happens if we again try to form a square with side length z? We get a hole in the middle which seems to be a square. We may have enough for a proof here… z z x z z y z Practice Proofs Theorem (Pythagoras) For a right triangle T with side lengths x, y, and z (x ≤ y ≤ z), it holds that x2 + y2 = z2. Proof Consider the configuration of four copies of T shown in the figure. Note that, for a right triangle, the sum of the non-right angles is 90°, since the sum of all angles is 180°. Hence, the outer shape is a square with side length z and has area z2. The z area of a single triangle is xy/2. Because T is a right triangle, the middle hole is also a square with side length (y - x) and area (y - x)2. We z z thus obtain: z2 = 2xy + (y - x)2 = x2 + y2. □ z Basic Proving Techniques Proving 101… Overview Basic Proving Techniques Overview Basic Proving Techniques 1. Forward-backward method 2. Mathematical induction 3. Case analysis 4. Proof by contradiction Forward-Backward Method How to get from A to B and B to A… Forward-Backward Method The most basic approach Logically combine axioms, definitions, and earlier theorems (forward) Simplify the goal (backward) FACTS This should always be your default approach GOAL Usage When to use? Generally used for statements of the form: If P then Q Premise Reason forward from the premise Reason backward from the goal Goal Basic Example Theorem If my hamsters do excessive exercise, I will be tired in the morning. Proof My hamsters do excessive exercise They are running in their exercise wheel The exercise wheel is making noise Something is keeping me awake I do not get a good night’s sleep I am tired in the morning Example Theorem If 0 ≤ 𝑥 ≤ 2, then – 𝑥3 + 4𝑥 + 1 > 0 Proof Perhaps first plot the function – 𝑥3 + 4𝑥 + 1. Smallest around 𝑥 = 0 and 𝑥 = 2. But −03 + 4 ∗ 0 + 1 = 1, and −23 + 4 ∗ 2 + 1 = 1. It seems that – 𝑥3 + 4𝑥 + 1 ≥ 1 for 0 ≤ 𝑥 ≤ 2. Simplify goal: −𝑥3 + 4𝑥 ≥ 0 for 0 ≤ 𝑥 ≤ 2. Example Theorem If 0 ≤ 𝑥 ≤ 2, then – 𝑥3 + 4𝑥 + 1 > 0 Proof It seems that – 𝑥3 + 4𝑥 + 1 ≥ 1 for 0 ≤ 𝑥 ≤ 2. Simplify goal: −𝑥3 + 4𝑥 ≥ 0 for 0 ≤ 𝑥 ≤ 2. Let’s simplify further: −𝑥3 + 4𝑥 = 𝑥 (4 – 𝑥2) ≥ 0 Also, 𝑥 (4 – 𝑥2) ≥ 0 if 𝑥 ≥ 0 and 4 – 𝑥2 ≥ 0. I think we’re ready for the proof… Example Theorem If 0 ≤ 𝑥 ≤ 2, then – 𝑥3 + 4𝑥 + 1 > 0 Proof We first show that – 𝑥3 + 4𝑥 ≥ 0. We can rewrite the function as : – 𝑥3 + 4𝑥 = 𝑥 (4 – 𝑥2) = 𝑥 (2 – 𝑥) (2 + 𝑥). Since 𝑥, (2 − 𝑥), and (2 + 𝑥) are all nonnegative for 0 ≤ 𝑥 ≤ 2. It follows that 𝑥 (2 – 𝑥) (2 + 𝑥) = – 𝑥3 + 4𝑥 ≥ 0 for 0 ≤ 𝑥 ≤ 2. By adding 1 to both sides we obtain – 𝑥3 + 4𝑥 + 1 ≥ 1 > 0. □ Another Example Theorem If f1(n) = O(g1(n)) and f2(n) = O(g2(n)), then f1(n)f2(n) = O(g1(n)g2(n)) Proof What does f(n) = O(g(n)) mean again? There exist positive constants c and n0 such that f(n) ≤ c g(n) for all n ≥ n0. Another Example Theorem If f1(n) = O(g1(n)) and f2(n) = O(g2(n)), then f1(n)f2(n) = O(g1(n)g2(n)) Proof By definition there exist positive constants c’ and n0’ such that f1(n) ≤ c’ g1(n) for all n ≥ n0’. Similarly, there exist positive constants c’’ and n0’’ such that f2(n) ≤ c’’ g2(n) for all n ≥ n0’’. We need to show that there exist positive constants c and n0 such that f1(n)f2(n) ≤ c g1(n)g2(n) for all n ≥ n0. We must be very careful with variables! Use different names! Another Example Theorem If f1(n) = O(g1(n)) and f2(n) = O(g2(n)), then f1(n)f2(n) = O(g1(n)g2(n)) Proof By definition there exist positive constants c’ and n0’ such that f1(n) ≤ c’ g1(n) for all n ≥ n0’. Similarly, there exist positive constants c’’ and n0’’ such that f2(n) ≤ c’’ g2(n) for all n ≥ n0’’. We need to show that there exist positive constants c and n0 such that f1(n)f2(n) ≤ c g1(n)g2(n) for all n ≥ n0. Another Example Theorem If f1(n) = O(g1(n)) and f2(n) = O(g2(n)), then f1(n)f2(n) = O(g1(n)g2(n)) Proof By definition there exist positive constants c’ and n0’ such that f1(n) ≤ c’ g1(n) for all n ≥ n0’. Similarly, there exist positive constants c’’ and n0’’ such that f2(n) ≤ c’’ g2(n) for all n ≥ n0’’. We need to show that there exist positive constants c and n0 such that f1(n)f2(n) ≤ c g1(n)g2(n) for all n ≥ n0. To establish this, we need to find suitable values for c and n0. Another Example Theorem If f1(n) = O(g1(n)) and f2(n) = O(g2(n)), then f1(n)f2(n) = O(g1(n)g2(n)) Proof By definition there exist positive constants c’ and n0’ such that f1(n) ≤ c’ g1(n) for all n ≥ n0’. Similarly, there exist positive constants c’’ and n0’’ such that f2(n) ≤ c’’ g2(n) for all n ≥ n0’’. We need to show that there exist positive constants c and n0 such that f1(n)f2(n) ≤ c g1(n)g2(n) for all n ≥ n0. To establish this, we need to find suitable values for c and n0. We already are given constants c’, c’’, n0’, and n0’’. Another Example Theorem If f1(n) = O(g1(n)) and f2(n) = O(g2(n)), then f1(n)f2(n) = O(g1(n)g2(n)) Proof By definition there exist positive constants c’ and n0’ such that f1(n) ≤ c’ g1(n) for all n ≥ n0’. Similarly, there exist positive constants c’’ and n0’’ such that f2(n) ≤ c’’ g2(n) for all n ≥ n0’’. We need to show that there exist positive constants c and n0 such that f1(n)f2(n) ≤ c g1(n)g2(n) for all n ≥ n0. To establish this, we need to find suitable values for c and n0. We already are given constants c’, c’’, n0’, and n0’’. Note that f1(n)f2(n) ≤ c’g1(n) c’’g2(n) = c’ c’’ g1(n)g2(n) , Another Example Theorem If f1(n) = O(g1(n)) and f2(n) = O(g2(n)), then f1(n)f2(n) = O(g1(n)g2(n)) Proof By definition there exist positive constants c’ and n0’ such that f1(n) ≤ c’ g1(n) for all n ≥ n0’. Similarly, there exist positive constants c’’ and n0’’ such that f2(n) ≤ c’’ g2(n) for all n ≥ n0’’. We need to show that there exist positive constants c and n0 such that f1(n)f2(n) ≤ c g1(n)g2(n) for all n ≥ n0. To establish this, we need to find suitable values for c and n0. We already are given constants c’, c’’, n0’, and n0’’. Note that f1(n)f2(n) ≤ c’g1(n) c’’g2(n) = c’ c’’ g1(n)g2(n) , but only if n ≥ n0’ and n ≥ n0’’ . So n ≥ n0 should imply n ≥ n0’ and n ≥ n0’’ . Another Example Theorem If f1(n) = O(g1(n)) and f2(n) = O(g2(n)), then f1(n)f2(n) = O(g1(n)g2(n)) Proof By definition there exist positive constants c’ and n0’ such that f1(n) ≤ c’ g1(n) for all n ≥ n0’. Similarly, there exist positive constants c’’ and n0’’ such that f2(n) ≤ c’’ g2(n) for all n ≥ n0’’. We need to show that there exist positive constants c and n0 such that f1(n)f2(n) ≤ c g1(n)g2(n) for all n ≥ n0. Let n0 = max(n0’, n0’’) and c = c’ c’’. Then, for all n ≥ n0 (which implies n ≥ n0’ and n ≥ n0’’), f1(n)f2(n) ≤ c’ g1(n) c’’ g2(n) = c’ c’’ g1(n) g2(n) = c g1(n) g2(n). We have f1(n)f2(n) ≤ c g1(n) g2(n) for all n ≥ n0. Thus, f1(n)f2(n) = O(g1(n)g2(n)). □ Another Example Theorem If f1(n) = O(g1(n)) and f2(n) = O(g2(n)), then f1(n)f2(n) = O(g1(n)g2(n)) Proof By definition there exist positive constants c’ and n0’ such that f1(n) ≤ c’ g1(n) for all n ≥ n0’. Similarly, there exist positive constants c’’ and n0’’ such that f2(n) ≤ c’’ g2(n) for all n ≥ n0’’. We need to show that there exist positive constants c and n0 such that f1(n)f2(n) ≤ c g1(n)g2(n) for all n ≥ n0. Let n0 = max(n0’, n0’’) and c = c’ c’’. Then, for all n ≥ n0 (which implies n ≥ n0’ and n ≥ n0’’), f1(n)f2(n) ≤ c’ g1(n) c’’ g2(n) = c’ c’’ g1(n) g2(n) = c g1(n) g2(n). We have f1(n)f2(n) ≤ c g1(n) g2(n) for all n ≥ n0. Thus, f1(n)f2(n) = O(g1(n)g2(n)). □ Mathematical Induction An introduction to induction… Recurring structures Many algorithms and (data)structures have recurrences sum = 0 Loop for i = 1 to A.length do sum = sum + A[i] List Tree The Idea The Base Idea of Induction P(1) P(2) P(3) … P(n) Base Case One (or more) very simple cases that we can trivially proof. Induction Hypothesis The statement that we want to prove (for any 𝑛). Step Prove that if the statement holds for a small instance, it must also hold for a larger instance. Usage When to use? Whenever you need to prove something is true for all values of 𝑛. (or all values ≥ 𝑥) Infinite possibilities! When there is a clear structure in the problem (e.g., trees) We will talk more about this later Basic Example Theorem If 𝑛 dominos are placed in a row and I push the first; they all fall. Proof: We use induction on 𝑛. Base Case (𝑛 = 1): If there is only 1 domino, it must also be the first. I will push the first over, so trivially they all fall. Basic Example Proof: We use induction on 𝑛. Base Case (𝑛 = 1): If there is only 1 domino, it must also be the first. I will push the first over, so trivially they all fall and the IH holds. Induction Hypothesis: If 𝑛 dominos are placed in a row and I push the first; they all fall. Step: Assume the IH holds for 𝑛 dominos. If there were 𝑛 + 1 dominos in a row, the first 𝑛 form a row of length 𝑛. By IH the first 𝑛 dominos will all fall. As all 𝑛 dominos fall, so must the 𝑛th domino. If the 𝑛th domino falls, then it will tip over the 𝑛 + 1th. The first 𝑛 dominos fall over and the 𝑛 + 1th domino also falls over. So all 𝑛 + 1 dominos fall over. Thus, the IH holds. Basic Example Proof: We use induction on 𝑛. Base Case (𝑛 = 1): If there is only 1 domino, it must also be the first. I will push the first over, so trivially they all fall and the IH holds. Induction Hypothesis: If 𝑛 dominos are placed in a row and I push the first; they all fall. Step: Assume the IH holds for 𝑛 dominos. If there were 𝑛 + 1 dominos in a row, the first 𝑛 form a row of length 𝑛. By IH the first 𝑛 dominos will all fall. As all 𝑛 dominos fall, so must the 𝑛th domino. If the 𝑛th domino falls, then it will tip over the 𝑛 + 1th. The first 𝑛 dominos fall over and the 𝑛 + 1th domino also falls over. So all 𝑛 + 1 dominos fall over. Thus, the IH holds. Example Theorem For all positive integers 𝑛, 3𝑛 − 1 is even. Proof: We use induction on 𝑛. Base Case (n = 1): 31 − 1 = 2, which is indeed even. IH: 3𝑛 − 1 is even. Step (n >= 1): Assume that 3𝑛 − 1 is even. (IH) We need to show that 3𝑛+1 − 1 is even. We have: 3𝑛+1 − 1 = 3 ∗ 3𝑛 − 1 = (2 ∗ 3𝑛 ) + (3𝑛 − 1). A multiplication with an even number is always even (2 ∗ 3𝑛 ). By IH, (3𝑛 − 1) is also even. The sum of two even numbers is also even. Thus, 3𝑛+1 − 1 must be even. The IH holds. □ Practice 1 Theorem For all positive integers 𝑛, 𝑛 𝑘=1 𝑘 = 𝑛(𝑛 + 1)/2 Practice 1 Proof: We use induction on 𝑛. Base case (𝑛 = 1): 𝑛 𝑘=1 𝑘 1 𝑘=1 𝑘 = = 1 = 1 1+1 2 = 𝑛 𝑛+1 2 . As both values equate to the same the IH holds. IH: 𝑛𝑘=1 𝑘 = 𝑛(𝑛 + 1)/2 Step (𝑛 ≥ 1): Suppose that 𝑛𝑘=1 𝑘 = 𝑛(𝑛 + 1)/2. (IH) We need to show that 𝑛+1 𝑘=1 𝑘 = (𝑛 + 1)(𝑛 + 2)/2. We have: 𝑛+1 𝑘=1 𝑘 = = 𝑛 𝑘=1 𝑘 𝑛 𝑛+1 +2(𝑛+1) 2 + 𝑛+1 = = 𝑛 𝑛+1 2 + 𝑛 + 1 (by IH) 𝑛+1 (𝑛+2) . 2 Thus the IH holds and it follows by induction that 𝑛 𝑘=1 𝑘 = 𝑛(𝑛 + 1)/2 for all positive integers 𝑛. □ Practice 2 Theorem For every integer 𝑛 ≥ 5, 2𝑛 > 𝑛2 Practice 2 Proof: We use induction on 𝑛. Base case (𝑛 = 5): 2𝑛 = 25 = 32 > 25 = 52 = 𝑛2 IH: 2𝑛 > 𝑛2 Step (𝑛 ≥ 5): Suppose that 2𝑛 > 𝑛2 (IH). We need to show that 2𝑛+1 > (𝑛 + 1)2 . We have: 2𝑛+1 = 2 ∗ 2𝑛 > 2 ∗ 𝑛2 (by IH) So it is sufficient to show that 2 ∗ 𝑛2 ≥ (𝑛 + 1)2 = 𝑛2 + 2𝑛 + 1 for 𝑛 ≥ 5. This can be simplified to 𝑛2 – 2𝑛 – 1 ≥ 0 or (𝑛 − 1)2 ≥ 2. This is clearly true for 𝑛 ≥ 5. So it follows by induction that 2𝑛 > 𝑛2 for 𝑛 ≥ 5. □ Loop Invariant An introduction to proving loops… Loop Invariant sum = 0 for i = 1 to A.length do sum = sum + A[i] What do we want? How do we prove something is true at the end? What do we really know? Loop Invariant sum = 0 𝑖=1 𝑖=1 for i = 1 to A.length do sum = sum + A[i] What do we really know? At the start: Sum = 0 = Sum first 0 elements Loop Invariant sum = 0 𝑖=2 𝑖=2 for i = 1 to A.length do sum = sum + A[i] What do we really know? At the start: Sum = 0 After first iteration: = Sum first 0 elements Sum = 0 + 𝐴[1] = Sum first 1 element Loop Invariant sum = 0 𝑖=3 for i = 1 to A.length do sum = sum + A[i] What do we really know? At the start: Sum = 0 After first iteration: = Sum first 0 elements Sum = 0 + 𝐴[1] = Sum first 1 element Not an Axiom!! After second iteration: Sum = 0 + 𝐴 1 + 𝐴[2] = Sum first 2 elements Loop Invariant sum = 0 𝑖=3 for i = 1 to A.length do sum = sum + A[i] What do we really know? At the start: Sum = 0 After first iteration: = Sum first 0 elements Sum = 0 + 𝐴[1] After second iteration: = Sum first 1 element Sum = 0 + 𝐴 1 + 𝐴[2] = Sum first 2 elements Loop Invariant Loop Invariants replicate the chain of logical derivations At the start: Sum = 0 After first iteration: = Sum first 0 elements Sum = 0 + 𝐴[1] After second iteration: = Sum first 1 element Sum = 0 + 𝐴 1 + 𝐴[2] = Sum first 2 elements To prove a claim is true at the end, we show… …it is true at the start …if it is true at the start of a random iteration 𝑖, it is still true at the start of the next iteration 𝑖 + 1 …the claim is true at the end of the loop. Loop Invariant Loop Invariants replicate the chain of logical derivations At the start: Sum = 0 After first iteration: = Sum first 0 elements Sum = 0 + 𝐴[1] After second iteration: = Sum first 1 element Sum = 0 + 𝐴 1 + 𝐴[2] = Sum first 2 elements To prove a statement is true at the end, we need.. Invariant (What remains true) Initialization (Starting conditions) Maintenance (Making sure it remains true) Termination (Ending conditions) Basic Example sum = 0 for i = 1 to A.length do sum = sum + A[i] Invariant At the start of iteration 𝑖, 𝑠𝑢𝑚 contains the sum of 𝐴[1. . 𝑖 − 1]. Initialization At the start of the loop 𝑖 = 1 and 𝑠𝑢𝑚 = 0. For the loop invariant to hold, 𝑠𝑢𝑚 must contains the sum of 𝐴 1. . 0 = ∅. The sum of no elements is trivially 0. So 𝑠𝑢𝑚 is correctly set to 0. Basic Example sum = 0 for i = 1 to A.length do sum = sum + A[i] Invariant At the start of iteration 𝑖, 𝑠𝑢𝑚 contains the sum of 𝐴[1. . 𝑖 − 1]. Maintenance At the start of iteration 𝑖, by the loop invariant 𝑠𝑢𝑚 contains the sum of 𝐴[1. . 𝑖 − 1]. In iteration 𝑖, sum is increased by 𝐴[𝑖]. So 𝑠𝑢𝑚 is the sum of elements 𝐴[1. . 𝑖 − 1] + 𝐴[𝑖] = the sum of elements 𝐴 1. . 𝑖 = 𝐴[1. . 𝑖 + 1 − 1]. Thus the invariant will be maintained. Basic Example sum = 0 for i = 1 to A.length do sum = sum + A[i] Invariant At the start of iteration 𝑖, 𝑠𝑢𝑚 contains the sum of 𝐴[1. . 𝑖 − 1]. Maintenance At the start of iteration 𝑖, by the loop invariant 𝑠𝑢𝑚 contains the sum of 𝐴[1. . 𝑖 − 1]. In iteration 𝑖, sum is increased by 𝐴[𝑖]. So 𝑠𝑢𝑚 is the sum of elements 𝐴[1. . 𝑖 − 1] + 𝐴[𝑖] = the sum of elements 𝐴 1. . 𝑖 = 𝐴[1. . 𝑖 + 1 − 1]. Thus the invariant will be maintained. Basic Example sum = 0 for i = 1 to A.length do sum = sum + A[i] Invariant At the start of iteration 𝑖, 𝑠𝑢𝑚 contains the sum of 𝐴 1. . 𝑖 − 1 . Termination The loop terminates when 𝑖 > 𝐴. 𝑙𝑒𝑛𝑔𝑡ℎ, so 𝑖 = 𝐴. 𝑙𝑒𝑛𝑔𝑡ℎ + 1. By the loop invariant we know that 𝑠𝑢𝑚 contains the sum of 𝐴 1. . (𝐴. 𝑙𝑒𝑛𝑔𝑡ℎ + 1) − 1 = 𝐴[1. . 𝐴. 𝑙𝑒𝑛𝑔𝑡ℎ]. This is exactly what we wanted to compute. Next time on: Reading and Writing Mathematical Proofs Next time! “Proving Teletubbies are unlikable!” “How to prove you are cool (and own a Ferrari).” “How to store way too many pigeons.”