Unit 1 of 8 · about 4 hours · explain, derive

Cost models, asymptotic notation and cases

Every later choice in this stage starts by reading the constraint line. A wrong budget rejects a working algorithm before it is written, or accepts one that cannot finish.

You'll be able to

  • Translate an input bound into a cost budget: state which complexity classes fit up to 5000, and .
  • Define Big-O, Big-Omega and Big-Theta with quantifiers, and prove a polynomial bound such as from the definition.
  • Derive time and auxiliary-space bounds for nested and input-dependent loops, naming the variable each bound is in.

Covers

Input size, operation model and resource budgetBig , and with stated variablesWorst, average, expected and output-sensitive cost
1

Read

In this order, only the parts listed

0 of 4 done

  1. Computational complexity and Big-ODSA Handbook - Foundations and linear patterns

    What complexity actually means; How to read a complexity bound (the five notations); The hierarchy you will meet with the input-size table; Space-time tradeoffs

  2. Mathematics for Computer Science, Chapter 13, Section 13.7MIT 6.042J - asymptotics and invariants

    Section 13.7 Asymptotic Notation (printed pp. 528 to 549): 13.7.1 Little O, 13.7.2 Big O with Definitions 13.7.5 and 13.7.9 plus Proposition 13.7.10 (), 13.7.3 with Definition 13.7.13, 13.7.4 Pitfalls, 13.7.5 with Definitions 13.7.15 and 13.7.16

  3. Complexity ModelsAlgorithmica - memory and data layout

    Classical Complexity Theory and Asymptotic Complexity: the instruction-count model and where it breaks (clock speeds and instruction latencies)

  4. Programming Languages and constant factorsAlgorithmica - memory and data layout

    Interpreted languages, Compiled Languages and Takeaway: the pure-Python versus C versus OpenBLAS matrix-multiply comparison

    Read for the constant-factor warning only. Algorithmica's timings are C++ measurements; do not carry any crossover point to Python without your own timeit run.

2

Focus on

  • Constraints are budgets: up to 10 admits brute force, up to admits or better, and a value up to admits only , , a closed form or a lookup.
  • The equality in is one-way: holds and the reverse is nonsense.
  • Worst case is the default reading of complexity; average, expected and amortized each need their own argument, supplied by later units.
  • Constants are invisible in the notation: the handbook plans Python at about simple ops per second against for compiled languages.
Common mistakes · 3
  • Reading Big-O as an exact operation count. It bounds growth up to a constant, and the constant decides close races.
  • Quoting the handbook's operations-per-second rates, its sort crossover or its ten-to-thirty-second estimate for scanning items as measured facts. They are rough compiled-language planning numbers; time your own code with timeit before repeating them.
  • Calling one fast observed run an average case. A single input is one sample; an average needs a stated distribution.
3

Try it yourself

Closed-book, before checking anything

Without notes, write the -and- definitions of , and and prove . Then time both Two Sum variants at with seed 0 and an absent target in Python 3.13: expect the brute force at about 0.05 s per run against the hashmap at about 0.1 ms per run, a ratio of several hundred, on a 12th Gen Intel Core i9-12900K.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.Prove from the definition.Answer

    Take and . For all , . This is Proposition 13.7.10.

  2. 2. is true. Is ?Answer

    No. For every fixed , once , so no constant works. The equality reads one way only.

  3. 3.A problem allows up to on a one-second budget. Why is a linear scan rejected before coding?Answer

    At the planning rate of about simple operations per second for compiled code, steps take about 10 s, and Python at about per second needs over a minute. An input that large is usually one number, not an array, so (about steps), , a closed form or a lookup fits.

  4. 4.A dynamic-array push is amortized. Does that bound the slowest single push?Answer

    No. The push that triggers a resize copies every element, which is . Amortized averages over the sequence; latency-critical code cannot use the average.

5

Practice

  • LeetCode 1: Two Sum

    Implement the brute-force double loop and the complement hashmap. Time both with timeit at , seed 0, target absent: expect about 0.05 s per brute run against about 0.1 ms per hash run. State both bounds with the named variable .

    After submitting, read the handbook hash-maps section LC 1: value-to-index map for the lookup-then-insert order that avoids matching an element with itself on inputs like [3, 3].

You're done when

You prove a polynomial bound from the definitions without notes, and your Two Sum timing shows the predicted gap with both bounds stated.

Only when you can do this without notes.

Leads to

Notes

Resources · 3 for this unit, 5 more in the section
  • DSA Handbook - Foundations and linear patternsHandbook Academy · primary

    Study Part 0, Part 1, Part 3 and Part 4 in order, plus the Part 5 Linked-list fundamentals chapter. Use the routing-rule pages for arrays versus lists, map choice, stack versus queue, two pointers, sliding windows and prefix methods before seeing solved code.

  • MIT 6.042J - asymptotics and invariantsMIT OpenCourseWare · support

    Use the asymptotics, state-machine invariants and proof-method readings when a cost or correctness argument is weak. Solve only the matching problems.

  • Algorithmica - memory and data layoutSergey Slotin · support

    Read the memory hierarchy, cache and complexity-model chapters, then measure one contiguous-versus-linked and one hash-versus-ordered workload crossover.

Also in this section

  • AlgoMaster DSA - linear structures sliceAlgoMaster · required

    Attempt Design HashMap, Longest Substring Without Repeating Characters and Sliding Window Maximum cold. Hide tags, state the invariant first, and add randomized operation histories to Design HashMap.

  • Algorithms lecture notes - amortized analysis and hashingJeff Erickson · support

    Use note 9 Amortized Analysis for the summation, taxation, charging and potential-function proofs, and note 5 Hashing for chaining, universal hashing and open-addressing analysis, when the handbook treatment is too brief for a proof.

  • NeetCode - unseen linear problemsNeetCode · optional

    Use only for two tag-hidden transfer problems after the AlgoMaster slice. Do not start another full roadmap or watch a solution before the attempt is complete.

  • Hypothesis documentation - Quickstart and stateful testingHypothesis · support

    Follow Quickstart for @given strategies and the pytest combination, Configuring test settings for max_examples and derandomize, then Rule-based state machines (rule, precondition, invariant, run_state_machine_as_test) for randomized operation histories against dict and deque oracles.

  • CPython source - list and dict objectsPython Software Foundation · reference

    Read list_resize in Objects/listobject.c for the growth formula and pattern comment, and the USABLE_FRACTION definition, perturbation-probing comment and dummy-entry handling in Objects/dictobject.c. Read the cited functions and comments only.