Level 4 — ApplicationComplexity Analysis

Complexity Analysis

60 minutes50 marksprintable — key stays hidden on paper

Level: 4 — Application (novel problems, no hints) Time Limit: 60 minutes Total Marks: 50


Q1. [10 marks] A programmer writes a function on an array of size nn:

process(A, n):
    if n <= 1: return
    for i in 0..n-1:            # loop L1
        for j in 0..i:         # loop L2
            visit(A[j])
    process(A, n/3)
    process(A, n/3)

(a) Write the recurrence T(n)T(n) for the running time in terms of the work done at each level and the recursive calls. [3]

(b) Solve T(n)T(n) using the Master Theorem, stating aa, bb, and f(n)f(n), and identifying which case applies. [5]

(c) Give the tight Θ\Theta bound. [2]


Q2. [12 marks] Consider a dynamic array (initially capacity 1) that doubles its capacity when full, but on each doubling it copies all existing elements. Additionally, whenever the array becomes exactly one-quarter full after deletions, it halves its capacity (copying survivors).

(a) Using the aggregate method, prove that a sequence of nn push operations (no deletions) costs O(n)O(n) total, and state the amortized cost per push. [5]

(b) Using the accounting method, assign a charge per push that pays for future copies. State the charge and justify it covers all copy work. [4]

(c) Explain in one or two sentences why the quarter-full (rather than half-full) shrink threshold prevents O(n)O(n) amortized cost from thrashing between grow and shrink. [3]


Q3. [10 marks] Prove the following statements directly from the formal definition of the asymptotic notations (produce explicit constants where required).

(a) Prove 3n2+5n+2=O(n2)3n^2 + 5n + 2 = O(n^2) by exhibiting constants cc and n0n_0. [3]

(b) Prove 3n2+5n+2=Ω(n2)3n^2 + 5n + 2 = \Omega(n^2) with explicit cc, n0n_0. [3]

(c) Hence conclude Θ(n2)\Theta(n^2), and prove that 2nO(nk)2^{n} \neq O(n^{k}) for any fixed constant kk. [4]


Q4. [10 marks] Solve the recurrence using the recursion tree method, then verify by substitution: T(n)=3T(n/4)+n,T(1)=1.T(n) = 3\,T(n/4) + n, \qquad T(1) = 1.

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

(b) Verify your answer by the substitution method (guess T(n)cnT(n) \le cn and find a valid cc). [4]


Q5. [8 marks] For the algorithm below (searching for a target x in an unsorted array of nn distinct elements, each equally likely to be the target and target guaranteed present):

linsearch(A, x):
    for i in 0..n-1:
        if A[i] == x: return i

(a) State best-case and worst-case comparison counts with a one-line justification each. [3]

(b) Derive the average-case number of comparisons exactly (closed form), assuming uniform position distribution. [3]

(c) State the auxiliary space complexity and the total space complexity, distinguishing them. [2]


Answer keyMark scheme & solutions

Q1 [10]

(a) Work in loops L1/L2: sum over ii of (i+1)=n(n+1)2=Θ(n2)(i+1) = \frac{n(n+1)}{2} = \Theta(n^2). Two recursive calls of size n/3n/3: T(n)=2T(n/3)+Θ(n2).[3]T(n) = 2\,T(n/3) + \Theta(n^2). \quad \textbf{[3]}

(b) Master theorem with a=2a=2, b=3b=3, f(n)=Θ(n2)f(n)=\Theta(n^2). [1] Compare nlogba=nlog32n0.631n^{\log_b a} = n^{\log_3 2} \approx n^{0.631} with f(n)=n2f(n)=n^2. [2] Since f(n)=Ω(nlog32+ε)f(n) = \Omega(n^{\log_3 2 + \varepsilon}) and the regularity condition 2(n/3)2=29n2cn22\,(n/3)^2 = \frac{2}{9}n^2 \le c\,n^2 holds for c=29<1c=\frac{2}{9}<1, Case 3 applies. [2]

(c) T(n)=Θ(n2)T(n) = \Theta(n^2). [2]


Q2 [12]

(a) Copies occur at sizes 1,2,4,,2log2n1,2,4,\dots,2^{\lfloor\log_2 n\rfloor}. Total copy work 1+2+4++n<2n\le 1+2+4+\dots+n < 2n. Plus nn units for the pushes themselves. Total =n+(2n1)=O(n)= n + (2n-1) = O(n). [3] Amortized cost per push =O(n)/n=O(1)= O(n)/n = O(1) (a small constant, 3\le 3). [2]

