Complexity Analysis
Level: 3 — Production (from-scratch derivations, code-from-memory, explain-out-loud) Time limit: 45 minutes Total marks: 60
Question 1 — Big-O from the formal definition (10 marks)
(a) State the formal definition of using constants and . (2)
(b) Using the definition directly, prove that . You must explicitly exhibit valid constants and and justify the inequality. (5)
(c) Prove, from the definition, that but . (3)
Question 2 — Best / worst / average case (10 marks)
Consider linear search for a key in an unsorted array of distinct elements.
(a) Write the linear-search function from memory (pseudocode or any language) and mark where the comparison count is incurred. (3)
(b) State the best-case and worst-case comparison counts with the input that triggers each. (3)
(c) Assuming is present and equally likely to be at any of the positions, derive the average-case number of comparisons as a closed-form expression. Show the summation. (4)
Question 3 — Master theorem (12 marks)
For each recurrence, state which Master-theorem case applies and give the tight bound. Show the comparison of with .
(a) (3)
(b) (3)
(c) (3)
(d) . Explain why the standard three cases fail and give the correct bound. (3)
Question 4 — Recursion tree + substitution (12 marks)
Consider , with .
(a) Draw/describe the recursion tree: cost per level, number of levels, and total. (5)
(b) From the tree, conjecture a closed form for . (2)
(c) Prove your conjectured upper bound by the substitution method (induction). State and use the inductive hypothesis explicitly. (5)
Question 5 — Amortized analysis (10 marks)
A dynamic array starts empty. On overflow it doubles its capacity (copying all existing elements), otherwise an insert costs 1. Consider consecutive push operations.
(a) Using the aggregate method, show the total cost of pushes is , hence amortized per push. Sum the doubling (copy) costs explicitly. (5)
(b) Using the accounting method, assign a charge of 3 per push and argue every copy is paid for. (3)
(c) Give a potential function and state what property it must satisfy for the amortized cost to bound the actual cost. (2)
Question 6 — Θ and Ω (6 marks)
(a) State the formal definitions of and . (2)
(b) Prove that by exhibiting constants . (4)
Answer keyMark scheme & solutions
Question 1 (10)
(a) iff such that for all . (2) (why: bounds growth above beyond a threshold.)
(b) For : and , so Choose , . Then for all . (5) (1 for bounding each lower term by an term, 2 for exhibiting , 2 for the final inequality.)
(c) , so , works → . (1.5) . Suppose ; then for all large — impossible since . Hence . (1.5)
Question 2 (10)
(a) (3)
LinearSearch(A, n, x):
for i = 0 to n-1:
if A[i] == x: # comparison counted here
return i
return -1
(comparison incurred once per loop iteration.)
(b) Best case: at index 0 → 1 comparison. Worst case: at last index (or absent) → comparisons. (3)
(c) If is at position (1-indexed), comparisons . Each position equally likely, probability : (4) (2 for summation setup, 2 for closed form.) This is .
Question 3 (12)
Here is the watershed.
(a) : . → Case 2. . (3)
(b) : . → Case 1. . (3)
(c) : . and regularity holds () → Case 3. . (3)
(d) : . is larger than but not by a polynomial factor (only by ), so Case 3 fails; it's not so Case 2 fails; not polynomially smaller so Case 1 fails. Using the extended case / recursion tree: . (3)
Question 4 (12)
(a) Tree for : (5)
- Level 0: cost (1 node).
- Level 1: 2 nodes, each → total .
- Level : nodes, each → total .
- Depth: gives levels.
- Total .
(b) . (2)
(c) Claim for suitable , . (5) Hypothesis: assume . So with the induction step holds. (1 for stating hypothesis, 2 for substitution, 2 for closing with the condition.)
Question 5 (10)
(a) Over pushes, capacity doublings occur at sizes Copy costs sum to Plus unit insert costs. Total ; amortized . (5) (3 for the geometric sum , 2 for combining and dividing.)
(b) Charge 3 per push: 1 pays the immediate insert, 2 are saved as credit. When capacity doubles from to , the old elements each carry 2 saved credits → credit available, enough to pay the copies. Credit never goes negative → amortized . (3)
(c) Let . It must satisfy and for all , so that (amortized costs upper-bound actual total). (2)
Question 6 (6)
(a) : with for . : with for . (2)
(b) Upper: → . (1.5) Lower: need . Take : . (1.5) So gives . (1)
[
{"claim":"Average linear search comparisons = (n+1)/2 via sum i/n", "code":"n=symbols('n',positive=True); i=symbols('i'); expr=summation(i/n,(i,1,n)); result = simplify(expr-(n+1)/2)==0"},
{"claim":"Bounding 3n^2+5n+7 <= 15n^2 at n0=1 holds", "code":"result = (3*1**2+5*1+7) <= 15*1**2"},
{"claim":"Doubling copy cost sum 2^0..2^k < 2n (test n=16, k=4)", "code":"result = sum(2**j for j in range(0,5)) < 2*16"},
{"claim":"Theta lower bound: n^2/2-3n >= n^2/4 for n>=12", "code":"n=12; result = (n**2/2-3*n) >= (n**2/4)"}
]