Discrete MathematicsAlgorithms and Growth of Functions3 of 4
Algorithms and Growth of Functions · 3

Divide and Conquer Recurrences and the Master Theorem

When an algorithm solves a problem of size nn by splitting it into smaller pieces and calling itself recursively, we need a way to write an equation for its runtime T(n)T(n) and solve it without TT on the right-hand side.

Writing Divide-and-Conquer Recurrences
1,000,000 names1{,}000{,}000 \text{ names}
T=  ?T = \;?

Every standard divide-and-conquer recurrence has three parts that we read directly from what the algorithm does at a single step:

T(n)\displaystyle T(n) =a⏟recursive calls⋅T ⁣(nb⏟piece size)+f(n)⏟split and combine work\displaystyle = \calls{\underbrace{a}_{\text{recursive calls}}} \cdot T\!\bigg(\piece{\underbrace{\frac{n}{b}}_{\text{piece size}}}\bigg) + \work{\underbrace{f(n)}_{\text{split and combine work}}}

Here a≥1\calls{a} \ge 1 and b>1\piece{b} > 1 are constants, and a\calls{a} does not have to equal b\piece{b}.

Text solution
Find the piece size, call count, and combine workSplitting an array of size nn into 3\piece{3} equal pieces, recursively solving 2\calls{2} of them, and spending n2\work{n^2} steps combining the answers gives each number directly:
b=3,a=2,f(n)=n2\displaystyle \piece{b = 3}, \qquad \calls{a = 2}, \qquad \work{f(n) = n^2}
Assemble the recurrencePlugging a\calls{a}, b\piece{b}, and f(n)\work{f(n)} into the divide-and-conquer formula gives the runtime equation:
T(n)\displaystyle T(n) =2T ⁣(n3)+n2\displaystyle = \calls{2}T\!\left(\piece{\frac{n}{3}}\right) + \work{n^2}
1

Adding Up Work Across a Recursion Tree

To turn a recurrence into a Big-Theta runtime, we want to find the total non-recursive work done across all levels of its recursion tree, from the single root problem of size nn down to the base-case leaves of size 11.

Three Recursion Trees: Balanced, Bottom-Heavy, and Top-Heavy
Merge Sort\htmlClass{lc-mute}{\text{Merge Sort}}
T(n)=2T(n2)+nT(n) = \htmlClass{lc-violet}{2}T(\htmlClass{lc-pink}{\tfrac{n}{2}}) + \htmlClass{lc-green}{n}
Level 0\htmlClass{lc-mute}{\text{Level } 0}
n\htmlClass{lc-pink}{n}
Level 1\htmlClass{lc-mute}{\text{Level } 1}

To compare the leaf count alog⁡bn\calls{a^{\log_b n}} against powers of nn, we rewrite the base a\calls{a} as a power of bb, a=blog⁡ba\calls{a} = b^{\log_b \calls{a}}, so the two exponents can trade places:

alog⁡bn=(blog⁡ba)log⁡bnWrite a in base b=(blog⁡bn)log⁡baSwap the multiplied exponents=nlog⁡baSimplify blog⁡bn=n\displaystyle \begin{aligned} \calls{a^{\log_b n}} &= \left(b^{\log_b \calls{a}}\right)^{\log_b n} \\ &\quad \text{Write } \calls{a} \text{ in base } b \\[2pt] &= \left(b^{\log_b n}\right)^{\log_b \calls{a}} \\ &\quad \text{Swap the multiplied exponents} \\[2pt] &= \calls{n^{\log_b a}} \\ &\quad \text{Simplify } b^{\log_b n} = n \end{aligned}
Text solution
Balanced tree: T(n)=2T(n2)+nT(n) = \calls{2}T(\piece{\tfrac{n}{2}}) + \work{n}Doubling the number of nodes at each level cancels the halved work per node, so every one of the log⁡2n+1\log_2 n + 1 levels does the same total work n\work{n}:
Level i work=2i⋅(n2i)=n\displaystyle \begin{aligned} \text{Level } i \text{ work} &= \calls{2^i} \cdot \work{\left(\frac{n}{2^i}\right)} = \work{n} \end{aligned}
T(n)=n⏟work per row×(log⁡2n+1)⏟number of rows=Θ(nlog⁡n)\displaystyle \begin{aligned} T(n) &= \work{\underbrace{n}_{\text{work per row}}} \times \underbrace{(\log_2 n + 1)}_{\text{number of rows}} \\ &= \Theta(n \log n) \end{aligned}
Bottom-heavy tree: T(n)=4T(n2)+nT(n) = \calls{4}T(\piece{\tfrac{n}{2}}) + \work{n}Quadrupling the nodes while only halving each node's work doubles the row total at every level, so the bottom row of leaves outweighs all levels above it combined:
Level i work=4i⋅(n2i)=2inNumber of leaves=4log⁡2n=nlog⁡24=n2T(n)=Θ(n2)\displaystyle \begin{aligned} \text{Level } i \text{ work} &= \calls{4^i} \cdot \work{\left(\frac{n}{2^i}\right)} \\ &= \work{2^i n} \\ \text{Number of leaves} &= \calls{4}^{\log_2 n} \\ &= n^{\log_2 \calls{4}} = \calls{n^2} \\ T(n) &= \Theta(n^2) \end{aligned}
Top-heavy tree: T(n)=2T(n2)+n2T(n) = \calls{2}T(\piece{\tfrac{n}{2}}) + \work{n^2}Halving the input inside a squared work term divides each node's work by 44, so the row totals halve at each step and the root node alone does at least half of the entire tree's work:
Level i work=2i⋅(n2i)2=2i⋅n24i=(12)in2\displaystyle \begin{aligned} \text{Level } i \text{ work} &= \calls{2^i} \cdot \work{\left(\frac{n}{2^i}\right)^2} \\ &= \calls{2^i} \cdot \work{\frac{n^2}{4^i}} \\ &= \work{\left(\frac{1}{2}\right)^i n^2} \end{aligned}
T(n)=n2(1+12+14+… )=Θ(n2)\displaystyle \begin{aligned} T(n) &= \work{n^2 \left(1 + \frac{1}{2} + \frac{1}{4} + \dots\right)} \\ &= \Theta(n^2) \end{aligned}

