3.7.2 · D3Algorithm Paradigms

Worked examples — Divide and conquer — template, correctness, recurrence

3,647 words17 min readBack to topic

This page is a drill. The parent note built the machinery — the template, the recurrence , and the Master Theorem. Here we throw every kind of input at that machinery and grind out the answer, so that when an exam or a real problem hands you a recurrence, you have already seen its shape.

Before we start, one reminder of what each letter means, because we will not re-derive it — we will use it every single time:

Recall The numbers you always read off first

Question ::: In , what are , , ? ::: how many subproblems each call spawns ::: the factor the input shrinks by (size goes ) ::: the non-recursive work — divide cost plus combine cost ::: the derived watershed exponent , i.e. the cost of the leaves (computed from , not read off)

The whole game is: compute , then compare against . Three outcomes are possible — smaller, equal, bigger — and there are also degenerate recurrences where this comparison does not even apply. That is exactly our matrix.


The scenario matrix

Every divide-and-conquer recurrence you meet falls into one of these cells. The columns are how sits relative to the watershed ; the rows are the special / degenerate inputs that break the naive routine.

Cell What makes it this cell Example we work
A — Balanced (Case 2) : divide-cost matches leaf-cost Ex 1 (Mergesort), Ex 8 (word problem)
B — Leaf-heavy (Case 1) is polynomially smaller than Ex 2 (Karatsuba)
C — Root-heavy (Case 3) is polynomially bigger, plus regularity holds Ex 3
D — Single subproblem () tree is a chain, Ex 4 (Binary Search)
E — Uneven / degenerate split subproblem sizes differ, or one is size ; shrink is not a constant factor (), so floors/ceilings can no longer be waved away Ex 5 (bad Quicksort pivot)
F — Basic Master Theorem fails only a log factor off the watershed — needs the extended rule or the tree Ex 6
G — Regularity fails looks root-heavy but Ex 7
H — Exam twist shrink / disguised recurrence Ex 8 (word problem), Ex 9

We now hit every cell.


Cell A — Balanced (Master Theorem Case 2)

Figure — Divide and conquer — template, correctness, recurrence

Read the figure: each row is one level of recursion. The black dots are the subproblems; the red label on the right shows their total work. Notice the punchline — although the dots double every level and shrink by half, their widths cancel, so every red label reads . There are rows (the black double-arrow on the left), and work per row times rows is exactly the we computed.


Cell B — Leaf-heavy (Master Theorem Case 1)


Cell C — Root-heavy (Master Theorem Case 3)

Figure — Divide and conquer — template, correctness, recurrence

Read the figure: each bar is the total work at one recursion level, measured in units of . Unlike Ex 1's flat profile, here the bars shrink geometrically — each is half the previous. The tall red bar at depth is the root, and because the bars fall off so fast, the entire tower sums to just that first bar. That is what "the root dominates" looks like, and it is why the answer is , the cost of the root alone.


Cell D — Single subproblem ()

Figure — Divide and conquer — template, correctness, recurrence

Read the figure: because , the picture is a single vertical stack, not a branching tree — one dot per level, each labelled with its shrinking size () and its constant work. The red dot at the bottom is the base case, size . The black double-arrow counts the rungs: of them, each doing , so the total is .


Cell E — Uneven / degenerate split


Cell F — Basic Master Theorem fails (the log-factor gap)


Cell G — Regularity fails (root-heavy in shape only)


Cell A / H — Real-world word problem


Cell H — Exam twist ( shrink)


Recall Self-test

solves to? ::: , ties → Case 2 → solves to? ::: not Master-able (); unroll → Why does the tournament give not ? ::: the depth is , but total work (leaves) dominates → matches

Related paradigms build on this same recurrence machinery: Strassen Matrix Multiplication (a Case-1 win like Karatsuba), Mathematical Induction (the correctness engine), and Dynamic Programming (what you reach for when subproblems overlap instead of being independent).