Princeton Algorithms, Part I & Part II
Every core algorithm, animated, in Python
The two Princeton courses cover roughly fifty algorithms and data structures. Below is all of them, in lecture order. Pick one: you get the implementation, a picture that moves one step at a time, the invariant that keeps it correct, and the running-time claim it is famous for. Nothing here is a recording — the animation is generated by running the algorithm on whatever input you give it.
Part I — data types, sorting, searching
Union–Findweek 1
Quick-findKeep a component identifier for every site. Find is instant; union has to relabel a whole component, and that is what…
Quick-unionReinterpret the same array as parent pointers. Union changes exactly one entry, but the trees can grow tall, and then…
Weighted quick-unionOne comparison added to union guarantees no tree is ever deeper than lg n.
Path compressionEvery find already walks to the root, so pay one more pass and point the whole path at it.
Analysis of algorithmsweek 1
Stacks and queuesweek 2
Elementary sortsweek 2
Selection sortFind the smallest remaining key and swap it into place.
Insertion sortSlide each key left until it lands. Its cost is exactly the number of inversions in the input, so almost-sorted data is…
ShellsortInsertion sort, but comparing keys h apart. Big first steps move keys most of the way home, so the final h = 1 pass has…
Knuth shuffleSwap each item with a random earlier one. The range you draw from is the whole algorithm: get it wrong and the shuffle…
Sorting applicationsweek 2
Mergesortweek 3
Quicksortweek 3
Priority queuesweek 4
Binary search treesweek 4
Balanced search treesweek 5
Hash tablesweek 6
Part II — graphs, strings, compression
Undirected graphsweek 1
Directed graphsweek 2
Directed cycle detectionAn edge to a vertex still on the recursion stack closes a cycle.
Topological sortAdd each vertex to a list only after everything it points to is finished, then reverse the list.
Strong components (Kosaraju–Sharir)Reverse the digraph, take its reverse postorder, then DFS the original in that order.
Minimum spanning treesweek 3
Shortest pathsweek 4
Dijkstra's algorithmRepeatedly settle the unsettled vertex closest to the source and relax its edges.
Shortest paths in a DAGIn an acyclic digraph, relax vertices in topological order and one pass is enough.
Bellman–FordRelax every edge, V−1 times. Slower than Dijkstra, but it handles negative weights and reports a negative cycle instead…
Maximum flowweek 4
Radix sortsweek 5
Substring searchweek 6
Brute-force substring searchTry every alignment, comparing left to right. Simple and usually fine — but it re-reads text it has already seen, which…
Knuth–Morris–PrattPrecompute, for every state and character, where a mismatch leaves you.
Boyer–MooreCompare the pattern right to left. A mismatched text character that does not occur in the pattern at all lets you skip…
Rabin–KarpHash the pattern once, then hash every window of the text — each in constant time from the previous one.
Visualization
0