The whole, before the parts

Analysis of algorithms asks one question: as the input grows, how fast does the cost grow? Raw seconds depend on this year's hardware; growth rates do not, so the growth rate is the durable fact about an algorithm. The single idea that carries the topic is that a handful of design strategies — halving the problem, dividing and recombining, taking the locally best step, remembering sub-answers — each stamp a characteristic growth rate on whatever they touch, so you can often read an algorithm's cost straight off its shape. Citation needed

Terminology and core concepts

  1. Big-O notation

    f is O(g) when, for all large enough inputs, f is at most a constant multiple of g — an upper bound on growth that deliberately ignores constant factors and small inputs, because those are facts about machines, not algorithms. Citation needed

    What does O(g) bound?
    growth from above, up to a constant factor Citation needed
    Why does Big-O drop constant factors?
    they reflect the machine, not the algorithm Citation needed
    Is 3n² + 50n in O(n²)?
    yes — for large n, 4n² dominates it Citation needed
    Is an O(n) algorithm always faster than an O(n²) one?
    no — only beyond some input size Citation needed
    What does Θ(g) add to O(g)?
    a matching lower bound — growth exactly like g Citation needed
  2. Reading cost from structure

    Cost follows shape: a single pass is O(n), a loop within a loop over the same data is O(n²), repeatedly halving is O(log n), and halving with a pass at each level is O(n log n). Citation needed

    What growth does a loop nested inside a loop over n items give?
    O(n²) Citation needed
    What growth does repeated halving give?
    O(log n) Citation needed
    What shape of algorithm costs O(n log n)?
    a full pass at each of log n halving levels Citation needed
    Assuming runtime scales as n², 1,000 items take 1 s. About how long do 10,000 items take?
    about 100 s Citation needed
    Which case — best, worst, or average — does analysis quote by default?
    the worst case Citation needed
  3. Binary search

    On sorted data, comparing against the middle discards half the candidates per step, finding any item in O(log n) — the standing proof that paying once to organise data can repay itself on every later query. Citation needed

    What precondition does binary search demand?
    the data is sorted Citation needed
    What does each comparison in binary search discard?
    half the remaining candidates Citation needed
    At most how many probes into a sorted array of 1,000,000?
    about 20 Citation needed
    What does linear search cost on the same array?
    up to 1,000,000 probes Citation needed
    When is sorting first not worth it for searching?
    when you will search only once or twice Citation needed
  4. Sorting, slow and fast

    Insertion sort grows items into a sorted prefix at O(n²) worst case; merge sort splits, sorts halves, and merges at O(n log n) always; quicksort partitions around a pivot at O(n log n) on average — and no sort that works by comparing items can beat O(n log n) in the worst case. Citation needed

    What is insertion sort's worst-case growth?
    O(n²) Citation needed
    When is insertion sort nonetheless excellent?
    on small or nearly sorted inputs Citation needed
    What is merge sort's growth on every input?
    O(n log n) Citation needed
    What triggers quicksort's O(n²) worst case?
    pivots that split lopsidedly every time Citation needed
    What is the comparison-sort lower bound?
    no comparison sort beats O(n log n) worst case Citation needed
  5. Divide and conquer, and recurrences

    Divide and conquer splits a problem into smaller copies of itself, solves them recursively, and recombines; its cost is written as a recurrence — an equation giving the cost at size n in terms of the cost at smaller sizes — and solving the recurrence is solving the analysis. Citation needed

    What does divide and conquer do before solving subproblems?
    splits the problem Citation needed
    How does divide and conquer solve its smaller subproblems?
    recursively Citation needed
    What does divide and conquer do with the subproblem solutions?
    combines them Citation needed
    What does a recurrence express?
    cost at size n in terms of smaller sizes Citation needed
    What recurrence describes merge sort?
    T(n) = 2T(n/2) + O(n) Citation needed
    What does T(n) = T(n/2) + O(1) solve to?
    O(log n) — binary search's cost Citation needed
  6. Greedy algorithms

    A greedy algorithm takes the locally best step and never reconsiders; it is fast everywhere but correct only on problems where local best provably leads to global best — so a greedy answer without that proof is just a guess. Citation needed

    What does a greedy algorithm never do?
    revisit a choice already made Citation needed
    What must be proved before trusting a greedy answer?
    that local best leads to global best here Citation needed
    Does greedy coin-change work for coins 1, 5, 10, 25?
    yes — that system is provably greedy-safe Citation needed
    Does greedy change-making work for coins 1, 3, 4 on amount 6?
    no — greedy gives 4+1+1, optimal is 3+3 Citation needed
  7. Dynamic programming

    When a recursion meets the same subproblem over and over, dynamic programming stores each sub-answer the first time and reuses it ever after — trading memory for time, often collapsing exponential work to polynomial. Citation needed

    What symptom calls for dynamic programming?
    the same subproblems recomputed many times Citation needed
    What does dynamic programming trade for speed?
    memory to store sub-answers Citation needed
    What is naive recursive Fibonacci's growth?
    exponential in n Citation needed
    What is Fibonacci's growth with stored sub-answers?
    linear in n Citation needed
    What must subproblems have for stored answers to be reusable?
    answers independent of how they were reached Citation needed
  8. Graph traversal

    Breadth-first search explores a graph outward in rings using a queue, finding fewest-edge paths; depth-first search follows one path to its end using a stack before backing up, revealing structure like cycles and ordering — both visiting every vertex and edge once, in O(V + E). Citation needed

    What structure drives breadth-first search?
    a queue Citation needed
    What structure drives depth-first search?
    a stack Citation needed
    Which traversal finds the fewest-edge path?
    breadth-first search Citation needed
    What is the cost of either traversal?
    O(V + E) Citation needed
    Why must visited vertices be marked?
    otherwise cycles trap the traversal forever Citation needed
  9. Shortest paths by greedy: Dijkstra

    With non-negative edge weights, Dijkstra's algorithm repeatedly finalises the unvisited vertex with the smallest known distance — a greedy choice that is safe precisely because no later path through heavier edges could undercut it. Citation needed

    Which vertex does Dijkstra finalise next?
    the unvisited one with smallest known distance Citation needed
    What edge condition makes Dijkstra's greedy choice safe?
    no negative weights Citation needed
    Why does a negative edge break Dijkstra?
    a finalised distance could later be undercut Citation needed
    What structure serves the smallest-distance vertex efficiently?
    a priority queue Citation needed