When the branching and the shrinking inside each node cancel out, every row has the same total. When branching is stronger, the row totals multiply at each step down, so the bottom row of leaves outweighs all the rows above it combined. When shrinking is stronger, the row totals shrink at each step down, so the single root node outweighs all the rows below it combined.

When each call halves the input size, how many branches grow versus how fast each node's work shrinks decides whether most of the total work happens at the root, spreads evenly across every level, or piles up at the leaves.
2

Unrolling for an Exact Closed Form

Big-Theta tells us how the runtime grows for large nn. When we also have a base case like T(1)=3\given{T(1) = 3} and want an exact formula for T(n)T(n) (assuming n=bk\given{n = b^k} is a clean power of bb), we unroll the recurrence step by step down to T(1)\given{T(1)}.

Unrolling Recurrences for Exact Closed Forms
T(n)=2T(n2)+nT(1)=3n=2kT(n)=  ?\displaystyle \begin{aligned} T(n) &= \htmlClass{lc-violet}{2}T(\htmlClass{lc-pink}{\tfrac{n}{2}}) + \htmlClass{lc-green}{n} \\ \htmlClass{lc-blue}{T(1)} &\htmlClass{lc-blue}{{}= 3} \\ \htmlClass{lc-blue}{n} &\htmlClass{lc-blue}{{}= 2^k} \\ T(n) &= \;? \end{aligned}

When the row totals form a growing geometric series, we add them using the finite geometric sum formula before combining them with the leaf term:

1+r+r2+⋯+rk−1\displaystyle \work{1 + r + r^2 + \dots + r^{k-1}} =rk−1r−1\displaystyle = \frac{r^k - 1}{r - 1}
Text solution
Unroll a balanced recurrence: T(n)=2T(n2)+nT(n) = \calls{2}T(\piece{\tfrac{n}{2}}) + \work{n} with T(1)=3\given{T(1) = 3}Substituting the recurrence into itself ii times produces ii copies of n\work{n} from the expanded rows plus 2i\calls{2^i} remaining sub-problems of size n2i\piece{\tfrac{n}{2^i}}:
T(n)=n+2T ⁣(n2)=n+2(n2+2T ⁣(n4))=n+n+4T ⁣(n4)=n+n+⋯+n⏟i times+2iT ⁣(n2i)\displaystyle \begin{aligned} T(n) &= \work{n} + \calls{2}T\!\left(\piece{\frac{n}{2}}\right) \\ &= \work{n} + \calls{2}\left(\work{\frac{n}{2}} + \calls{2}T\!\left(\piece{\frac{n}{4}}\right)\right) \\ &= \work{n} + \work{n} + \calls{4}T\!\left(\piece{\frac{n}{4}}\right) \\ &= \underbrace{\work{n + n + \dots + n}}_{i \text{ times}} + \calls{2^i} T\!\left(\piece{\frac{n}{2^i}}\right) \end{aligned}
Stop at the leaves (i=k=log⁡2ni = k = \log_2 n) and plug in n=8\given{n = 8}Setting n2k=1\piece{\tfrac{n}{2^k} = 1} gives k=log⁡2nk = \log_2 n internal rows and 2k=n\calls{2^k = n} leaves of cost T(1)=3\given{T(1) = 3}:
T(n)=k⋅n+2k⋅T(1)=nlog⁡2n+3nT(8)=8log⁡28+3(8)=24+24=48\displaystyle \begin{aligned} T(n) &= k \cdot \work{n} + \calls{2^k} \cdot \given{T(1)} \\ &= \work{n \log_2 n} + \given{3}\calls{n} \\ \given{T(8)} &= 8 \log_2 8 + 3(8) \\ &= 24 + 24 = 48 \end{aligned}
Unroll a bottom-heavy recurrence: T(n)=4T(n2)+nT(n) = \calls{4}T(\piece{\tfrac{n}{2}}) + \work{n} with T(1)=1\given{T(1) = 1}Expanding k=log⁡2nk = \log_2 n levels produces a doubling geometric sum across the internal rows plus 4k\calls{4^k} leaves at the bottom:
T(n)=n+4(n2+4T ⁣(n4))=n+2n+16T ⁣(n4)=n(1+2+4+⋯+2k−1)+4k⋅T(1)\displaystyle \begin{aligned} T(n) &= \work{n} + \calls{4}\left(\work{\frac{n}{2}} + \calls{4}T\!\left(\piece{\frac{n}{4}}\right)\right) \\ &= \work{n} + \work{2n} + \calls{16}T\!\left(\piece{\frac{n}{4}}\right) \\ &= \work{n\left(1 + 2 + 4 + \dots + 2^{k-1}\right)} \\ &\qquad + \calls{4^k} \cdot \given{T(1)} \end{aligned}
Sum the internal rows and add the leavesUsing 2k=n\given{2^k = n} in the geometric sum and 4k=(2k)2=n2\calls{4^k} = (2^k)^2 = \calls{n^2} at the leaves gives the exact polynomial formula:
T(n)=n(2k−12−1)+n2(1)=n(n−1)+n2=2n2−nT(4)=2(4)2−4=28\displaystyle \begin{aligned} T(n) &= \work{n\left(\frac{2^k - 1}{2 - 1}\right)} + \calls{n^2}(\given{1}) \\ &= \work{n(n - 1)} + \calls{n^2} \\ &= 2n^2 - n \\ \given{T(4)} &= 2(4)^2 - 4 = 28 \end{aligned}

