Divide-and-conquer and greedy choices
A design technique is justified by what makes its subproblems or choices safe. Divide-and-conquer combines recursively solved parts; greedy methods commit to a locally best choice only when an exchange or cut argument supports it.
5.1 Design invariants
Past papers · CS 2014–2026 (23 papers): 3 questions, 4 marks
| Technique | What to identify | Typical applications |
|---|---|---|
| Divide-and-conquer | Smaller inputs, base case, combine cost | Mergesort, quicksort, selection |
| Greedy | A safe irrevocable choice and a preserved feasible optimum | Earliest-finish activities, Huffman, Prim, Kruskal, Dijkstra |
| Dynamic programming | A state sufficient for future choices and an acyclic dependency order | Knapsack, LCS, matrix chains, Floyd–Warshall |
Optimal substructure alone does not justify a greedy algorithm. For a proposed greedy rule, replace the first choice of an optimum by the greedy choice and show feasibility and value do not worsen. If this fails, seek a counterexample or a richer state. Backtracking explores choices and undoes them, typically by DFS; it does not guarantee polynomial time.
5.2 Divide-and-conquer arithmetic
Past papers · CS 2014–2026 (23 papers): 1 question, 2 marks
Reusing a half-size answer matters: computing the same recursive power twice changes the recurrence. Count arithmetic operations here; arbitrary-precision bit costs require a different model.
For exponent bits processed most significant first, start and update , then if the bit is . After any prefix, . Reduce after each operation for modular exponentiation. For a positive exponent with bits this version makes multiplications, including the initial square of ; versions skipping the leading bit have different exact counts.
Strassen's seven products can be remembered through the following recipe. For block matrices with entries , define , , , , , , . Recombine as , , , .
5.3 Interval selection and deadlines
For maximum cardinality of compatible intervals on one resource, sort by increasing finish time and repeatedly take the next compatible interval. Represent an interval as so that a finish at time is compatible with a start at . Assume .
| Objective and assumptions | Greedy choice | Trap |
|---|---|---|
| Most mutually compatible intervals | Earliest finish | Earliest start or shortest duration need not work |
| Maximum profit, unit-time jobs, integer deadlines, all available initially | Descending profit; place each accepted job in the latest free slot at or before its deadline | Variable job durations invalidate this rule |
| Minimum maximum lateness, all jobs available initially, one machine, no preemption | Earliest deadline | Does not minimize number of late jobs in general |
For the unit-job rule, slots are , job finishes at its assigned slot and must satisfy slot . Ignore nonpositive-profit jobs when selection is optional. A direct scan of slots costs after sorting; a disjoint-set slot structure can improve this.
5.4 Fractional knapsack
Sort by value per unit weight, then take as much of each item as fits. This applies when fractions are allowed and values add proportionally. An exchange of equal weight from a lower-density item to an available higher-density item cannot reduce value.
5.5 Huffman coding and optimal merging
Past papers · CS 2014–2026 (23 papers): 3 questions, 6 marks
Repeatedly combine the two smallest current weights and reinsert their sum. A min-heap implements the process in time. The resulting binary tree minimizes total weighted leaf depth; the binary digits on branches may be exchanged without changing the cost.
For a binary prefix code with lengths , Kraft's condition is ; it is also sufficient for the existence of some binary prefix code with those lengths. With at least two positive-probability symbols, Huffman gives , where is entropy in bits/symbol. A one-symbol alphabet needs an explicit empty-code versus one-bit convention. Equal frequencies allow multiple optimal trees and sometimes different length assignments; compare cost, not a unique bit pattern.
Last-minute recall
- Prove a greedy choice safe before applying it repeatedly.
- Optimal substructure is shared by many methods; it is not a greedy proof.
- Compute and reuse the half-power once.
- Interval compatibility here includes touching endpoints.
- Unit-time job scheduling uses the latest available feasible slot.
- Density is exact for fractional, not general 0/1, knapsack.
- Huffman repeatedly takes the two smallest current weights.
- Merge movements and worst comparisons differ by the number of merges.