Algorithms & DP

Algorithms & DP

Algorithms & Dynamic Programming — animated visualizations, DP tables, recursion trees, complexity walkthroughs.

745Articles
745Topics covered
Articles in this category

All 97 articles, sorted alphabetically

Advertisement
ARTICLE · 01

A* Pathfinding, in depth: admissible and consistent heuristics, grid heuristics, tie-breaking, weighted A* and when to move beyond it

How A* finds shortest paths: f = g + h from first principles, a correct Python implementation with lazy deletion, what admissibility and consistency e…

Read article →
ARTICLE · 02

ARC architecture

Deep-dive on ARC, the Adaptive Replacement Cache that beats LRU and LFU by adapting the recency-versus-frequency balance automatically per workload, w…

Read article →
ARTICLE · 03

Backtracking, in depth: choose, explore, unchoose, and the pruning that makes exponential search usable

A first-principles guide to backtracking: the state-space tree, the choose/explore/unchoose template, a bitmask N-Queens solver with measured node cou…

Read article →
ARTICLE · 04

BFS and DFS, in depth: frontiers, visited sets, shortest paths, edge classification, cycle detection and the bugs that make traversals wrong or slow

Breadth-first and depth-first search from first principles: the frontier container, when to mark visited, BFS shortest paths and parent maps, DFS disc…

Read article →
ARTICLE · 05

Big-O Notation

Asymptotic complexity done properly: the formal definitions of O, Omega and Theta, why worst case is a different axis from big-O, amortized and expect…

Read article →
ARTICLE · 06

Bloom filter architecture

Deep-dive on Bloom filter architecture: hash family, sizing math, counting and cuckoo variants, and the ops layer for production use.

Read article →
ARTICLE · 07

Count-Min Sketch Architecture in Depth

A 2500-word walkthrough of Count-Min Sketch: d × w counter matrix, hash functions, insert + query, overestimate math, parameters, applications, varian…

Read article →
ARTICLE · 08

Consistent hashing architecture

Deep-dive on consistent hashing: ring, virtual nodes, replica walk, bounded loads, Anchor/Jump/Rendezvous variants, and cluster management.

Read article →
ARTICLE · 09

Count-Min Sketch, in depth: the error proof, and the queries beyond point lookups: ranges, quantiles, heavy-hitter recovery and join sizes

Count-Min Sketch from first principles, past the point query: a four-step proof of the error bound, the strict-turnstile caveat and skew, dyadic range…

Read article →
ARTICLE · 10

Count-Min Sketch Architecture, in depth: an implementation guide to memory layout, hashing once, concurrency, serialization and testing the error bound

How to build a production Count-Min sketch: a contiguous counter layout with power-of-two width, deriving all row indices from one stable hash, a refe…

Read article →
ARTICLE · 11

Cuckoo filters

Deep-dive on cuckoo filters: partial-key cuckoo hashing and the i2 = i1 XOR hash(fp) involution, fingerprint sizing and the 2b/2^f false-positive math…

Read article →
ARTICLE · 12

Dijkstra's Algorithm, in depth: the settled-frontier invariant, a correct implementation, negative edges and production pitfalls

A complete guide to Dijkstra's shortest-path …

Read article →
ARTICLE · 13

DiskANN architecture

Deep-dive on DiskANN: the Vamana low-diameter graph, product-quantized guidance in RAM with full vectors on disk, beam search and exact re-rank, alpha…

Read article →
ARTICLE · 14

Distributed Consensus Architecture: Raft, Paxos, and Beyond

A 2500-word walkthrough of a modern consensus system: client, coordinator, leader, log, followers, state machines, and snapshots.

Read article →
ARTICLE · 15

Divide and Conquer, in depth: recursion trees, the master theorem, worked algorithms and making recursive code fast

Divide and conquer from first principles: divide, conquer, combine, reading a recursion tree, the master theorem with worked recurrences, counting inv…

Read article →
ARTICLE · 16

Dynamic programming, in depth: states, recurrences, evaluation order and the bugs that break them

A first-principles guide to dynamic programming: optimal substructure and overlapping subproblems, the subproblem DAG, defining state and recurrence, …

Read article →
ARTICLE · 17

Fenwick tree architecture

