Algorithm Cookbook
A practical cookbook of algorithmic patterns, correctness arguments, and implementation techniques.
Chapter 1. Foundations
- Problem statements
- Input and output models
- Correctness arguments
- Loop invariants
- Recursion invariants
- Time complexity
- Space complexity
- Big O notation
- Lower bounds
- Edge cases
- Testing algorithms
- Brute force baselines
- Greedy choices
- Divide and conquer
- Dynamic programming
- Randomization
- Amortized analysis
- Reductions
- Pseudocode style
- Implementation discipline
- Data representation
- Numerical limits
- Stability and determinism
- Benchmarking
- Common failure modes
Chapter 2. Arrays and Strings
- Array traversal
- Prefix sums
- Difference arrays
- Two pointers
- Sliding windows
- Partitioning
- Rotation
- In-place modification
- Deduplication
- String scanning
- Tokenization
- Substring search
- Palindromes
- Anagrams
- Frequency tables
- Run-length encoding
- Rolling hashes
- String comparison
- Unicode concerns
- Sparse arrays
- Matrix traversal
- Spiral order
- Interval arrays
- Common patterns
- Case studies
Chapter 3. Linked Lists and Pointers
- Singly linked lists
- Doubly linked lists
- Sentinel nodes
- Reversal
- Cycle detection
- Fast and slow pointers
- Merge patterns
- Split patterns
- Deletion patterns
- Insertion patterns
- Dummy heads
- Pointer aliasing
- Persistent lists
- Skip lists
- Intrusive lists
- Memory ownership
- Iterators
- Stack via list
- Queue via list
- LRU cache structure
- Edge cases
- Testing pointer code
- Complexity analysis
- Common bugs
- Case studies
Chapter 4. Stacks, Queues, and Deques
- Stack interface
- Queue interface
- Deque interface
- Monotonic stacks
- Monotonic queues
- Parentheses matching
- Expression parsing
- Undo stacks
- Next greater element
- Histogram rectangles
- Sliding window maximum
- BFS queues
- Circular buffers
- Priority queues overview
- Double-ended BFS
- Work queues
- Rate-limited queues
- Buffer management
- Lock-free concepts
- Memory layout
- Amortized bounds
- Invariant checks
- Testing
- Common bugs
- Case studies
Chapter 5. Hashing and Maps
- Hash tables
- Hash functions
- Collision handling
- Chaining
- Open addressing
- Load factor
- Rehashing
- Sets
- Maps
- Counting maps
- Grouping keys
- Composite keys
- Rolling hashes
- Bloom filters
- Count-min sketch
- Consistent hashing
- Hash joins
- Hash-based deduplication
- Cache behavior
- Attack resistance
- Deterministic hashing
- Testing hash logic
- Complexity guarantees
- Common bugs
- Case studies
Chapter 6. Sorting
- Sorting contracts
- Selection sort
- Insertion sort
- Merge sort
- Quick sort
- Heap sort
- Counting sort
- Radix sort
- Bucket sort
- Stable sorting
- Partial sorting
- Top k selection
- Quickselect
- Median selection
- External sorting
- Nearly sorted data
- Custom comparators
- Sorting records
- Coordinate compression
- Inversion counting
- Lower bounds
- Parallel sorting
- Testing sort correctness
- Common bugs
- Case studies
Chapter 7. Binary Search and Ordered Data
- Binary search template
- Lower bound
- Upper bound
- Search on answer
- Monotone predicates
- Floating-point binary search
- Rotated arrays
- Peak finding
- Interval search
- Ordered maps
- Balanced trees
- Binary search trees
- Tree rotations
- Treaps
- Red-black trees
- AVL trees
- B-trees
- Range queries
- Order statistics
- Coordinate search
- Parametric search
- Boundary bugs
- Testing
- Complexity analysis
- Case studies
Chapter 8. Trees
- Tree representation
- DFS traversal
- BFS traversal
- Recursion on trees
- Tree height
- Tree diameter
- Lowest common ancestor
- Binary lifting
- Euler tour
- Subtree queries
- Rerooting
- Tries
- Segment trees
- Fenwick trees
- Lazy propagation
- Persistent trees
- Heavy-light decomposition
- Centroid decomposition
- Suffix trees overview
- Expression trees
- Serialization
- Testing tree algorithms
- Complexity analysis
- Common bugs
- Case studies
Chapter 9. Graph Fundamentals
- Graph models
- Adjacency lists
- Adjacency matrices
- Edge lists
- Directed graphs
- Undirected graphs
- Weighted graphs
- Degree and connectivity
- DFS
- BFS
- Connected components
- Topological sort
- Cycle detection
- Bipartite graphs
- Strongly connected components
- Articulation points
- Bridges
- Euler paths
- Hamiltonian paths overview
- Graph coloring basics
- Graph representation tradeoffs
- Testing graph code
- Complexity analysis
- Common bugs
- Case studies
Chapter 10. Shortest Paths
- Unweighted shortest paths
- BFS shortest paths
- Dijkstra algorithm
- Priority queue variants
- Bellman-Ford
- Negative cycles
- Floyd-Warshall
- Johnson algorithm
- DAG shortest paths
- Multi-source shortest paths
- 0-1 BFS
- A* search
- Bidirectional search
- Path reconstruction
- Distance labels
- Potentials
- Sparse vs dense graphs
- Road networks
- Grid shortest paths
- Dynamic shortest paths
- Correctness proofs
- Complexity analysis
- Testing
- Common bugs
- Case studies
Chapter 11. Minimum Spanning Trees and Connectivity
- Spanning trees
- Cut property
- Cycle property
- Kruskal algorithm
- Prim algorithm
- Boruvka algorithm
- Union-find
- Path compression
- Union by rank
- Offline connectivity
- Dynamic connectivity overview
- Minimum bottleneck spanning tree
- Second-best spanning tree
- Clustering
- Network design
- Sparse graph handling
- Dense graph handling
- Correctness proofs
- Complexity analysis
- Implementation patterns
- Testing
- Common bugs
- Case studies
- Variants
- Exercises
Chapter 12. Network Flow and Matching
- Flow networks
- Residual graphs
- Ford-Fulkerson
- Edmonds-Karp
- Dinic algorithm
- Push-relabel
- Min cut
- Max-flow min-cut theorem
- Bipartite matching
- Hopcroft-Karp
- Assignment problem
- Hungarian algorithm
- Min-cost max-flow
- Circulation with demands
- Lower bounds on edges
- Vertex capacities
- Edge-disjoint paths
- Project selection
- Scheduling applications
- Correctness proofs
- Complexity analysis
- Testing flow code
- Common bugs
- Modeling patterns
- Case studies
Chapter 13. Dynamic Programming
- DP state design
- Recurrence relations
- Memoization
- Tabulation
- One-dimensional DP
- Two-dimensional DP
- Knapsack
- Longest common subsequence
- Longest increasing subsequence
- Edit distance
- Interval DP
- Tree DP
- Digit DP
- Bitmask DP
- Probability DP
- Counting DP
- Optimization DP
- Convex hull trick
- Divide-and-conquer optimization
- Knuth optimization
- DP on graphs
- Space optimization
- Correctness proofs
- Testing
- Case studies
Chapter 14. Greedy Algorithms
- Greedy choice property
- Exchange arguments
- Matroids overview
- Interval scheduling
- Activity selection
- Huffman coding
- Fractional knapsack
- Job sequencing
- Minimum refueling stops
- Prefix constraints
- Sorting plus greedy
- Priority queue greedy
- Two-pointer greedy
- Graph greedy
- String greedy
- Local vs global optimum
- Counterexamples
- Correctness proofs
- Complexity analysis
- Testing
- Common bugs
- Design patterns
- Case studies
- Variants
- Exercises
Chapter 15. Divide and Conquer
- Recursion trees
- Master theorem
- Merge sort revisited
- Quick sort revisited
- Closest pair of points
- Karatsuba multiplication
- Strassen multiplication
- Fast exponentiation
- Binary splitting
- Divide-and-conquer DP
- Parallel divide and conquer
- Cache-oblivious algorithms
- Selection algorithms
- Median of medians
- Counting inversions
- Range decomposition
- Offline queries
- Recurrence solving
- Correctness proofs
- Complexity analysis
- Testing
- Common bugs
- Implementation patterns
- Case studies
- Exercises
Chapter 16. String Algorithms
- Exact matching
- Knuth-Morris-Pratt
- Z algorithm
- Rabin-Karp
- Boyer-Moore overview
- Trie matching
- Aho-Corasick
- Suffix arrays
- LCP arrays
- Suffix automata
- Palindromic trees
- Manacher algorithm
- Edit distance
- Longest repeated substring
- Longest common substring
- Lexicographic order
- String hashing
- Compressed strings
- Unicode and normalization
- Token streams
- Text indexing
- Complexity analysis
- Testing
- Common bugs
- Case studies
Chapter 17. Computational Geometry
- Points and vectors
- Orientation tests
- Line intersection
- Segment intersection
- Polygon area
- Point in polygon
- Convex hull
- Rotating calipers
- Closest pair
- Sweep line
- Interval geometry
- Rectangle union area
- Half-plane intersection
- Voronoi diagrams overview
- Delaunay triangulation overview
- Precision errors
- Integer geometry
- Floating-point robustness
- Geometric hashing
- Spatial indexes
- R-trees overview
- Correctness proofs
- Testing
- Common bugs
- Case studies
Chapter 18. Number Theory
- Divisibility
- Euclidean algorithm
- Extended gcd
- Modular arithmetic
- Modular inverse
- Fast exponentiation
- Primality testing
- Sieve of Eratosthenes
- Factorization
- Chinese remainder theorem
- Euler phi function
- Mobius function
- Modular combinatorics
- Linear congruences
- Diophantine equations
- Primitive roots
- Discrete logarithms overview
- Miller-Rabin
- Pollard rho
- Big integer concerns
- Cryptographic caveats
- Correctness proofs
- Complexity analysis
- Testing
- Case studies
Chapter 19. Randomized and Approximate Algorithms
- Random variables in algorithms
- Las Vegas algorithms
- Monte Carlo algorithms
- Randomized quicksort
- Random sampling
- Reservoir sampling
- Shuffling
- Skip lists revisited
- Hashing with randomness
- MinHash
- HyperLogLog
- Bloom filters revisited
- Count-min sketch revisited
- Locality-sensitive hashing
- Approximation ratios
- Greedy approximation
- Set cover approximation
- Vertex cover approximation
- Streaming algorithms
- Online algorithms
- Competitive analysis
- Probability bounds
- Testing randomized code
- Common bugs
- Case studies
Chapter 20. End-to-End Case Studies
- Autocomplete engine
- Search ranking pipeline
- Shortest path service
- Event scheduler
- Text diff tool
- Compression tool
- Log deduplication
- Graph analytics job
- Recommendation prototype
- Spell checker
- Rate limiter
- Cache eviction policy
- Web crawler frontier
- Static analyzer
- Mini database index
- Packet routing simulation
- Task dependency planner
- Plagiarism detector
- Geometry query engine
- Constraint solver
- Streaming analytics
- External merge sort
- Matchmaking system
- Verified algorithm sketch
- Performance tuning report