3.7.2 · D4Algorithm Paradigms

Exercises — Divide and conquer — template, correctness, recurrence

3,176 words14 min readBack to topic

Before we start, let us pin down the ONE machine we use again and again. The parent note derived it, so here we only restate the vocabulary so no symbol appears unexplained.

See the parent's Master Theorem and Recurrence Relations for the deep "why". Here we drill it.


Level 1 — Recognition

Goal: read off a recurrence and name the case. No calculation trickery.

Exercise 1.1

State , , and for .

Recall Solution
  • (four recursive calls), (each child is half-size), .
  • .
  • Compare with : since , is polynomially smallerCase 1.
  • Answer: .

Exercise 1.2

Which of the three phases (Divide / Conquer / Combine) does the merge step of Mergesort belong to, and which phase does the "cut the array in half" step belong to?

Recall Solution
  • "Cut the array in half" is Divide — it produces the two subproblems.
  • merge is Combine — it glues two sorted halves into one sorted whole.
  • The two recursive mergesort calls are the Conquer phase.

Exercise 1.3

Match each algorithm to its recurrence: Binary Search, Mergesort, Karatsuba. Recurrences: ; ; .

Recall Solution
  • Binary Search (one recursive call, constant compare).
  • Mergesort (two halves, linear merge).
  • Karatsuba (three half-size multiplies, linear add/shift).

Level 2 — Application

Goal: run the Master Theorem end to end and get a bound.

Exercise 2.1

Solve .

Recall Solution
  • .
  • .
  • vs : Case 1.
  • .

Exercise 2.2

Solve .

Recall Solution
  • ; .
  • vs : , so is polynomially larger ⟹ candidate Case 3.
  • Regularity check: . With ✓.
  • Case 3 confirmed: .

Exercise 2.3

Solve — the recurrence of Strassen Matrix Multiplication.

Recall Solution
  • ; .
  • vs : smaller ⟹ Case 1.
  • .
  • Meaning: Strassen beats naïve matrix multiply because .

Level 3 — Analysis

Goal: recurrences where the Master Theorem is subtle or fails; use the recursion tree.

The recursion tree is our fallback: draw the work at every level, then sum the levels. The figure below shows the same three qualitative shapes as three horizontal bar charts, one per case. Each row is one level of the tree (level 0 = root at the top, deeper levels below) and each bar's length is the total work done at that level. Left panel (Case 1, coral): bars grow longer as you go down — the bottom (leaves) does the most work, so the sum is dominated by the leaves. Middle panel (Case 2, lavender): every bar is the same length — all levels tie, so the total is (work per level) (number of levels ). Right panel (Case 3, mint): bars shrink as you go down — the top (root) dominates. Each exercise below is really asking "which of these three pictures am I in?".

Figure — Divide and conquer — template, correctness, recurrence
Figure s01 — Recursion tree, three regimes. Row = tree level (top is the root); bar length = total work at that level. Coral grows downward (leaves win, Case 1); lavender is flat (all levels tie, Case 2); mint shrinks downward (root wins, Case 3).

Exercise 3.1

Explain why the plain three-case Master Theorem does not apply to , then solve it by the recursion tree.

Recall Solution
  • , so and . Here .
  • Is polynomially bigger than ? We would need for some . But grows slower than for every , so no such exists. The gap is only logarithmic: is neither polynomially smaller (Case 1) nor (plain Case 2) nor polynomially larger (Case 3). So the plain three-case statement gives no answer. (It is covered by the generalized Case 2 with from our definition above — and we confirm that answer by hand below.)
  • Recursion tree. Level has nodes each of size , doing work. Level total:
  • Sum over to :
  • Answer: — matching generalized Case 2 ().

Exercise 3.2

