Short answer: The Z-algorithm computes, for every index i of a string s, the length Z[i] of the longest substring starting at i that matches a prefix of s. It does this in O(n) time by reusing a window [left, right] that is the rightmost prefix match found so…
How the Viterbi algorithm decodes a hidden path
Short answer: The Viterbi algorithm finds the most probable sequence of hidden states for a sequence of observations in a hidden Markov model. The cell v[t][s] is the probability of the best path that emits the first t observations and ends in state s. It multiplies the best predecessor…
How the simplex algorithm pivots a linear program
Short answer: The simplex algorithm solves a linear program by walking from one vertex of the feasible polyhedron to a better neighbor. A basic feasible solution is a vertex. A pivot swaps one basic variable for one nonbasic variable and updates the tableau. If no neighbor improves the objective,…
How the sieve of Eratosthenes strikes composites
Short answer: The sieve of Eratosthenes lists every prime up to n by striking multiples. For each prime p whose square is still at most n, it marks p·p, p·p+p, p·p+2p, and so on. Multiples below p² were already marked by smaller primes. The algorithm runs in O(n log…
How the Lucas-Lehmer test checks a Mersenne number
Short answer: The Lucas-Lehmer test decides whether a Mersenne number M = 2^p − 1 is prime, for an odd prime p. It sets s = 4 and repeats s = s² − 2, modulo M, p−2 times. M is prime exactly when the final s is 0 modulo…
How the longest common subsequence fills a grid
Short answer: The longest common subsequence of two strings is the longest sequence that appears in both, in order, not necessarily contiguously. The cell L[i][j] equals L[i−1][j−1] + 1 when the i-th and j-th characters match, and otherwise the maximum of L[i−1][j] and L[i][j−1]. The table is O(nm) time.…
How the linear sieve marks each composite once
Short answer: The linear sieve computes the smallest prime factor of every integer up to n in O(n) arithmetic steps. It walks i from 2 to n, records i as prime when it has no factor yet, and for each known prime p marks p·i, stopping when p divides…
How the Hungarian algorithm solves an assignment
Short answer: The Hungarian algorithm, or Kuhn-Munkres, solves the assignment problem: pair n agents with n jobs so that the sum of costs is minimum. It maintains dual variables on agents and jobs so that a reduced cost c(i, j) − u(i) − v(j) is never negative, and it…
How the Graham scan builds a convex hull
Short answer: The Graham scan computes the convex hull of a finite set of points in the plane. It anchors the lowest point, sorts the rest by polar angle around that anchor, and walks the sorted list with a stack. A right turn, or a non-left turn, pops the…
How the extended Euclidean algorithm writes Bezout
Short answer: The extended Euclidean algorithm returns integers x and y such that a·x + b·y = gcd(a, b). It runs the same divisions as the Euclidean algorithm and also updates two coefficient rows, one for each remainder. The modular inverse of a modulo m exists exactly when the…
How the Euclidean algorithm divides out a gcd
Short answer: The Euclidean algorithm computes gcd(a, b) by replacing the pair with (b, a mod b) until the remainder is 0. The other number is the gcd. It runs in O(log min(a, b)) division steps, and the worst case is a pair of consecutive Fibonacci numbers, which makes…
How the egg dropping recurrence limits the worst case
Short answer: The egg dropping problem asks for the minimum number of trials that suffice, in the worst case, to find a critical floor with e eggs and f floors. Dropping at floor x leaves either e−1 eggs and x−1 floors below, if the egg breaks, or e eggs…
How the CYK algorithm parses a Chomsky grammar
Short answer: The CYK algorithm decides whether a string belongs to a context-free grammar in Chomsky normal form, and it builds a parse table while doing so. Cell (length, start, variable) is true when that variable derives the substring. A production A → B C splits the substring into…
How the closest pair of points searches a strip
Short answer: The closest pair algorithm finds the minimum Euclidean distance in a planar set by divide and conquer. It sorts by x, splits the set in half, solves both halves, and then checks only the points that lie in a strip of width δ around the cut, where…
How the clock algorithm approximates LRU
Short answer: The clock algorithm, or second chance, approximates LRU with a circular list of frames and one reference bit each. The clock hand walks the circle. A frame whose bit is 1 gets the bit cleared and a second chance. A frame whose bit is 0 is evicted.…
How the Chinese remainder theorem builds a residue
Short answer: The Chinese remainder theorem says that if the moduli m1, …, mk are pairwise coprime, then for any residues a1, …, ak there is one solution modulo M = m1·…·mk of the system x ≡ ai (mod mi). The constructive form computes, for each i, the product…
How the Burrows-Wheeler transform sorts rotations
Short answer: The Burrows-Wheeler transform sorts every rotation of a string and emits the last character of each rotation, plus the index of the original string in that sorted list. The output is a permutation that tends to group identical characters, which is why it sits in front of…
How the Boyer-Moore vote finds a majority candidate
Short answer: The Boyer-Moore majority vote finds a candidate that is the only value that could be a strict majority of an array, using one counter and one candidate. It scans once. On a match it increments. On a mismatch it decrements. At zero it takes the current value…