Unrolling in algebra matches going down the tree: after ii steps, the loose terms are the top ii rows of work, and the 2iT(n2i)\calls{2^i} T(\piece{\tfrac{n}{2^i}}) term holds the remaining sub-problems on level ii until it hits 2kT(1)\calls{2^k} \given{T(1)} at the leaves.

Unrolling a recurrence step by step in algebra is the exact same thing as expanding the recursion tree level by level into row sums and leaving the unexpanded calls on the frontier.
3

The Master Method: Bottom Weight vs. Top Weight

When we only need the Big-Theta runtime of T(n)=aT(nb)+f(n)T(n) = \calls{a}T(\piece{\tfrac{n}{b}}) + \work{f(n)}, we don't need to draw or unroll the tree. We can tell whether the tree is bottom-heavy, balanced, or top-heavy by comparing the work at its two ends.

Top weight: f(n)\text{Top weight: } \htmlClass{lc-green}{f(n)}
Bottom weight: nlog⁡ba leaves\text{Bottom weight: } \htmlClass{lc-violet}{n^{\log_b a}} \text{ leaves}
The Master Theorem: Leaf Weight vs. Root Weight
Bottom-Heavy\text{Bottom-Heavy}
T(n)=4T(n2)+nT(n) = \htmlClass{lc-violet}{4}T(\htmlClass{lc-pink}{\tfrac{n}{2}}) + \htmlClass{lc-green}{n}
Balanced\text{Balanced}
T(n)=2T(n2)+nT(n) = \htmlClass{lc-violet}{2}T(\htmlClass{lc-pink}{\tfrac{n}{2}}) + \htmlClass{lc-green}{n}
Top-Heavy\text{Top-Heavy}
T(n)=2T(n2)+n2T(n) = \htmlClass{lc-violet}{2}T(\htmlClass{lc-pink}{\tfrac{n}{2}}) + \htmlClass{lc-green}{n^2}
T(n)=aT(nb)+f(n)T(n) = \htmlClass{lc-violet}{a}T(\htmlClass{lc-pink}{\tfrac{n}{b}}) + \htmlClass{lc-green}{f(n)}

We compare the leaf count at the bottom of the tree against the non-recursive work at the root:

nlog⁡ba⏟bottom weight (leaves)vs.f(n)⏟top weight (root)\displaystyle \calls{\underbrace{n^{\log_b a}}_{\text{bottom weight (leaves)}}} \quad \text{vs.} \quad \work{\underbrace{f(n)}_{\text{top weight (root)}}}

Whichever end grows faster by a power of nn dominates the total runtime, and when they grow at the same rate, we multiply by the height of the tree, log⁡n\log n:

Bottom wins by a power of n:T(n)=Θ ⁣(nlog⁡ba)Tie:T(n)=Θ ⁣(nlog⁡balog⁡n)Top wins by a power of n:T(n)=Θ(f(n))\displaystyle \begin{aligned} &\text{Bottom wins by a power of } n: \\ &\qquad T(n) = \Theta\!\left(\calls{n^{\log_b a}}\right) \\[4pt] &\text{Tie}: \\ &\qquad T(n) = \Theta\!\left(\calls{n^{\log_b a}} \log n\right) \\[4pt] &\text{Top wins by a power of } n: \\ &\qquad T(n) = \Theta(\work{f(n)}) \end{aligned}
Text solution
Quadratic tie: T(n)=9T(n3)+n2T(n) = \calls{9}T(\piece{\tfrac{n}{3}}) + \work{n^2}With a=9\calls{a = 9} and b=3\piece{b = 3}, the leaf count matches the root work f(n)=n2\work{f(n) = n^2}, so we multiply by log⁡n\log n:
Bottom weight=nlog⁡39=n2Top weight=n2(tie)T(n)=Θ(n2log⁡n)\displaystyle \begin{aligned} \text{Bottom weight} &= n^{\log_3 \calls{9}} = \calls{n^2} \\ \text{Top weight} &= \work{n^2} \quad (\text{tie}) \\ T(n) &= \Theta(n^2 \log n) \end{aligned}
Binary search: T(n)=T(n2)+1T(n) = T(\piece{\tfrac{n}{2}}) + \work{1}With a=1\calls{a = 1} recursive call on half the list (b=2\piece{b = 2}), the tree has only 1\calls{1} leaf at the bottom, which ties the constant work f(n)=1\work{f(n) = 1} at the root:
Bottom weight=nlog⁡21=n0=1Top weight=1(tie)T(n)=Θ(1⋅log⁡n)=Θ(log⁡n)\displaystyle \begin{aligned} \text{Bottom weight} &= n^{\log_2 \calls{1}} = n^0 = \calls{1} \\ \text{Top weight} &= \work{1} \quad (\text{tie}) \\ T(n) &= \Theta(\work{1} \cdot \log n) \\ &= \Theta(\log n) \end{aligned}
Matrix multiplication: T(n)=8T(n2)+n2T(n) = \calls{8}T(\piece{\tfrac{n}{2}}) + \work{n^2} vs. Strassen's T(n)=7T(n2)+n2T(n) = \calls{7}T(\piece{\tfrac{n}{2}}) + \work{n^2}Both algorithms spend f(n)=n2\work{f(n) = n^2} work adding matrix blocks at the root, so the bottom weight wins in both, and cutting a\calls{a} from 8\calls{8} to 7\calls{7} lowers the exponent of nn:
Standard (a=8):nlog⁡28=n3>n2  ⟹  T(n)=Θ(n3)Strassen (a=7):nlog⁡27≈n2.81>n2  ⟹  T(n)=Θ ⁣(nlog⁡27)≈Θ(n2.81)\displaystyle \begin{aligned} &\text{Standard } (\calls{a = 8}): \\ &\quad n^{\log_2 \calls{8}} = \calls{n^3} > \work{n^2} \\ &\quad \implies T(n) = \Theta(n^3) \\[4pt] &\text{Strassen } (\calls{a = 7}): \\ &\quad n^{\log_2 \calls{7}} \approx \calls{n^{2.81}} > \work{n^2} \\ &\quad \implies T(n) = \Theta\!\left(n^{\log_2 7}\right) \\ &\quad \qquad\qquad\quad \approx \Theta(n^{2.81}) \end{aligned}
4

