COMPUTER ENGINEER

← BlogDATA STRUCTURES / ALGORITHMSPython example pack ↓

Problems, mathematics and analysis · 02 / 53

Big-O, Θ, Ω and the cost model

Understand → trace → implement → justify

3 min reading · allow extra time for practice

Big-O is an asymptotic upper bound, not a synonym for worst case. Identify the function: worst, average, best or amortised cost. For 3n²+7n+4, sufficiently large n permits f(n)≤cn². Θ supplies matching upper and lower bounds; Ω supplies a lower bound only.

Nested syntax does not prove quadratic cost. A constant inner loop stays linear, and two forward-only pointers may move at most 2n times across the entire run. Sequential costs add; independent nested repetitions multiply. log₂ n measures repeated halving. Logarithm bases differ by a constant factor for asymptotic classification.

Separate input, output and auxiliary space. Producing an n-element result can require O(n) output space while using O(1) auxiliary space. Python arbitrary-precision arithmetic depends on bit length; state when you are using a unit-cost RAM model.

Building the mathematical argument

For f(n)=3n²+7n+4 and n≥1, we have 7n≤7n² and 4≤4n². Thus f(n)≤14n²: c=14 and n₀=1 satisfy O(n²). Since f(n)≥3n², the tight bound is Θ(n²). O(n³) is also true but loose. O(n²) does not mean exactly n² executed steps.

An inner loop running i times yields 0+1+…+(n−1)=n(n−1)/2 operations. A loop that halves its counter needs a different count. An n-iteration outer loop with log n inner work gives Θ(n log n). A sliding window may contain nested while loops yet take Θ(n), because each pointer advances at most n times overall.

GrowthApproximate effect of doubling nReason
log nA constant extra stepHalving the range
n2×One scan
n log nSlightly more than 2×Linear work on logarithmic levels
n²4×All pairs
2ⁿA factor of 2ⁿAll subsets

These ratios describe dominant asymptotic terms. Real latency includes allocation, caches, JIT, scheduling and I/O. Record input distributions, warm-up, repetitions and environment. A faster algorithm at 100 elements may lose at ten million.

Three practice stages

Foundation: Compare three consecutive n loops with three nested n loops: Θ(n) versus Θ(n³).

Intermediate: Bound total advances in a two-pointer algorithm where j is never reset and only increases.

Advanced: Replace unit-cost large-integer multiplication with a bit-cost model. Separate item count from bit length. The algorithm can remain identical while its complexity expression changes.

A worked trace

One million elements need about 20 halvings, while n² comparisons reach 10¹². These quantify work, not elapsed-time guarantees.

Cost and assumptions

1+2+…+n=n(n+1)/2 ⇒ Θ(n²). Distinguish logarithmic, linear, n log n, quadratic and exponential growth.

Executable example

Run with Python 3. Assertions check a small example; they do not constitute a general correctness proof. Some advanced examples are explicitly small models or reference oracles, not full production implementations.

Python 3
def triangular_work(n):
    count = 0
    for i in range(n):
        for j in range(i + 1):
            count += 1
    return count
assert triangular_work(4) == 10

TEST YOUR UNDERSTANDING

Your turn

What changes if the inner loop runs min(i,10) times?

Reveal the reasoning

Total work is at most 10n and also linear from below for large n, hence Θ(n). Early iterations do not change the class.

Take it further

  1. Write down the input contract and the invariant before changing the example.
  2. Add empty, boundary, duplicate and adversarial cases where they are relevant. Compare with a simple oracle.
  3. Derive the cost under the stated model; measure several input sizes and explain discrepancies.

Primary reading & lecture notes

Original explanations in this course; these references provide proofs and further reading. Source links may cover the broader module.

MIT 6.006 · Introduction to Algorithms ↗
Solve the exercise before moving on.