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 n by splitting it into smaller pieces and calling itself recursively, we need a way to write an equation for its runtime T(n) and solve it without T on the right-hand side.
Writing Divide-and-Conquer Recurrences
Every standard divide-and-conquer recurrence has three parts that we read directly from what the algorithm does at a single step:
T(n)=recursive callsa⋅T(piece sizebn)+split and combine workf(n)
Here a≥1 and b>1 are constants, and a does not have to equal b.
Text solution
Find the piece size, call count, and combine workSplitting an array of size n into 3 equal pieces, recursively solving 2 of them, and spending n2 steps combining the answers gives each number directly:
b=3,a=2,f(n)=n2
Assemble the recurrencePlugging a,b, and f(n) into the divide-and-conquer formula gives the runtime equation:
T(n)=2T(3n)+n2
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 n down to the base-case leaves of size 1.
Three Recursion Trees: Balanced, Bottom-Heavy, and Top-Heavy
To compare the leaf count alogbn against powers of n, we rewrite the base a as a power of b,a=blogba, so the two exponents can trade places:
alogbn=(blogba)logbnWrite a in base b=(blogbn)logbaSwap the multiplied exponents=nlogbaSimplify blogbn=n
Text solution
Balanced tree: T(n)=2T(2n)+nDoubling the number of nodes at each level cancels the halved work per node, so every one of the log2n+1 levels does the same total work n:
Level i work=2i⋅(2in)=n
T(n)=work per rown×number of rows(log2n+1)=Θ(nlogn)
Bottom-heavy tree: T(n)=4T(2n)+nQuadrupling 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 workNumber of leavesT(n)=4i⋅(2in)=2in=4log2n=nlog24=n2=Θ(n2)
Top-heavy tree: T(n)=2T(2n)+n2Halving the input inside a squared work term divides each node's work by 4, 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⋅(2in)2=2i⋅4in2=(21)in2
T(n)=n2(1+21+41+…)=Θ(n2)
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 n. When we also have a base case like T(1)=3 and want an exact formula for T(n) (assuming n=bk is a clean power of b), we unroll the recurrence step by step down to T(1).
Unrolling Recurrences for Exact Closed Forms
T(n)T(1)nT(n)=2T(2n)+n=3=2k=?
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=r−1rk−1
Text solution
Unroll a balanced recurrence: T(n)=2T(2n)+n with T(1)=3Substituting the recurrence into itself i times produces i copies of n from the expanded rows plus 2i remaining sub-problems of size 2in:
Unroll a bottom-heavy recurrence: T(n)=4T(2n)+n with T(1)=1Expanding k=log2n levels produces a doubling geometric sum across the internal rows plus 4k leaves at the bottom:
Unrolling in algebra matches going down the tree: after i steps, the loose terms are the top i rows of work, and the 2iT(2in) term holds the remaining sub-problems on level i until it hits 2kT(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(bn)+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.
The Master Theorem: Leaf Weight vs. Root Weight
We compare the leaf count at the bottom of the tree against the non-recursive work at the root:
Whichever end grows faster by a power of n dominates the total runtime, and when they grow at the same rate, we multiply by the height of the tree, logn:
Bottom wins by a power of n:T(n)=Θ(nlogba)Tie:T(n)=Θ(nlogbalogn)Top wins by a power of n:T(n)=Θ(f(n))
Text solution
Quadratic tie: T(n)=9T(3n)+n2With a=9 and b=3, the leaf count matches the root work f(n)=n2, so we multiply by logn:
Binary search: T(n)=T(2n)+1With a=1 recursive call on half the list (b=2), the tree has only 1 leaf at the bottom, which ties the constant work f(n)=1 at the root:
Matrix multiplication: T(n)=8T(2n)+n2 vs. Strassen's T(n)=7T(2n)+n2Both algorithms spend f(n)=n2 work adding matrix blocks at the root, so the bottom weight wins in both, and cutting a from 8 to 7 lowers the exponent of n:
What happens when the bottom weight and top weight have the same power of n except for a logn factor, or when a recurrence doesn't split the input into equal bn pieces at all?
Traps and Red Flags for the Master Theorem
Text solution
Tie power multiplied by logn:T(n)=2T(2n)+nlognThe top weight nlogn has the same power of n as the bottom weight nlog22=n1 with a multiplied logn factor, so the tie rule still multiplies f(n) by logn:
Tie power divided by logn (gap case): T(n)=2T(2n)+lognnBecause lognn is smaller than n1 by only a logn factor rather than a power of n (and divides by logn instead of multiplying), the Master Method does not apply; unrolling the log2n rows gives a harmonic sum:
Subtracting instead of dividing: T(n)=T(n−1)+n and T(n)=2T(n−1)+1Because the input shrinks to n−1 rather than bn, the tree is n levels tall instead of logn levels tall, so we unroll n steps directly:
Every divide-and-conquer recurrence we analyze with the Master Method must have the form T(n)=aT(bn)+f(n) with constants a≥1 and b>1. Check the inside of T(…) 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) or T(n)=2T(n−1)+1=Θ(2n)), splits into uneven pieces (like T(n)=T(3n)+T(32n)+n), or has a non-constant branching factor (like T(n)=nT(2n)+n), the Master Method does not apply and you should unroll the tree directly.
When a problem gives a base case T(1) and asks for an exact closed-form formula (with n=bk), unroll the recurrence for k=logbn levels and add the sum of the internal rows to the leaf work akT(1)=nlogbaT(1). For T(n)=2T(2n)+n with T(1)=3, the k=log2n internal rows each cost n and the 2k=n leaves each cost 3, giving T(n)=nlog2n+3n.
When you only need the Big-Theta runtime, compare the bottom weight (the leaf count nlogba) against the top weight (the root work f(n)):
Bottom wins by a power of n
If nlogba has a larger exponent of n than f(n), the leaves dominate and T(n)=Θ(nlogba) (for 4T(2n)+n,nlog24=n2 beats n1, giving Θ(n2)).
Top wins by a power of n
If f(n) has a larger exponent of n than nlogba, the root dominates and T(n)=Θ(f(n)) (for 2T(2n)+n2,n2 beats nlog22=n1, giving Θ(n2)).
Tie in the power of n
If f(n) has the exact same exponent of n as nlogba (optionally multiplied by (logn)k, never divided), the levels balance and T(n)=Θ(f(n)logn) (for 2T(2n)+n,n1 ties n1 to give Θ(nlogn); for 2T(2n)+nlogn, multiplying by logn gives Θ(nlog2n); but for 2T(2n)+lognn, dividing by logn falls in the gap between the tie rule and a bottom win, and unrolls to Θ(nloglogn)).
5
Practice
1
An algorithm splits an array of size n into 3 equal pieces, recursively solves 2 of them, and spends n2 steps splitting and combining the results. Write the recurrence T(n) for its runtime and find its Big-Theta complexity.
Watch out for swapping a and b into 3T(2n)+n2, which mistakes the 3 pieces for 3 recursive calls.
Match the number of recursive calls to a and the piece-size denominator to b, then compare the exponent of n in nlogba against n2.
Splitting into 3 equal pieces gives b=3, solving 2 of them gives a=2, and spending n2 steps outside the recursive calls gives f(n)=n2 (watch out for swapping a and b into 3T(2n)+n2, which mistakes the 3 pieces for 3 recursive calls):
T(n)=2T(3n)+n2
Now compare the bottom weight against the top weight:
Bottom weight=nlog32≈n0.63,Top weight=f(n)=n2
Because the top weight n2 wins over nlog32 by a power of n (top-heavy), the root dominates and T(n)=Θ(n2).
2
For the recurrence T(n)=8T(2n)+n2, find in its work tree: (a) the number of sub-problems, the size of each sub-problem, and the total work across Level i; (b) the total number of leaves at the bottom of the tree as a polynomial in n; and (c) the Big-Theta runtime T(n).
At Level i, remember to square the entire shrunk input size 2in when finding the work inside each node.
(a) At Level i, branching by 8 creates 8i sub-problems, and halving the input i times gives each sub-problem size 2in. Each node of size m=2in does m2=(2in)2=4in2 work (be careful not to forget to square the 2i in the denominator, which would give 2in2 and a wrong row total of 4in2). Multiplying the node count by the work per node gives the Level i total:
Level i work=8i⋅4in2=2in2
(b) At the bottom level i=log2n, the number of leaves is:
8log2n=nlog28=n3
(Watch out for computing 28=4 and writing n4 instead of using log28=3.)
(c) Since the bottom weight n3 beats the top weight n2 by a power of n (row totals double at each level), the leaves dominate and T(n)=Θ(n3).
3
Find the Big-Theta runtime of (a) T(n)=4T(2n)+n2 and (b) T(n)=T(3n)+1.
Find the leaf count nlogba for each recurrence and compare it to f(n); in part (b), ask what power of 3 equals 1.
(a) Comparing the two weights gives:
Bottom weight=nlog24=n2,Top weight=f(n)=n2
Because the bottom weight and top weight are tied at n2, we multiply by logn (don't stop at Θ(n2) without the logn factor for the height of the tree) to get T(n)=Θ(n2logn).
(b) With a=1 and b=3, the bottom weight is:
nlog31=n0=1
(Be careful not to evaluate log31 as 1, which would give n1 and the wrong answer Θ(n).) Since the bottom weight 1 ties the top weight f(n)=1, multiplying by logn gives T(n)=Θ(logn).
4
Given T(n)=3T(3n)+2n with base case T(1)=5, assume n=3k and unroll the recurrence to find an exact closed-form formula for T(n) in terms of n, then compute the exact value of T(27).
Watch out for adding T(1)=5 just once at the end as 2nlog3n+5 instead of multiplying by all 3k=n leaves, which would give 167 for T(27).
Unroll a few levels to see what happens to the 2n term at each row, then stop at i=k=log3n and include the leaf term 3k⋅T(1).
Unrolling step by step shows that every internal level contributes 2n:
Stopping at the leaves when i=k=log3n (so 3kn=1 and 3k=n) gives:
T(n)=2nlog3n+n⋅T(1)=2nlog3n+5n
(Watch out for adding T(1)=5 just once at the end as 2nlog3n+5 instead of multiplying by all 3k=n leaves, which would give 167 for T(27).) Plugging in n=27 with log327=3 gives:
T(27)=2(27)(3)+5(27)=162+135=297
5
Given T(n)=9T(3n)+n with base case T(1)=2, assume n=3k and unroll the recurrence to find an exact closed-form formula for T(n) in terms of n, then compute the exact value of T(9).
Don't forget the denominator 3−1=2 in the geometric sum formula, which would double the internal sum to n2−n and give 234 at the end.
Factor n out of the internal row sum to get a geometric series with ratio r=3, and rewrite 9k at the leaves in terms of n=3k.
Unrolling k=log3n steps gives a row total that triples at every level:
Since 3k=n, the geometric sum is 3−13k−1=2n−1, so the internal levels sum to 2n(n−1)=2n2−n (don't forget the denominator 3−1=2 in the geometric sum formula, which would double the internal sum to n2−n and give 234 at the end). At the leaves, 9k=(3k)2=n2 (not n), so 9k⋅T(1)=2n2. Adding the two parts together gives:
T(n)=2n2−n+2n2=25n2−n
Plugging in n=9 gives:
T(9)=25(81)−9=198
6
Two divide-and-conquer algorithms solve the same problem by splitting an input of size n into 2 halves and spending n2 steps combining the results, but Algorithm A makes 5 recursive calls while Algorithm B makes 3 recursive calls. Write both recurrences, find their Big-Theta runtimes, and determine which algorithm is faster for large n.
Compare each algorithm's leaf count nlog2a against n2 by checking whether a is greater than or less than 22=4.
Algorithm A has recurrence TA(n)=5TA(2n)+n2. Its bottom weight is nlog25≈n2.32, which beats the top weight n2 by a power of n, giving:
TA(n)=Θ(nlog25)≈Θ(n2.32)
Algorithm B has recurrence TB(n)=3TB(2n)+n2. Its bottom weight is nlog23≈n1.58, so the top weight n2 wins by a power of n, giving:
TB(n)=Θ(n2)
(Watch out for assuming both are Θ(n2) just because both end in +n2, or writing Θ(nlog23) for Algorithm B when its leaf count is smaller than the root work n2.) Because cutting the branching factor from 5 to 3 pushes the leaf count below n2, Algorithm B is faster for large n.
7
For each recurrence, state whether the Master Method applies and find its Big-Theta runtime: (a) T(n)=4T(2n)+n2logn and (b) T(n)=4T(2n)+lognn2.
Both recurrences have bottom weight nlog24=n2; check whether the logn factor on the tie power n2 is multiplied or divided.
In both recurrences, the bottom weight is nlog24=n2.
(a) The Master Method applies: f(n)=n2logn has the tie power n2 multiplied by logn (do not treat n2logn>n2 as a top win, which requires winning by a power of n and would miss a logn factor). Multiplying f(n) by logn gives:
T(n)=Θ(n2logn⋅logn)=Θ(n2log2n)
(b) The Master Method does not apply: f(n)=lognn2 is smaller than n2 by only a logn factor rather than a power of n (and divides by logn instead of multiplying, so neither the bottom-win rule's Θ(n2) nor the tie rule applies). Unrolling the log2n levels gives a harmonic series:
T(n)=n2(log2n1+⋯+21+1)+n2T(1)=Θ(n2loglogn)
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)+n2 with T(1)=1, (b) T(n)=3T(n−1)+1 with T(1)=1, and (c) T(n)=T(4n)+T(43n)+n.
Look inside T(…) to see whether the input is divided by a single constant b>1 or reduced by 1 across n levels.
(a) The Master Method does not apply because the recurrence subtracts 1(n−1) instead of dividing by a constant b>1 (treating it as a top-heavy Master Method case would wrongly give Θ(n2)). Unrolling across all n levels sums the squares from 12 to n2:
T(n)=n2+(n−1)2+⋯+12=6n(n+1)(2n+1)=Θ(n3)
(b) The Master Method does not apply because it subtracts 1 instead of dividing by b>1 (treating a=3 as a polynomial leaf count nlogb3 misses that the tree has height n, not logn). Unrolling n levels gives a geometric sum of length n:
T(n)=1+3+9+⋯+3n−1=23n−1=Θ(3n)
(c) The Master Method does not apply because it splits the problem into uneven pieces of sizes 4n and 43n rather than equal pieces of a single size bn.