TactivergeLite

Lite / Intelligent Robotic Systems / Week 6

Big-O: the shape of growth

Algorithm A takes 3n² + 50n + 1000 steps; algorithm B takes 400n. Which would you choose for a map with a million cells?

About 40 min · 3 goals · Checked by automated tests · Full interactive version

Why should I care?

Big-O is the engineer's shorthand for 'how does the cost grow?'. It ignores details that don't matter for large inputs, so you can compare designs on paper and spot the one that will collapse when the map, the log or the parts list gets ten times bigger.

Predict

A = 3n² + 50n + 1000 steps, B = 400n steps. For n = 1,000,000, roughly how many times more steps does A take?

  1. About the same
  2. About 10 times
  3. About 7,500 times
  4. About a million times
Guess first, then reveal

About 7,500 times: A ≈ 3 × 10¹² and B = 4 × 10⁸. For n between about 3 and 113, A is actually cheaper, but its n² term wins in the end. That's big-O: O(n²) versus O(n).

Discover

Switch on the logarithmic y-axis so all four curves fit. Then raise the constant in front of n log n to 20 and find where n² overtakes it. What does that tell you about small inputs versus large ones?

What the interactive shows (text version)

The plotter shows log n, n, n log n and n² against n, optionally on a logarithmic y-axis. A slider multiplies n log n by a constant; with a constant of 20, n² is cheaper until about n = 145, after which n² grows far faster.

You can also open the interactive on the full site when you have a faster connection.

Watch (optional)

Watch for: Watch why constants and smaller terms are dropped, and how nested loops lead to O(n²).

Explains why big-O drops constants and smaller terms, with worked examples of loops and nested loops.

“Big O Notation” by HackerRank (opens YouTube; streaming video uses a lot of data)

The video is optional. Everything you need is in this lesson's text.

Understand

Keep the top term, drop the constant

From an exact count, keep the term that grows fastest and drop its coefficient: 5n + 20 → O(n); n(n − 1)/2 → O(n²); 4 log₂ n + 7 → O(log n). Big-O describes the shape of growth, not the exact number of steps.

The usual ladder

O(1) constant (dict lookup) < O(log n) (binary search) < O(n) (one scan) < O(n log n) (good sorts) < O(n²) (all pairs, insertion sort) < O(n³) (three nested loops). Doubling n adds a step, doubles, slightly more than doubles, quadruples, and multiplies by 8.

Predicting from a measurement

If you measure 20 ms at n = 1,000 and the code is O(n²), expect about 20 × 4 = 80 ms at n = 2,000 and 20 × 100 = 2 s at n = 10,000. Then test the prediction: if the measurement disagrees, your model of the code is wrong somewhere.

Explain it another way

Intuitive

Big-O is like describing a hill by its steepness rather than its exact height: a steep hill always ends up higher eventually, however far back it starts.

Technical

f(n) is O(g(n)) if there are constants c and n₀ with f(n) ≤ c·g(n) for all n ≥ n₀. Usually we quote the worst case; average and best cases can differ (insertion sort: best O(n), worst O(n²)).

Mathematical

Ratios on doubling: (2n)²/n² = 4; (2n)³/n³ = 8; (2n log 2n)/(n log n) = 2(1 + 1/log₂ n); log₂(2n) − log₂ n = 1.

Applied

Choosing an O(n log n) planner step over an O(n²) one is often the difference between a robot that replans in real time on a big warehouse map and one that freezes.

Derivation: Why constants and smaller terms are dropped
  1. Take T(n) = 3n² + 50n + 1000. Divide by n²: T(n)/n² = 3 + 50/n + 1000/n².

  2. As n grows, 50/n and 1000/n² shrink towards 0, so T(n)/n² approaches 3. The n² term ends up doing almost all the work.

  3. Big-O asks only how T grows, up to a constant factor: T(n) ≤ c·n² for some constant c once n is large enough (here c = 4 works for n ≥ 66). So T(n) is O(n²).

  4. The constant 3 is dropped because it depends on the machine and the language; the exponent 2 doesn't. Doubling n multiplies any O(n²) cost by about 4, whatever the constant.

Worked example 1

Predict from a measurement for three growth rates.

import math


def predict(t_ms, n_old, n_new, growth):
    f = {
        "n": lambda n: n,
        "n log n": lambda n: n * math.log2(n),
        "n^2": lambda n: n * n,
    }[growth]
    return t_ms * f(n_new) / f(n_old)


for g in ["n", "n log n", "n^2"]:
    print(g, round(predict(20, 1024, 4096, g), 1))
# n 80.0
# n log n 96.0
# n^2 320.0

Common mix-ups

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

Practice 1. An exact count is T(n) = 7n + 3n·log₂n + 12. What is its big-O?

ms

ms

Practice 4. Which list is in order from slowest-growing to fastest-growing?
Practice 5. Algorithm X is O(n log n) and Y is O(n²). Which statement is true?

Apply it

Write the big-O of each function in your planner package (loading, neighbours, BFS, report). Pick the one with the worst growth and predict how its time changes when the map grows from 50 × 50 to 200 × 200. Keep the prediction for the next lesson's measurements.

Reflect

In one sentence each: what does big-O tell you, and what doesn't it tell you?

Write your answer on paper or in your own notes. Lite never stores what you write.

← Count the steps before you time themModule check: measure growth, then explain it →