Deep-dive on the Fenwick tree (binary indexed tree): the range each cell owns via lowbit(i) = i &a…

Read article →
ARTICLE · 18

Fast Fourier Transform, in depth: roots of unity, the butterfly, iterative in-place code, fast convolution, precision limits and where FFTs run in ML

The Fast Fourier Transform from first principles: the DFT as polynomial evaluation at roots of unity, the even/odd split and butterfly, recursive and …

Read article →
ARTICLE · 19

Graph Coloring, in depth: bounds, greedy orderings, DSATUR, exact backtracking, and how compilers and schedulers use it

A practical guide to graph colouring: proper colourings and the chromatic number, the bounds that tell you when you are done, why greedy colouring dep…

Read article →
ARTICLE · 20

Greedy Algorithms, in depth: when the locally best choice is globally right, and how to prove it

Greedy algorithms from first principles: the greedy-choice property and optimal substructure, exchange and stays-ahead proofs, activity selection and …

Read article →
ARTICLE · 21

Hash Tables

What the O(1) in a hash table really promises: index reduction, probe-count arithmetic, chaining vs open addressing vs SwissTable, deletion, and resiz…

Read article →
ARTICLE · 22

Heap Operations

How binary heaps implement priority queues in O(log n), the heapify trick, and applications from Dijkstra to scheduling.

Read article →
ARTICLE · 23

HyperLogLog Algorithm Architecture in Depth

A 2500-word walkthrough of HyperLogLog: hash function, bucketing, rank via leading zeros, harmonic mean estimator, bias correction, merge, space-error…

Read article →
ARTICLE · 24

HNSW architecture

Deep-dive on HNSW: layered proximity graphs, greedy descent and efSearch beam search, geometric level assignment, the diversity neighbor heuristic, fi…

Read article →
ARTICLE · 25

HyperLogLog architecture, in depth: registers, sparse encoding, estimators and merge in production

How a production HyperLogLog is built: hash choice and canonicalisation, precision and memory, dense 6-bit register packing, sparse encodings in HLL++…

Read article →
ARTICLE · 26

IVF-PQ vector index architecture

Deep-dive on the IVF-PQ approximate nearest-neighbor index: coarse quantization and inverted lists (nlist/nprobe), product quantization into per-subsp…

Read article →
ARTICLE · 27

KMP Algorithm

How the Knuth-Morris-Pratt algorithm finds a pattern in text in O(n+m) by precomputing a failure function.

Read article →
ARTICLE · 28

LRU cache architecture

Deep-dive on LRU cache: hash map + linked list, admission policies (TinyLFU), concurrency, TTL, weighted entries, variants.

Read article →
ARTICLE · 29

LSM Tree Compaction Architecture in Depth

A 2500-word walkthrough of LSM compaction: memtable → SSTables, STCS/LCS/TWCS strategies, triggers, throttling, and read/write/space amplification.

Read article →
ARTICLE · 30

LSM trees vs B-trees

Deep-dive on LSM trees vs B-trees: in-place read-optimized B-trees vs append-merge write-optimized LSM trees, write/read/space amplification, compacti…

Read article →
ARTICLE · 31

Mergesort

Mergesort in depth: the merge step and where stability comes from, top-down versus bottom-up, TimSort&…

Read article →
ARTICLE · 32

Merkle trees -- efficient verification of large data

Deep-dive on Merkle trees: hashing data blocks into leaves and up to a single root, tamper evidence (any change alters the root), O(1) root comparison…

Read article →
ARTICLE · 33

Number Theory Algorithms, in depth: gcd, extended Euclid, modular inverses, fast exponentiation, sieves, Euler's phi and the Chinese remainder theorem

The core number theory toolkit for programmers, built from first principles: Euclid and extended Euclid, modular inverses, square-and-multiply exponen…

Read article →
ARTICLE · 34

How a Matrix Multiply Runs on a GPU: Tiling, Shared Memory, and Tensor Cores, with Pseudocode

A step-by-step walk from a naive CUDA matrix multiply to shared-memory tiling, register blocking and tensor-core WMMA kernels, with the arithmetic-int…

Read article →
ARTICLE · 35

Sieve of Eratosthenes, in depth: a hand trace, the n log log n argument, odd-only and segmented sieves, memory layouts and cache-aware production use

