Short answer: Patience sorting deals each item onto the leftmost pile whose top is greater than or equal to the item. The number of piles equals the length of a longest increasing subsequence. With binary search over the pile tops, building the piles is O(n log n). A priority…
Author: HoHo Zhao
How PageRank iterates a damped link matrix
Short answer: PageRank assigns every page a score that is the stationary distribution of a random surfer. With probability d the surfer follows a link chosen uniformly among the page’s out-links. With probability 1−d the surfer jumps to a uniformly random page. The usual damping factor is 0.85. The…
How Newton’s method updates a root by the tangent
Short answer: Newton’s method solves f(x) = 0 by replacing the current guess x with x − f(x)/f'(x), the root of the tangent line. When f is smooth, f’ is nonzero at a simple root, and the guess is close enough, the error squares at every iteration. That quadratic…
How Needleman-Wunsch aligns two sequences globally
Short answer: Needleman-Wunsch computes a global alignment of two sequences by the same prefix grid as edit distance, with a substitution score and a gap penalty in place of unit costs. The corner cell is the optimum over alignments of the entire strings, including gaps at the ends. Affine…
How Mo’s algorithm orders offline range queries
Short answer: Mo’s algorithm answers a batch of range queries offline when a window can grow or shrink by one cell at a known cost. Sort the queries by the block of the left endpoint and, inside a block, by the right endpoint. Move the current window’s ends to…
How Monte Carlo tree search balances a game tree
Short answer: Monte Carlo tree search grows a game tree by repeating selection, expansion, rollout, and backup. Selection walks from the root by a bandit rule, usually UCT: choose the child with the best win rate plus c · sqrt(ln N / n), where N is the parent’s visit…
How Misra-Gries tracks heavy hitters
Short answer: The Misra-Gries summary keeps k − 1 counters and finds every value whose frequency in a stream of length n is greater than n/k. A value that is already counted increments its counter. A value that finds a free counter takes it. When every counter is busy,…
How minimax values a perfect-information game
Short answer: Minimax values a two-player zero-sum game of perfect information by recursion on the game tree. A terminal position returns its payoff to the player about to move, or a fixed score for the player who just lost. A maximizing node takes the maximum of its children. A…
How MinHash estimates a Jaccard similarity
Short answer: MinHash estimates the Jaccard similarity of two sets. With a random hash, the probability that the element of minimum hash is the same in both sets equals the Jaccard index, the size of the intersection divided by the size of the union. k independent hashes give k…
How Miller-Rabin tests a strong pseudoprime
Short answer: The Miller-Rabin test writes an odd number n as d·2^s + 1 and looks at a^d mod n. The test passes for a base a when that power is 1, or when some a^{d·2^r} mod n equals n−1 for an r before s. A prime passes for…
How Metropolis-Hastings samples a target density
Short answer: Metropolis-Hastings draws samples from a target density π that is known up to a constant. From the current state it proposes a neighbor from a density q, and it accepts the proposal with probability min(1, π(y)q(x|y) / π(x)q(y|x)). The π constants cancel, so an unnormalized density is…
How meet-in-the-middle splits a subset sum
Short answer: Meet-in-the-middle solves subset sum by splitting the items into two halves. Each half has about n/2 elements, so each half has 2^{n/2} subset sums. Sort one list. For every sum on the other list, binary search the complement that comes closest to the target without passing it.…
How median of medians selects in linear time
Short answer: Median of medians chooses a quickselect pivot by splitting the array into groups of five, taking each group’s median, and recursively selecting the median of those medians. That pivot is guaranteed to drop a constant fraction of the array. The recurrence T(n) ≤ T(n/5) + T(7n/10) +…
How matrix-chain order minimizes scalar multiplications
Short answer: Matrix-chain multiplication chooses where to parenthesize a product of matrices so the number of scalar multiplications is minimized. Matrix i has dimensions p[i−1] by p[i]. The cost of multiplying a chain from i through j is the minimum, over the last multiplication’s split k, of the two…
How Manacher mirrors palindrome radii
Short answer: Manacher’s algorithm finds every palindromic substring’s radius, and in particular the longest one, in O(n) time. It transforms the string so odd and even lengths share one code path, then fills a radius array. A window around the rightmost palindrome found so far mirrors radii across its…
How LZW grows a dictionary of phrases
Short answer: LZW compresses by emitting codes for phrases in a dictionary that starts as the alphabet and grows by one new phrase per step. The encoder finds the longest dictionary phrase that matches the upcoming bytes, emits its code, and adds the phrase plus the next input byte…
How LZ77 points back into a sliding window
Short answer: LZ77 compresses a stream by replacing a repeated string with a pointer into a recent window: an offset, a length, and sometimes the next unmatched character. The decoder copies those bytes from the bytes it has already emitted. A match must lie inside the window and behind…
How LRU replaces the least recently used page
Short answer: LRU page replacement evicts the page whose last use is oldest. It is the optimal policy among policies that do not see the future, in the sense of the stack property: a larger memory never causes more misses than a smaller one on the same trace. Implementing…
