Analysis of Algorithm Efficiency and Asymptotic Notations
Algorithm analysis investigates how resource consumption—primarily running time and memory space—scales as the input size n grows.
The Analysis Framework
Input Size Metric (n): The parameter measuring instance size (e.g., number of array items n, matrix dimension n×n, or number of bits b≈⌊log2n⌋+1).
Basic Operation: The operation contributing the most to the total running time, typically the innermost operation inside the deepest loop (e.g., key comparison, arithmetic multiplication).
Execution Time Model:
T(n)≈cop⋅C(n)
where cop is the hardware/execution cost of one basic operation, and C(n) is the total count of basic operations executed.
4. Best, Worst, and Average Cases:
Worst-case Cworst(n): Maximum operations over all inputs of size n. Guarantees an upper bound.
Best-case Cbest(n): Minimum operations over all inputs of size n.
Average-case Cavg(n): Expected operations under a defined probability distribution of inputs.
Asymptotic Notations (O,Ω,Θ)
Asymptotic notation abstracts away machine constants and low-order terms, categorizing functions by their order of growth.
Diagram loads as you scroll.
Formal Definitions
O(g(n)) (Asymptotic Upper Bound):
t(n)∈O(g(n)) if there exist positive constants c>0 and n0≥1 such that:
t(n)≤c⋅g(n)for all n≥n0
Ω(g(n)) (Asymptotic Lower Bound):
t(n)∈Ω(g(n)) if there exist positive constants c>0 and n0≥1 such that:
t(n)≥c⋅g(n)for all n≥n0
Θ(g(n)) (Asymptotically Tight Bound):
t(n)∈Θ(g(n)) if there exist positive constants c1,c2>0 and n0≥1 such that:
c1⋅g(n)≤t(n)≤c2⋅g(n)for all n≥n0
Equivalently: t(n)∈Θ(g(n))⟺t(n)∈O(g(n)) and t(n)∈Ω(g(n)).
Limit Quotient Method
To compare orders of growth for t(n) and g(n):
L=n→∞limg(n)t(n)
If L=0: t(n) has a strictly smaller order of growth than g(n), so t(n)∈O(g(n)) and t(n)∈/Θ(g(n)).
If L=c>0: t(n) and g(n) have the same order of growth, so t(n)∈Θ(g(n)).
If L=∞: t(n) grows strictly faster than g(n), so t(n)∈Ω(g(n)) and t(n)∈/O(g(n)).
Useful tools: L’Hôpital’s Rule limg(n)f(n)=limg′(n)f′(n) and Stirling’s formula n!≈2πn(en)n.
Standard Efficiency Classes
c<loglogn<logn<nc(0<c<1)<n<nlogn<n2<n3<2n<n!<nn
Loop Complexity Analysis Patterns
The following patterns represent the classic loop structures tested in algorithm examinations:
Pattern 1: Dependent Nested Loop
for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) sum++;}
Analysis:
i=1∑nj=1∑i1=i=1∑ni=2n(n+1)=Θ(n2)
Pattern 2: Stepped Nested Loop
for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j += 4) { for (int k = j + 1; k < n; k++) count++; }}
Analysis:
Outer loop runs n times.
Middle loop runs ≈(n−i)/4=Θ(n) times because constant step size +4 does not alter asymptotic growth.
Innermost loop runs ≈n−j=Θ(n) times.
Total=Θ(n×n×n)=Θ(n3)
Pattern 3: Quadratic Loop Bound
for (int i = 0; i < n; i++) { for (int j = i + 1; j < n * n; j++) count++;}
Analysis:
The inner loop upper bound is n2. For each of the n iterations of i, the inner loop executes ≈n2 times:
i=0∑n−1(n2−i−1)≈n⋅n2=Θ(n3)
Pattern 4: Repeated Squaring (j=j×j) vs Logarithmic Loops
for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) count++; for (int k = 0; k < n; k++) { for (int j = 2; j <= n; j = j * j) count++; } for (int m = 0; m < n; m++) count++;}
Analysis:
Sequence of values for j: 2,22=4,(22)2=16,22t≤n⟹2t≤log2n⟹t≤log2(log2n).
Hence, for (j = 2; j <= n; j = j * j) executes Θ(loglogn) times.
Inside loop i, the operations are sequential:
First loop: Θ(n)
Second nested loop: n×Θ(loglogn)=Θ(nloglogn)
Third loop: Θ(n)
Sum per outer iteration = Θ(nloglogn). With n outer iterations:
Total=n⋅Θ(nloglogn)=Θ(n2loglogn)
(Note: If the inner step was multiplicative j *= 5, it would be Θ(log5n), yielding Θ(n2logn)).
Pattern 5: Division Step (j=j/2)
for (int i = 1; i <= n; i++) { for (int j = n; j >= 1; j = j / 2) sum++;}
Analysis:
Values of j: n,n/2,n/4,…,1. The number of halving steps until ≤1 is ⌊log2n⌋+1=Θ(logn).
Total=i=1∑nΘ(logn)=Θ(nlogn)
Pattern 6: Branching Inside Loop
for (int i = 0; i < n; i++) { if (i < sqrt(n)) { for (int j = 0; j < n; j++) count++; } else { for (int k = 0; k < n; k++) count++; }}
Analysis:
Regardless of whether i<n (true for n iterations) or i≥n (true for n−n iterations), exactly one branch is taken, and both branches execute exactly n operations:
Work per outer iteration=Θ(n)⟹Total=n×Θ(n)=Θ(n2)
Exercises
Exercise 1
Determine whether 2n+1∈O(2n) and whether 22n∈O(2n). Justify using limits.
Show solution ↓Hide solution ↑
Solution
Part 1:
n→∞lim2n2n+1=n→∞lim2n2⋅2n=2
Since the limit is a finite positive constant (L=2), 2n+1∈Θ(2n)⊆O(2n) (True).
Part 2:
n→∞lim2n22n=n→∞lim2n(2n)2=n→∞lim2n=∞
Since the limit is ∞, 22n∈/O(2n); rather, 22n∈Ω(2n) (False).