Algorithms
From sorting to dynamic programming β step-by-step guides with interactive examples.
Complexity & Analysis
Why Learn Algorithms?
The recipes behind every search result, GPS route, and recommendationβ
Big-O, Theta, Omega
How fast does your recipe slow down as the guest list grows?β
Amortized Analysis
One big bill, paid in tiny installmentsβ
Recurrence Relations
A recipe that calls itself β until it's simple enough to solveβ
Sorting
Basic Sorts
Sorting a hand of cards β three ways, one speedβ
Merge Sort
Split, sort each half, merge β guaranteed \(O(n \log n)\) every timeβ
Quick Sort
Pick a pivot, split the crowd β everyone shorter left, taller rightβ
Heap Sort
A tournament bracket where the champion always rises to the topβ
Linear-Time Sorting
Sort by mailbox number, not by comparing β break the \(O(n \log n)\) wallβ
Comparison Lower Bound
Why no comparison sort can ever beat \(O(n \log n)\) β a mathematical proofβ
Searching & Selection
Binary Search
Cut the problem in half β every single timeβ
Binary Search on Answer
When you can't search the array β search the answer itselfβ
Exponential & Interpolation Search
Find the range first, then binary search itβ
QuickSelect
Find the kth smallest without fully sortingβ
Median of Medians
A guaranteed-good pivot β every single timeβ
Divide & Conquer
D&C Pattern
Split the problem, solve the pieces, combine the resultsβ
Count Inversions
Measure how shuffled a sequence is β in \(O(n \log n)\)β
Fast Power
Compute \(x^n\) in \(O(\log n)\) β not \(O(n)\) multiplicationsβ
Karatsuba Multiplication
3 multiplications instead of 4 β per recursive levelβ
Closest Pair of Points
Find the two nearest cities on a map β without measuring every pairβ
Greedy Algorithms
Greedy Pattern
Always grab the biggest slice of pizza β sometimes that's optimal, sometimes it isn'tβ
Interval Scheduling
Book as many non-overlapping meetings as possible β always pick the one that ends earliestβ
Fractional Knapsack
Filling a backpack with gold dust β take the most valuable per gram firstβ
Huffman Encoding
Give shorter Morse code to the letters you use most β save space on every messageβ
Task Scheduling
Do homework by closest deadline first β miss nothing, earn the mostβ
MST β Greedy Lens
Connect all towns with the least road β always add the cheapest road that doesn't create a loopβ
Dynamic Programming
DP Pattern
Never solve the same puzzle piece twice β write it down and look it up next timeβ
Memoization vs Tabulation
Top-down is calling a friend who calls a friend. Bottom-up is filling a spreadsheet row by row.β
1D DP
Build the answer one step at a time β just like climbing stairsβ
0/1 Knapsack
Pack a suitcase for maximum value β each item is all-or-nothingβ
Unbounded Knapsack
Take as many of each item as you want β the vending machine versionβ
Longest Common Subsequence
Find the longest story two sequences share β skipping is allowedβ
Longest Increasing Subsequence
Find the longest staircase hidden inside a jumbled sequenceβ
Edit Distance
How many typo-fixes does it take to turn one word into another?β
Matrix Chain Multiplication
The order you multiply matrices matters enormously β find the cheapest orderβ
DP on Intervals
Solve tiny sections first, then merge them into bigger onesβ
DP on Trees
Each node's answer depends on its children β compute bottom-upβ
Bitmask DP
Track every possible subset of choices in a single integerβ
Digit DP
Count numbers with digit constraints β without checking each oneβ
Graph Algorithms
BFS & DFS Revisited
Two ways to explore a graph β level by level, or all the way inβ
Topological Sort
Order tasks by their dependencies β no chicken-before-eggβ
Dijkstra's Algorithm
Always extend the cheapest road first β GPS navigation for graphsβ
Bellman-Ford & SPFA
Handle negative weights and catch negative cyclesβ
Floyd-Warshall
Find shortest paths between every pair of cities in one sweepβ
A* Search
Smarter than Dijkstra β use a hint to head in the right directionβ
Tarjan's Algorithm
Find all tightly-knit groups in a directed graph in one passβ
Kosaraju's Algorithm
Find mutual-follow groups with two clever graph walksβ
Eulerian Path & Circuit
Can you cross every bridge exactly once? Euler answered this in 1736β
Hamiltonian Path
Visit every house exactly once β far harder than every bridgeβ
Network Flow
Max Flow / Min Cut
How much water fits through your pipe network? The bottleneck decidesβ
Edmonds-Karp
Ford-Fulkerson with BFS β guaranteed polynomial time, no infinite loopsβ
Dinic's Algorithm
Batch all shortest augmenting paths in one go β dramatically faster than one at a timeβ
Bipartite Matching
Match students to internships β find the maximum number of happy pairsβ
Hungarian Algorithm
Assign workers to tasks at the lowest possible total cost β the perfect matchβ
String Algorithms
KMP Algorithm
When searching for a word, never go backwards β use what you already knowβ
Rabin-Karp
Find a word in a book by its fingerprint β check character-by-character only when fingerprints matchβ
Z-Algorithm
Z-array: match length at every position β linear pattern matchingβ
Boyer-Moore
Read the pattern backwards, skip huge chunks β sublinear in practiceβ
Suffix Array Construction
All suffixes sorted β \(O(m \log n)\) substring searchβ
Aho-Corasick
Find all patterns at once β one pass, \(O(n + matches)\)β
Randomized Algorithms
Las Vegas vs Monte Carlo
Correct-always vs probably-correct β two flavors of randomized algorithmsβ
Randomized Quick Sort
A random pivot destroys adversarial worst-case inputsβ
Reservoir Sampling
Sample k items fairly from a stream you see only onceβ
Skip List
Probabilistic BST alternative β \(O(\log n)\) expectedβ
Bloom Filter
Probably yes, definitely no β a memory-sipping membership checkerβ
Advanced Topics
Backtracking
Explore every path, retreat the moment you hit a dead endβ
Branch & Bound
Backtracking with a crystal ball β prune branches that can't possibly winβ
Approximation Algorithms
90% of the way there in 1% of the time β with a proof to back it upβ
NP, NP-Complete, NP-Hard
The million-dollar question: can finding an answer ever be as easy as checking one?β
Bit Manipulation
Flip switches at the speed of electricity β and do it in \(O(1)\)β
Mo's Algorithm
Answer a thousand range queries by sorting them cleverly β not answering them in orderβ
Sqrt Decomposition
Split the array into βn chunks β too big to check every element, too small to need a fancy treeβ