Traps and Red Flags

What happens when the bottom weight and top weight have the same power of nn except for a log⁡n\log n factor, or when a recurrence doesn't split the input into equal nb\piece{\tfrac{n}{b}} pieces at all?

n\htmlClass{lc-pink}{n}
n−1\htmlClass{lc-pink}{n - 1}
n−2\htmlClass{lc-pink}{n - 2}
1\htmlClass{lc-pink}{1}
nn
T(n)=T(n−1)+nT(n) = T(\htmlClass{lc-orange}{\boxed{\htmlClass{lc-pink}{n - 1}}}) + \htmlClass{lc-green}{n}
n\htmlClass{lc-pink}{n}
n3\htmlClass{lc-pink}{\tfrac{n}{3}}
2n3\htmlClass{lc-pink}{\tfrac{2n}{3}}
log⁡3n\log_3 n
log⁡3/2n\log_{3/2} n
T(n)=T(n3)+T(2n3)+nT(n) = T(\htmlClass{lc-orange}{\boxed{\htmlClass{lc-pink}{\tfrac{n}{3}}}}) + T(\htmlClass{lc-orange}{\boxed{\htmlClass{lc-pink}{\tfrac{2n}{3}}}}) + \htmlClass{lc-green}{n}
Traps and Red Flags for the Master Theorem
T(n)=2T(n2)+nlog⁡nΘ(?)T(n)=2T(n2)+nlog⁡nΘ(?)\begin{alignedat}{2} T(n) &= \htmlClass{lc-violet}{2}T(\htmlClass{lc-pink}{\tfrac{n}{2}}) + \htmlClass{lc-green}{n \log n} &\qquad &\Theta(?) \\ T(n) &= \htmlClass{lc-violet}{2}T(\htmlClass{lc-pink}{\tfrac{n}{2}}) + \htmlClass{lc-green}{\tfrac{n}{\log n}} &\qquad &\Theta(?) \end{alignedat}
bottom weight\htmlClass{lc-violet}{\text{bottom weight}}
top weight\htmlClass{lc-green}{\text{top weight}}
n0\htmlClass{lc-mute}{n^{0}}
n1\htmlClass{lc-mute}{n^{1}}
n2\htmlClass{lc-mute}{n^{2}}
Text solution
Tie power multiplied by log⁡n\log n: T(n)=2T(n2)+nlog⁡nT(n) = \calls{2}T(\piece{\tfrac{n}{2}}) + \work{n \log n}The top weight nlog⁡n\work{n \log n} has the same power of nn as the bottom weight nlog⁡22=n1n^{\log_2 \calls{2}} = \calls{n^1} with a multiplied log⁡n\log n factor, so the tie rule still multiplies f(n)\work{f(n)} by log⁡n\log n:
Bottom weight=nlog⁡22=n1,Top weight=n1log⁡nT(n)=Θ(nlog⁡n⋅log⁡n)=Θ(nlog⁡2n)\displaystyle \begin{aligned} \text{Bottom weight} &= n^{\log_2 \calls{2}} = \calls{n^1}, \\ \text{Top weight} &= \work{n^1 \log n} \\ T(n) &= \Theta(\work{n \log n} \cdot \log n) \\ &= \Theta(n \log^2 n) \end{aligned}
Tie power divided by log⁡n\log n (gap case): T(n)=2T(n2)+nlog⁡nT(n) = \calls{2}T(\piece{\tfrac{n}{2}}) + \work{\tfrac{n}{\log n}}Because nlog⁡n\work{\tfrac{n}{\log n}} is smaller than n1\calls{n^1} by only a log⁡n\log n factor rather than a power of nn (and divides by log⁡n\log n instead of multiplying), the Master Method does not apply; unrolling the log⁡2n\log_2 n rows gives a harmonic sum:
T(n)=nlog⁡2n+nlog⁡2n−1+…+n1+nT(1)=n(1+12+⋯+1log⁡2n)+nT(1)=Θ(nlog⁡log⁡n)\displaystyle \begin{aligned} T(n) &= \work{\frac{n}{\log_2 n}} + \work{\frac{n}{\log_2 n - 1}} + \dots \\ &\qquad + \work{\frac{n}{1}} + \calls{n}\given{T(1)} \\ &= \work{n\left(1 + \frac{1}{2} + \dots + \frac{1}{\log_2 n}\right)} \\ &\qquad + \calls{n}\given{T(1)} \\ &= \Theta(n \log \log n) \end{aligned}
Subtracting instead of dividing: T(n)=T(n−1)+nT(n) = T(\piece{n - 1}) + \work{n} and T(n)=2T(n−1)+1T(n) = \calls{2}T(\piece{n - 1}) + \work{1}Because the input shrinks to n−1\piece{n - 1} rather than nb\piece{\tfrac{n}{b}}, the tree is nn levels tall instead of log⁡n\log n levels tall, so we unroll nn steps directly:
T(n)=T(n−1)+n=n+(n−1)+⋯+1=n(n+1)2=Θ(n2)T(n)=2T(n−1)+1=1+2+4+⋯+2n−1=2n−1=Θ(2n)\displaystyle \begin{aligned} T(n) &= T(\piece{n - 1}) + \work{n} \\ &= \work{n} + \work{(n - 1)} + \dots + \work{1} \\ &= \frac{n(n + 1)}{2} = \Theta(n^2) \\[8pt] T(n) &= \calls{2}T(\piece{n - 1}) + \work{1} \\ &= \work{1} + \work{2} + \work{4} + \dots + \work{2^{n-1}} \\ &= 2^n - 1 = \Theta(2^n) \end{aligned}

