Different algorithms solve the same problem but with different resource requirements. Complexity analysis provides a principled way to:
- Compare algorithms independent of hardware
- Predict performance as input size grows
- Determine feasibility for large inputs
KEY TAKEAWAY: Complexity classifies how an algorithm’s resource usage (time or space) scales with input size $n$. This tells us whether an algorithm is practical for large inputs.
Time complexity measures how the number of fundamental operations grows as a function of input size $n$.
Example:
for i ← 0 to n-1 do
for j ← 0 to n-1 do
A[i][j] ← 0
end for
end for
This performs $n^2$ assignments. Time complexity: $O(n^2)$.
Space complexity measures how much memory an algorithm uses as a function of $n$.
Example:
- In-place sorting (e.g., Quicksort): $O(\log n)$ auxiliary space (call stack)
- Mergesort: $O(n)$ auxiliary space (temporary array for merging)
- Floyd-Warshall: $O(n^2)$ space (distance matrix)
Often, using more space allows faster computation:
- Memoisation (dynamic programming): Store computed results to avoid recomputation — trades space for time
- Hash tables: Use extra memory for $O(1)$ lookup vs. $O(n)$ search in a list
| Problem | $n$ represents |
|---|---|
| Sorting | Number of elements |
| Graph algorithms | Number of vertices $V$ and/or edges $E$ |
| String algorithms | Length of the string |
| Number theory | Magnitude of the number (or its number of digits) |
EXAM TIP: Always specify what $n$ represents when stating complexity. “The algorithm runs in $O(n^2)$” is incomplete without “where $n$ is the number of vertices.”
| Complexity | Name | Example |
|---|---|---|
| $O(1)$ | Constant | Array access, hash table lookup |
| $O(\log n)$ | Logarithmic | Binary search, heap operations |
| $O(n)$ | Linear | Linear search, single-pass scan |
| $O(n \log n)$ | Log-linear | Mergesort, Quicksort (average) |
| $O(n^2)$ | Quadratic | Bubble sort, Floyd-Warshall $O(n^3)$ |
| $O(2^n)$ | Exponential | Brute-force subsets |
| $O(n!)$ | Factorial | Brute-force permutations |
VCAA FOCUS: Be able to classify the time and space complexity of a given algorithm by analysing its structure. Also be able to argue why a complexity class is appropriate (not just state it).