[{"data":1,"prerenderedAt":462},["ShallowReactive",2],{"subject:algorithms":3,"course-wordcounts":36,"nav:algorithms":92},{"id":4,"title":5,"blurb":6,"body":7,"description":18,"extension":19,"meta":20,"module":15,"navigation":21,"path":22,"practice":23,"rawbody":24,"readingTime":25,"seo":30,"sources":31,"status":32,"stem":33,"summary":15,"topics":34,"__hash__":35},"course\u002F01.algorithms\u002Findex.md","Algorithms","The classic algorithms curriculum as one coherent course — divide & conquer, sorting, data structures, sequence & string techniques, graphs, greedy, dynamic programming, backtracking, number theory, computational geometry, and intractability.",{"type":8,"value":9,"toc":14},"minimark",[10],[11,12,13],"p",{},"Start at the foundations — what an algorithm is, how we measure cost, and how to\nsolve recurrences — then build up through divide & conquer, the core data\nstructures (from linked lists to balanced and indexed trees), the everyday\nsequence-and-string techniques (two pointers, sliding windows, monotonic stacks,\nbinary search on the answer, string matching, tries), graph algorithms (traversal\nthrough flow, plus bridges, LCA, 2-SAT, and Eulerian tours), greedy methods\n(scheduling and matroids), dynamic programming (sequence, knapsack, interval,\ntree, bitmask, and the optimizations), backtracking and combinatorial search,\nnumber-theoretic and combinatorial algorithms, computational geometry, and\nfinally the theory of intractability. Each topic stands on the ones before it.",{"title":15,"searchDepth":16,"depth":16,"links":17},"",2,[],"A single, rigorous pass through the classic algorithms curriculum — synthesised from CLRS, Skiena, and Erickson into one coherent sequence. Every article is written to be read closely: precise definitions, worked pseudocode in the algorithm2e style, proofs of correctness, and interactive figures and visualisers.","md",{},true,"\u002Falgorithms",[],"---\ntitle: Algorithms\nstatus: available\nblurb: >-\n  The classic algorithms curriculum as one coherent course — divide & conquer,\n  sorting, data structures, sequence & string techniques, graphs, greedy,\n  dynamic programming, backtracking, number theory, computational geometry, and\n  intractability.\ndescription: >-\n  A single, rigorous pass through the classic algorithms curriculum — synthesised\n  from CLRS, Skiena, and Erickson into one coherent\n  sequence. Every article is written to be read closely: precise definitions,\n  worked pseudocode in the algorithm2e style, proofs of correctness, and\n  interactive figures and visualisers.\n---\n\nStart at the foundations — what an algorithm is, how we measure cost, and how to\nsolve recurrences — then build up through divide & conquer, the core data\nstructures (from linked lists to balanced and indexed trees), the everyday\nsequence-and-string techniques (two pointers, sliding windows, monotonic stacks,\nbinary search on the answer, string matching, tries), graph algorithms (traversal\nthrough flow, plus bridges, LCA, 2-SAT, and Eulerian tours), greedy methods\n(scheduling and matroids), dynamic programming (sequence, knapsack, interval,\ntree, bitmask, and the optimizations), backtracking and combinatorial search,\nnumber-theoretic and combinatorial algorithms, computational geometry, and\nfinally the theory of intractability. Each topic stands on the ones before it.\n",{"text":26,"minutes":27,"time":28,"words":29},"1 min read",0.535,32100,107,{"title":5,"description":18},[],"available","01.algorithms\u002Findex",[],"oygroBdDzlfKim2BqtPQ8HJw7LYImfGcy4JypU2Ar4U",{"\u002Falgorithms\u002Ffoundations\u002Fwhat-is-an-algorithm":37,"\u002Falgorithms\u002Ffoundations\u002Fasymptotic-analysis":38,"\u002Falgorithms\u002Ffoundations\u002Frecurrences":39,"\u002Falgorithms\u002Fdivide-and-conquer\u002Fmergesort":40,"\u002Falgorithms\u002Fdivide-and-conquer\u002Fquicksort":41,"\u002Falgorithms\u002Fdivide-and-conquer\u002Fselection":42,"\u002Falgorithms\u002Fsorting\u002Fheaps-and-heapsort":43,"\u002Falgorithms\u002Fsorting\u002Fsorting-lower-bounds":44,"\u002Falgorithms\u002Fsorting\u002Flinear-time-sorting":45,"\u002Falgorithms\u002Fdata-structures\u002Felementary-structures":46,"\u002Falgorithms\u002Fdata-structures\u002Fhash-tables":47,"\u002Falgorithms\u002Fdata-structures\u002Fbinary-search-trees":48,"\u002Falgorithms\u002Fdata-structures\u002Favl-trees":49,"\u002Falgorithms\u002Fdata-structures\u002Fbalanced-trees":50,"\u002Falgorithms\u002Fdata-structures\u002Funion-find":51,"\u002Falgorithms\u002Fdata-structures\u002Ffenwick-and-segment-trees":52,"\u002Falgorithms\u002Fsequences\u002Ftwo-pointers-and-windows":53,"\u002Falgorithms\u002Fsequences\u002Fmonotonic-stacks":54,"\u002Falgorithms\u002Fsequences\u002Fbinary-search-on-the-answer":55,"\u002Falgorithms\u002Fsequences\u002Fstring-matching":56,"\u002Falgorithms\u002Fsequences\u002Ftries":57,"\u002Falgorithms\u002Fgraphs\u002Frepresentations-and-traversal":58,"\u002Falgorithms\u002Fgraphs\u002Ftopological-sort-and-scc":59,"\u002Falgorithms\u002Fgraphs\u002Fminimum-spanning-trees":60,"\u002Falgorithms\u002Fgraphs\u002Fshortest-paths":61,"\u002Falgorithms\u002Fgraphs\u002Fnetwork-flow":62,"\u002Falgorithms\u002Fgraphs\u002Fbridges-and-articulation-points":63,"\u002Falgorithms\u002Fgraphs\u002Flowest-common-ancestor":64,"\u002Falgorithms\u002Fgraphs\u002Ftwo-sat":65,"\u002Falgorithms\u002Fgraphs\u002Feulerian-tours":66,"\u002Falgorithms\u002Fgreedy\u002Fthe-greedy-method":67,"\u002Falgorithms\u002Fgreedy\u002Fscheduling-and-intervals":68,"\u002Falgorithms\u002Fgreedy\u002Fhuffman-codes":69,"\u002Falgorithms\u002Fgreedy\u002Fmatroids":70,"\u002Falgorithms\u002Fdynamic-programming\u002Fprinciples":71,"\u002Falgorithms\u002Fdynamic-programming\u002Fsequence-dp":72,"\u002Falgorithms\u002Fdynamic-programming\u002Flongest-increasing-subsequence":73,"\u002Falgorithms\u002Fdynamic-programming\u002Fknapsack":74,"\u002Falgorithms\u002Fdynamic-programming\u002Fcoin-change-and-unbounded":75,"\u002Falgorithms\u002Fdynamic-programming\u002Finterval-dp":76,"\u002Falgorithms\u002Fdynamic-programming\u002Ftree-dp":77,"\u002Falgorithms\u002Fdynamic-programming\u002Fbitmask-dp":78,"\u002Falgorithms\u002Fdynamic-programming\u002Fdp-optimizations":79,"\u002Falgorithms\u002Fdynamic-programming\u002Fdp-on-graphs":80,"\u002Falgorithms\u002Fbacktracking\u002Fbacktracking-fundamentals":81,"\u002Falgorithms\u002Fbacktracking\u002Fconstraint-search":82,"\u002Falgorithms\u002Fbacktracking\u002Fbranch-and-bound":83,"\u002Falgorithms\u002Fmathematical-algorithms\u002Fnumber-theory-basics":53,"\u002Falgorithms\u002Fmathematical-algorithms\u002Fmodular-exponentiation-and-primality":84,"\u002Falgorithms\u002Fmathematical-algorithms\u002Fsieve-and-factorization":85,"\u002Falgorithms\u002Fmathematical-algorithms\u002Fcombinatorics":86,"\u002Falgorithms\u002Fcomputational-geometry\u002Fgeometric-primitives":87,"\u002Falgorithms\u002Fcomputational-geometry\u002Fconvex-hull":69,"\u002Falgorithms\u002Fcomputational-geometry\u002Fsweep-line":88,"\u002Falgorithms\u002Fintractability\u002Fp-np-reductions":89,"\u002Falgorithms\u002Fintractability\u002Fnp-completeness":49,"\u002Falgorithms\u002Fintractability\u002Fcoping-with-hardness":90,"\u002Falgorithms":29,"\u002Ftheory-of-computation":91,"\u002Fcomputer-architecture":91,"\u002Fphysical-computing":91,"\u002Fdatabases":91,"\u002Fdeep-learning":91},1763,2107,1738,2628,1723,2048,1697,1044,1542,1565,1679,1586,1388,1465,1971,1455,1533,1483,1578,1791,1481,2704,1658,2070,1978,2080,1568,1451,1291,1543,1883,1443,1599,2038,2241,1744,1678,2288,1929,1657,1412,1554,1418,1713,1798,1694,1762,1534,1595,1262,1495,1630,2306,2142,0,[93,116,138,159,208,240,293,318,374,394,419,440],{"module":94,"moduleNumber":95,"slug":96,"lessons":97},"Foundations",1,"foundations",[98,104,109],{"title":99,"path":100,"lessonNumber":95,"topics":101,"summary":103},"What Is an Algorithm?","\u002Falgorithms\u002Ffoundations\u002Fwhat-is-an-algorithm",[94,102],"Correctness & Induction","An algorithm is a finite, mechanical recipe that transforms inputs into outputs. We pin down what counts as an algorithm, how we write one down, and the three things we always ask of it: is it correct, is it fast, and can we prove it.\n",{"title":105,"path":106,"lessonNumber":16,"topics":107,"summary":108},"Asymptotic Analysis","\u002Falgorithms\u002Ffoundations\u002Fasymptotic-analysis",[105],"We measure an algorithm's running time as a function of its input size, then strip away machine-specific constants and lower-order terms to compare algorithms cleanly. This lesson defines the RAM model and the $O$, $\\Omega$, $\\Theta$, $o$, and $\\omega$ notations, and shows how to read the cost of loops off the page.\n",{"title":110,"path":111,"lessonNumber":112,"topics":113,"summary":115},"Recurrences and the Master Theorem","\u002Falgorithms\u002Ffoundations\u002Frecurrences",3,[114],"Recurrences","Recursive and divide-and-conquer algorithms describe their own running time with a recurrence: $T(n)$ in terms of $T$ on smaller inputs. We solve recurrences three ways — drawing the recursion tree, guessing-and-verifying by induction, and applying the Master Theorem — using merge sort as the running example.\n",{"module":117,"moduleNumber":16,"slug":118,"lessons":119},"Divide & Conquer","divide-and-conquer",[120,126,132],{"title":121,"path":122,"lessonNumber":95,"topics":123,"summary":125},"Divide and Conquer & Mergesort","\u002Falgorithms\u002Fdivide-and-conquer\u002Fmergesort",[117,124],"Comparison Sorting","Divide and conquer breaks a problem into smaller copies of itself, solves them recursively, and stitches the answers together. We meet the paradigm through mergesort — its merge step, its loop-invariant proof, and the recursion tree that pins its cost at $\\Theta(n\\log n)$ — then glimpse Karatsuba multiplication as a second example of the same idea.",{"title":127,"path":128,"lessonNumber":16,"topics":129,"summary":131},"Quicksort","\u002Falgorithms\u002Fdivide-and-conquer\u002Fquicksort",[124,130],"Probabilistic Analysis","Quicksort sorts in place by partitioning around a pivot and recursing on each side. We give Lomuto and Hoare partitioning with a correctness invariant, see why a bad pivot costs $\\Theta(n^2)$ while a balanced one gives $\\Theta(n\\log n)$, and prove that randomizing the pivot makes the expected cost $\\Theta(n\\log n)$ on every input.",{"title":133,"path":134,"lessonNumber":112,"topics":135,"summary":137},"Linear-Time Selection","\u002Falgorithms\u002Fdivide-and-conquer\u002Fselection",[136,117],"Order Statistics","Finding the $k$-th smallest element looks like it should require sorting, but it does not. Quickselect adapts quicksort's partition to recurse on just one side, achieving expected $O(n)$. The median-of-medians algorithm guarantees a good pivot with the groups-of-five trick, pushing the worst case down to a provable $O(n)$.",{"module":139,"moduleNumber":112,"slug":140,"lessons":141},"Sorting & Order Statistics","sorting",[142,148,153],{"title":143,"path":144,"lessonNumber":95,"topics":145,"summary":147},"Heaps and Heapsort","\u002Falgorithms\u002Fsorting\u002Fheaps-and-heapsort",[146,124],"Heaps","A binary heap is a tree we store flat in an array, with index arithmetic standing in for pointers. We build the max-heap property bottom-up in $O(n)$ time, sort in place in $\\Theta(n\\log n)$ by repeatedly extracting the maximum, and reuse the same structure to implement a priority queue.",{"title":149,"path":150,"lessonNumber":16,"topics":151,"summary":152},"Lower Bounds for Comparison Sorting","\u002Falgorithms\u002Fsorting\u002Fsorting-lower-bounds",[124],"Every sort we have seen runs in $\\Omega(n\\log n)$, and that is no accident. Modeling a sort as a decision tree of comparisons, we show any such tree must have $n!$ leaves, forcing height $\\ge \\log_2(n!) = \\Omega(n\\log n)$ — a worst-case bound no comparison sort can ever beat.",{"title":154,"path":155,"lessonNumber":112,"topics":156,"summary":158},"Sorting in Linear Time","\u002Falgorithms\u002Fsorting\u002Flinear-time-sorting",[157],"Linear-Time Sorting","The $\\Omega(n\\log n)$ barrier only binds algorithms that compare. By instead using keys as array indices we slip past it: counting sort runs in $\\Theta(n+k)$ and is stable, radix sort layers it digit by digit, and bucket sort averages $\\Theta(n)$ on uniform data. We see exactly when each applies.",{"module":160,"moduleNumber":161,"slug":162,"lessons":163},"Data Structures",4,"data-structures",[164,170,176,181,187,193,201],{"title":165,"path":166,"lessonNumber":95,"topics":167,"summary":169},"Elementary Data Structures","\u002Falgorithms\u002Fdata-structures\u002Felementary-structures",[168],"Linear Structures","Every container is built one of two ways: **contiguous** in an array, or **linked** through pointers. We trade cache-friendly random access against $O(1)$ splicing, derive the **amortized $O(1)$** append of a doubling dynamic array, and assemble the two ordered access disciplines — the LIFO **stack** and the FIFO **queue** (with its generalization, the **deque**) — on top of both.",{"title":171,"path":172,"lessonNumber":16,"topics":173,"summary":175},"Hash Tables","\u002Falgorithms\u002Fdata-structures\u002Fhash-tables",[174],"Hashing","A hash table implements the dictionary — insert, search, delete — in expected $O(1)$ time by scattering keys across an array with a hash function. We build up from direct addressing, handle collisions by chaining and by open addressing, analyze the load factor $\\alpha$, and see how universal hashing earns its expected-time guarantee against every input.",{"title":177,"path":178,"lessonNumber":112,"topics":179,"summary":180},"Binary Search Trees","\u002Falgorithms\u002Fdata-structures\u002Fbinary-search-trees",[177],"A binary search tree keeps keys ordered so that every operation follows a single root-to-leaf path. We state the BST property, give search and insert, find minimum, maximum, and successor, see that an inorder walk emits the keys in sorted order, and confront the catch — every operation costs $O(h)$, and a carelessly built tree degrades to height $h = \\Theta(n)$, motivating balance.",{"title":182,"path":183,"lessonNumber":161,"topics":184,"summary":186},"AVL Trees","\u002Falgorithms\u002Fdata-structures\u002Favl-trees",[185],"Balanced Trees","An AVL tree is the first balanced BST: at every node the two subtrees' heights differ by at most $1$. A Fibonacci-style minimal-node argument forces height $h \\le 1.44\\log_2 n = O(\\log n)$, so search, insert, and delete are all $O(\\log n)$. Insertion rebalances with at most one of four rotation cases (LL, RR, LR, RL); deletion may rotate all the way to the root.",{"title":188,"path":189,"lessonNumber":190,"topics":191,"summary":192},"Balanced Search Trees","\u002Falgorithms\u002Fdata-structures\u002Fbalanced-trees",5,[185],"An ordinary BST can degrade to height $\\Theta(n)$; balanced search trees guarantee $h = O(\\log n)$ by maintaining invariants and repairing them after every update. We meet rotations, the local restructuring primitive, then red-black trees, whose color invariants force logarithmic height, and finally B-trees, which trade tall-and-thin for short-and-wide to win on disk.",{"title":194,"path":195,"lessonNumber":196,"topics":197,"summary":200},"Disjoint Sets (Union-Find)","\u002Falgorithms\u002Fdata-structures\u002Funion-find",6,[198,199],"Disjoint Sets","Amortized Analysis","The disjoint-set data structure tracks a partition of elements into groups, answering \"are these two in the same group?\" and merging groups on demand. A forest of parent pointers, sped up by union by rank and path compression, drives every operation to near-constant $O(\\alpha(n))$ amortized time — the engine behind connectivity queries and Kruskal's minimum spanning tree.",{"title":202,"path":203,"lessonNumber":204,"topics":205,"summary":207},"Fenwick & Segment Trees","\u002Falgorithms\u002Fdata-structures\u002Ffenwick-and-segment-trees",7,[206],"Range Queries","A prefix-sum array answers a range sum in $O(1)$ but pays $O(n)$ per update; a plain array updates in $O(1)$ but pays $O(n)$ per range sum. Fenwick and segment trees give us _both_ in $O(\\log n)$. The Fenwick (binary indexed) tree is a tiny array keyed by the low bit; the segment tree is a general balanced tree over canonical ranges that handles any associative aggregate and, with lazy propagation, range updates too.",{"module":209,"moduleNumber":190,"slug":210,"lessons":211},"Sequences & Strings","sequences",[212,218,223,229,235],{"title":213,"path":214,"lessonNumber":95,"topics":215,"summary":217},"Two Pointers, Sliding Windows & Prefix Sums","\u002Falgorithms\u002Fsequences\u002Ftwo-pointers-and-windows",[216],"Array Techniques","A family of array idioms that collapse an obvious $O(n^2)$ scan into a single $O(n)$ pass by maintaining an invariant as indices move. We meet two pointers (converging on a sorted array, and a fast\u002Fslow pair for in-place rewriting), the sliding window (fixed and variable size, amortized $O(n)$), and prefix sums, which answer any range-sum in $O(1)$ and count subarrays summing to $k$.",{"title":219,"path":220,"lessonNumber":16,"topics":221,"summary":222},"Monotonic Stacks & Queues","\u002Falgorithms\u002Fsequences\u002Fmonotonic-stacks",[216],"A **monotonic stack** keeps its contents sorted by popping every element that would break the order before each push — turning a family of \"previous\u002Fnext greater (or smaller) element\" questions into a single $O(n)$ scan. We derive the next-greater-element routine and its amortized analysis, fuse two such scans to measure the **largest rectangle in a histogram** in linear time, and extend the idea to a **monotonic deque** that streams the **sliding-window maximum** in $O(n)$.",{"title":224,"path":225,"lessonNumber":112,"topics":226,"summary":228},"Binary Search on the Answer","\u002Falgorithms\u002Fsequences\u002Fbinary-search-on-the-answer",[227],"Searching","Binary search is not really about arrays — it is about locating the boundary of a **monotone predicate** $p(x)$ in $O(\\log(\\text{range}))$ probes. We first pin down the half-open `while (lo \u003C hi)` template for $\\textsc{lower\\_bound}$ and $\\textsc{upper\\_bound}$, then generalize to \"binary search on the answer\": whenever feasibility is monotone in a numeric parameter, we binary search the parameter itself, calling a feasibility check at each step.",{"title":230,"path":231,"lessonNumber":161,"topics":232,"summary":234},"String Matching: Rabin–Karp, KMP & Z","\u002Falgorithms\u002Fsequences\u002Fstring-matching",[233],"Strings","Given a text $T$ of length $n$ and a pattern $P$ of length $m$, find every occurrence of $P$ in $T$. The naive scan costs $O(nm)$; we beat it three ways. Rabin–Karp uses a **rolling hash** to test alignments in $O(1)$ amortised each, with expected $O(n+m)$. KMP precomputes a **failure function** so the scan never re-reads a text character, for worst-case $O(n+m)$. The **Z-function** gives the same bound from a different angle and converts freely to KMP's table.",{"title":236,"path":237,"lessonNumber":190,"topics":238,"summary":239},"Tries & Prefix Trees","\u002Falgorithms\u002Fsequences\u002Ftries",[233],"A **trie** stores a set of strings in a tree keyed by _characters_, so that insert, search, and prefix-test all run in $O(L)$ time — the length of the key, _independent of how many keys are stored_. Shared prefixes are stored once, which makes tries the natural structure for autocomplete, wildcard dictionaries, board word-search, and — over the alphabet $\\{0,1\\}$ — the maximum-XOR-pair problem.",{"module":241,"moduleNumber":196,"slug":242,"lessons":243},"Graphs","graphs",[244,251,256,261,266,271,276,281,287],{"title":245,"path":246,"lessonNumber":95,"topics":247,"summary":250},"Graph Representations and Traversal","\u002Falgorithms\u002Fgraphs\u002Frepresentations-and-traversal",[248,249],"Graph Representations","Graph Traversal","A graph captures _relationships_ — who connects to whom. We fix the vocabulary, weigh the two standard representations (adjacency list versus matrix), then meet the two explorations you'll use constantly: breadth-first search, which finds shortest paths by number of edges, and depth-first search, whose discovery and finish times reveal a graph's hidden structure. Both run in $O(V + E)$.",{"title":252,"path":253,"lessonNumber":16,"topics":254,"summary":255},"Topological Sort and Strong Connectivity","\u002Falgorithms\u002Fgraphs\u002Ftopological-sort-and-scc",[249],"Directed acyclic graphs model dependencies: tasks that must precede other tasks. A _topological order_ lays such a graph out in a line so every edge points forward, and depth-first finish times hand it to us almost for free. We then ask the harder question for graphs _with_ cycles: which vertices can reach each other? The answer is the strongly connected components, found by a two-pass DFS.",{"title":257,"path":258,"lessonNumber":112,"topics":259,"summary":260},"Minimum Spanning Trees","\u002Falgorithms\u002Fgraphs\u002Fminimum-spanning-trees",[257],"Given a weighted network, how do we connect everything as cheaply as possible? The answer is a minimum spanning tree. One lemma, the cut property, justifies _every_ correct MST algorithm, and from it two famous greedy methods fall out: Kruskal's, which grows a forest edge by edge with a union-find structure, and Prim's, which grows a single tree using a priority queue.",{"title":262,"path":263,"lessonNumber":161,"topics":264,"summary":265},"Shortest Paths","\u002Falgorithms\u002Fgraphs\u002Fshortest-paths",[262],"Finding the cheapest route through a weighted network is one of the most-used algorithms in computing. A single operation — _relaxation_ — underlies them all. Dijkstra's algorithm solves the non-negative case greedily; Bellman-Ford handles negative edges and detects negative cycles; and Floyd-Warshall finds the shortest path between _every_ pair of vertices.",{"title":267,"path":268,"lessonNumber":190,"topics":269,"summary":270},"Network Flow","\u002Falgorithms\u002Fgraphs\u002Fnetwork-flow",[267],"How much can flow through a network from source to sink? Max-flow is a surprisingly general model — once you see a problem as flow, a whole toolbox opens up. We build flow networks, find maximum flows by repeatedly pushing along augmenting paths in the residual graph, prove the max-flow min-cut theorem, and watch bipartite matching fall out as a special case.",{"title":272,"path":273,"lessonNumber":196,"topics":274,"summary":275},"Bridges & Articulation Points","\u002Falgorithms\u002Fgraphs\u002Fbridges-and-articulation-points",[241],"A **bridge** is an edge whose removal disconnects the graph; an **articulation point** is a vertex whose removal does. Both are single points of failure in a network. A single depth-first search computes discovery times and **low-links**, and two local criteria — $low[v] > disc[u]$ for bridges, $low[v] \\ge disc[u]$ for cut vertices — find them all in $O(V+E)$.",{"title":277,"path":278,"lessonNumber":204,"topics":279,"summary":280},"Lowest Common Ancestor & Binary Lifting","\u002Falgorithms\u002Fgraphs\u002Flowest-common-ancestor",[241],"Given a rooted tree, the lowest common ancestor of $u$ and $v$ is the deepest node that is an ancestor of both. A naive walk answers one query in $O(h)$; **binary lifting** precomputes the $2^k$-th ancestor of every node in $O(n\\log n)$, then answers $k$-th-ancestor and LCA queries in $O(\\log n)$ each. We derive both jumps, apply them to tree distance, and compare against the Euler-tour + RMQ and Tarjan offline alternatives.",{"title":282,"path":283,"lessonNumber":284,"topics":285,"summary":286},"2-SAT via Implication Graphs","\u002Falgorithms\u002Fgraphs\u002Ftwo-sat",8,[241],"A boolean formula whose every clause has exactly two literals can be solved in _linear_ time — even though its three-literal cousin is NP-complete. The trick is to read each clause as a pair of implications, build a directed graph on the $2n$ literals, and ask a question we already know how to answer: which literals share a strongly connected component? The formula is satisfiable iff no variable lands in the same SCC as its own negation, and the SCCs' topological order hands us a satisfying assignment for free.",{"title":288,"path":289,"lessonNumber":290,"topics":291,"summary":292},"Eulerian Tours","\u002Falgorithms\u002Fgraphs\u002Feulerian-tours",9,[241],"An **Eulerian tour** uses every _edge_ of a graph exactly once. We give the exact parity and balance conditions under which one exists (even degree for undirected graphs, in-degree equal to out-degree for directed) and Hierholzer's $O(E)$ algorithm that constructs one by splicing closed sub-tours. We contrast this sharply with the **Hamiltonian** problem (visit every _vertex_ once), which is NP-complete: visiting edges is easy, visiting vertices is hard.",{"module":294,"moduleNumber":204,"slug":295,"lessons":296},"Greedy Algorithms","greedy",[297,302,308,313],{"title":298,"path":299,"lessonNumber":95,"topics":300,"summary":301},"The Greedy Method","\u002Falgorithms\u002Fgreedy\u002Fthe-greedy-method",[294],"A greedy algorithm builds a solution one locally-best choice at a time and never looks back. We pin down the two properties that make this work — the greedy-choice property and optimal substructure — prove the canonical activity-selection algorithm correct with an exchange argument, watch greedy fail spectacularly on the 0\u002F1 knapsack, and glimpse matroids as the theory that says exactly when greed is good.",{"title":303,"path":304,"lessonNumber":16,"topics":305,"summary":307},"Scheduling & Interval Partitioning","\u002Falgorithms\u002Fgreedy\u002Fscheduling-and-intervals",[306],"Greedy","Three classic scheduling problems all yield to greedy algorithms — and all three turn on a single design decision: which key to sort by. Interval scheduling sorts by **finish** time to pack the most compatible jobs; interval partitioning sorts by **start** time and proves the rooms needed equal the maximum overlap **depth**; minimizing maximum lateness sorts by **deadline** and is justified by an adjacent-swap exchange argument.",{"title":309,"path":310,"lessonNumber":112,"topics":311,"summary":312},"Huffman Codes","\u002Falgorithms\u002Fgreedy\u002Fhuffman-codes",[294],"Huffman coding is the greedy method's most beautiful application: it builds a provably optimal prefix-free binary code by repeatedly merging the two least frequent symbols. We develop prefix-free codes as binary trees, give the algorithm with a priority queue, build a Huffman tree from example frequencies, prove optimality with the same greedy-choice-plus-substructure argument, and pin the running time at $O(n\\log n)$.",{"title":314,"path":315,"lessonNumber":161,"topics":316,"summary":317},"Matroids & Exchange Arguments","\u002Falgorithms\u002Fgreedy\u002Fmatroids",[306],"The capstone of the greedy module: _why_ and _when_ a greedy algorithm is provably optimal. We recap the two correctness templates — **greedy-stays-ahead** and the **exchange argument** — then meet the **matroid** $M=(S,\\mathcal{I})$, an abstraction whose **exchange property** is exactly the structure greedy needs. The matroid–greedy theorem says sorting by weight and taking what stays independent yields a maximum-weight basis _if and only if_ the structure is a matroid. Kruskal's MST is the canonical instance; 0\u002F1 knapsack and TSP are the canonical failures.",{"module":319,"moduleNumber":284,"slug":320,"lessons":321},"Dynamic Programming","dynamic-programming",[322,327,333,338,343,348,353,358,363,368],{"title":323,"path":324,"lessonNumber":95,"topics":325,"summary":326},"Principles of Dynamic Programming","\u002Falgorithms\u002Fdynamic-programming\u002Fprinciples",[319,114],"Dynamic programming is recursion with memory: when a recursive solution re-solves the same subproblems again and again, we solve each one once and store the answer. We pin down the two structural conditions that make this work — overlapping subproblems and optimal substructure — contrast top-down memoization with bottom-up tabulation, and distil the whole method into a five-step recipe.",{"title":328,"path":329,"lessonNumber":16,"topics":330,"summary":332},"Sequence Alignment & LCS","\u002Falgorithms\u002Fdynamic-programming\u002Fsequence-dp",[319,331],"String Structures","Two strings can be compared by asking how much of one survives inside the other. The longest common subsequence (LCS) and edit distance are the two classic answers, and they are the _same_ dynamic program wearing different costs. We derive the LCS recurrence by examining the last characters, fill a worked DP table, reconstruct the subsequence, and then show edit distance as the identical $\\Theta(mn)$ pattern.",{"title":334,"path":335,"lessonNumber":112,"topics":336,"summary":337},"Longest Increasing Subsequence","\u002Falgorithms\u002Fdynamic-programming\u002Flongest-increasing-subsequence",[319],"Given a sequence of numbers, how long is its longest strictly increasing subsequence? A first dynamic program indexes subproblems by the element each subsequence _ends at_, giving an $O(n^2)$ solution with parent-pointer reconstruction. A sharper idea, the patience-sorting _tails_ array searched by binary search, drops the time to $O(n\\log n)$. We then fold in the variants: non-decreasing, counting, Russian-doll envelopes, and bitonic.",{"title":339,"path":340,"lessonNumber":161,"topics":341,"summary":342},"Knapsack & Subset Problems","\u002Falgorithms\u002Fdynamic-programming\u002Fknapsack",[319],"We start from $\\textsc{Subset-sum}$ — does some sublist hit a target $t$? — and its include\u002Fexclude recurrence over a boolean table $A(i, u)$, then bolt on values to get 0\u002F1 knapsack as the same machine with $\\lor$ promoted to $\\max$. We fill both tables, recover the chosen items, and confront the surprise that the $\\Theta(nt)$ running time is only _pseudo-polynomial_ — exponential in the bit length $b$, and unimprovable unless $\\mathrm{P}=\\mathrm{NP}$ since subset-sum is $\\textsc{NP-complete}$. The fractional variant reveals the sharp line between greedy and dynamic programming.",{"title":344,"path":345,"lessonNumber":190,"topics":346,"summary":347},"Coin Change & Unbounded Knapsack","\u002Falgorithms\u002Fdynamic-programming\u002Fcoin-change-and-unbounded",[319],"The previous lesson let each item be taken at most once. Drop that cap — items may be reused _any number of times_ — and the 0\u002F1 knapsack collapses from a two-dimensional table to a one-dimensional one, because there is no longer a prefix of \"already-used\" items to track. We meet **unbounded knapsack**, then its most famous instance, **coin change**: the minimum-coins recurrence $C[a] = 1 + \\min_c C[a-c]$, and the counting variant where the _order of the loops_ decides whether you count unordered combinations or ordered sequences — the classic bug. Greed fails in general but works for canonical coin systems.",{"title":349,"path":350,"lessonNumber":196,"topics":351,"summary":352},"Interval DP","\u002Falgorithms\u002Fdynamic-programming\u002Finterval-dp",[319],"Many problems ask for the best way to combine a contiguous range of items, and the answer is a dynamic program over subintervals $[i,j]$ that chooses a split point $k$. We derive the pattern from matrix-chain multiplication — parenthesising a product to minimize scalar multiplications in $O(n^3)$ — distil it into a reusable template filled by increasing interval length, and then meet its sharpest variant: the \"last operation\" trick behind Burst Balloons and cutting a stick, where fixing the _last_ move (not the first) makes the two sides independent.",{"title":354,"path":355,"lessonNumber":204,"topics":356,"summary":357},"Dynamic Programming on Trees","\u002Falgorithms\u002Fdynamic-programming\u002Ftree-dp",[319],"When the subproblems of a dynamic program are _rooted subtrees_, a single post-order DFS solves the whole thing in $O(n)$: each node combines the already-computed answers of its children. We meet the archetype — maximum-weight independent set on a tree — then the \"path through a node\" pattern behind tree diameter and maximum path sum, and finally **rerooting**, which computes a per-node answer for _every_ node as root in $O(n)$ with two passes.",{"title":359,"path":360,"lessonNumber":284,"topics":361,"summary":362},"Bitmask DP","\u002Falgorithms\u002Fdynamic-programming\u002Fbitmask-dp",[319],"When a subproblem depends not on an index or a prefix but on _which subset_ of a small ground set has been used, we can encode that subset as the bits of an integer and index a DP table by it. With $n \\le \\sim 20$ the $2^n$ subsets fit in a table, turning $\\Theta(n!)$ brute force into $O(2^n \\cdot \\text{poly}(n))$. We meet the bit tricks, the Held–Karp TSP archetype, assignment by mask, subset-sum partitioning, and submask enumeration with its $3^n$ bound.",{"title":364,"path":365,"lessonNumber":290,"topics":366,"summary":367},"DP Optimizations","\u002Falgorithms\u002Fdynamic-programming\u002Fdp-optimizations",[319],"A correct DP recurrence is only half the battle; its naive evaluation is often a factor of $n$ slower than necessary. This capstone surveys five techniques, monotonic-queue, the convex hull trick, divide-and-conquer optimization, Knuth's optimization, and SOS DP, that each exploit _structure in the transition_ (a sliding window, linear costs, monotone optimal splits, the quadrangle inequality, or subset lattices) to shave an $O(n)$, $O(\\log n)$, or worse factor off the running time.",{"title":369,"path":370,"lessonNumber":371,"topics":372,"summary":373},"Dynamic Programming on Graphs","\u002Falgorithms\u002Fdynamic-programming\u002Fdp-on-graphs",10,[319],"Many graph algorithms are dynamic programs in disguise: the subproblem is the _best value reachable under a restricted resource_ — intermediate vertices allowed, edges allowed, or a topological prefix — and edge _relaxation_ is the DP transition. We frame Floyd–Warshall as the archetype ($O(V^3)$ all-pairs shortest paths), Bellman–Ford as a DP over path length (the at-most-$K$-stops variant), DAG-DP in topological order ($O(V+E)$), and Warshall's transitive closure as the boolean analog.",{"module":375,"moduleNumber":290,"slug":376,"lessons":377},"Backtracking & Search","backtracking",[378,384,389],{"title":379,"path":380,"lessonNumber":95,"topics":381,"summary":383},"Backtracking: Subsets, Permutations & Combinations","\u002Falgorithms\u002Fbacktracking\u002Fbacktracking-fundamentals",[382],"Backtracking","Backtracking builds a solution one choice at a time and abandons a partial solution the moment it cannot be completed, exploring a state-space tree by depth-first search. We meet the universal choose\u002Fexplore\u002Fun-choose template, derive the canonical enumerations — subsets ($2^n$), permutations ($n!$), and combinations ($\\binom{n}{k}$) — handle duplicate elements by skipping equal siblings, and see how pruning turns an exponential search into a tractable one.",{"title":385,"path":386,"lessonNumber":16,"topics":387,"summary":388},"Constraint Search: N-Queens & Sudoku","\u002Falgorithms\u002Fbacktracking\u002Fconstraint-search",[382],"Many hard puzzles are **constraint satisfaction problems**: assign each variable a value from its domain so that every constraint holds. Backtracking solves them by assigning variables one at a time and rejecting a partial assignment the instant a constraint breaks. We make the rejection cheap — $O(1)$ conflict checks for N-Queens via column and diagonal sets — and prune harder with **forward checking**, **MRV** ordering, and **constraint propagation**, which is what lets an exponential search actually finish.",{"title":390,"path":391,"lessonNumber":112,"topics":392,"summary":393},"Branch & Bound and Meet in the Middle","\u002Falgorithms\u002Fbacktracking\u002Fbranch-and-bound",[382],"Plain backtracking prunes a search tree by _feasibility_; for _optimization_ problems we can prune far more aggressively by _value_. **Branch and bound** keeps the best complete solution found so far and discards any partial solution whose optimistic bound cannot beat it. **Meet in the middle** splits the instance in two, enumerates each half, and recombines by binary search — turning $2^n$ into $O(2^{n\u002F2}\\,n)$ and pushing exact search out to $n \\approx 40$.",{"module":395,"moduleNumber":371,"slug":396,"lessons":397},"Mathematical Algorithms","mathematical-algorithms",[398,404,409,414],{"title":399,"path":400,"lessonNumber":95,"topics":401,"summary":403},"Number Theory: GCD & Modular Arithmetic","\u002Falgorithms\u002Fmathematical-algorithms\u002Fnumber-theory-basics",[402],"Number Theory","This lesson opens the mathematical-algorithms module with the bedrock of computational number theory. We prove Euclid's recurrence $\\gcd(a,b)=\\gcd(b,\\,a\\bmod b)$ and its $O(\\log\\min(a,b))$ running time, extend it to recover Bézout coefficients $x,y$ with $ax+by=\\gcd(a,b)$, and build modular arithmetic on residue classes — including when a modular inverse $a^{-1}\\bmod m$ exists and how to compute it.",{"title":405,"path":406,"lessonNumber":16,"topics":407,"summary":408},"Modular Exponentiation & Primality","\u002Falgorithms\u002Fmathematical-algorithms\u002Fmodular-exponentiation-and-primality",[402],"Computing $a^n \\bmod m$ naively costs $n$ multiplications; **repeated squaring** does it in $O(\\log n)$ by reading the bits of the exponent. We use this routine to state **Fermat's little theorem** (and the modular inverse it gives), then to test primality — trial division, the probabilistic **Fermat** and **Miller–Rabin** tests, and the deterministic witness set that settles primality for every 64-bit number.",{"title":410,"path":411,"lessonNumber":112,"topics":412,"summary":413},"Sieves & Factorization","\u002Falgorithms\u002Fmathematical-algorithms\u002Fsieve-and-factorization",[402],"The previous lesson tested one number for primality; here we ask for _all_ primes up to $n$ at once. The **sieve of Eratosthenes** cross-cuts composites in $O(n\\log\\log n)$, and a **linear sieve** does it in $O(n)$ while recording each number's **smallest prime factor**, which then factors any $x \\le n$ in $O(\\log x)$. From a factorization $x = \\prod p_i^{e_i}$ the multiplicative functions $\\tau$, $\\sigma$, and Euler's totient $\\varphi$ fall out immediately.",{"title":415,"path":416,"lessonNumber":161,"topics":417,"summary":418},"Combinatorics & Counting","\u002Falgorithms\u002Fmathematical-algorithms\u002Fcombinatorics",[402],"Counting is the arithmetic of finite sets. We build up from permutations $n!$ and combinations $\\binom{n}{k}$, prove Pascal's rule by a bijection, and count multisets with stars and bars. The practical core is computing $\\binom{n}{k}\\bmod p$ in $O(1)$ from precomputed factorials and inverse factorials. We close with inclusion–exclusion and the Chinese Remainder Theorem, both of which lean on the modular inverse from the previous lesson.",{"module":420,"moduleNumber":421,"slug":422,"lessons":423},"Computational Geometry",11,"computational-geometry",[424,430,435],{"title":425,"path":426,"lessonNumber":95,"topics":427,"summary":429},"Geometric Primitives & Orientation","\u002Falgorithms\u002Fcomputational-geometry\u002Fgeometric-primitives",[428],"Geometry","Computational geometry is built on a single reliable primitive — the **orientation test**, a sign of a cross product that tells whether three points turn left, right, or lie collinear. From points-as-vectors and the dot and cross products we derive orientation, segment intersection, the shoelace area formula, and point-in-polygon tests, keeping all arithmetic **exact and integer** so that no floating-point rounding can corrupt a sign.",{"title":431,"path":432,"lessonNumber":16,"topics":433,"summary":434},"Convex Hull","\u002Falgorithms\u002Fcomputational-geometry\u002Fconvex-hull",[428],"The convex hull is the smallest convex polygon enclosing a point set — the rubber band snapped around the nails. We build it with Andrew's monotone chain, sorting by $(x,y)$ and sweeping a lower and upper hull while popping any non-left turn via the orientation primitive, in $O(n\\log n)$. A reduction from sorting shows that bound is optimal, and the hull unlocks diameter, smallest enclosing rectangle, and more through rotating calipers.",{"title":436,"path":437,"lessonNumber":112,"topics":438,"summary":439},"Sweep-Line Algorithms","\u002Falgorithms\u002Fcomputational-geometry\u002Fsweep-line",[428],"The plane-sweep paradigm turns a static $2$-D geometry problem into a dynamic $1$-D ordered-set problem: a vertical line sweeps left to right, stopping at an $x$-sorted **event queue** while a balanced-BST **status structure** tracks the objects it currently crosses, ordered by $y$. We derive Bentley–Ottmann segment intersection in $O((n+k)\\log n)$, recover closest-pair in $O(n\\log n)$, and reduce skyline, rectangle-area, and overlap problems to $\\pm1$ event sweeps.",{"module":441,"moduleNumber":442,"slug":443,"lessons":444},"Intractability",12,"intractability",[445,451,455],{"title":446,"path":447,"lessonNumber":95,"topics":448,"summary":450},"P, NP, and Reductions","\u002Falgorithms\u002Fintractability\u002Fp-np-reductions",[449],"NP-Completeness","Most problems we have met so far have fast algorithms. A vast and important family seemingly does not. This lesson builds the vocabulary for that divide: decision problems, the class $\\mathsf{P}$ of problems we can solve quickly, the class $\\mathsf{NP}$ of problems whose solutions we can _check_ quickly, and polynomial-time reductions, the tool that lets us compare the difficulty of two problems without solving either.",{"title":449,"path":452,"lessonNumber":16,"topics":453,"summary":454},"\u002Falgorithms\u002Fintractability\u002Fnp-completeness",[449],"Some problems in $\\mathsf{NP}$ are universally hardest: every other problem in $\\mathsf{NP}$ reduces to them. This lesson defines $\\mathsf{NP}$-hard and $\\mathsf{NP}$-complete, states the Cook–Levin theorem that anchors the whole edifice on **SAT**, walks the web of reductions that grows from it, and gives the four-step recipe for proving a brand-new problem $\\mathsf{NP}$-complete.",{"title":456,"path":457,"lessonNumber":112,"topics":458,"summary":461},"Coping with NP-Hardness","\u002Falgorithms\u002Fintractability\u002Fcoping-with-hardness",[459,460],"Approximation","Heuristics","Proving a problem $\\mathsf{NP}$-hard is the beginning, not the end. The world still needs answers. This lesson surveys the four honest responses to hardness: approximation algorithms with a provable ratio (worked through a 2-approximation for vertex cover), heuristics and local search, exact exponential methods like branch and bound, and exploiting special structure in the instances you actually face.",1781526653001]