Level 2 — RecallComplexity Analysis

Complexity Analysis

30 minutes40 marksprintable — key stays hidden on paper

Subject: Coding · Chapter: Complexity Analysis Difficulty Level: 2 (Recall — definitions, standard problems, short derivations) Time Limit: 30 minutes Total Marks: 40


Instructions

  • Answer all questions.
  • Show working where derivations are required.
  • Use ...... notation for mathematical expressions.

Q1. State the formal (mathematical) definition of Big-O notation: what does f(n)=O(g(n))f(n) = O(g(n)) mean? (3 marks)

Q2. Arrange the following complexities in increasing order of growth rate: (4 marks) O(n2),  O(1),  O(2n),  O(nlogn),  O(logn),  O(n!),  O(n),  O(n3)O(n^2),\; O(1),\; O(2^n),\; O(n\log n),\; O(\log n),\; O(n!),\; O(n),\; O(n^3)

Q3. For linear search in an unsorted array of nn elements, state the best-case, worst-case, and average-case time complexity, giving a one-line justification for each. (4 marks)

Q4. Define auxiliary space and total space complexity. State the auxiliary space complexity of iterative merge on two sorted halves (the merge step of merge sort) that uses a temporary array of size nn. (4 marks)

Q5. Using the Master Theorem, solve the following recurrences (state which case applies): (6 marks) (a) T(n)=2T(n/2)+nT(n) = 2T(n/2) + n (b) T(n)=4T(n/2)+nT(n) = 4T(n/2) + n (c) T(n)=3T(n/2)+n2T(n) = 3T(n/2) + n^2

Q6. Give the formal definitions of Θ\Theta-notation and Ω\Omega-notation. State the relationship between OO, Ω\Omega, and Θ\Theta. (5 marks)

Q7. Using the substitution method, prove that T(n)=T(n1)+nT(n) = T(n-1) + n with T(1)=1T(1)=1 is O(n2)O(n^2). (5 marks)

Q8. A dynamic array doubles its capacity when full. Using the aggregate method of amortized analysis, show that nn successive append operations cost O(n)O(n) total, and hence the amortized cost per append is O(1)O(1). (5 marks)

Q9. Draw/describe the recursion tree for T(n)=2T(n/2)+nT(n) = 2T(n/2) + n and use it to determine the total work. State the number of levels and work per level. (4 marks)


End of Paper

Answer keyMark scheme & solutions

Q1. (3 marks) f(n)=O(g(n))f(n) = O(g(n)) means there exist positive constants c>0c > 0 and n0>0n_0 > 0 such that 0f(n)cg(n)for all nn0.0 \le f(n) \le c\,g(n) \quad \text{for all } n \ge n_0.

  • Statement of "exist constants c,n0c, n_0" — 1 mark
  • Inequality f(n)cg(n)f(n) \le c\,g(n)1 mark
  • "for all nn0n \ge n_0" (asymptotic condition) — 1 mark

Why: Big-O is an asymptotic upper bound; it holds only beyond a threshold n0n_0 and up to a constant factor cc.


Q2. (4 marks) O(1)<O(logn)<O(n)<O(nlogn)<O(n2)<O(n3)<O(2n)<O(n!)O(1) < O(\log n) < O(n) < O(n\log n) < O(n^2) < O(n^3) < O(2^n) < O(n!)

  • Constant/log/linear/linearithmic block correct — 2 marks
  • Polynomial (n2,n3n^2, n^3) before exponential/factorial — 1 mark
  • Fully correct order — 1 mark

Why: Growth ranks as constant → logarithmic → polynomial (by degree) → exponential → factorial.


Q3. (4 marks)

  • Best case: O(1)O(1) — target is the first element checked. (1)
  • Worst case: O(n)O(n) — target is last or absent; all nn checked. (1.5)
  • Average case: O(n)O(n) — on average (n+1)/2\approx (n+1)/2 comparisons if present, linear in nn. (1.5)

Why: Best case counts the luckiest input; worst the hardest; average is the expected number of comparisons over inputs.


Q4. (4 marks)

  • Auxiliary space: extra/temporary space used by an algorithm excluding the input itself. (1.5)
  • Total space complexity: auxiliary space plus the space taken by the input. (1.5)
  • Merge step using temp array of size nn ⇒ auxiliary space =O(n)= O(n). (1)

Why: Auxiliary isolates the algorithm's own overhead; total includes input storage.


Q5. (6 marks) — Master Theorem, T(n)=aT(n/b)+f(n)T(n)=aT(n/b)+f(n), compare f(n)f(n) with nlogban^{\log_b a}.

(a) a=2,b=2nlog22=na=2,\,b=2 \Rightarrow n^{\log_2 2}=n. f(n)=n=Θ(n)f(n)=n=\Theta(n)Case 2. T(n)=Θ(nlogn).(2)T(n) = \Theta(n\log n). \quad \textbf{(2)}

(b) a=4,b=2nlog24=n2a=4,\,b=2 \Rightarrow n^{\log_2 4}=n^2. f(n)=n=O(n2ϵ)f(n)=n=O(n^{2-\epsilon})Case 1. T(n)=Θ(n2).(2)T(n) = \Theta(n^2). \quad \textbf{(2)}

