






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







A divide-and-conquer algorithm has three steps:
Stanford’s slides summarize the idea as breaking a big problem into smaller, easier subproblems, then solving those recursively.stanford-cs161.github+
Every recursive algorithm needs a base case: an input small enough to solve directly.
For sorting, the base case is usually an array of size 0 or 1, which is already sorted.stanford- cs161.github+
text DIVIDE-AND-CONQUER(input): if input is small enough: solve directly
return solution
divide input into subproblems recursively solve each subproblem combine subproblem solutions
return solution
A recurrence relation describes the running time of a problem of size nnn in terms of the running time of smaller subproblems.stanford-cs161.github
For a divide-and-conquer algorithm that creates aaa subproblems of size n/bn/bn/b, and spends O(nd)O(n^d)O(nd) time dividing and combining, the recurrence is:
T(n)=aT(n/b)+O(nd)T(n) = aT(n/b) + O(n^d)T(n)=aT(n/b)+O(nd)
where:
a≥1a \ge 1a≥1 is the number of recursive subproblems b>1b > 1b>1 is the factor by which the input shrinks d≥0d \ge 0d≥0 is the exponent in the divide/combine coststanford-cs161.github+
MergeSort splits an array into two halves, recursively sorts each half, and merges them in linear time:
T(n)=2T(n/2)+O(n)T(n) = 2T(n/2) + O(n)T(n)=2T(n/2)+O(n)
stanford-cs161.github+
A recursion tree shows the work done at each level of recursion.
For:
T(n)=2T(n/2)+nT(n) = 2T(n/2) + nT(n)=2T(n/2)+n
Level 0: one problem of size nnn, with nnn combine work Level 1: two problems of size n/2n/2n/2, with 2(n/2)=n2(n/2) = n2(n/2)=n combine work
Recurrence aaa bbb ddd Comparison Solution
T(n)=2T(n/2)+nT(n) = 2T(n/2) + nT(n)=2T(n/2)+n 2 2 1 2=22 = 2 2 = 2 Θ(nlogn)^ \Theta(n^ \log n)Θ(nlogn)
T(n)=4T(n/2)+nT(n) = 4T(n/2) + nT(n)=4T(n/2)+n 4 2 1 4>24 > 2 4 > 2 Θ(n2)\Theta(n^2)Θ(n2)
T(n)=2T(n/2)+n2T(n) = 2T(n/2) + n^2T(n)=2T(n/2)+n 2 2 2 2<42 < 4 2 < 4 Θ(n2)\Theta(n^2)Θ(n2)
T(n)=8T(n/2)+n2T(n) = 8T(n/2) + n^2T(n)=8T(n/2)+n 8 2 2 8>48 > 4 8 > 4 Θ(n3)\Theta(n^3)Θ(n3)
Stanford’s notes use MergeSort as the primary example: a=2a = 2a=2, b=2b = 2b=2, d=1d = 1d=1, so a=bda = b^da=bd, giving O(nlogn)O(n \log n)O(nlogn).stanford-cs161.github+
MergeSort is the canonical divide-and-conquer sorting algorithm.stanford-cs161.github+
text MERGE-SORT(A): if length(A) <= 1: return A
L = MERGE-SORT(first half of A) R = MERGE-SORT(second half of A)
return MERGE(L, R)
Base case: An array of size 1 is sorted. Inductive step: If both halves are sorted correctly, MERGE produces a sorted array containing all original elements.
Therefore, MergeSort correctly sorts any array.
T(n)=2T(n/2)+O(n)T(n) = 2T(n/2) + O(n)T(n)=2T(n/2)+O(n)
Using the Master Theorem:
T(n)=O(nlogn)T(n) = O(n \log n)T(n)=O(nlogn)
Stanford’s notes explicitly derive this result.stanford-cs161.github+
Stable, if equal elements from the left half are merged first Worst-case time: Θ(nlogn) \Theta(n \log n)Θ(nlogn) Extra space: Θ(n)\Theta(n)Θ(n)
Stanford introduces divide-and-conquer through multiplying two nnn-digit integers.
Given two nnn-digit numbers xxx and yyy, split them into halves:
x=10n/2a+bx = 10^{n/2}a + bx=10n/2a+b y=10n/2c+dy = 10^{n/2}c + dy=10n/2c+d
Then:
xy=10nac+10n/2(ad+bc)+bdxy = 10^n ac + 10^{n/2}(ad + bc) + bdxy=10nac+10n/2(ad+bc)+bd
This creates four subproblems:
ac,ad,bc,bdac, \quad ad, \quad bc, \quad bdac,ad,bc,bd
The recurrence is:
T(n)=4T(n/2)+O(n)T(n) = 4T(n/2) + O(n)T(n)=4T(n/2)+O(n)
By the Master Theorem:
T(n)=Θ(n2)T(n) = \Theta(n^2)T(n)=Θ(n2)
The deterministic linear-time selection algorithm uses groups of five elements:
This guarantees a constant-fraction reduction in problem size and yields worst-case linear time:
Θ(n)\Theta(n)Θ(n)
Stanford’s Lecture 4 notes discuss median selection and recurrence-based analysis for this algorithm.stanford-cs161.github
Feature Divide and conquer Dynamic programming
Subproblems Usually disjoint Often overlapping
Combination Combine solutions directly Store and reuse subproblem solutions
Recurrence aT(n/b)+f(n)aT(n/b) + f(n)aT(n/b)+f(n) Often depends on multiple prior states
Typical analysis Recursion trees and Master Theorem Table size and work per state
Example MergeSort Longest common subsequence
Divide-and-conquer works especially well when subproblems are independent. When the same subproblems recur, dynamic programming is usually more appropriate.stanford-cs161.github+
A recurrence must include both recursive work and divide/combine work.
For MergeSort:
T(n)=2T(n/2)+O(n)T(n) = 2T(n/2) + O(n)T(n)=2T(n/2)+O(n)
not merely:
T(n)=2T(n/2)T(n) = 2T(n/2)T(n)=2T(n/2)
stanford-cs161.github+
For:
T(n)=4T(n/2)+nT(n) = 4T(n/2) + nT(n)=4T(n/2)+n
the parameters are:
a=4,b=2,d=1a = 4, \qquad b = 2, \qquad d = 1a=4,b=2,d=
not a=2a = 2a=2.stanford-cs161.github+
The standard Master Theorem assumes all subproblems have the same size. If subproblem sizes differ, use a recursion tree or substitution instead.stanford-cs161.github+
For MergeSort, the recursion depth is Θ(logn) \Theta(\log n)Θ(logn), but each level does Θ(n)\Theta(n)Θ(n) work. Therefore, total runtime is:
Θ(nlogn) \Theta(n \log n)Θ(nlogn)
not merely Θ(logn) \Theta(\log n)Θ(logn).stanford-cs161.github+
A divide-and-conquer algorithm can be slower than a simple iterative algorithm if the divide/combine cost is too high or the recursion creates too many subproblems. Always derive and compare the recurrence.