Complexity Analysis
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 mean? (3 marks)
Q2. Arrange the following complexities in increasing order of growth rate: (4 marks)
Q3. For linear search in an unsorted array of 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 . (4 marks)
Q5. Using the Master Theorem, solve the following recurrences (state which case applies): (6 marks) (a) (b) (c)
Q6. Give the formal definitions of -notation and -notation. State the relationship between , , and . (5 marks)
Q7. Using the substitution method, prove that with is . (5 marks)
Q8. A dynamic array doubles its capacity when full. Using the aggregate method of amortized analysis, show that successive append operations cost total, and hence the amortized cost per append is . (5 marks)
Q9. Draw/describe the recursion tree for 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) means there exist positive constants and such that
- Statement of "exist constants " — 1 mark
- Inequality — 1 mark
- "for all " (asymptotic condition) — 1 mark
Why: Big-O is an asymptotic upper bound; it holds only beyond a threshold and up to a constant factor .
Q2. (4 marks)
- Constant/log/linear/linearithmic block correct — 2 marks
- Polynomial () 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: — target is the first element checked. (1)
- Worst case: — target is last or absent; all checked. (1.5)
- Average case: — on average comparisons if present, linear in . (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 ⇒ auxiliary space . (1)
Why: Auxiliary isolates the algorithm's own overhead; total includes input storage.
Q5. (6 marks) — Master Theorem, , compare with .
(a) . → Case 2.
(b) . → Case 1.
(c) . and regularity holds → Case 3.
Why: Compare driving function to ; smaller ⇒ leaves dominate (Case 1), equal ⇒ log factor (Case 2), larger ⇒ root dominates (Case 3).
Q6. (5 marks)
- : iff with for all . (2)
- : iff with for all . (2)
- Relationship: iff and . (1)
Why: = upper bound, = lower bound, = both (tight bound).
Q7. (5 marks) Guess . Unrolling: . Substitution proof: assume . Then This is provided , i.e. for and (choose : for ). ✓ Hence .
- Correct guess — 1
- Inductive substitution — 2
- Showing residual / valid — 1
- 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 appends, resizing occurs when size hits powers of . Copy cost when growing to size is (moving elements). Total copy cost: Plus simple insertions costing . Total . Amortized cost per append .
- Copy costs are powers of 2 — 1.5
- Geometric sum — 1.5
- Total — 1
- Amortized — 1
Why: Rare expensive doublings sum to a geometric series bounded by ; averaging over ops gives constant amortized cost.
Q9. (4 marks) Recursion tree for :
- Root does work ; splits into 2 subproblems of size .
- Level : nodes each of size → work per level. (1.5)
- Number of levels: (until size 1). (1)
- Total work: . (1.5)
Why: Each level contributes equal work ; multiplying by the tree height gives .
[
{"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)"}
]