(c) a=3,b=2nlog23n1.585a=3,\,b=2 \Rightarrow n^{\log_2 3}\approx n^{1.585}. f(n)=n2=Ω(nlog23+ϵ)f(n)=n^2=\Omega(n^{\log_2 3+\epsilon}) and regularity holds → Case 3. T(n)=Θ(n2).(2)T(n) = \Theta(n^2). \quad \textbf{(2)}

Why: Compare driving function to nlogban^{\log_b a}; smaller ⇒ leaves dominate (Case 1), equal ⇒ log factor (Case 2), larger ⇒ root dominates (Case 3).


Q6. (5 marks)

  • Θ\Theta: f(n)=Θ(g(n))f(n)=\Theta(g(n)) iff 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 all nn0n\ge n_0. (2)
  • Ω\Omega: f(n)=Ω(g(n))f(n)=\Omega(g(n)) iff c,n0>0\exists\, c,n_0>0 with 0cg(n)f(n)0\le c\,g(n)\le f(n) for all nn0n\ge n_0. (2)
  • Relationship: f(n)=Θ(g(n))f(n)=\Theta(g(n)) iff f(n)=O(g(n))f(n)=O(g(n)) and f(n)=Ω(g(n))f(n)=\Omega(g(n)). (1)

Why: OO = upper bound, Ω\Omega = lower bound, Θ\Theta = both (tight bound).


Q7. (5 marks) Guess T(n)cn2T(n) \le c\,n^2. Unrolling: T(n)=n+(n1)++1=n(n+1)2T(n) = n + (n-1) + \dots + 1 = \frac{n(n+1)}{2}. Substitution proof: assume T(n1)c(n1)2T(n-1) \le c(n-1)^2. Then T(n)=T(n1)+nc(n1)2+n=cn22cn+c+n.T(n) = T(n-1) + n \le c(n-1)^2 + n = cn^2 - 2cn + c + n. This is cn2\le cn^2 provided 2cn+c+n0-2cn + c + n \le 0, i.e. for c1c \ge 1 and n1n \ge 1 (choose c=1c=1: 2n+1+n=1n0-2n+1+n = 1-n \le 0 for n1n\ge1). ✓ Hence T(n)=O(n2)T(n) = O(n^2).

  • Correct guess cn2cn^21
  • Inductive substitution — 2
  • Showing residual 0\le 0 / valid cc1
  • Conclusion — 1

Why: Substitution assumes the bound for smaller inputs and verifies the induction step holds with a fixed constant.


Q8. (5 marks) Aggregate method: Over nn appends, resizing occurs when size hits powers of 22. Copy cost when growing to size 2k2^k is 2k2^k (moving elements). Total copy cost: k=0log2n2k=2log2n+11<2n.\sum_{k=0}^{\lfloor \log_2 n\rfloor} 2^k = 2^{\lfloor\log_2 n\rfloor+1}-1 < 2n. Plus nn simple insertions costing nn. Total =n+2n=O(n)= n + 2n = O(n). Amortized cost per append =O(n)n=O(1)= \dfrac{O(n)}{n} = O(1).

  • Copy costs are powers of 2 — 1.5
  • Geometric sum <2n< 2n1.5
  • Total O(n)O(n)1
  • Amortized O(1)O(1)1

Why: Rare expensive doublings sum to a geometric series bounded by 2n2n; averaging over nn ops gives constant amortized cost.


Q9. (4 marks) Recursion tree for T(n)=2T(n/2)+nT(n)=2T(n/2)+n:

  • Root does work nn; splits into 2 subproblems of size n/2n/2.
  • Level ii: 2i2^i nodes each of size n/2in/2^i → work 2in2i=n2^i \cdot \frac{n}{2^i} = n per level. (1.5)
  • Number of levels: log2n+1\log_2 n + 1 (until size 1). (1)
  • Total work: n×(log2n+1)=Θ(nlogn)n \times (\log_2 n + 1) = \Theta(n\log n). (1.5)

Why: Each level contributes equal work nn; multiplying by the tree height log2n\log_2 n gives nlognn\log n.


[
  {"claim":"Master (a): 2T(n/2)+n gives n log n; work per level constant = n, levels = log2 n","code":"import sympy as sp\nn=sp.Symbol('n',positive=True)\n# n^(log_b a) with a=2,b=2\nexp=sp.log(2,2)\nresult=(exp==1)"},
  {"claim":"Master (b): 4T(n/2)+n, log_2 4 = 2, so Theta(n^2)","code":"import sympy as sp\nresult=(sp.log(4,2)==2)"},
  {"claim":"Master (c): 3T(n/2)+n^2, log_2 3 < 2 so f=n^2 dominates -> Theta(n^2)","code":"import sympy as sp\nresult=(sp.log(3,2) < 2)"},
  {"claim":"Q7 closed form: sum 1..n = n(n+1)/2 which is O(n^2)","code":"import sympy as sp\nn=sp.Symbol('n',positive=True,integer=True)\ns=sp.summation(sp.Symbol('k'),(sp.Symbol('k'),1,n))\nresult=(sp.simplify(s - n*(n+1)/2)==0)"},
  {"claim":"Q8 aggregate: sum of 2^k for k=0..m = 2^(m+1)-1 < 2*2^m","code":"import sympy as sp\nm=sp.Symbol('m',positive=True,integer=True)\ns=sp.summation(2**sp.Symbol('k'),(sp.Symbol('k'),0,m))\nresult=(sp.simplify(s-(2**(m+1)-1))==0)"}
]