🖨️ Printing Instructions: Press Ctrl/Cmd + P and select "Save as PDF".
1
Correctness & Loop Invariants
2
Learning Goals
- Understand Pre/Post conditions.
- Master Loop Invariants.
- Prove Insertion Sort correctness.
3
Introduction to Correctness
4
Does it work?
- We learned how to analyze SPEED (Big-O).
- But a fast algorithm that gives the WRONG answer is useless.
- How do we PROVE an algorithm always works?
- Testing is not enough. (Dijkstra: "Testing shows the presence, not the absence of bugs").
5
Formal Verification
- We treat an algorithm like a mathematical theorem.
- Input: Assumptions (Preconditions).
- Output: Conclusion (Postconditions).
- Algorithm: The Proof.
6
Preconditions vs Postconditions
- Precondition: What must be true BEFORE calling the function.
- Example: "Array A must be sorted."
- Postcondition: What will be true AFTER the function returns.
- Example: "Returns index of x or -1."
7
Contracts
- If the Caller satisfies the Precondition...
- The Callee (Function) promises to satisfy the Postcondition.
- If Precondition is violated (e.g., passing null), behavior is undefined.
9
The Loop Invariant Concept
- A statement $P$ that is true at the start of every iteration of the loop.
- It acts like an induction hypothesis.
- Critical for proving loops correct.
10
Three Properties
- 1. Initialization: It is true before the first iteration.
- 2. Maintenance: If it is true before iteration $i$, it remains true before iteration $i+1$.
- 3. Termination: When the loop ends, the invariant gives us the correct answer.
11
Example 1: Summing an Array
12
function Sum(A[0..n-1])
sum = 0
i = 0
while i < n:
sum = sum + A[i]
i = i + 1
return sum13
Defining the Invariant
- What have we done so far at step $i$?
- Invariant: At line 4 (while loop check), variable `sum` holds the sum of the subarray $A[0..i-1]$.
14
Proof: Initialization
- Before first loop:
- $i=0$. `sum` = 0.
- Subarray $A[0..-1]$ is empty.
- Sum of empty set is 0.
- Statement is TRUE.
15
Proof: Maintenance
- Assume invariant holds for $i$ (`sum` = $\sum_{k=0}^{i-1} A[k]$).
- Body of loop:
- `sum_new` = `sum` + $A[i]$.
- `i_new` = $i + 1$.
- After body: `sum_new` = $(\sum_{k=0}^{i-1} A[k]) + A[i]$ = $\sum_{k=0}^{i} A[k]$.
- This matches $A[0 .. i_{new}-1]$.
- Statement is TRUE.
16
Proof: Termination
- Loop ends when: $i < n$ is false. So $i = n$.
- Invariant says: `sum` holds sum of $A[0..n-1]$.
- Conclusion: This is exactly the sum of the entire array.
- Algorithm is CORRECT.
17
Example 2: Finding Maximum
18
function FindMax(A[0..n-1])
max_val = -infinity
for i = 0 to n-1:
if A[i] > max_val:
max_val = A[i]
return max_val19
Invariant for Max
- Invariant: At the start of iteration $i$, `max_val` contains the maximum value in $A[0..i-1]$.
20
Initialization
- $i=0$. Subarray empty.
- Max of empty set is $-\infty$ (identity element).
- True.
21
Maintenance
- Assume `max_val` is max of $A[0..i-1]$.
- We inspect $A[i]$.
- If $A[i] > max\_val$, we update.
- New `max_val` is $\max(old\_max, A[i])$.
- This is exactly the max of $A[0..i]$.
- True.
22
Termination
- Variable $i$ goes up to $n$.
- Invariant says `max_val` is max of $A[0..n-1]$.
- Returns correct answer.
23
Example 3: Insertion Sort
24
Insertion Sort Concept
- Like sorting cards in your hand.
- Left hand holds sorted cards.
- Right hand picks next card and inserts it into correct spot.
25
for j = 1 to n-1:
key = A[j]
i = j - 1
while i >= 0 and A[i] > key:
A[i+1] = A[i]
i = i - 1
A[i+1] = key26
Outer Loop Invariant
- Invariant: At start of iteration $j$, the subarray $A[0..j-1]$ consists of the elements originally in $A[0..j-1]$ but in sorted order.
27
Initialization
- $j=1$. Subarray is $A[0..0]$ (Single element).
- A single element is always sorted.
- True.
28
Maintenance Step (Tricky)
- We need to show that inserting $A[j]$ preserves the sorted property.
- The Inner While Loop shifts elements larger than `key` to the right.
- It opens a hole at correct position $i+1$.
- We place `key` there.
- Now $A[0..j]$ is sorted.
29
Termination
- Loop ends when $j=n$.
- Invariant says $A[0..n-1]$ is sorted.
- Done!
30
Interactive: Insertion Sort
32
Proving the Inner Loop
- The inner loop also has an invariant!
- Invariant: Elements $A[i+1..j]$ are greater than `key`.
- And they are sorted relative to each other (shifted versions of original sorted array).
33
Correctness of Recursion
34
Mathematical Induction
- To prove recursive function $F(n)$ works:
- 1. Base Case: Prove $F(base)$ works.
- 2. Inductive Step: Assume $F(k)$ works for $k < n$. Prove $F(n)$ works using that assumption.
37
Off-by-one Errors
- Often caused by wrong initialization or termination logic.
- Invariant analysis helps catch these.
- Does loop go to $n$ or $n-1$?
- Check Invariant termination.
38
Infinite Loops
- We must prove the loop makes progress.
- Example: $i$ increases by 1.
- Example: Binary Search window size decreases.
40
function BadSum(A, n)
sum = 0
i = 1 // Bug here?
while i < n:
sum = sum + A[i]
i = i + 1
return sum41
Analysis of Bug 1
- Init: $i=1$. `sum`=0.
- Invariant says sum should be $A[0..0]$? No.
- This skips $A[0]$.
- Correct initialization: $i=0$.
43
function BinarySearch(A, x)
...
if x < A[mid]:
high = mid // Bug here?
else:
low = mid44
Analysis of Bug 2
- If $low = mid$ and $high = low + 1$,
- $mid = low$.
- If we go to else branch ($low = mid$),
- Values don't change! Infinite loop.
- Correct: $low = mid + 1$.
45
Writing Your Own Proofs
46
Template for Assignment
- 1. State the Invariant clearly.
- 2. Init: Show it holds at start.
- 3. Maintenance: Show it is preserved.
- 4. Termination: Show what it implies at end.
- Don't just wave hands!
47
Common Pitfalls & Anti-Patterns
48
Watch Out For...
- Forgetting to prove 'Termination' properties.
- Stating invariant that isn't useful for the goal.
50
Activity 1: Sum Array
- ❓ Problem:
- Invariant for code summing array A.
-
- Take 2 minutes to solve this.
51
Solution 1
- 💡 Answer:
- At start of loop i, 'sum' equals sum of A[0..i-1].
52
Activity 2: Max Finding
- ❓ Problem:
- Invariant for Finding Max.
-
- Take 2 minutes to solve this.
53
Solution 2
- 💡 Answer:
- 'max' holds largest value in A[0..i-1].
54
Activity 3: Selection Sort
- ❓ Problem:
- State Invariant.
-
- Take 2 minutes to solve this.
55
Solution 3
- 💡 Answer:
- Subarray A[0..i-1] contains the i smallest elements in sorted order.
56
Activity 4: Binary Search
- ❓ Problem:
- State Invariant.
-
- Take 2 minutes to solve this.
57
Solution 4
- 💡 Answer:
- If target exists, it is in A[low..high].
58
Interactive: Binary Search
59
Activity 5: Termination
- ❓ Problem:
- Why does Binary Search terminate?
-
- Take 2 minutes to solve this.
60
Solution 5
- 💡 Answer:
- Range size (high-low) decreases strictly monotonically.
61
Activity 6: Bubble Sort
- ❓ Problem:
- State Invariant (Outer loop).
-
- Take 2 minutes to solve this.
62
Solution 6
- 💡 Answer:
- Subarray A[n-i..n] contains the i largest elements in sorted order.
63
All Interactive Demos
- 📊 Basic Invariants:
- 📏 Searching:
- 🔢 Sorting:
64
Supplementary Resources