Short answer: Binary lifting precomputes the 2^k-th ancestor of every node in a rooted tree. The table up[k][v] is the parent of up[k−1][v] taken 2^{k−1} more steps, and it fills in O(n log n). The lowest common ancestor of two nodes is found by lifting the deeper node up…
Author: HoHo Zhao
How binary exponentiation squares a power
Short answer: Binary exponentiation computes a^e with O(log e) multiplications by scanning the bits of the exponent. The square-and-multiply loop squares a running power for every bit and multiplies by the base when the bit is 1. The same loop with every multiplication reduced modulo m computes a^e mod…
How bidirectional BFS meets in the middle of a path
Short answer: Bidirectional breadth-first search grows one frontier from the source and one from the target on an unweighted graph. A shortest path has some meeting vertex, and the two searches together expand about O(b^{d/2}) vertices instead of O(b^d) when every vertex has about b unused edges and the…
How Bentley-Ottmann sweeps segment intersections
Short answer: The Bentley-Ottmann algorithm reports every intersection of a set of line segments by sweeping a vertical line from left to right. A balanced tree holds the segments that cross the sweep line, ordered by y. A priority queue holds left endpoints, right endpoints, and future intersection points…
How Bellman-Ford relaxes every edge V minus 1 times
Short answer: Bellman-Ford relaxes every edge once per round, for |V|−1 rounds. A shortest path is simple when no negative cycle is reachable, so it has at most |V|−1 edges, and round k has considered every path of k edges. One more round that still relaxes an edge is…
How baby-step giant-step solves a discrete log
Short answer: Baby-step giant-step solves g^x ≡ h (mod p) for a small x by writing x = i·m − j with m = ceil(sqrt(N)) and x in a range of length N. It stores the baby steps g^j for j = 0..m−1 in a hash table, then walks…
How arithmetic coding narrows a fraction
Short answer: Arithmetic coding encodes a message as a single fraction of the unit interval. Each symbol narrows the current interval to the subinterval proportional to the symbol’s probability. The encoder emits bits as soon as they are determined by the shrinking interval, and it uses renormalization to keep…
How Andrew’s algorithm builds a monotone chain
Short answer: Andrew’s monotone chain computes a planar convex hull by sorting points by increasing x, then building the lower hull and the upper hull as stacks that pop non-left turns. The sort is O(n log n) and each chain is linear, so the total matches the Graham scan…
How an optimal BST weights search cost
Short answer: An optimal binary search tree puts keys in search-tree order and chooses the shape that minimizes the expected search cost. Key i is requested with probability p[i], and the gap between keys is requested with probability q[i]. The cost of a subtree is the sum of its…
How an AVL tree restores height balance
Short answer: An AVL tree is a binary search tree whose balance factor, left height minus right height, stays in −1, 0, +1 at every node. An insertion or deletion that pushes a factor to ±2 is repaired by a single or a double rotation. The height is at…
How alpha-beta pruning cuts a minimax tree
Short answer: Alpha-beta pruning computes the same value as minimax and skips branches that cannot affect it. Alpha is the best value the maximizer can already force. Beta is the best value the minimizer can already force. A maximizing node stops when its value reaches beta. A minimizing node…
How Aho-Corasick matches many patterns at once
Short answer: Aho-Corasick builds a trie of every pattern and adds a failure link from each node to the longest proper suffix of its path that is still a node in the trie. A text character is consumed exactly once. On a missing edge the walk follows failure links,…
How activity selection takes the earliest finish
Short answer: Activity selection chooses a largest set of mutually compatible intervals. Sort the activities by finishing time and take the next activity that starts at or after the finish of the last chosen one. The greedy choice is optimal: some maximum set uses an activity that finishes first,…
How a van Emde Boas tree searches in log log universe
Short answer: A van Emde Boas tree stores a set of integers from a universe 0, 1, …, u−1 and supports insert, delete, member, successor, and predecessor in O(log log u) time. It splits the universe into √u clusters of √u values, keeps a summary structure on which clusters…
How a trie stores keys on a path from the root
Short answer: A trie stores a set of strings so that each edge is a character and each root-to-node path is a prefix. Lookup of a string of length L examines O(L) edges, independent of how many other keys exist. A node that ends a key is marked terminal.…
How a treap uses random priorities
Short answer: A treap is a binary search tree on keys and a heap on independently random priorities. Each node stores a key and a priority. The in-order walk is the key order, and every parent has a better priority than its children. Insert and delete are rotations that…
How a splay tree moves the accessed node to the root
Short answer: A splay tree is a binary search tree that splays the accessed node to the root by zig-zag and zig-zig rotations. It stores no balance bit and no priority. A single operation can take O(n) time, and a sequence of m operations on a tree of size…
How a sparse table answers a static range minimum
Short answer: A sparse table answers a static range minimum, or any other idempotent range query, in O(1) time after O(n log n) preprocessing. Cell (k, i) stores the minimum of the 2^k elements that start at i. A query of length L uses the two overlapping blocks of…
