Card 08/ 08

RecapDifficulty: Advanced2 min

Recap: Divide and Conquer

Three steps, one recurrence, a tree to sum it, and three cases to shortcut it.

Divide and conquer comes down to the seven ideas below, each listed with the card that covers it. One picture holds them together: the recursion tree, whose shape is set by how many calls each call makes and how small they are, and whose level totals are the cost.

What the topic established, and which card it was on
The ideaWhere
Divide, conquer by recursive calls, combine; the work sits in the divide for quicksort and in the combine for merge sortDivide and Conquer: Split, Solve, Combine
T(n) = a·T(n/b) + f(n): count the calls that run, read their size from the arguments, add the work outside themWrite the Recurrence From the Code
Total each level of the calls and add the levels; 2T(n/2) + n costs n on each of log₂ n + 1 levelsSolve a Recurrence With a Recursion Tree
Compare f(n) with n^(log_b a): the leaves win, they tie and gain a log n, or the root winsThe Master Theorem's Three Cases
Four calls on halves with linear work are O(n²), not O(n log n), because a sits in the exponentAssuming Every Halving Recurrence Is O(n log n)
Pass bounds rather than slices, or the copying adds O(n) to every callDivide and Conquer in Python, Java and TypeScript
One fewer call changes the exponent: three half-size multiplications give O(n^1.585)Practice: Costing a Faster Way to Multiply Large Numbers

The recurrences to keep are these, each solved by the master theorem.

Divide-and-conquer recurrences at a glance
AlgorithmRecurrencen^(log_b a)CaseCost
Binary searchT(n) = T(n/2) + O(1)12O(log n)
Largest by halvesT(n) = 2T(n/2) + O(1)n1O(n)
Merge sortT(n) = 2T(n/2) + O(n)n2O(n log n)
Karatsuba multiplicationT(n) = 3T(n/2) + O(n)n^1.5851O(n^1.585)
Four half-size multiplicationsT(n) = 4T(n/2) + O(n)n²1O(n²)

Together they let you name the three steps of a recursive algorithm and say where its work is done, write its recurrence from the code, solve the recurrence with a recursion tree, and shortcut the tree with the master theorem when the recurrence fits its form.