Algorithm learning paths

Study one algorithm at a time: understand the idea, trace a small example, read and run the Python code, explain its cost, then solve a practice problem. Start with searching and sorting before moving to graphs and optimization. Citation needed

New to analysis? Review the core concepts above before choosing a path. These paths cover a core introductory curriculum; they are not an exhaustive catalog. Citation needed

Searching and sorting

  1. Binary Search

    Halve a sorted search interval and reason about its boundaries. Citation needed

  2. Insertion Sort

    Grow a sorted prefix by inserting one value at a time. Citation needed

  3. Merge Sort

    Split a sequence, sort its halves, and merge in linear time. Citation needed

  4. Quicksort

    Partition around a pivot and see why balance determines cost. Citation needed

  5. Heapsort

    Use a max-heap to place the largest remaining item at the end. Citation needed

Graphs and shortest paths

  1. Breadth-First Search

    Explore a graph in layers and find paths with the fewest edges. Citation needed

  2. Depth-First Search

    Follow a branch to its end, then backtrack to explore alternatives. Citation needed

  3. Topological Sort

    Order dependencies and detect when a cycle makes an order impossible. Citation needed

  4. Dijkstra’s Algorithm

    Find minimum-weight paths when every edge weight is non-negative. Citation needed

  5. Bellman–Ford

    Relax every edge repeatedly to handle negative weights. Citation needed

Greedy optimization

  1. Kruskal’s Algorithm

    Connect all vertices as cheaply as possible without forming cycles. Citation needed

  2. Interval Scheduling

    Choose the largest compatible set of activities by earliest finish time. Citation needed