(b) Charge 3 per push: 1 for placing the element, 1 saved to move itself on the next copy, 1 saved to move an element that was moved for free previously. When array of size kk doubles, the k/2k/2 elements added since the last doubling each carry 2 saved credits ⇒ kk credits, enough to copy all kk elements. [4]

(c) Halving only at quarter-full leaves the array half-full immediately after a shrink, so Θ(n)\Theta(n) operations are required before either another grow or shrink is triggered — the copy cost is amortized over Θ(n)\Theta(n) ops, avoiding thrashing (which a half-full shrink threshold would cause). [3]


Q3 [10]

(a) For n1n\ge1: 3n2+5n+23n2+5n2+2n2=10n23n^2+5n+2 \le 3n^2+5n^2+2n^2 = 10n^2. So c=10, n0=1c=10,\ n_0=1. [3]

(b) 3n2+5n+23n23n^2+5n+2 \ge 3n^2 for all n1n\ge1 (extra terms positive). So c=3, n0=1c=3,\ n_0=1. [3]

(c) Both O(n2)O(n^2) and Ω(n2)\Omega(n^2)Θ(n2)\Theta(n^2). [2] For 2nO(nk)2^n \ne O(n^k): limn2nnk=\lim_{n\to\infty} \frac{2^n}{n^k} = \infty (exponential beats any polynomial, provable by L'Hôpital kk times or ratio test). Hence no constants c,n0c,n_0 can bound 2ncnk2^n \le c\,n^k for all large nn. [2]


Q4 [10]

(a) Root cost nn. Level ii has 3i3^i nodes each of size n/4in/4^i, cost per level =3in/4i=n(3/4)i= 3^i \cdot n/4^i = n(3/4)^i. [2] Levels: i=0log4ni=0\dots\log_4 n. [1] Total =ni=0log4n(3/4)i<n113/4=4n= n\sum_{i=0}^{\log_4 n}(3/4)^i < n\cdot\frac{1}{1-3/4} = 4n. [2] So T(n)=Θ(n)T(n)=\Theta(n). [1]

(b) Guess T(n)cnT(n)\le cn. Then T(n)=3T(n/4)+n3c(n/4)+n=3c4n+n=(3c4+1)nT(n)=3T(n/4)+n \le 3\cdot c(n/4)+n = \frac{3c}{4}n + n = \left(\frac{3c}{4}+1\right)n. Need 3c4+1c1c/4c4\frac{3c}{4}+1 \le c \Rightarrow 1 \le c/4 \Rightarrow c\ge4. Take c=4c=4: holds. [4]


Q5 [8]

(a) Best case =1=1 (target at index 0). Worst case =n=n (target at last index / not found early). [3]

(b) Average =1ni=1ni=1nn(n+1)2=n+12=\frac{1}{n}\sum_{i=1}^{n} i = \frac{1}{n}\cdot\frac{n(n+1)}{2} = \frac{n+1}{2}. [3]

(c) Auxiliary space =O(1)=O(1) (only loop index / return value). Total space =O(n)=O(n) (includes the input array). Distinction: auxiliary excludes input storage; total includes it. [2]


[
  {"claim":"Q4 recursion tree geometric sum bounded by 4n, Theta(n)","code":"i=symbols('i'); S=summation((Rational(3,4))**i,(i,0,oo)); result=(S==4)"},
  {"claim":"Q4 substitution constant c=4 satisfies 3c/4+1<=c","code":"c=4; result=(Rational(3,4)*c+1<=c)"},
  {"claim":"Q5 average comparisons = (n+1)/2","code":"n=symbols('n',positive=True); k=symbols('k'); avg=summation(k,(k,1,n))/n; result=(simplify(avg-(n+1)/2)==0)"},
  {"claim":"Q3a bound 3n^2+5n+2 <= 10n^2 for n>=1","code":"n=symbols('n',positive=True); expr=10*n**2-(3*n**2+5*n+2); result=(expr.subs(n,1)>=0) and (simplify(diff(expr,n)).subs(n,1)>0)"},
  {"claim":"Q1 master case3: f=n^2 dominates n^(log_3 2)","code":"result=(2>log(2)/log(3))"}
]