Level 3 — ProductionComplexity Analysis

Complexity Analysis

45 minutes60 marksprintable — key stays hidden on paper

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 f(n)=O(g(n))f(n) = O(g(n)) using constants cc and n0n_0. (2)

(b) Using the definition directly, prove that 3n2+5n+7=O(n2)3n^2 + 5n + 7 = O(n^2). You must explicitly exhibit valid constants cc and n0n_0 and justify the inequality. (5)

(c) Prove, from the definition, that 2n+1=O(2n)2^{n+1} = O(2^n) but 22nO(2n)2^{2n} \neq O(2^n). (3)


Question 2 — Best / worst / average case (10 marks)

Consider linear search for a key xx in an unsorted array of nn 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 xx is present and equally likely to be at any of the nn 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 Θ\Theta bound. Show the comparison of f(n)f(n) with nlogban^{\log_b a}.

(a) T(n)=2T(n/2)+nT(n) = 2T(n/2) + n (3)

(b) T(n)=4T(n/2)+nT(n) = 4T(n/2) + n (3)

(c) T(n)=3T(n/2)+n2T(n) = 3T(n/2) + n^2 (3)

(d) T(n)=2T(n/2)+nlognT(n) = 2T(n/2) + n\log n. Explain why the standard three cases fail and give the correct bound. (3)


Question 4 — Recursion tree + substitution (12 marks)

Consider T(n)=2T(n/2)+nT(n) = 2T(n/2) + n, with T(1)=1T(1) = 1.

(a) Draw/describe the recursion tree: cost per level, number of levels, and total. (5)

(b) From the tree, conjecture a closed form for T(n)T(n). (2)

(c) Prove your conjectured upper bound T(n)=O(nlogn)T(n) = O(n\log n) 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 nn consecutive push operations.

(a) Using the aggregate method, show the total cost of nn pushes is O(n)O(n), hence amortized O(1)O(1) 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 Φ\Phi 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 Θ(g(n))\Theta(g(n)) and Ω(g(n))\Omega(g(n)). (2)

(b) Prove that 12n23n=Θ(n2)\tfrac{1}{2}n^2 - 3n = \Theta(n^2) by exhibiting constants c1,c2,n0c_1, c_2, n_0. (4)

Answer keyMark scheme & solutions

Question 1 (10)

(a) f(n)=O(g(n))f(n)=O(g(n)) iff c>0, n0>0\exists c>0,\ n_0>0 such that 0f(n)cg(n)0 \le f(n) \le c\,g(n) for all nn0n \ge n_0. (2) (why: bounds growth above beyond a threshold.)

(b) For n1n \ge 1: 5n5n25n \le 5n^2 and 77n27 \le 7n^2, so 3n2+5n+73n2+5n2+7n2=15n2.3n^2 + 5n + 7 \le 3n^2 + 5n^2 + 7n^2 = 15n^2. Choose c=15c = 15, n0=1n_0 = 1. Then 3n2+5n+715n23n^2+5n+7 \le 15n^2 for all n1n\ge 1. (5) (1 for bounding each lower term by an n2n^2 term, 2 for exhibiting c,n0c,n_0, 2 for the final inequality.)