The Sieve of Eratosthenes from first principles: a worked trace to 30, why crossing off starts at p squared, why the cost is n log log n, tested Pytho…

Read article →
ARTICLE · 36

Quicksort

Quicksort in depth: the partition invariant, Lomuto vs Hoare, pivot choice, three-way partitioning, stack bounding, and what introsort and pdqsort rea…

Read article →
ARTICLE · 37

The quotient filter

Deep-dive on the quotient filter: splitting a key&…

Read article →
ARTICLE · 38

Rendezvous Hashing Architecture, in depth: score functions, weighted HRW, failure-domain-aware replicas and precomputed tables

A first-principles guide to rendezvous (highest random weight) hashing: why only K/n keys move, building a score function that really mixes key and no…

Read article →
ARTICLE · 39

Reservoir sampling architecture, in depth: skip-based sampling, mergeable bottom-k keys, weighted variants and windows

Reservoir sampling as a production system: Algorithm L to skip most random draws, the bottom-k random-key form that merges across shards, correct merg…

Read article →
ARTICLE · 40

Reservoir sampling

Deep-dive on reservoir sampling (Algorithm R): drawing k items uniformly at random from a stream of unknown length in a single forward pass with O(k) …

Read article →
ARTICLE · 41

Ribbon filter architecture

Deep-dive on the ribbon filter: a static approximate-membership structure that encodes the key set as a solved banded linear system over GF(2), reachi…

Read article →
ARTICLE · 42

Roaring bitmaps -- compressed bitmaps that stay fast

Deep-dive on roaring bitmaps: the bitmap dilemma (fast but big, or small but slow), chunking the integer space by the high 16 bits, the three adaptive…

Read article →
ARTICLE · 43

Segment Tree

Segment trees end to end: canonical decomposition, the 4n array layout, O(n) build, lazy propagation, and when a Fenwick tree beats one.

Read article →
ARTICLE · 44

Skip list architecture

Deep-dive on skip lists: levels, level selection, search, insert/delete, concurrent variants, range iterators, memory, use cases.

Read article →
ARTICLE · 45

Space-Saving architecture

Deep-dive on the Space-Saving algorithm for approximate top-k heavy hitters: a fixed table of k counters, the increment-or-evict-the-minimum rule that…

Read article →
ARTICLE · 46

t-digest architecture

Deep-dive on the t-digest quantile sketch: (mean, count) centroids, the scale function that warps cluster sizes to keep the tails sharp, the compressi…

Read article →
ARTICLE · 47

Top-K / heavy hitters -- finding the most frequent in a stream

