Introduction

Divide and conquer solves a problem by splitting it into smaller instances of the same problem, solving those recursively, and combining their answers. Merge sort, quicksort and binary search are the familiar examples. This note states the paradigm, shows how its running time becomes a recurrence, and works through the other standard examples: the minimum and maximum together, fast exponentiation, the maximum subarray, and fast integer and matrix multiplication.

The pattern pays off when the combine step is cheap compared with solving the whole problem directly, or when the split lets us avoid work (binary search discards half; Strassen's method uses 7 recursive products instead of 8).

Prerequisites

Core concepts

The three steps

  1. Divide the instance of size nn into aa subinstances, each of size about n/bn/b.
  2. Conquer each subinstance recursively; instances below a fixed size are solved directly (the base case).
  3. Combine the sub-answers into the answer for the whole instance.
Flowchart: a problem of size n is divided into subproblems of size n/b, each is solved recursively into a sub-answer, and the sub-answers are combined into the answer for size n.
Figure 1. The divide-and-conquer pattern: divide a problem of size nn into aa subproblems of size n/bn/b, solve each recursively, and combine the sub-answers.

The running-time recurrence

If dividing and combining together cost f(n)f(n), the running time satisfies

T(n)=a T(n/b)+f(n),T(n)=Θ(1) for n≤n0.T(n) = a\,T(n/b) + f(n), \qquad T(n) = \Theta(1) \text{ for } n \le n_0 .

The master theorem compares f(n)f(n) with nlog⁡ban^{\log_b a}, the number of leaves of the recursion tree. Two ways to make an algorithm faster follow directly: reduce aa (fewer recursive calls, as in Karatsuba and Strassen) or reduce f(n)f(n) (a cheaper combine).

Algorithmaabbf(n)f(n)T(n)T(n)
Binary search12Θ(1)\Theta(1)Θ(lg⁡n)\Theta(\lg n)
Fast exponentiation12Θ(1)\Theta(1)Θ(lg⁡n)\Theta(\lg n) multiplications
Maximum and minimum2222 comparisons3n/2−23n/2 - 2 comparisons (n≥2n\ge2 a power of 2)
Merge sort22Θ(n)\Theta(n)Θ(nlg⁡n)\Theta(n \lg n)
Maximum subarray22Θ(n)\Theta(n)Θ(nlg⁡n)\Theta(n \lg n)
Karatsuba multiplication32Θ(n)\Theta(n)Θ(nlg⁡3)≈Θ(n1.585)\Theta(n^{\lg 3}) \approx \Theta(n^{1.585})
Matrix product, 8 subproducts82Θ(n2)\Theta(n^2)Θ(n3)\Theta(n^3)
Strassen72Θ(n2)\Theta(n^2)Θ(nlg⁡7)≈Θ(n2.807)\Theta(n^{\lg 7}) \approx \Theta(n^{2.807})

For binary search, merge sort and quicksort see Searching, Merge Sort and Quicksort.

Algorithms and pseudocode

Pseudocode uses 1-based arrays, <- for assignment and A[i..j] for the subarray from position ii to position jj.

Maximum and minimum together

Finding the maximum alone needs n−1n - 1 comparisons, and the minimum alone another n−1n - 1. Divide and conquer finds both with fewer:

Text
MAX-MIN(A, i, j)
1  if i = j then
2      return (A[i], A[i])
3  if j = i + 1 then
4      if A[i] > A[j] then return (A[i], A[j])
5      else return (A[j], A[i])
6  mid <- floor((i + j) / 2)
7  (max1, min1) <- MAX-MIN(A, i, mid)
8  (max2, min2) <- MAX-MIN(A, mid + 1, j)
9  return (max(max1, max2), min(min1, min2))      // 2 comparisons

The comparison count satisfies C(1)=0C(1) = 0, C(2)=1C(2) = 1 and C(n)=2C(n/2)+2C(n) = 2C(n/2) + 2. For n=2kn = 2^k with k≥1k\ge1 (n≥2n\ge2), the solution is exactly

C(n)=3n2−2.C(n) = \frac{3n}{2} - 2 .

Proof by unrolling: there are n/2n/2 leaves of size 2 costing 1 each, and n/2−1n/2 - 1 internal combines costing 2 each, so C(n)=n/2+2(n/2−1)=3n/2−2C(n) = n/2 + 2(n/2 - 1) = 3n/2 - 2. This matches the lower bound ⌈3n/2⌉−2\lceil 3n/2 \rceil - 2 for finding both, which is derived in Comparison of Sorting Algorithms. For nn not a power of 2 the halving version (left part ⌈n/2⌉\lceil n/2 \rceil elements, as floor((i + j) / 2) gives) can use more; the pairwise (non-recursive) method that compares elements in pairs always achieves ⌈3n/2⌉−2\lceil 3n/2 \rceil - 2.

nn56781012
MAX-MIN with halving689101418
⌈3n/2⌉−2\lceil 3n/2 \rceil - 2679101316

The loss appears when a halving step creates two parts of odd size: for n=6n = 6 the split 3+33 + 3 costs 3+3+2=83 + 3 + 2 = 8, while 2+42 + 4 costs 1+4+2=71 + 4 + 2 = 7. A question that says "nn is a power of 2" avoids the issue; otherwise ask which split the code uses.

Recursion tree of MAX-MIN on eight elements
Figure 2. MAX-MIN on 3 8 1 9 5 2 7 4: four leaves of size 2 cost 1 comparison each and three combine nodes cost 2 each, 10 comparisons in total, returning maximum 9 and minimum 1.

Fast exponentiation

Text
POWER(x, n)                         // n >= 1
1  if n = 1 then
2      return x
3  y <- POWER(x, floor(n / 2))
4  y <- y * y
5  if n is odd then
6      y <- y * x
7  return y

M(n)=M(⌊n/2⌋)+1M(n) = M(\lfloor n/2 \rfloor) + 1 or +2+2, so Θ(lg⁡n)\Theta(\lg n) multiplications instead of n−1n - 1. Exactly, POWER uses ⌊lg⁡n⌋\lfloor \lg n \rfloor squarings plus one extra multiplication for each 1 bit of nn except the leading one: ⌊lg⁡n⌋+ν(n)−1\lfloor \lg n \rfloor + \nu(n) - 1 multiplications, where ν(n)\nu(n) is the number of 1 bits. For n=13=11012n = 13 = 1101_2: 3+3−1=53 + 3 - 1 = 5 multiplications (x13x^{13} from x6x^6, x6x^6 from x3x^3, x3x^3 from xx).

Maximum subarray

Problem. Given A[1..n]A[1..n] with positive and negative values, find indices i≤ji \le j maximising A[i]+⋯+A[j]A[i] + \cdots + A[j].

Split at midmid. The best subarray lies entirely in the left half, entirely in the right half, or crosses the midpoint. A crossing subarray is a best suffix of A[low..mid]A[low..mid] followed by a best prefix of A[mid+1..high]A[mid+1..high], found in linear time:

Text
FIND-MAX-CROSSING-SUBARRAY(A, low, mid, high)
 1  left-sum <- -infinity;  sum <- 0
 2  for i <- mid downto low
 3      sum <- sum + A[i]
 4      if sum > left-sum then
 5          left-sum <- sum;  max-left <- i
 6  right-sum <- -infinity;  sum <- 0
 7  for j <- mid + 1 to high
 8      sum <- sum + A[j]
 9      if sum > right-sum then
10          right-sum <- sum;  max-right <- j
11  return (max-left, max-right, left-sum + right-sum)

FIND-MAXIMUM-SUBARRAY(A, low, high)
 1  if high = low then
 2      return (low, high, A[low])
 3  mid <- floor((low + high) / 2)
 4  (l1, h1, s1) <- FIND-MAXIMUM-SUBARRAY(A, low, mid)
 5  (l2, h2, s2) <- FIND-MAXIMUM-SUBARRAY(A, mid + 1, high)
 6  (l3, h3, s3) <- FIND-MAX-CROSSING-SUBARRAY(A, low, mid, high)
 7  return the triple with the largest sum

T(n)=2T(n/2)+Θ(n)=Θ(nlg⁡n)T(n) = 2T(n/2) + \Theta(n) = \Theta(n \lg n). With all entries negative, the answer is the single largest entry (the problem as stated requires a non-empty subarray).

Related material. Kadane's scan solves the same problem in Θ(n)\Theta(n) by keeping the best sum ending at each position; it is dynamic programming, not divide and conquer, and is commonly presented together with this algorithm.

Karatsuba multiplication

To multiply two nn-digit numbers, write x=x110n/2+x0x = x_1 10^{n/2} + x_0 and y=y110n/2+y0y = y_1 10^{n/2} + y_0. Then

xy=x1y1 10n+(x1y0+x0y1) 10n/2+x0y0,xy = x_1 y_1\, 10^{n} + (x_1 y_0 + x_0 y_1)\, 10^{n/2} + x_0 y_0 ,

which needs four half-size products and gives T(n)=4T(n/2)+Θ(n)=Θ(n2)T(n) = 4T(n/2) + \Theta(n) = \Theta(n^2), no better than school multiplication. Karatsuba's observation is that the middle term equals (x1+x0)(y1+y0)−x1y1−x0y0(x_1 + x_0)(y_1 + y_0) - x_1 y_1 - x_0 y_0, so three products suffice: T(n)=3T(n/2)+Θ(n)=Θ(nlg⁡3)T(n) = 3T(n/2) + \Theta(n) = \Theta(n^{\lg 3}).

Counting one-digit products, M(n)=3M(n/2)M(n) = 3M(n/2) with M(1)=1M(1) = 1 gives M(n)=3lg⁡n=nlg⁡3M(n) = 3^{\lg n} = n^{\lg 3}: 9 for 4-digit numbers and 81 for 16-digit numbers, against n2=16n^2 = 16 and 256 for the four-product split (ignoring the extra digit that the sums x1+x0x_1 + x_0 may carry).

Example: 1234×56781234 \times 5678 with x1=12x_1 = 12, x0=34x_0 = 34, y1=56y_1 = 56, y0=78y_0 = 78: x1y1=672x_1 y_1 = 672, x0y0=2652x_0 y_0 = 2652, (12+34)(56+78)=46×134=6164(12 + 34)(56 + 78) = 46 \times 134 = 6164, middle term 6164−672−2652=28406164 - 672 - 2652 = 2840, and 672⋅104+2840⋅102+2652=7006652672 \cdot 10^4 + 2840 \cdot 10^2 + 2652 = 7006652.

Strassen's matrix multiplication

Splitting n×nn \times n matrices into four n/2×n/2n/2 \times n/2 blocks gives 8 block products and T(n)=8T(n/2)+Θ(n2)=Θ(n3)T(n) = 8T(n/2) + \Theta(n^2) = \Theta(n^3). Strassen's method forms 7 block products from sums and differences of blocks, using 18 block additions and subtractions in total, so

T(n)=7T(n/2)+Θ(n2)=Θ(nlg⁡7),lg⁡7≈2.807.T(n) = 7T(n/2) + \Theta(n^2) = \Theta(n^{\lg 7}), \qquad \lg 7 \approx 2.807 .

The exact seven products are not needed for the running-time analysis here; the recurrence, the count 7 and the result nlg⁡7n^{\lg 7} are.

Worked examples

Maximum subarray on 16 elements

A=13,−3,−25,20,−3,−16,−23,18,20,−7,12,−5,−22,15,−4,7A = 13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7, low=1low = 1, high=16high = 16, mid=8mid = 8.

  • Left half A[1..8]A[1..8]: best subarray A[4..4]=20A[4..4] = 20.
  • Right half A[9..16]A[9..16]: best subarray A[9..11]=20−7+12=25A[9..11] = 20 - 7 + 12 = 25.
  • Crossing: suffix sums of the left half from i=8i = 8 down are 18,−5,−21,−24,−4,−29,−32,−1918, -5, -21, -24, -4, -29, -32, -19, best 1818 at i=8i = 8. Prefix sums of the right half from j=9j = 9 up are 20,13,25,20,−2,13,9,1620, 13, 25, 20, -2, 13, 9, 16, best 2525 at j=11j = 11. The crossing sum is 18+25=4318 + 25 = 43 for A[8..11]A[8..11].

The answer is the largest of 2020, 2525 and 4343: A[8..11]=18+20−7+12=43A[8..11] = 18 + 20 - 7 + 12 = 43.

Crossing step of the maximum-subarray algorithm
Figure 3. The top-level crossing step on the 16-element array: running suffix sums leftward from index 8 (best 18 at index 8) and prefix sums rightward from index 9 (best 25 at index 11) give the crossing subarray A[8..11]A[8..11] with sum 43.

Recognising when divide and conquer helps

RecurrenceMaster theorem caseResult
T(n)=2T(n/2)+2T(n) = 2T(n/2) + 2case 1 (f=O(n1−ϵ)f = O(n^{1-\epsilon}))Θ(n)\Theta(n)
T(n)=3T(n/2)+nT(n) = 3T(n/2) + ncase 1Θ(nlg⁡3)\Theta(n^{\lg 3})
T(n)=7T(n/2)+n2T(n) = 7T(n/2) + n^2case 1Θ(nlg⁡7)\Theta(n^{\lg 7})
T(n)=2T(n/2)+nT(n) = 2T(n/2) + ncase 2Θ(nlg⁡n)\Theta(n \lg n)
T(n)=T(n/2)+1T(n) = T(n/2) + 1case 2Θ(lg⁡n)\Theta(\lg n)
T(n)=2T(n/2)+n2T(n) = 2T(n/2) + n^2case 3 (regularity holds)Θ(n2)\Theta(n^2)

The last row is a warning: when the combine step dominates, splitting gains nothing.

Important properties and observations

  • Divide and conquer suits problems whose subproblems are independent. When the same subproblem recurs many times (Fibonacci, LCS), plain recursion repeats work exponentially and dynamic programming is the right tool.
  • The recursion depth is Θ(log⁡bn)\Theta(\log_b n) for an even split, so the stack uses Θ(lg⁡n)\Theta(\lg n) space unless the algorithm also needs work arrays (merge sort's Θ(n)\Theta(n) buffer).
  • Balanced splits matter: quicksort's Θ(n2)\Theta(n^2) worst case comes from a split of sizes n−1n-1 and 00, not from the paradigm.
  • Small base cases are often solved by a simpler method (insertion sort below about 16 elements) because recursion overhead dominates there; this changes constants, not the asymptotic bound.

Common misconceptions.

  • "Divide and conquer is always faster." T(n)=4T(n/2)+nT(n) = 4T(n/2) + n is Θ(n2)\Theta(n^2), the same as the direct method; only reducing aa or f(n)f(n) helps.
  • "Strassen is Θ(n2.81)\Theta(n^{2.81}) for any matrix size with no cost." The constant and the extra additions make it slower than the cubic method for small nn.
  • "MAX-MIN by divide and conquer uses 2n−22n - 2 comparisons." That is the cost of two separate scans; divide and conquer uses 3n/2−23n/2 - 2 for n≥2n\ge2 a power of 2.

GATE examination perspective

  • COUNT THE COMPARISONS. For maximum and minimum, 3n/2−23n/2 - 2 (power of 2, n≥2n\ge2) or ⌈3n/2⌉−2\lceil 3n/2 \rceil - 2 in general (the pairwise method); halving code can use more when nn is not a power of 2 (8 for n=6n = 6); for maximum alone, n−1n - 1.
  • WRITE THE RECURRENCE FIRST. Identify aa, bb and f(n)f(n) from the code (number of recursive calls, how much smaller each is, work outside the calls), then apply the master theorem.
  • NUMBER OF MULTIPLICATIONS. Fast exponentiation needs ⌊lg⁡n⌋+ν(n)−1\lfloor \lg n \rfloor + \nu(n) - 1; Strassen uses 7 block products, the naive split 8; Karatsuba 3 instead of 4.
  • CROSSING CASE. In the maximum-subarray algorithm the crossing subarray must include both A[mid]A[mid] and A[mid+1]A[mid+1].

Summary

Sources

  1. T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein, Introduction to Algorithms, 3rd ed., MIT Press, 2009: Chapter 4, Section 4.1 (the maximum-subarray problem) and Section 4.2 (Strassen's algorithm for matrix multiplication); Section 9.1 (minimum and maximum).

Next steps