Proof Strategies for Algorithms I
Why Proofs Matter in Algorithms I
The exam frequently asks for proofs: correctness of an algorithm, a height bound for a tree, optimality of a greedy choice, or tightness of an asymptotic bound. Marks are awarded for clarity, rigour, and correctly identifying the induction variable and hypothesis.
Two main proof strategies appear throughout the course: breakpoint induction (loop invariants) and along-a-path induction (used in Algorithms II but also relevant in Algorithms I for tree and heap properties).
Breakpoint Induction (Loop Invariants)
Used when an algorithm contains a loop. The invariant is a property that holds at a specific “breakpoint” — typically the start of each loop iteration.
Three-Part Structure
- Initialisation: the invariant holds before the first iteration.
- Maintenance: if the invariant holds at the start of an iteration, it holds at the start of the next.
- Termination: when the loop terminates, the invariant (combined with the termination condition) proves the algorithm’s correctness.
Example: Insertion Sort Correctness
Invariant P: At the start of each iteration of the FOR loop (index ), the subarray contains the same elements as originally in positions , but in sorted order.
Initialisation (): is trivially sorted (one element).
Maintenance: The WHILE loop shifts larger elements right by one position, creating space for at its correct sorted position within . After insertion, is sorted. The next iteration starts with , so is sorted.
Termination: When , the invariant says is sorted. This is exactly the desired output.
Example: Partition Correctness
Invariant P: At the start of each FOR loop iteration (with index ), for any array index :
- If , then (the pivot).
- If , then .
- If , then .
Initialisation: , so no satisfies condition 1 (vacuously true); similarly condition 2 vacuous. Condition 3 holds because .
Maintenance: Case split on whether or . In both cases, incrementing and swapping maintains the three regions.
Termination: , so the unprocessed region is empty. The final swap of with places the pivot correctly, satisfying the post-condition: all elements left of the pivot are pivot, all right of pivot are pivot.
Pitfalls in Loop Invariant Proofs
- Vague invariants: “the array is sorted so far” is too vague. Must specify which subarray, what “sorted” means, and the relationship to the original input.
- Missing the induction variable: must specify what the induction is over (the loop counter , the number of iterations, etc.).
- Invariant that is true but useless: e.g. "" holds throughout but proves nothing.
- Forgetting the termination condition: the invariant must combine with the loop exit condition to yield correctness.
Along-a-Path Induction
Used in graph and tree algorithms where correctness propagates along a path or from root to leaves.
Structure: Prove a property for vertices on a shortest path (or along a tree traversal), showing:
- Base: holds for the start vertex (or root).
- Inductive step: If holds for a vertex on the path, and the algorithm processes ‘s successor next, then holds for after processing.
Example (BFS shortest paths) used in Algorithms II but with relevance to Algorithms I proofs: prove that when BFS first discovers a vertex at distance , the discovered path is indeed a shortest path.
Proving Height Bounds for Balanced Trees
B-Tree Height
Claim: For a B-tree with minimum degree and keys, .
Proof: Count minimum keys per level. Root: key. Level 1: keys (minimum keys in each of 2 nodes). Level 2: keys. Level : keys. Summing the geometric series for levels (root through deepest internal nodes) and rearranging gives the bound.
Red-Black Tree Height
Claim: .
Proof: Show the subtree rooted at any node has internal nodes (by induction on the subtree height). The black-height of the root (at least half the nodes on any path are black by Property 4). Hence , so .
Proving Greedy Optimality (Exchange Argument)
Structure:
- Assume an optimal solution exists.
- Show that the greedy choice can be swapped into without decreasing optimality.
- By induction, iteratively transforming to match greedy choices yields a greedy solution that is also optimal.
Example: Activity selection (choose max number of non-overlapping activities). Greedy choice: pick activity with earliest finish time. Proof: if the optimal solution does not include the earliest-finishing activity, replacing its first activity with the earliest finisher cannot increase conflicts (the earliest finisher leaves the maximum remaining time for others).
Proving Asymptotic Bounds
From definitions: means such that .
Example: Show . Choose and . For : .
Example: Show . For any , for sufficiently large (since dominates). Use proof by contradiction: assume for large , divide by , get , which fails as .
Exam Proof Checklist
When writing a proof in the Tripos:
- State the inductive hypothesis clearly: what is and what is ?
- Specify the induction type (ordinary, strong, over program execution).
- Cover base case(s) explicitly.
- Show the inductive step (or for strong induction).
- Conclude explicitly: “Therefore, the algorithm is correct” (or the bound holds).
- Avoid vague language: “clearly”, “obviously”, “it follows that” without justification.
Summary
| Proof Strategy | When to Use | Example |
|---|---|---|
| Loop invariant (breakpoint) | Algorithm contains a loop | Insertion sort, partition, BFS |
| Along-a-path induction | Graph/tree traversal | BFS distances, Dijkstra (Algs II) |
| Induction on tree height | Balanced tree bounds | B-tree/RBT height proofs |
| Exchange argument | Greedy optimality | Activity selection |
| Asymptotic from definition | proofs | |
| Induction variable | Must be explicit | (loop counter), (tree size), (distance) |