Complexity Analysis Workshop
Retrieval Prompts
- State from memory the formal definition of
f(n) = O(g(n)). - State the Master Theorem's three cases and the regularity condition for case 3.
- Write the summation
sum_{i=1}^{n} iin closed form and itsThetaclass. - State the difference between amortized cost and average-case cost.
- Explain why a
while i < n: i *= 2loop isTheta(log n).
Compare and Distinguish
Separate these pairs clearly:
O(g(n))versusTheta(g(n))versus "runs ing(n)time"- amortized cost versus average-case cost versus worst-case-per-operation cost
- recursion tree method versus Master Theorem (when does each apply?)
T(n) = T(n-1) + nversusT(n) = 2 T(n/2) + n
Common Mistake Check
Identify the error in each statement:
- "Because the algorithm is
O(n^2), onn = 10it runs in roughly 100 operations." - "
T(n) = 2 T(n/2) + n^2solves toTheta(n^2 log n)by the Master Theorem." - "Merging two linked lists of length
nisTheta(log n)because we halve the work." - "A binary counter's
nincrements costTheta(n log n)because each increment flips up tolog nbits." - "Because my algorithm has the same big-O as quicksort, it will run equally fast in practice."
Mini Application
For each snippet, derive the tightest Theta bound with a written summation:
for i in range(n): for j in range(i): for k in range(j): work()for i in range(n): for j in range(n): if j > i: break- A recursive function
T(n) = T(n/2) + T(n/4) + n - A recursive function
T(n) = 3 T(n/2) + n - A binary-counter
incrementoperation, analyzed amortized overncalls
For each recurrence, choose the best solution method (substitution, recursion tree, or Master Theorem) and justify why.
Evidence Check
This page is complete only if, given any of the snippets above, you can:
- translate to a summation or recurrence without looking up the pattern
- solve that summation or recurrence to
Thetaclass - defend the bound with a one-paragraph written argument
Integrated Loop-Counting Drill
For each snippet, give the tightest bound and one correctness or cost sentence:
- Single loop: scan an array once and update a maximum.
- Nested fixed loop: compare every pair
i, j. - Additive loops: one loop over
n, followed by one loop overm. - Triangular loop:
for i in range(n): for j in range(i): work(). - Multiplicative nested loop: for every user, scan every permission.
- Halving loop: repeatedly divide
nby2until it reaches1. - Nested halving loop: for each
i, run a halving loop overn.
Evidence check: include one small input trace for at least three snippets and explain why constants and lower-order terms disappear only after the exact count is understood.