Algorithm Analysis — Asymptotic Notation and Recurrences
Measuring running time by counting operations, best/worst/average cases, Big-O, Ω and Θ with their properties, the growth-rate hierarchy, counting loops (nested, logarithmic, dependent), solving recurrences by substitution, recursion trees and the master theorem, amortised analysis, and space complexity.
Running time is measured by counting basic operations (comparisons, assignments, arithmetic) as a function of the input size n, ignoring machine-dependent constants.
Worst case: maximum over all inputs of size n (the usual guarantee).
Best case: minimum (e.g. insertion sort on sorted input: n−1 comparisons).
Average case: expectation over a distribution of inputs (quicksort: ≈1.39nlog2n comparisons on random input).
Asymptotic notation
Notation
Meaning
Informally
f=O(g)
∃c,n0:f(n)≤cg(n)∀n≥n0
f grows at most like g
f=Ω(g)
∃c,n0:f(n)≥cg(n)∀n≥n0
at least like g
f=Θ(g)
both O and Ω
exactly like g
f=o(g)
f/g→0
strictly slower
f=ω(g)
f/g→∞
strictly faster
Properties: transitivity; O(f)+O(g)=O(max(f,g)); constants and lower-order terms vanish (3n2+10nlogn+7=Θ(n2)); logarithm bases differ by constants (log2n=Θ(log10n)), but exponential bases do not (2n=Θ(3n)).
Limit test: if limf/g=c with 0<c<∞, then f=Θ(g); if the limit is 0, f=o(g).
The growth hierarchy
1≪loglogn≪logn≪n≪n≪nlogn≪n2≪n3≪2n≪n!≪nn
n
log2n
nlog2n
n2
2n
10
3.3
33
100
1024
1000
10
104
106
≈10301
106
20
2×107
1012
—
Counting loops
Loop shape
Iterations
for i in range(n)
n
nested for i in range(n): for j in range(n)
n2
for i in range(n): for j in range(i)
2n(n−1)
i = 1; while i < n: i *= 2
⌈log2n⌉
i = n; while i > 0: i //= 2
⌊log2n⌋+1
for i in range(n): j = 1; while j < n: j *= 2
nlogn
i = 1; while i * i <= n: i += 1
n
Recurrences
Recursive algorithms give recurrences. Three ways to solve them:
Each level of merge sort's recursion tree does n total work.
3. Master theorem for T(n)=aT(n/b)+f(n) with a≥1, b>1. Compare f(n) with nlogba:
Case
Condition
T(n)
1
f(n)=O(nlogba−ϵ) (leaves dominate)
Θ(nlogba)
2
f(n)=Θ(nlogbalogkn), k≥0 (balanced)
Θ(nlogbalogk+1n)
3
f(n)=Ω(nlogba+ϵ) and af(n/b)≤cf(n), c<1 (root dominates)
Θ(f(n))
Recurrences worth memorising
Recurrence
Solution
Algorithm
T(n)=T(n/2)+1
Θ(logn)
binary search
T(n)=T(n−1)+1
Θ(n)
linear recursion, linear search
T(n)=2T(n/2)+1
Θ(n)
tree traversal
T(n)=2T(n/2)+n
Θ(nlogn)
merge sort, balanced quicksort
T(n)=T(n−1)+n
Θ(n2)
worst-case quicksort, selection sort
T(n)=2T(n−1)+1
Θ(2n)
Towers of Hanoi
T(n)=T(n−1)+T(n−2)+1
Θ(φn), φ≈1.618
naive Fibonacci
T(n)=T(n)+1
Θ(loglogn)
Amortised analysis
The average cost per operation over a worst-case sequence of operations. Example: a dynamic array (Python list) doubles its capacity when full; a single append may cost O(n) for copying, but n appends cost O(n) in total, so each is amortised O(1).
Space complexity
Extra memory used besides the input: merge sort O(n), quicksort O(logn) expected for the recursion stack, heapsort and insertion sort O(1). Recursion depth counts as space.
Quick revision
Practice questions
GATE-style questions: 4 one-mark and 5 two-mark. Wrong MCQs cost ⅓ of their marks in the real exam; MSQ and NAT have no negative marking. Attempt each before opening the solution.
Q1MCQ1 mark
The recurrence T(n)=2T(n/2)+n solves to
Q2MCQ1 mark
The recurrence T(n)=T(n/2)+c (binary search) solves to
Q3NAT2 marks
What does this program print?
Python
count = 0
i = 1while i < 100:
i *= 2
count += 1print(count)
Q4MSQ2 marks
Which of the following are true?
One or more options are correct.
Q5NAT2 marks
What does this program print?
Python
c = 0for i inrange(10):
for j inrange(i, 10):
c += 1print(c)
Q6MCQ1 mark
The recurrence T(n)=T(n−1)+n, T(1)=1 solves to
Q7MCQ2 marks
The recurrence T(n)=3T(n/2)+n solves to
Q8MCQ1 mark
Which ordering of growth rates (slowest to fastest) is correct?
Q9NAT2 marks
T(n)=2T(n/2)+1 with T(1)=1. The value of T(8) is ______.