(c) 2n+1=22n22n2^{n+1} = 2\cdot 2^n \le 2\cdot 2^n, so c=2c=2, n0=1n_0=1 works → O(2n)O(2^n). (1.5) 22n=(2n)22^{2n} = (2^n)^2. Suppose 22nc2n2^{2n} \le c\,2^n; then 2nc2^n \le c for all large nn — impossible since 2n2^n \to \infty. Hence 22nO(2n)2^{2n} \neq O(2^n). (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: xx at index 0 → 1 comparison. Worst case: xx at last index (or absent) → nn comparisons. (3)

(c) If xx is at position ii (1-indexed), comparisons =i= i. Each position equally likely, probability 1/n1/n: E[C]=i=1n1ni=1nn(n+1)2=n+12.E[C] = \sum_{i=1}^{n} \frac{1}{n}\cdot i = \frac{1}{n}\cdot\frac{n(n+1)}{2} = \frac{n+1}{2}. (4) (2 for summation setup, 2 for closed form.) This is Θ(n)\Theta(n).


Question 3 (12)

Here nlogban^{\log_b a} is the watershed.

(a) a=2,b=2a=2,b=2: nlog22=n1=nn^{\log_2 2}=n^1=n. f(n)=n=Θ(n)f(n)=n=\Theta(n)Case 2. T(n)=Θ(nlogn)\boxed{T(n)=\Theta(n\log n)}. (3)

(b) a=4,b=2a=4,b=2: nlog24=n2n^{\log_2 4}=n^2. f(n)=n=O(n2ϵ)f(n)=n=O(n^{2-\epsilon})Case 1. T(n)=Θ(n2)\boxed{T(n)=\Theta(n^2)}. (3)

(c) a=3,b=2a=3,b=2: nlog23n1.585n^{\log_2 3}\approx n^{1.585}. f(n)=n2=Ω(nlog23+ϵ)f(n)=n^2 = \Omega(n^{\log_2 3 + \epsilon}) and regularity holds (3(n/2)2=34n2cn23(n/2)^2 = \tfrac34 n^2 \le c n^2) → Case 3. T(n)=Θ(n2)\boxed{T(n)=\Theta(n^2)}. (3)

(d) a=2,b=2a=2,b=2: nlog22=nn^{\log_2 2}=n. f(n)=nlognf(n)=n\log n is larger than nn but not by a polynomial factor nϵn^\epsilon (only by logn\log n), so Case 3 fails; it's not Θ(n)\Theta(n) so Case 2 fails; not polynomially smaller so Case 1 fails. Using the extended case / recursion tree: T(n)=Θ(nlog2n)T(n)=\Theta(n\log^2 n). (3)


Question 4 (12)

(a) Tree for T(n)=2T(n/2)+nT(n)=2T(n/2)+n: (5)

  • Level 0: cost nn (1 node).
  • Level 1: 2 nodes, each n/2n/2 → total nn.
  • Level kk: 2k2^k nodes, each n/2kn/2^k → total nn.
  • Depth: nn/21n\to n/2\to\dots\to 1 gives log2n\log_2 n levels.
  • Total =n(log2n+1)= n\cdot(\log_2 n + 1).

(b) T(n)=nlog2n+n=Θ(nlogn)T(n) = n\log_2 n + n = \Theta(n\log n). (2)

(c) Claim T(n)cnlog2nT(n) \le c\,n\log_2 n for suitable cc, n2n\ge 2. (5) Hypothesis: assume T(n/2)c(n/2)log2(n/2)T(n/2) \le c\,(n/2)\log_2(n/2). T(n)=2T(n/2)+n2cn2log2n2+n=cn(log2n1)+nT(n) = 2T(n/2)+n \le 2\cdot c\,\tfrac{n}{2}\log_2\tfrac{n}{2} + n = c n(\log_2 n - 1) + n =cnlog2ncn+ncnlog2nprovided c1.= c n\log_2 n - cn + n \le c n\log_2 n \quad\text{provided } c \ge 1. So with c=1c=1 the induction step holds. (1 for stating hypothesis, 2 for substitution, 2 for closing with the cn+n0-cn+n\le0 condition.)


Question 5 (10)

(a) Over nn pushes, capacity doublings occur at sizes 1,2,4,1,2,4,\dots Copy costs sum to j=0log2n2j<2n.\sum_{j=0}^{\lfloor\log_2 n\rfloor} 2^j < 2n. Plus nn unit insert costs. Total <n+2n=3n=O(n)< n + 2n = 3n = O(n); amortized =3n/n=O(1)= 3n/n = O(1). (5) (3 for the geometric sum <2n<2n, 2 for combining and dividing.)

(b) Charge 3 per push: 1 pays the immediate insert, 2 are saved as credit. When capacity doubles from kk to 2k2k, the kk old elements each carry 2 saved credits → 2k2k credit available, enough to pay the kk copies. Credit never goes negative → amortized O(1)O(1). (3)

(c) Let Φ(Di)=2(num elements)(capacity)\Phi(D_i) = 2\cdot(\text{num elements}) - (\text{capacity}). It must satisfy Φ(D0)=0\Phi(D_0)=0 and Φ(Di)0\Phi(D_i)\ge 0 for all ii, so that c^ici\sum \hat c_i \ge \sum c_i (amortized costs upper-bound actual total). (2)


Question 6 (6)

(a) Θ(g)\Theta(g): c1,c2,n0>0\exists c_1,c_2,n_0>0 with 0c1g(n)f(n)c2g(n)0\le c_1 g(n)\le f(n)\le c_2 g(n) for nn0n\ge n_0. Ω(g)\Omega(g): c,n0>0\exists c,n_0>0 with 0cg(n)f(n)0\le c\,g(n)\le f(n) for nn0n\ge n_0. (2)

(b) Upper: 12n23n12n2\tfrac12 n^2 - 3n \le \tfrac12 n^2c2=12c_2=\tfrac12. (1.5) Lower: need 12n23nc1n2\tfrac12 n^2 - 3n \ge c_1 n^2. Take c1=14c_1=\tfrac14: 12n23n14n2    14n23n    n12\tfrac12 n^2 - 3n \ge \tfrac14 n^2 \iff \tfrac14 n^2 \ge 3n \iff n \ge 12. (1.5) So c1=14,c2=12,n0=12c_1=\tfrac14, c_2=\tfrac12, n_0=12 gives Θ(n2)\Theta(n^2). (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)"}
]