Deep-dive on top-K / heavy-hitter algorithms: the most-frequent-items problem, why exact counting is memory-prohibitive, Space-Saving (bounded counter…

Read article →
ARTICLE · 48

Topological Sort, in depth: Kahn and DFS, cycle reporting, deterministic orders, parallel levels and build systems

Topological sort from first principles: what a dependency order is, Kahn's in-degree algorith…

Read article →
ARTICLE · 49

Trie in Depth: Prefix Trees From First Principles to Radix Trees, Autocomplete and Longest-Prefix Match

How a trie works and when to use one: node representations and their memory cost, insert, search, prefix counting, deletion with pruning, cached top-k…

Read article →
ARTICLE · 50

Union-Find, in depth: disjoint sets, union by size, path compression and the inverse Ackermann bound

Union-Find (disjoint set union) explained from first principles: the parent-pointer forest, why naive unions degrade to linear chains, union by size o…

Read article →
ARTICLE · 51

Balanced Binary Tree Check, in depth: the definition, the O(n) post-order pass, iterative versions and the traps

How to test whether a binary tree is height-balanced: the precise definition, why the top-down approach repeats work, the single post-order pass with …

Read article →
ARTICLE · 52

BFS on Implicit Graphs, in depth: designing states, generating neighbours, visited-set memory, the 8-puzzle, bidirectional search and when the state space is too big

How to run breadth-first search on graphs that are never stored: what a state must contain, encoding and visited-set memory, testing goals at generati…

Read article →
ARTICLE · 53

0-1 BFS, in depth: shortest paths with 0 and 1 weights using a deque, the invariant, stale entries and how to model problems for it

A first-principles guide to 0-1 BFS: why plain BFS fails once some edges are free, the two-value deque invariant that replaces Dijkstra&#x…

Read article →
ARTICLE · 54

Bridges and Articulation Points with Tarjan's Low-Link Algorithm: One DFS, Two Answers, and the Details That Break Real Implementations

Find every bridge and articulation point of an undirected graph in O(V + E) with one depth-first search: discovery times and low-links from first prin…

Read article →
ARTICLE · 55

BST Insert, Search and Delete, in depth: the invariant, iterative code, all three delete cases traced by hand, height analysis and testing

A first-principles guide to binary search tree operations: the ordering invariant and why every operation costs O(height), iterative search, floor and…

Read article →
ARTICLE · 56

BST Validation, in depth: bounds recursion, in-order checks, duplicates and validating real ordered structures

How to check that a binary tree is a valid binary search tree: the global invariant, why the local parent-child check is wrong, bounds recursion, iter…

Read article →
ARTICLE · 57

Build a Binary Tree From Two Traversals, in depth: why preorder or postorder plus inorder is unique, O(n) recursive and stack builds, validation, and why preorder plus postorder is not enough

Reconstructing a binary tree from two traversal sequences: the uniqueness argument, a traced worked example, O(n) builders for preorder+inorder and po…

Read article →
ARTICLE · 58

Cartesian Tree, in depth: linear-time construction, range minimum via LCA, ties, treaps and when to use one

A first-principles guide to Cartesian trees: the heap-plus-in-order definition, the O(n) stack construction traced by hand, why range minimum equals l…

Read article →
ARTICLE · 59

Deep Copy List with Random Pointer, in depth: the identity map, the O(1)-space interleaving trick, a copy verifier and graph cloning

How to deep-copy a linked list whose nodes also carry a random pointer: why a naive copy fails, the two-pass hash map, a one-pass memoised version, th…

Read article →
ARTICLE · 60

Cycle Detection in Directed Graph, in depth: three-colour DFS that returns the cycle, an explicit-stack version for deep graphs, Kahn leftovers, all cycles via SCCs and where it runs in production

How to detect and report cycles in directed graphs: why undirected tricks fail, three-colour DFS with a cycle witness, a worked example, an iterative …

Read article →
ARTICLE · 61

Cycle Detection, in depth: Floyd's tortoise and hare, Brent's algorithm, the rho shape, proofs, linked lists, Pollard's rho and production pitfalls

A first-principles guide to cycle detection on iterated functions: the rho shape and its tail length mu and cycle length lambda, Floyd&amp…

Read article →
ARTICLE · 62

Dijkstra's Algorithm, in depth: a reusable engine for state graphs, custom path costs, time-dependent edges and certified answers

Dijkstra's algorithm as a reusable engine: one loop with a pluggable expansion fu…

Read article →
ARTICLE · 63

Dijkstra's Shortest Path, in depth: the algorithm as an event stream, a frame-by-frame trace, and rendering the wavefront

Dijkstra's algorithm rebuilt as a stream of observable events so you can animate it: an instrumented generator that y…

Read article →
ARTICLE · 64

Edmonds-Karp in Depth: Why Shortest Augmenting Paths Bound Max Flow at O(VE²), With a Traced Proof and an Implementation You Can Trust

Edmonds-Karp from first principles: why augmenting along BFS shortest paths guarantees polynomial time, the monotone-distance and critical-edge proof …

Read article →
ARTICLE · 65

Huffman Coding, in depth: optimal prefix codes, canonical codes, length limits and table-driven decoding

Huffman coding from first principles to a working codec: prefix codes, the Kraft inequality and the entropy bound, a worked six-symbol example, canoni…

Read article →
ARTICLE · 66

Job Scheduling with Deadlines, in depth: the profit-maximising greedy, union-find slots, Moore-Hodgson, EDF and where greedy stops working

Job sequencing with deadlines from first principles: the unit-time profit problem, the latest-free-slot greedy with a worked example and proof sketch,…

Read article →
ARTICLE · 67

K-d Tree, in depth: median construction, nearest-neighbour pruning, range search, the curse of dimensionality and production layouts

How a k-d tree partitions k-dimensional space and answers nearest-neighbour, k-NN and range queries: median construction, the plane-pruning rule trace…

Read article →
ARTICLE · 68

Kosaraju's Algorithm, in depth: the finish-time lemma, iterative two-pass SCC, output order and real uses

Kosaraju's algorithm for strongly connected components explained from first principles: why two depth-fir…

Read article →
ARTICLE · 69

Kth Smallest in a BST, in depth: early-exit inorder, Morris traversal without leaving threads behind, and size-augmented order-statistic trees for repeated queries

A complete guide to finding the kth smallest key in a binary search tree: why inorder order is sorted order, recursive and iterative early-exit traver…

Read article →
ARTICLE · 70

Kuhn's Algorithm, in depth: maximum bipartite matching with augmenting paths, a traced example, recursive and iterative Python, correctness, complexity and when to switch

Kuhn's algorithm for maximum bipartite matching from first principles: matchings, alternating and augmenting paths, Berge's theorem,…

Read article →
ARTICLE · 71

LCP Array, in depth: Kasai linear-time construction, the proof, a full banana trace, the PLCP variant and the string problems it solves

What the longest-common-prefix array is and how Kasai's algorithm builds it from a suffix array in O(n): the key lemma and the amortised argument…

Read article →
ARTICLE · 72

Legendre and Jacobi Symbols, in depth: quadratic residues, reciprocity, a gcd-speed algorithm and where primality tests use them

A first-principles guide to the Legendre and Jacobi symbols: what quadratic residues are, Euler&am…

Read article →
ARTICLE · 73

LFU Cache, in depth: the O(1) frequency-bucket design, tie-breaking, aging, Redis's approximated LFU and when frequency beats recency

Build a Least Frequently Used cache from first principles: why frequency, the naive scan and heap designs, the O(1) structure of a key map plus per-co…

Read article →
ARTICLE · 74

Longest Palindromic Subsequence

Find the longest subsequence (not contiguous) that reads the same forwards and backwards. O(N²) DP via LCS reduction or direct recurrence; examples, c…

Read article →
ARTICLE · 75

Lowest Common Ancestor (LCA), in depth: naive climbing, binary lifting, Euler tour with sparse tables, Tarjan's offline algorithm, and how to choose

A practical guide to lowest common ancestor queries on trees: the definition, the naive climb, binary lifting, Euler tour plus sparse-table RMQ, Tarja…

Read article →
ARTICLE · 76

Lucas' Theorem, in depth: binomial coefficients modulo a prime when n is larger than p, with proof, worked examples, code and the CRT extension

Lucas' theorem from first principles: why the factorial-inverse method for C(n, k) mod p breaks once n reaches p, the base-p digit statement and …

Read article →
ARTICLE · 77

Max Flow in Depth: Ford-Fulkerson, Residual Graphs, Edmonds-Karp and the Minimum Cut, with Working Code

A from-first-principles guide to maximum flow: flow networks, residual graphs and reverse edges, the Ford-Fulkerson method, Edmonds-Karp&a…

Read article →
ARTICLE · 78

Merge K Sorted Lists, in depth: the min-heap merge, divide and conquer, and k-way merging in real systems

How to merge k sorted lists into one sorted output: why naive approaches cost O(N log N) or O(kN), the min-heap algorithm with its O(N log k) proof, c…

Read article →
ARTICLE · 79

Miller-Rabin, in depth: strong probable primes, witnesses, deterministic base sets and the bugs that break primality tests

How the Miller-Rabin primality test works from first principles: the n-1 = 2^s d decomposition, square roots of one, worked traces, error bounds, dete…

Read article →
ARTICLE · 80

Mo's Algorithm, in depth: answering offline range queries in O((n + q) sqrt n) by reordering them

A first-principles guide to Mo's algorithm: the cost model behind sqrt decomposition of queries, choosing the block s…

Read article →
ARTICLE · 81

Morris Traversal, in depth: threading a binary tree for O(1)-space inorder, preorder and postorder walks

How Morris traversal walks a binary tree with no stack and no recursion by temporarily threading null right pointers: the invariant, a full worked tra…

Read article →
ARTICLE · 82

Path Sum Problems Family, in depth: root-to-leaf checks, all paths, prefix-sum counting, maximum path sum and grid paths

One mental model for the path sum family of tree and grid problems: carrying state down a DFS, returning gains up, counting any downward path with pre…

Read article →
ARTICLE · 83

Planarity Testing

Determine if a graph can be drawn on a plane without edge crossings in linear time O(V+E). Kuratowski and Wagner characterizations, Hopcroft-Tarjan pa…

Read article →
ARTICLE · 84

Populate Next Right Pointers in a Binary Tree, in depth: queue BFS, O(1)-space level walking, the dummy-head trick and the traps in each

A complete guide to populating next right pointers in perfect and arbitrary binary trees: the level-order linked list model, queue BFS, the constant-s…

Read article →
ARTICLE · 85

Prim's Minimum Spanning Tree, in depth: the cut property, lazy and eager heaps, the dense-graph version and the bugs that break it

Prim's algorithm from first principles: what a minimum spanning tree is, why growing one tree greedily is…

Read article →
ARTICLE · 86

Priority Queue via Binary Heap, in depth: an indexed heap with update and remove, stable ties, a scheduler and the tests that keep it honest

Build a production priority queue on a binary heap: the abstract contract, a complete indexed min-heap with push, pop, update and remove, why removal …

Read article →
ARTICLE · 87

Reverse a Linked List, in depth: the three-pointer loop, its invariant, recursion and the stack limit, sublist and k-group reversal

How to reverse a singly linked list in place from first principles: the iterative three-pointer loop and why it is correct, a worked trace, the recurs…

Read article →
ARTICLE · 88

Detecting Negative Cycles, in depth: the Bellman-Ford proof, extracting the cycle, finding every affected vertex, and currency arbitrage

How to detect negative cycles in a weighted directed graph and act on the result: why the n-th Bellman-Ford pass is a proof, the virtual-source trick,…

Read article →
ARTICLE · 89

Subset Sum, in depth: existence and counting tables, bitsets, every target in one pass, size-k counts, undoing an item and the pseudo-polynomial wall

Subset sum from first principles: the boolean and counting dynamic programs, why the inner loop runs backwards, a big-integer bitset, counting under a…

Read article →
ARTICLE · 90

Symmetric Tree Check, in depth: the mirror invariant, recursive and iterative solutions, traps that look correct, and checking every subtree in linear time

How to decide whether a binary tree is a mirror image of itself: the pair-based invariant, recursive and queue-based code in Python and Java, a pair-b…

Read article →
ARTICLE · 91

Target Sum, in depth: from 2^n sign assignments to a subset-sum count, with the offset table, the parity rule, zeros, reconstruction and meet in the middle

How to count the ways to put + or - in front of each number so the total equals a target: brute force as a reference, memoised recursion, the offset t…

Read article →
ARTICLE · 92

Tarjan's SCC, in depth: low-links, the on-stack rule, an iterative implementation and what the condensation buys you

Strongly connected components of a directed graph in one depth-first search: discovery indices and low-links from first principles, why the on-stack c…

Read article →
ARTICLE · 93

Topological Sort, in depth: batch versus online, and keeping an order valid as edges arrive with Pearce-Kelly

The two ways software needs a topological order: once, over a finished graph (Kahn or DFS), or continuously, while edges keep arriving. A compact batc…

Read article →
ARTICLE · 94

Traveling Salesman Problem

Bitmask DP: O(2^N × N²) beats brute-force O(N!). Subset enumeration, state definition, recurrence, and the exact boundary where approximation takes ov…

Read article →
ARTICLE · 95

Treap, in depth: a binary search tree balanced by random priorities, with split, merge, order statistics and implicit keys

How a treap works and how to implement one: the BST and heap invariants, why random priorities give expected O(log n) depth, split and merge as the tw…

Read article →
ARTICLE · 96

Trie (Prefix Tree), in depth: building a ranked, typo-tolerant autocomplete with best-first top-k, Levenshtein pruning and snapshot builds

Use a trie as the engine of a real autocomplete service: normalised keys, subtree-maximum scores, best-first top-k search with a heap, a worked trace,…

Read article →
ARTICLE · 97

Word Search

Word search on a 2D grid: why the no-revisit constraint kills memoisation, in-place marking vs a visited bitmask, the 3^L bound, frequency and reversa…

Read article →