Learning objectives
- Choose the most suitable proof method for a given statement.
- Prove a recursive function correct by induction.
- Use a loop invariant to prove a loop correct.
- Find and explain the error in a flawed proof.
Roman Urdu. Is lecture mein hum proof ke tamam tareeqon ko aik jagah jama karte hain aur dekhte hain ke kaun sa tareeqa kab istemal karna chahiye. Hum induction se recursive function aur loop ki correctness sabit karte hain, aur ek ghalat proof ki ghalti pakadna seekhte hain.
Core concepts
Proof strategy summary
| Statement looks like | First method to try | Why |
|---|---|---|
| p → q with a clear definition for p | Direct proof | Unpack p, build q |
| p → q with "not" in q, or q easy to negate | Contraposition | ¬q gives clean algebra |
| "There is no…", "… is irrational" | Contradiction | Negation gives a concrete object |
| ∀n ≥ n₀, formula or property of n | Induction | Property builds step by step |
| Property depends on smaller cases | Strong induction | Hypothesis covers all earlier cases |
| ∃x P(x) | Constructive: show a witness | One example suffices |
| ∀x P(x) false | Counterexample | One example suffices |
| p ↔ q | Prove both directions | Two implications |
Loop invariant. A condition that is true before the loop starts and stays true after every iteration. A proof has three parts: initialisation (true before the first iteration), maintenance (if true before an iteration, true after it) and termination (when the loop ends, the invariant gives the wanted result). It is induction on the number of iterations.
Worked examples
Example 1 (basic). Choose the method. (a) "If n² is odd then n is odd": contraposition (or direct on the contrapositive). (b) "There is no rational number whose square is 2": contradiction. (c) "For every n ≥ 1, 1 + 2 + … + n = n(n + 1)/2": induction. (d) "Every amount ≥ 12 can be paid with 4- and 5-cent stamps": strong induction.
Example 2 (intermediate). A recursive function. Define fact(0) = 1 and fact(n) = n · fact(n − 1) for n ≥ 1. Prove fact(n) = n! for all n ≥ 0. Basis: fact(0) = 1 = 0!. Step: assume fact(k) = k!. Then fact(k + 1) = (k + 1)·fact(k) = (k + 1)·k! = (k + 1)!. ∎ The structure of the proof copies the structure of the recursion.
Example 3 (advanced). A loop invariant. Code: s = 0; for i = 1 to n: s = s + i. Claim: when the loop ends, s = n(n + 1)/2.
Invariant: at the start of the iteration with index i, s = (i − 1)i/2.
- Initialisation: i = 1, s = 0 = 0·1/2. True.
- Maintenance: after the iteration, s = (i − 1)i/2 + i = i(i + 1)/2, which is the invariant for index i + 1.
- Termination: the loop ends with i = n + 1, so s = n(n + 1)/2. ∎
Example 4 (error analysis). Find the flaw. "Theorem: all horses in any set of n horses have the same colour. Basis n = 1: trivial. Step: assume it for k horses. Take k + 1 horses. Remove one: the remaining k horses share a colour. Put it back and remove another: again the remaining k share a colour. The two groups overlap, so all k + 1 share a colour." The flaw is in the step for k = 1. With 2 horses, removing one leaves {horse B} and removing the other leaves {horse A}. The two groups do not overlap, so the argument does not link them. The step P(k) → P(k + 1) fails for k = 1, so the induction collapses even though the basis is true.
Computer science connection
Program verification and formal specification (CLO-3): loop invariants, preconditions and postconditions are the basis of tools such as Dafny, Frama-C and Hoare logic; proofs of recursive algorithms; reasoning about correctness before testing.
Key takeaways
- Match the proof method to the shape of the statement.
- Loop invariants prove loops correct. They are induction on iterations.
- Every step of a proof must be justified for all cases it claims to cover.
Roman Urdu. Direct, contraposition, contradiction, induction aur strong induction — har tareeqay ka apna maqam hai. Loop invariant mein teen cheezen: shuru mein sahih, har iteration ke baad sahih, aur akhir mein matlub natija. Induction ka step har k ke liye sahih hona chahiye, warna proof ghalat hai.
Five revision questions
- For each statement, name the best proof method: (a) √5 is irrational; (b) the sum of two even integers is even; (c) 2ⁿ ≥ n + 1 for n ≥ 0.
- State the three parts of a loop-invariant proof.
- Prove by induction that fact(n) ≥ 1 for all n ≥ 0.
- Why does the horse proof fail only at k = 1 and not elsewhere?
- Write an invariant for a loop that computes 2ⁿ by repeated doubling.
Homework (assignment 4). (a) Prove the correctness of a recursive function that sums the first n positive integers. (b) State and prove a loop invariant for a loop computing n! iteratively. (c) Write a one-page comparison of the five proof methods with one example each from your own course material.
Next: Week 5, Lectures 13–15 — Set Theory (coming soon).
Quiz
'Har prime odd hai' ko ghalat sabit karne ka tareeqa?
Ek counterexample kaafi hai: 2 prime hai magar even.
All lectures
- Lecture 0: Discrete vs Continuous
- Lecture 1: Propositions and Connectives
- Lecture 2: Truth Tables, Tautologies and Contradictions
- Lecture 3: Logical Equivalence and the Laws of Logic
- Lecture 4: Predicate Logic and Quantifiers
- Lecture 5: Rules of Inference
- Lecture 6: Logical Arguments and Puzzle Solving
- Lecture 7: Direct Proof and Proof by Implication
- Lecture 8: Proof by Contraposition
- Lecture 9: Proof by Contradiction
- Lecture 10: Mathematical Induction
- Lecture 11: Strong Induction
- Lecture 12: Applications of Proofs and Proof Strategy
START WELL TO END WELL