Key concepts

Every divide-and-conquer recurrence we analyze with the Master Method must have the form T(n)=aT(nb)+f(n)T(n) = \calls{a}T(\piece{\tfrac{n}{b}}) + \work{f(n)} with constants a≥1\calls{a} \ge 1 and b>1\piece{b} > 1. Check the inside of T(…)T(\piece{\dots}) and the coefficient out front first so you know when not to use the Master Method: if the recurrence subtracts instead of dividing (like T(n)=T(n−1)+n=Θ(n2)T(n) = T(\piece{n - 1}) + \work{n} = \Theta(n^2) or T(n)=2T(n−1)+1=Θ(2n)T(n) = \calls{2}T(\piece{n - 1}) + \work{1} = \Theta(2^n)), splits into uneven pieces (like T(n)=T(n3)+T(2n3)+nT(n) = T(\piece{\tfrac{n}{3}}) + T(\piece{\tfrac{2n}{3}}) + \work{n}), or has a non-constant branching factor (like T(n)=nT(n2)+nT(n) = \calls{n}T(\piece{\tfrac{n}{2}}) + \work{n}), the Master Method does not apply and you should unroll the tree directly.

When a problem gives a base case T(1)\given{T(1)} and asks for an exact closed-form formula (with n=bk\given{n = b^k}), unroll the recurrence for k=log⁡bnk = \log_b n levels and add the sum of the internal rows to the leaf work akT(1)=nlog⁡baT(1)\calls{a^k} \given{T(1)} = \calls{n^{\log_b a}} \given{T(1)}. For T(n)=2T(n2)+nT(n) = \calls{2}T(\piece{\tfrac{n}{2}}) + \work{n} with T(1)=3\given{T(1) = 3}, the k=log⁡2nk = \log_2 n internal rows each cost n\work{n} and the 2k=n\calls{2^k = n} leaves each cost 3\given{3}, giving T(n)=nlog⁡2n+3nT(n) = \work{n \log_2 n} + \given{3}\calls{n}.

When you only need the Big-Theta runtime, compare the bottom weight (the leaf count nlog⁡ba\calls{n^{\log_b a}}) against the top weight (the root work f(n)\work{f(n)}):

Bottom wins by a power of nn

If nlog⁡ba\calls{n^{\log_b a}} has a larger exponent of nn than f(n)\work{f(n)}, the leaves dominate and T(n)=Θ(nlog⁡ba)T(n) = \Theta(\calls{n^{\log_b a}}) (for 4T(n2)+n\calls{4}T(\piece{\tfrac{n}{2}}) + \work{n}, nlog⁡24=n2n^{\log_2 \calls{4}} = \calls{n^2} beats n1\work{n^1}, giving Θ(n2)\Theta(n^2)).

Top wins by a power of nn

If f(n)\work{f(n)} has a larger exponent of nn than nlog⁡ba\calls{n^{\log_b a}}, the root dominates and T(n)=Θ(f(n))T(n) = \Theta(\work{f(n)}) (for 2T(n2)+n2\calls{2}T(\piece{\tfrac{n}{2}}) + \work{n^2}, n2\work{n^2} beats nlog⁡22=n1n^{\log_2 \calls{2}} = \calls{n^1}, giving Θ(n2)\Theta(n^2)).

Tie in the power of nn

If f(n)\work{f(n)} has the exact same exponent of nn as nlog⁡ba\calls{n^{\log_b a}} (optionally multiplied by (log⁡n)k(\log n)^k, never divided), the levels balance and T(n)=Θ(f(n)log⁡n)T(n) = \Theta(\work{f(n)} \log n) (for 2T(n2)+n\calls{2}T(\piece{\tfrac{n}{2}}) + \work{n}, n1\work{n^1} ties n1\calls{n^1} to give Θ(nlog⁡n)\Theta(n \log n); for 2T(n2)+nlog⁡n\calls{2}T(\piece{\tfrac{n}{2}}) + \work{n \log n}, multiplying by log⁡n\log n gives Θ(nlog⁡2n)\Theta(n \log^2 n); but for 2T(n2)+nlog⁡n\calls{2}T(\piece{\tfrac{n}{2}}) + \work{\tfrac{n}{\log n}}, dividing by log⁡n\log n falls in the gap between the tie rule and a bottom win, and unrolls to Θ(nlog⁡log⁡n)\Theta(n \log \log n)).

5

Practice

1

An algorithm splits an array of size nn into 33 equal pieces, recursively solves 22 of them, and spends n2n^2 steps splitting and combining the results. Write the recurrence T(n)T(n) for its runtime and find its Big-Theta complexity.

