Algorithms
How to make a program fast enough, and how to know that it is. Asymptotics, divide and conquer, greedy, dynamic programming, graphs, flow, strings, and what to do when a problem turns out to be intractable. Taught in Python or C++, your choice.
Contents
Algorithms
Foundations: correctness and asymptotic analysis
Two questions follow every algorithm you will ever write: is it right, and what does it cost? This section answers both from first principles. You will state a problem precisely enough to argue about, prove a loop right with an invariant and a recursion right by induction, count operations honestly, and then learn the notation that throws away everything about that count except how it grows. It finishes with the working habits: reading a constraint backwards to a target running time, measuring growth on a real machine, and naming the brute force before trying to beat it.
- 9 min
What an algorithm is, exactly
A problem is a contract between input and output. An algorithm is one way of honouring it, and correctness means honouring it on every input, not on the ones you tried.
- 10 min
Counting steps: pseudocode and the machine we pretend to have
Before you can say one algorithm costs less than another, you have to agree what one step costs. That agreement is called a cost model.
- 11 min
Proving a loop right: the invariant
A sentence that is true before the loop, stays true through every pass, and says something useful when the loop stops.
- 10 min
Proving a recursion right: induction
Assume the smaller calls are already right, show the step is right, and check that the input really is getting smaller.
- 11 min
T(n): best case, worst case, average case
The same procedure on two inputs of the same size can do wildly different amounts of work, so cost is not one number but three.
- 13 min
O, Omega and Theta, defined properly
Three statements about how one function compares with another once n is large enough, each with two constants you have to be able to produce.
- 12 min
Working with the notation
The equals sign is not equals, a Theta inside a formula means something specific, and two functions need not be comparable at all.
- 11 min
Proving a cost cannot be beaten: lower bounds
An upper bound is a promise about every input. A worst-case lower bound needs only one bad input for each size, and finding that family is a skill of its own.
- 10 min
The ladder of growth rates
Constant, logarithmic, linear, n log n, polynomial, exponential and factorial, plus the two odd ones that turn up in real analysis.
- 9 min
Reading the constraints backwards
The largest input a problem allows is a message about which technique is expected. Learn to read it before you write anything.
- 10 min
Space, in-place work, and the cost of recursion
Memory is the second budget, and a recursion spends it invisibly, one stack frame per call.
- 10 min
Measuring growth on a real machine
Double the input, watch what the time does, and read the exponent straight off the ratio.
- 11 min
Worst case, amortised, expected: three different promises
All three get quoted as though they were the same guarantee. They are not, and the difference decides whether a system misses a deadline.
- 10 min
Name the brute force before you optimise anything
Write the obvious solution, cost it against the constraint, and if it does not fit, name the one line that is doing all the work.
- Easy
How hard did the scanner work?
Coding problem · Counting operations, Best and worst case
- Medium
Moves to tidy a bookshelf
Coding problem · Insertion sort, Worst case
- Medium
What fits in the budget?
Coding problem · Growth classes, Reading constraints
- Easy
Peak memory from an allocation log
Coding problem · Space complexity, Auxiliary space
- Medium
Rebinding the photo album
Coding problem · Amortised cost, Growable arrays
- Medium
Reading an exponent off a doubling experiment
Coding problem · Empirical analysis, Order of growth
- Medium
Constants, sums and growths, worked on paper
Coding problem · Asymptotic notation, Summations, Growth rates
Section quiz
Divide and conquer, and recurrences
Opens once you finish Foundations: correctness and asymptotic analysis, including its quiz.
Sorting, comparators, and order statistics
Opens once you finish Divide and conquer, and recurrences, including its quiz.
Binary Search and Search on the Answer
Opens once you finish Sorting, comparators, and order statistics, including its quiz.
Randomized algorithms and probabilistic analysis
Opens once you finish Binary Search and Search on the Answer, including its quiz.
Amortised Analysis and Linear-Scan Techniques
Opens once you finish Randomized algorithms and probabilistic analysis, including its quiz.
Prefix Sums, Difference Arrays, and Range Queries
Opens once you finish Amortised Analysis and Linear-Scan Techniques, including its quiz.
Greedy Algorithms, Intervals, Sweep Line, and Online Choice
Opens once you finish Prefix Sums, Difference Arrays, and Range Queries, including its quiz.
Recursion, Backtracking, and Combinatorial Search
Opens once you finish Greedy Algorithms, Intervals, Sweep Line, and Online Choice, including its quiz.
Dynamic programming
Opens once you finish Recursion, Backtracking, and Combinatorial Search, including its quiz.
Graph Search, Grids, and Topological Order
Opens once you finish Dynamic programming, including its quiz.
Union-Find, Minimum Spanning Trees, and Shortest Paths
Opens once you finish Graph Search, Grids, and Topological Order, including its quiz.
Network Flow, Matching, and Linear Programming
Opens once you finish Union-Find, Minimum Spanning Trees, and Shortest Paths, including its quiz.
String Algorithms and Tries
Opens once you finish Network Flow, Matching, and Linear Programming, including its quiz.
NP-completeness, reductions, and coping with intractability
Opens once you finish String Algorithms and Tries, including its quiz.
Bitwise, Numeric, and Specialized Algorithms
Opens once you finish NP-completeness, reductions, and coping with intractability, including its quiz.
Algorithms unit exam
Opens once every section is finished and every quiz is passed.