Short answer: Lloyd’s algorithm is the standard k-means iteration. Assign every point to the nearest of k centers, then replace each center by the mean of the points assigned to it. Repeat until the assignment stops changing or a step budget runs out. Each step decreases the sum of…
Category: Algorithms
Linear search — find a key by scanning each item once
Linear search finds a key by reading items in order until one matches or the sequence ends. It fits an unordered sequence, a linked list, a short array, and a single query. It does not fit a long sorted array, or a collection that will be queried many times.…
How Levenshtein distance fills an edit grid
Short answer: Levenshtein distance is the minimum number of single-character insertions, deletions, and substitutions that turn one string into the other. The DP cell d[i][j] is that distance between the first i characters of the source and the first j characters of the target. It is the minimum of…
How Lamport’s bakery algorithm takes a ticket
Short answer: Lamport’s bakery algorithm is an n-thread mutual-exclusion lock built from atomic reads and writes. A thread takes a ticket numbered one more than any ticket it can see, and it waits until every thread that holds a smaller ticket, or an equal ticket and a smaller id,…
How Kuhn matches by augmenting DFS
Short answer: Kuhn’s algorithm builds a bipartite matching by a depth-first search for one augmenting path at a time. Each free left vertex tries to find an alternating path to a free right vertex, flipping the matching if it succeeds. With a visited mark cleared every search, the time…
How Kruskal adds the cheapest safe edge
Short answer: Kruskal’s algorithm sorts the undirected edges by weight and adds an edge when its ends lie in different trees of a forest. A union-find structure answers that test. The sort dominates, at O(E log E), and the union-find work is essentially linear. The first |V|−1 accepted edges…
How Kosaraju uses a transpose graph
Short answer: Kosaraju’s algorithm finds strongly connected components with two depth-first searches. The first search records vertices in reverse order of finish time on the original graph. The second search runs on the transpose, in which every edge is reversed, and starts vertices in that recorded order. Each tree…
How Knuth-Morris-Pratt skips with the prefix table
Short answer: The Knuth-Morris-Pratt algorithm scans a text for a pattern in O(n + m) time. It builds a prefix table π, where π[i] is the longest proper prefix of the pattern that is also a suffix of the pattern ending at i. When a mismatch occurs after i…
How Kasai builds the LCP array from a suffix array
Short answer: Kasai’s algorithm builds the LCP array of a string from its suffix array in O(n) time. LCP[i] is the length of the longest common prefix of the suffixes that are adjacent in sorted order, SA[i−1] and SA[i]. The algorithm walks the suffixes in text order and uses…
How Karger contracts a random min-cut
Short answer: Karger’s algorithm finds a global minimum cut of an undirected multigraph by randomly contracting edges until two supernodes remain. The edges between those two supernodes are a cut, and each run returns the minimum cut with probability at least 1/binom(n, 2), about 2/n². Repeating O(n² log n)…
How Karatsuba multiplies with three half-size products
Short answer: Karatsuba multiplication multiplies two n-digit numbers with three multiplications of n/2 digits instead of four. Split x = x1·B + x0 and y = y1·B + y0. The classical cross terms x1·y0 and x0·y1 are recovered from the single product (x1+x0)(y1+y0) by subtracting the two squares x1·y1…
How Kahan summation keeps a running correction
Short answer: Kahan summation adds a sequence of floating-point numbers while keeping a correction for the low bits the last addition lost. Each step subtracts the running correction from the next input, adds that adjusted value to the sum, and stores the bits that did not land in the…
How Kadane resets a running sum
Short answer: Kadane’s algorithm finds the contiguous subarray of maximum sum in O(n) time and O(1) extra memory. It walks the array once, keeping the best sum that ends at the current index and the best sum seen anywhere. The sum that ends here is the current element plus…
How jump search blocks a sorted array
Short answer: Jump search walks a sorted array in strides of length m and, once a stride overshoots the target, scans the previous block linearly. The cost is about n/m + m comparisons. That sum is minimized at m = √n, which makes the search Θ(√n). On random-access memory,…
How Johnson reweights edges and runs Dijkstra
Short answer: Johnson’s algorithm adds a super-source, runs Bellman-Ford to get potentials, and reweights every edge to w'(u,v) = w(u,v) + h(u) − h(v). The new weights are nonnegative, so Dijkstra from each vertex is legal. A path’s reweighted length differs from its true length by h(start) − h(end),…
How Jarvis march wraps a convex hull
Short answer: Jarvis march, or gift wrapping, builds a planar convex hull by starting at the leftmost point and repeatedly choosing the next hull vertex as the point with the smallest polar angle from the current vertex. Each step scans every point, so the time is O(n·h), where h…
How iterative deepening DFS saves the frontier
Short answer: Iterative deepening depth-first search runs a depth-limited depth-first search with limit 0, then 1, then 2, and so on, until the target is found. On an unweighted graph the first success is a shortest path. The space is O(d) for the current path, not O(b^d) for a…
How introsort caps quicksort with a heap
Short answer: Introsort runs quicksort until the recursion depth passes about 2 log₂ n, then finishes that slice with heapsort. Short slices drop into insertion sort. The worst case becomes O(n log n), the typical case keeps quicksort’s partition, and the sort stays in place and unstable. This is…