Match the number of recursive calls to a\calls{a} and the piece-size denominator to b\piece{b}, then compare the exponent of nn in nlog⁡ba\calls{n^{\log_b a}} against n2\work{n^2}.

Splitting into 3\piece{3} equal pieces gives b=3\piece{b = 3}, solving 2\calls{2} of them gives a=2\calls{a = 2}, and spending n2\work{n^2} steps outside the recursive calls gives f(n)=n2\work{f(n) = n^2} (watch out for swapping a\calls{a} and b\piece{b} into 3T(n2)+n2\fails{3T(\tfrac{n}{2}) + n^2}, which mistakes the 3\piece{3} pieces for 3\fails{3} recursive calls):
T(n)\displaystyle T(n) =2T ⁣(n3)+n2\displaystyle = \calls{2}T\!\left(\piece{\frac{n}{3}}\right) + \work{n^2}
Now compare the bottom weight against the top weight:
Bottom weight\displaystyle \text{Bottom weight} =nlog⁡32≈n0.63,Top weight\displaystyle = n^{\log_3 \calls{2}} \approx \calls{n^{0.63}}, \qquad \text{Top weight} =f(n)=n2\displaystyle = \work{f(n) = n^2}
Because the top weight n2\work{n^2} wins over nlog⁡32\calls{n^{\log_3 2}} by a power of nn (top-heavy), the root dominates and T(n)=Θ(n2)T(n) = \Theta(n^2).
2

For the recurrence T(n)=8T(n2)+n2T(n) = \calls{8}T(\piece{\tfrac{n}{2}}) + \work{n^2}, find in its work tree: (a) the number of sub-problems, the size of each sub-problem, and the total work across Level ii; (b) the total number of leaves at the bottom of the tree as a polynomial in nn; and (c) the Big-Theta runtime T(n)T(n).

At Level ii, remember to square the entire shrunk input size n2i\piece{\tfrac{n}{2^i}} when finding the work inside each node.

(a) At Level ii, branching by 8\calls{8} creates 8i\calls{8^i} sub-problems, and halving the input ii times gives each sub-problem size n2i\piece{\tfrac{n}{2^i}}. Each node of size m=n2i\piece{m = \tfrac{n}{2^i}} does m2=(n2i)2=n24i\work{m^2} = (\piece{\tfrac{n}{2^i}})^2 = \work{\tfrac{n^2}{4^i}} work (be careful not to forget to square the 2i2^i in the denominator, which would give n22i\fails{\tfrac{n^2}{2^i}} and a wrong row total of 4in2\fails{4^i n^2}). Multiplying the node count by the work per node gives the Level ii total:
Level i work\displaystyle \text{Level } i \text{ work} =8i⋅n24i\displaystyle = \calls{8^i} \cdot \work{\frac{n^2}{4^i}} =2in2\displaystyle = \work{2^i n^2}
(b) At the bottom level i=log⁡2ni = \log_2 n, the number of leaves is:
8log⁡2n\displaystyle \calls{8}^{\log_2 n} =nlog⁡28\displaystyle = n^{\log_2 \calls{8}} =n3\displaystyle = \calls{n^3}
(Watch out for computing 82=4\fails{\tfrac{8}{2} = 4} and writing n4\fails{n^4} instead of using log⁡28=3\log_2 8 = 3.)
(c) Since the bottom weight n3\calls{n^3} beats the top weight n2\work{n^2} by a power of nn (row totals double at each level), the leaves dominate and T(n)=Θ(n3)T(n) = \Theta(n^3).
3

Find the Big-Theta runtime of (a) T(n)=4T(n2)+n2T(n) = \calls{4}T(\piece{\tfrac{n}{2}}) + \work{n^2} and (b) T(n)=T(n3)+1T(n) = T(\piece{\tfrac{n}{3}}) + \work{1}.

Find the leaf count nlog⁡ba\calls{n^{\log_b a}} for each recurrence and compare it to f(n)\work{f(n)}; in part (b), ask what power of 33 equals 11.