Dynamic programming

  1. Fibonacci by Dynamic Programming

    Replace repeated recursive work with stored subproblem answers. Citation needed

  2. Minimum Coin Change

    Find the fewest coins even when a greedy choice fails. Citation needed

  3. 0/1 Knapsack

    Choose each item at most once while respecting a capacity limit. Citation needed

Problems that prove it

  1. Show from the definition that 5n² + 3n + 20 is O(n²).

    Drills: Big-O as a claim you exhibit constants for, not a vibe.

    Derivation steps
    1. Need a constant c and threshold n₀ with 5n² + 3n + 20 ≤ cn² for all n ≥ n₀.
    2. For n ≥ 1: 3n ≤ 3n² and 20 ≤ 20n².
    3. So 5n² + 3n + 20 ≤ 28n² for all n ≥ 1.
    4. Take c = 28, n₀ = 1. Done — the bound holds, so the claim does.

    Citation needed

  2. Unroll T(n) = 2T(n/2) + n, T(1) = 0, and read off merge sort's growth.

    Drills: solving a recurrence by expanding it into levels.

    Derivation steps
    1. Level 0 does n work and leaves two problems of size n/2.
    2. Level 1 does 2 × n/2 = n work; level 2 does 4 × n/4 = n.
    3. Every level does exactly n work.
    4. Halving from n to 1 takes log₂ n levels.
    5. Total: n work × log₂ n levels = O(n log n).

    Citation needed

  3. Count the calls in naive recursive Fibonacci of 6, then redo it storing sub-answers.

    Drills: seeing the repeated subproblems that dynamic programming eliminates.

    Derivation steps
    1. fib(6) calls fib(5) and fib(4); fib(5) calls fib(4) and fib(3) — fib(4) is already being computed twice.
    2. Full count: fib(6) spawns 25 calls; fib(2) alone is computed 5 times.
    3. With a table: compute fib(2) through fib(6) once each, reading stored values — 5 computations.
    4. 25 calls versus 5, and the gap widens exponentially: naive fib(50) makes billions of calls, the table makes 49.

    Citation needed

  4. Run Dijkstra from A on: A→B weight 1, A→C weight 4, B→C weight 2, B→D weight 6, C→D weight 3.

    Drills: the finalise-smallest loop, and why the greedy choice is safe.

    Derivation steps
    1. Start: A = 0; B, C, D unknown. Finalise A; relax its edges: B = 1, C = 4.
    2. Smallest unfinalised is B (1). Finalise; relax: C = min(4, 1+2) = 3, D = 1+6 = 7.
    3. Smallest is C (3). Finalise; relax: D = min(7, 3+3) = 6.
    4. Finalise D (6). Distances: A 0, B 1, C 3, D 6.
    5. Note C's first estimate (4) was undercut before finalising — after finalising, non-negative weights guarantee no undercut can come.

    Citation needed

  5. Merge the sorted lists (2, 7, 9) and (3, 5, 11), counting comparisons.

    Drills: the merge step that gives merge sort its linear per-level work.

    Derivation steps
    1. Compare fronts 2 vs 3: take 2. Compare 7 vs 3: take 3.
    2. Compare 7 vs 5: take 5. Compare 7 vs 11: take 7.
    3. Compare 9 vs 11: take 9. One list empty — append 11 free.
    4. Result: 2, 3, 5, 7, 9, 11 in 5 comparisons — at most n - 1 for n total items, hence linear.

    Citation needed

Sources

  • Thomas Cormen, Charles Leiserson, Ronald Rivest, and Clifford Stein, Introduction to Algorithms — the standard treatment of every algorithm and bound here.
  • Jon Kleinberg and Éva Tardos, Algorithm Design — the design strategies as the organising idea, with the correctness proofs greedy demands.
  • Steven Skiena, The Algorithm Design Manual — which strategy fits which problem, from war stories.

Learning path

Continue through Computer Science in order, using the concepts above to connect this topic to the next.