Solve (unequal splits — a job for Master Theorem's cousin, but do it by tree bounds).

Recall Solution
  • The two children shrink by different factors, so plain MT (which needs one clean ) does not apply.
  • A monotonicity note first. All the bounds below use that is nondecreasing in (bigger input ⟹ at least as much work). This holds because the recurrence's non-recursive term is nondecreasing and the recursive calls are on nonnegative sizes, so a standard induction shows whenever . We assume this throughout.
  • Upper bound. Since is nondecreasing, , so replacing the smaller child by the bigger one can only increase the total: , which is Case 2 ⟹ .
  • Lower bound. The root alone does work, so .
  • Why the total input size at each level multiplies by . Each node of size spawns two children of sizes and , whose sizes add to . So if the sizes of all nodes at level add up to , then summing this over every node gives . Starting from (the root), we get .
  • Sharper tree sum. The non-recursive work at a node equals its size (the term is linear), so level- work , a geometric series:
  • Since the series converges, the root dominates: (which also tightens the loose upper bound above).

Exercise 3.3

For (Mergesort), prove by Mathematical Induction that for a suitable constant (take , for , a power of 2).

Recall Solution
  • Guess: . We find by induction on powers of 2.
  • Base : . We need , i.e. .
  • Step: assume . Then
  • This is iff , i.e. . Combined with the base's , choose .
  • Therefore . ∎

Level 4 — Synthesis

Goal: design a divide-and-conquer algorithm and derive its recurrence yourself.

Exercise 4.1

Design an algorithm to find the maximum element of an unsorted array using divide and conquer. State its three phases, write the recurrence, and solve it. Compare to the obvious linear scan.

Recall Solution
  • Recall from the header that the non-recursive term is : the divide cost plus the combine cost.
  • Divide: split the array into left and right halves (, ; the split is just index arithmetic, so ).
  • Conquer: recursively find the max of each half.
  • Combine: return the larger of the two returned maxima — one comparison, so .
  • Hence .
  • Base: a single element is its own max.
  • Recurrence: . Then , ; since Case 1.
  • Comparison: same as a linear scan — divide and conquer buys nothing here because there is no cheap combine advantage; every element must still be looked at once. (Useful lesson: D&C only wins when the combine is cheaper than re-solving.)

Exercise 4.2

You have two already-sorted arrays of total length and want their merged sorted output. Someone proposes: recursively merge the first half of each with the second half of each (). Is this a valid divide-and-conquer merge? If not, what is the correct combine cost, and what recurrence does the standard mergesort merge give?

Recall Solution
  • The proposal is not valid: to split each sorted array "in half" and merge crosswise, the halves you pick are not guaranteed to interleave cleanly — an element in the left half of array A can be larger than one in the right half of array B, so the four merges overlap. The subproblems are not independent, breaking the paradigm's precondition.
  • The correct merge is a single linear pass with two pointers: , no recursion inside the merge itself.
  • Inside Mergesort the full recurrence is where the is this two-pointer merge. Case 2 ⟹ .

Exercise 4.3

Karatsuba multiplies two -digit numbers with giving . Suppose a researcher invents a scheme that splits each number into 3 parts (so ) and needs only 5 multiplications, with linear combine: . Is it faster than Karatsuba?

Recall Solution
  • New: , .
  • ; Case 1.
  • Karatsuba is .
  • Since , the new scheme is asymptotically faster. (This is the real Toom–3 algorithm.)

Level 5 — Mastery

Goal: prove a general statement or find the exact threshold where a design decision flips the answer.

Exercise 5.1

Consider the family (one fixed shrink factor , but a varying number of subproblems ). Find every value of at which the asymptotic solution changes form, and state the closed-form bound in each regime.

Recall Solution
  • ; compare against .
  • : , so is bigger ⟹ Case 3 (regularity: with ✓) ⟹ .
  • : , tie ⟹ Case 2 ⟹ .
  • : , leaves bigger ⟹ Case 1 ⟹ .
  • The single transition point is : below it the root dominates (), exactly at it every level ties (), and above it the leaves dominate ().

Exercise 5.2

Prove the leaf count of the tree for is , and hence explain why the leaf work is when each leaf costs . Use Mathematical Induction on the depth.

Recall Solution
  • The tree has depth (each step divides size by until size 1: ).
  • Claim: level has nodes. Base : one root, ✓. Step: if level has nodes and each spawns children, level has ✓. By induction the claim holds for all .
  • Leaves sit at , so their count is
  • Rewrite as . Take the natural log of the left side: . Take the natural log of the right side: . Both equal , and is one-to-one, so the original quantities are equal:
  • Hence the number of leaves is . Each leaf does work, so total leaf work . This is exactly the watershed cost from the parent note. ∎

Exercise 5.3

A student claims: "Since more subproblems always add work in the exponent, the fewest subproblems () is always fastest." Give a concrete recurrence where increasing from 1 to 8 leaves the asymptotic time unchanged, and explain why.

Recall Solution
  • Keep in the same case for the whole range. Take and , so ranges from (at ) up to (at ).
  • To keep strictly the dominant term we need , i.e. , i.e. . So for : is polynomially larger than Case 3, independent of .
  • Why unchanged: in Case 3 the root work dominates the whole sum; the number of subproblems only affects the (geometrically smaller) lower levels, which collapse into the constant. Increasing within the root-heavy regime does not touch the asymptotics.
  • So the student is wrong: matters only when it pushes you across a case boundary (here, reaching where ties and flips to Case 2, ).

Recall Quick self-check (cloze)

The leaf exponent is ====. Master Theorem's plain three cases fail when the gap between and is only logarithmic (not polynomial). Case 3 requires the extra regularity condition with .

Which method solves and what does it give?
The recursion-tree method (or generalized Case 2); it gives .
In , at which does the answer become ?
Exactly (Case 2 tie).