(a) Comparing the two weights gives:
Bottom weight\displaystyle \text{Bottom weight} =nlog⁡24\displaystyle = n^{\log_2 \calls{4}} =n2,Top weight\displaystyle = \calls{n^2}, \qquad \text{Top weight} =f(n)=n2\displaystyle = \work{f(n) = n^2}
Because the bottom weight and top weight are tied at n2n^2, we multiply by log⁡n\log n (don't stop at Θ(n2)\fails{\Theta(n^2)} without the log⁡n\log n factor for the height of the tree) to get T(n)=Θ(n2log⁡n)T(n) = \Theta(n^2 \log n).
(b) With a=1\calls{a = 1} and b=3\piece{b = 3}, the bottom weight is:
nlog⁡31\displaystyle n^{\log_3 \calls{1}} =n0\displaystyle = n^0 =1\displaystyle = \calls{1}
(Be careful not to evaluate log⁡31\log_3 1 as 1\fails{1}, which would give n1\fails{n^1} and the wrong answer Θ(n)\fails{\Theta(n)}.) Since the bottom weight 1\calls{1} ties the top weight f(n)=1\work{f(n) = 1}, multiplying by log⁡n\log n gives T(n)=Θ(log⁡n)T(n) = \Theta(\log n).
4

Given T(n)=3T(n3)+2nT(n) = \calls{3}T(\piece{\tfrac{n}{3}}) + \work{2n} with base case T(1)=5\given{T(1) = 5}, assume n=3k\given{n = 3^k} and unroll the recurrence to find an exact closed-form formula for T(n)T(n) in terms of nn, then compute the exact value of T(27)\given{T(27)}.

Unroll a few levels to see what happens to the 2n\work{2n} term at each row, then stop at i=k=log⁡3ni = k = \log_3 n and include the leaf term 3k⋅T(1)\calls{3^k} \cdot \given{T(1)}.

Unrolling step by step shows that every internal level contributes 2n\work{2n}:
T(n)=2n+3T ⁣(n3)=2n+3(2(n3)+3T ⁣(n9))=2n+2n+9T ⁣(n9)=2n+2n+⋯+2n⏟i times+3iT ⁣(n3i)\displaystyle \begin{aligned} T(n) &= \work{2n} + \calls{3}T\!\left(\piece{\frac{n}{3}}\right) \\ &= \work{2n} + \calls{3}\left(\work{2\left(\frac{n}{3}\right)} + \calls{3}T\!\left(\piece{\frac{n}{9}}\right)\right) = \work{2n} + \work{2n} + \calls{9}T\!\left(\piece{\frac{n}{9}}\right) \\ &= \underbrace{\work{2n + 2n + \dots + 2n}}_{i \text{ times}} + \calls{3^i} T\!\left(\piece{\frac{n}{3^i}}\right) \end{aligned}
Stopping at the leaves when i=k=log⁡3ni = k = \log_3 n (so n3k=1\piece{\tfrac{n}{3^k} = 1} and 3k=n\calls{3^k = n}) gives:
T(n)\displaystyle T(n) =2nlog⁡3n+n⋅T(1)\displaystyle = \work{2n \log_3 n} + \calls{n} \cdot \given{T(1)} =2nlog⁡3n+5n\displaystyle = \work{2n \log_3 n} + \given{5}\calls{n}
(Watch out for adding T(1)=5\given{T(1) = 5} just once at the end as 2nlog⁡3n+5\fails{2n \log_3 n + 5} instead of multiplying by all 3k=n\calls{3^k = n} leaves, which would give 167\fails{167} for T(27)\given{T(27)}.) Plugging in n=27\given{n = 27} with log⁡327=3\log_3 27 = 3 gives:
T(27)\displaystyle \given{T(27)} =2(27)(3)+5(27)\displaystyle = 2(27)(3) + 5(27) =162+135\displaystyle = 162 + 135 =297\displaystyle = 297
5

Given T(n)=9T(n3)+nT(n) = \calls{9}T(\piece{\tfrac{n}{3}}) + \work{n} with base case T(1)=2\given{T(1) = 2}, assume n=3k\given{n = 3^k} and unroll the recurrence to find an exact closed-form formula for T(n)T(n) in terms of nn, then compute the exact value of T(9)\given{T(9)}.

Factor nn out of the internal row sum to get a geometric series with ratio r=3r = 3, and rewrite 9k\calls{9^k} at the leaves in terms of n=3k\given{n = 3^k}.

Unrolling k=log⁡3nk = \log_3 n steps gives a row total that triples at every level:
T(n)=n+9(n3+9T ⁣(n9))=n+3n+81T ⁣(n9)=n(1+3+9+⋯+3k−1)+9k⋅T(1)\displaystyle \begin{aligned} T(n) &= \work{n} + \calls{9}\left(\work{\frac{n}{3}} + \calls{9}T\!\left(\piece{\frac{n}{9}}\right)\right) = \work{n} + \work{3n} + \calls{81}T\!\left(\piece{\frac{n}{9}}\right) \\ &= \work{n\left(1 + 3 + 9 + \dots + 3^{k-1}\right)} + \calls{9^k} \cdot \given{T(1)} \end{aligned}
Since 3k=n\given{3^k = n}, the geometric sum is 3k−13−1=n−12\frac{3^k - 1}{3 - 1} = \frac{n - 1}{2}, so the internal levels sum to n(n−1)2=n2−n2\work{\frac{n(n - 1)}{2}} = \work{\frac{n^2 - n}{2}} (don't forget the denominator 3−1=23 - 1 = 2 in the geometric sum formula, which would double the internal sum to n2−n\fails{n^2 - n} and give 234\fails{234} at the end). At the leaves, 9k=(3k)2=n2\calls{9^k} = (3^k)^2 = \calls{n^2} (not n\fails{n}), so 9k⋅T(1)=2n2\calls{9^k} \cdot \given{T(1)} = \given{2}\calls{n^2}. Adding the two parts together gives:
T(n)\displaystyle T(n) =n2−n2+2n2\displaystyle = \work{\frac{n^2 - n}{2}} + \given{2}\calls{n^2} =5n2−n2\displaystyle = \frac{5n^2 - n}{2}
Plugging in n=9\given{n = 9} gives:
T(9)\displaystyle \given{T(9)} =5(81)−92\displaystyle = \frac{5(81) - 9}{2} =198\displaystyle = 198
6

Two divide-and-conquer algorithms solve the same problem by splitting an input of size nn into 22 halves and spending n2n^2 steps combining the results, but Algorithm A makes 55 recursive calls while Algorithm B makes 33 recursive calls. Write both recurrences, find their Big-Theta runtimes, and determine which algorithm is faster for large nn.

Compare each algorithm's leaf count nlog⁡2a\calls{n^{\log_2 a}} against n2\work{n^2} by checking whether a\calls{a} is greater than or less than 22=42^2 = 4.

Algorithm A has recurrence TA(n)=5TA(n2)+n2T_A(n) = \calls{5}T_A(\piece{\tfrac{n}{2}}) + \work{n^2}. Its bottom weight is nlog⁡25≈n2.32n^{\log_2 \calls{5}} \approx \calls{n^{2.32}}, which beats the top weight n2\work{n^2} by a power of nn, giving:
TA(n)\displaystyle T_A(n) =Θ ⁣(nlog⁡25)≈Θ(n2.32)\displaystyle = \Theta\!\left(n^{\log_2 5}\right) \approx \Theta(n^{2.32})
Algorithm B has recurrence TB(n)=3TB(n2)+n2T_B(n) = \calls{3}T_B(\piece{\tfrac{n}{2}}) + \work{n^2}. Its bottom weight is nlog⁡23≈n1.58n^{\log_2 \calls{3}} \approx \calls{n^{1.58}}, so the top weight n2\work{n^2} wins by a power of nn, giving:
TB(n)\displaystyle T_B(n) =Θ(n2)\displaystyle = \Theta(n^2)
(Watch out for assuming both are Θ(n2)\fails{\Theta(n^2)} just because both end in +n2+ \work{n^2}, or writing Θ(nlog⁡23)\fails{\Theta(n^{\log_2 3})} for Algorithm B when its leaf count is smaller than the root work n2\work{n^2}.) Because cutting the branching factor from 5\calls{5} to 3\calls{3} pushes the leaf count below n2\work{n^2}, Algorithm B is faster for large nn.
7

For each recurrence, state whether the Master Method applies and find its Big-Theta runtime: (a) T(n)=4T(n2)+n2log⁡nT(n) = \calls{4}T(\piece{\tfrac{n}{2}}) + \work{n^2 \log n} and (b) T(n)=4T(n2)+n2log⁡nT(n) = \calls{4}T(\piece{\tfrac{n}{2}}) + \work{\tfrac{n^2}{\log n}}.

Both recurrences have bottom weight nlog⁡24=n2n^{\log_2 \calls{4}} = \calls{n^2}; check whether the log⁡n\log n factor on the tie power n2n^2 is multiplied or divided.

In both recurrences, the bottom weight is nlog⁡24=n2n^{\log_2 \calls{4}} = \calls{n^2}.
(a) The Master Method applies: f(n)=n2log⁡n\work{f(n) = n^2 \log n} has the tie power n2n^2 multiplied by log⁡n\log n (do not treat n2log⁡n>n2\fails{n^2 \log n > n^2} as a top win, which requires winning by a power of nn and would miss a log⁡n\log n factor). Multiplying f(n)\work{f(n)} by log⁡n\log n gives:
T(n)\displaystyle T(n) =Θ(n2log⁡n⋅log⁡n)\displaystyle = \Theta(\work{n^2 \log n} \cdot \log n) =Θ(n2log⁡2n)\displaystyle = \Theta(n^2 \log^2 n)
(b) The Master Method does not apply: f(n)=n2log⁡n\work{f(n) = \tfrac{n^2}{\log n}} is smaller than n2\calls{n^2} by only a log⁡n\log n factor rather than a power of nn (and divides by log⁡n\log n instead of multiplying, so neither the bottom-win rule's Θ(n2)\fails{\Theta(n^2)} nor the tie rule applies). Unrolling the log⁡2n\log_2 n levels gives a harmonic series:
T(n)\displaystyle T(n) =n2(1log⁡2n+⋯+12+1)+n2T(1)\displaystyle = \work{n^2\left(\frac{1}{\log_2 n} + \dots + \frac{1}{2} + 1\right)} + \calls{n^2} \given{T(1)} =Θ(n2log⁡log⁡n)\displaystyle = \Theta(n^2 \log \log n)
8

Explain why the Master Method does not apply to each recurrence, and unroll (a) and (b) to find their Big-Theta runtimes: (a) T(n)=T(n−1)+n2T(n) = T(\piece{n - 1}) + \work{n^2} with T(1)=1\given{T(1) = 1}, (b) T(n)=3T(n−1)+1T(n) = \calls{3}T(\piece{n - 1}) + \work{1} with T(1)=1\given{T(1) = 1}, and (c) T(n)=T(n4)+T(3n4)+nT(n) = T(\piece{\tfrac{n}{4}}) + T(\piece{\tfrac{3n}{4}}) + \work{n}.

Look inside T(…)T(\piece{\dots}) to see whether the input is divided by a single constant b>1\piece{b} > 1 or reduced by 11 across nn levels.

(a) The Master Method does not apply because the recurrence subtracts 11 (n−1\piece{n - 1}) instead of dividing by a constant b>1\piece{b} > 1 (treating it as a top-heavy Master Method case would wrongly give Θ(n2)\fails{\Theta(n^2)}). Unrolling across all nn levels sums the squares from 12\work{1^2} to n2\work{n^2}:
T(n)\displaystyle T(n) =n2+(n−1)2+⋯+12\displaystyle = \work{n^2} + \work{(n - 1)^2} + \dots + \work{1^2} =n(n+1)(2n+1)6\displaystyle = \frac{n(n + 1)(2n + 1)}{6} =Θ(n3)\displaystyle = \Theta(n^3)
(b) The Master Method does not apply because it subtracts 11 instead of dividing by b>1\piece{b} > 1 (treating a=3\calls{a = 3} as a polynomial leaf count nlog⁡b3\fails{n^{\log_b 3}} misses that the tree has height nn, not log⁡n\log n). Unrolling nn levels gives a geometric sum of length nn:
T(n)\displaystyle T(n) =1+3+9+⋯+3n−1\displaystyle = \work{1} + \work{3} + \work{9} + \dots + \work{3^{n-1}} =3n−12\displaystyle = \frac{3^n - 1}{2} =Θ(3n)\displaystyle = \Theta(3^n)
(c) The Master Method does not apply because it splits the problem into uneven pieces of sizes n4\piece{\tfrac{n}{4}} and 3n4\piece{\tfrac{3n}{4}} rather than equal pieces of a single size nb\piece{\tfrac{n}{b}}.