Lite / Independent Engineering Research / Week 19
How fast does the work grow?
Your code sorts 1,000 readings in a millisecond. Will a million readings take two seconds, or seventeen minutes?
Why should I care?
A benchmark on today's input tells you almost nothing about tomorrow's. Asymptotic analysis does: it says how the work grows, which is what decides whether a perception pipeline, a planner or a database survives contact with real data. It is the first question a reviewer asks about any algorithm.
- Count the basic operations of a loop nest and express the count in Big-O, Big-Ω and Big-Θ notation.
- Distinguish best, worst and average cases, and time from space complexity.
- Predict how run time scales when the input grows or the machine gets faster.
Predict
Algorithm A does 100·n operations; algorithm B does n². For which inputs is A faster?
- Always: 100·n is linear
- Never: 100 is a big constant
- Only when n > 100
- Only when n < 100
Guess first, then reveal
They cross where 100·n = n², at n = 100. Below that, B's smaller constant wins; above it, A wins and keeps winning by more. Big-O ignores the constant because, for large enough n, growth rate always beats it.
Discover
Set n = 10, then 30, then 60. Watch which bars turn orange (over a second) and red (over a year). Then make the machine 1,000× faster and see how much bigger an input each algorithm can now handle in one second.
What the interactive shows (text version)
The racer lists seven growth rates. At n = 20 on a billion-operations-per-second machine: log n and n are nanoseconds, n² is 400 ns, n³ is 8 µs, 2ⁿ is about 1 ms, n! is about 77 years. A 1,000× faster machine lets n² handle 32× bigger inputs but 2ⁿ only about 10 more items.
Watch (optional)
Watch for: Notice how the video compares selection sort's n² with merge sort's n log n by counting comparisons, not by timing a particular computer.
Shows how a step-by-step recipe of decisions and repeats lets a machine solve a problem such as sorting.
“Intro to Algorithms: Crash Course Computer Science #13” by CrashCourse (opens YouTube; streaming video uses a lot of data)
The video is optional. Everything you need is in this lesson's text.
Understand
Count operations, not seconds
Pick a basic operation (a comparison, a multiply, a memory read) and count how many times it runs as a function of input size n. Seconds depend on the machine; the count depends only on the algorithm. Two nested loops over n items give n² executions of the inner line; a loop that halves i each pass gives ⌊log₂ n⌋.
O, Ω and Θ
f(n) = O(g(n)) means f grows no faster than g: there are constants c and n₀ with f(n) ≤ c·g(n) for all n ≥ n₀. Ω is the matching lower bound, and Θ means both. So 3n² + 50n + 7 is Θ(n²): the leading term dominates and constants are dropped. Big-O is an upper bound, so n is also O(n²), but saying Θ(n) is more informative.
Best, worst, average
Linear search finds the item first time in the best case (Θ(1)), last in the worst (Θ(n)), and about halfway on average if every position is equally likely. Quicksort is Θ(n log n) on average but Θ(n²) in the worst case. Engineers designing real-time systems usually care about the worst case, because a deadline missed once can be a failure.
Space counts too
Memory is a resource with its own complexity. Merge sort needs Θ(n) extra space; heapsort needs Θ(1). On a microcontroller with 64 KB of RAM, an algorithm with the better time complexity can be useless if its space complexity does not fit.
Explain it another way
Intuitive
Imagine handing out exam papers. Giving one to each student is linear work. Having every student shake hands with every other is quadratic: double the class and the handshakes quadruple.
Technical
Asymptotic notation classifies functions by growth rate: Θ(1) ⊂ Θ(log n) ⊂ Θ(n) ⊂ Θ(n log n) ⊂ Θ(n²) ⊂ Θ(2ⁿ) ⊂ Θ(n!). Analysis fixes a cost model (for example, the RAM model where each primitive operation costs 1) and derives a bound on the count.
Mathematical
For f(n) = n(n−1)/2: f(n) ≤ ½n² for all n ≥ 1, so f = O(n²) with c = ½; and f(n) ≥ ¼n² for n ≥ 2, so f = Ω(n²). Hence Θ(n²). For the speed-up question: if T(n) = c·n^k and the machine is s times faster, the input size that fits a fixed time grows by s^(1/k).
Applied
A robot's obstacle check that compares every pair of points in a 10,000-point lidar scan does 5×10⁷ comparisons per scan. A k-d tree cuts that to roughly n log n ≈ 1.3×10⁵: the difference between 10 scans a second and none.
Worked example 1
Count the inner executions: for i in 0…n−1: for j in i+1…n−1: work() with n = 6.
For i = 0 the inner loop runs 5 times, then 4, 3, 2, 1, 0. Total 15 = 6×5/2. In general n(n−1)/2, which is Θ(n²).
Worked example 2
An n² algorithm handles n = 1,000 in 1 s. Roughly what n fits in 1 s on a machine 100× faster?
Time ∝ n², so n can grow by √100 = 10: about n = 10,000. For an n log n algorithm the gain would be a little under 100×; for 2ⁿ, only about log₂ 100 ≈ 7 more items.
Common mix-ups
O(n) always beats O(n²).
✓ Only for large enough n. Constants and lower-order terms decide small inputs, so measure there too.Big-O means the exact running time.
✓ It is an upper bound up to a constant factor. Θ gives a tight bound; neither gives seconds.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
Apply it
Pick three pieces of code you wrote this year (a filter, a search, a loop over sensor data). For each, state the basic operation, its count as a function of n, its Θ class and its extra space. Then estimate the largest n each could process in 10 ms on a 100 MHz microcontroller doing one operation per cycle.
Reflect
Which algorithm you have used would hurt most if its input grew 100 times, and why?
Write your answer on paper or in your own notes. Lite never stores what you write.