TL;DR
- Algorithms are not a checklist of interview trivia. In production systems, they represent a recurring set of architectural design moves — amortization, optimal substructure, dual bounds, goal-directed relaxation, probabilistic sketching, and online self-tuning — reused across matching engines, compilers, network routers, database kernels, and vector indexes.
- Match your algorithm to your required guarantee class:
- Provable stability: Gale–Shapley (two-sided markets, residency matching).
- Exact global cost minimization: Hungarian / Kuhn–Munkres (ride-share batching, task assignment).
- Guaranteed worst-case streaming search: Knuth–Morris–Pratt (lexers, DFA scanning).
- Practical high-throughput log scanning: Boyer–Moore–Horspool (grep, log ingest).
- Multi-pattern / rolling window signatures: Rabin–Karp (deep packet inspection, plagiarism checks).
- Exact capacity allocation: 0/1 Knapsack DP with space optimization (packet scheduling, cloud budgeting).
- Anytime combinatorial exploration: Ant Colony Optimization (ACO) (logistics routing, circuit layout).
- Dense all-pairs graph closure: Floyd–Warshall (SD-WAN transit matrices, FX currency closure).
- High-throughput network flow: Push–Relabel / Goldberg–Tarjan (telecom provisioning, graph-cut segmentation).
- Fraud ring and community mining: Bron–Kerbosch with pivoting (maximal clique enumeration).
- Goal-directed shortest path: A* (robotics, gaming navmeshes, logistics planners).
- Self-tuning memory management: Adaptive Replacement Cache (ARC) (ZFS ARC, database buffer pools).
- Bounded-memory cardinality: HyperLogLog (Redis PFCOUNT, analytics engines, shard merge).
- High-dimensional similarity retrieval: HNSW (Milvus, Qdrant, Pinecone, vector RAG retrieval).
- Every engineering choice trades between optimality, memory overhead, indexing latency, and failure semantics. Exact algorithms carry formal proofs; heuristics and sketches trade precision for massive scale; amortized structures buy throughput at the cost of latency variance.
What You Will Learn Here
- How fourteen modern algorithms cluster into six core architectural primitives that you can recognize and reuse when designing production services.
- Deep-dive breakdowns for each algorithm, including mathematical invariants, exact Big-O complexities (best, average, worst, space), pros/cons matrices, and idiomatic Python implementations.
- Clean Mermaid workflows and state machines illustrating state transitions and decision logic without ASCII shortcuts.
- Real-world systems contexts: exactly where these primitives ship inside production runtimes (ZFS, PostgreSQL, Redis, Linux kernels, SD-WAN controllers, LLM vector databases, and matching markets).
- A universal Quick-Selection Matrix & Decision Flow mapping problem shapes, constraints, and latency SLOs directly to recommended algorithms and fallbacks.
- Primary foundational citations pointing to the original literature and papers.
Part 1: Foundations, Matching and Assignment Problems
1. The Language of Algorithms: Guarantees, Invariants, and Bounds
Before analyzing specific routines, we must align on how systems engineers evaluate algorithms. In production, we do not simply ask “is it fast?” We ask:
- What invariant holds after each iteration? (Inductive correctness).
- What is the worst-case vs amortized cost under adversarial input? (Denial-of-Service resilience).
- Does it guarantee global optimality, local stability, or bounded approximation?
- Is the memory consumption static, streaming, or input-dependent?
flowchart LR
subgraph guarantees ["Algorithmic Guarantees"]
direction TB
G1["Provable Optimality<br/>Hungarian, Knapsack, Floyd-Warshall"]
G2["Game-Theoretic Stability<br/>Gale-Shapley"]
G3["Worst-Case Linear Latency<br/>KMP, Push-Relabel"]
G4["Probabilistic Bounded Error<br/>HyperLogLog, HNSW, Rabin-Karp"]
G5["Online Self-Tuning<br/>ARC Cache"]
end
2. Gale-Shapley Algorithm (Stable Matching)
Core Intuition & Mathematical Invariant
The Gale–Shapley algorithm (1962) solves the Stable Marriage Problem (and generalized hospital-resident allocation). Two disjoint sets of agents rank all members of the opposing set. Proposers make offers down their preference list; receivers hold the best offer seen so far (deferred acceptance) and reject worse offers.
Mathematical Invariant: At termination, there exists no blocking pair where proposer prefers receiver over their assigned partner and receiver prefers over their assigned partner. The proposer-oriented formulation is proposer-optimal (maximizes utility for all proposers simultaneously) and strategy-proof for proposers.
Big-O Complexity
| Metric | Best Case | Average Case | Worst Case | Space Complexity |
|---|---|---|---|---|
| Bound |
Note: represents the number of agents per partition. Space holds the preference rankings and inverse lookup matrices.
Pros & Cons
| Pros | Cons |
|---|---|
| Guaranteed stable matching in quadratic time | Requires complete, strictly ordered preference lists |
| Proposer strategy-proof (truth-telling is dominant) | Asymmetric: highly favors proposers over receivers |
| Deterministic execution with zero backtracking | Does not maximize global social utility (stable Pareto-optimal) |
| Native fit for quota extensions (hospitals) | Ties and couples introduce NP-hard matching constraints |
Production Python Implementation
from collections import deque
from dataclasses import dataclass
from typing import Mapping, Sequence
@dataclass(frozen=True, slots=True)
class StableMatchingResult:
"""Receiver -> proposer and proposer -> receiver stable matching."""
matching: dict[str, str]
inverse_matching: dict[str, str]
def gale_shapley(
proposers: Sequence[str],
receivers: Sequence[str],
proposer_prefs: Mapping[str, Sequence[str]],
receiver_prefs: Mapping[str, Sequence[str]],
) -> StableMatchingResult:
"""Proposer-optimal Gale–Shapley deferred acceptance."""
receiver_rank: dict[str, dict[str, int]] = {}
for receiver in receivers:
prefs = receiver_prefs.get(receiver, ())
receiver_rank[receiver] = {p: idx for idx, p in enumerate(prefs)}
next_proposal_index = {proposer: 0 for proposer in proposers}
free_proposers: deque[str] = deque(proposers)
receiver_matches: dict[str, str] = {}
while free_proposers:
proposer = free_proposers.popleft()
prefs = proposer_prefs.get(proposer, ())
proposal_idx = next_proposal_index[proposer]
if proposal_idx >= len(prefs):
continue
receiver = prefs[proposal_idx]
next_proposal_index[proposer] = proposal_idx + 1
incumbent = receiver_matches.get(receiver)
if incumbent is None:
receiver_matches[receiver] = proposer
continue
ranks = receiver_rank[receiver]
if ranks.get(proposer, float("inf")) < ranks.get(incumbent, float("inf")):
receiver_matches[receiver] = proposer
free_proposers.append(incumbent)
else:
free_proposers.append(proposer)
inverse_matching = {
proposer: receiver for receiver, proposer in receiver_matches.items()
}
return StableMatchingResult(
matching=receiver_matches,
inverse_matching=inverse_matching,
)
Real-World Production Use Cases
- National Resident Matching Program (NRMP): Allocates medical graduates to residency slots across the US healthcare system.
- CDN Edge Server & Client Bipartite Pairing: Assigning client ISP clusters to edge points-of-presence based on latency and load preferences.
- Decentralized Resource Slotting: Cloud VM spot instance allocation matching cost-sensitive workloads to excess capacity nodes.
Mermaid Execution Flowchart
flowchart TD
Start(["All proposers enqueued as free"]) --> CheckQueue{"Free proposers exist?"}
CheckQueue -- No --> Done(["Return stable matching"])
CheckQueue -- Yes --> Pop["Pop proposer P from queue"]
Pop --> Propose["Propose to next best receiver R"]
Propose --> CheckMatched{"Is R currently matched?"}
CheckMatched -- No --> Engage["Engage P with R"]
Engage --> CheckQueue
CheckMatched -- Yes --> Compare{"Does R prefer P over incumbent I?"}
Compare -- Yes --> Reassign["Engage P with R; push I to free queue"]
Reassign --> CheckQueue
Compare -- No --> Reject["Reject P; push P to free queue"]
Reject --> CheckQueue
3. Hungarian Algorithm (Kuhn-Munkres Bipartite Assignment)
Core Intuition & Mathematical Invariant
The Hungarian Algorithm (Kuhn 1955, Munkres 1957) finds the minimum weight perfect matching in a complete weighted bipartite graph.
It operates on Linear Programming Duality:
- Maintains dual potential variables (rows) and (columns) such that reduced costs .
- By Complementary Slackness, a matching using only tight edges () is globally optimal.
Big-O Complexity
| Metric | Best Case | Average Case | Worst Case | Space Complexity |
|---|---|---|---|---|
| Bound |
Note: Original formulation was ; modern potential tracking yields .
Pros & Cons
| Pros | Cons |
|---|---|
| Guaranteed global minimum-cost assignment | becomes prohibitive for |
| Dual variables provide shadow pricing / sensitivity analysis | Dense matrix representation requires memory |
| Handles negative weights with proper potential offsets | Requires square matrices (unbalanced matrices require dummy padding) |
| Standard exact solver for multi-target tracking | Cannot handle online streaming arrivals without recomputation |
Production Python Implementation
from dataclasses import dataclass
@dataclass(frozen=True, slots=True)
class AssignmentResult:
assignment: list[int]
min_cost: float
def hungarian_assignment(cost_matrix: list[list[float]]) -> AssignmentResult:
"""Minimum-weight perfect matching on an n×n cost matrix (Kuhn–Munkres)."""
n = len(cost_matrix)
if n == 0 or any(len(row) != n for row in cost_matrix):
raise ValueError("Hungarian algorithm requires a non-empty square matrix.")
u = [0.0] * (n + 1)
v = [0.0] * (n + 1)
p = [0] * (n + 1)
way = [0] * (n + 1)
for i in range(1, n + 1):
p[0] = i
j0 = 0
minv = [float("inf")] * (n + 1)
used = [False] * (n + 1)
while True:
used[j0] = True
i0 = p[j0]
delta = float("inf")
j1 = 0
for j in range(1, n + 1):
if used[j]:
continue
cur = cost_matrix[i0 - 1][j - 1] - u[i0] - v[j]
if cur < minv[j]:
minv[j] = cur
way[j] = j0
if minv[j] < delta:
delta = minv[j]
j1 = j
for j in range(n + 1):
if used[j]:
u[p[j]] += delta
v[j] -= delta
else:
minv[j] -= delta
j0 = j1
if p[j0] == 0:
break
while True:
j1 = way[j0]
p[j0] = p[j1]
j0 = j1
if j0 == 0:
break
assignment = [-1] * n
min_cost = 0.0
for j in range(1, n + 1):
row = p[j] - 1
assignment[row] = j - 1
min_cost += cost_matrix[row][j - 1]
return AssignmentResult(assignment=assignment, min_cost=min_cost)
Real-World Production Use Cases
- Ride-Share Fleet Dispatch (Uber / Lyft): Batching drivers to ride requests every 5 seconds to minimize total system pickup wait time.
- Multi-Object Radar / LIDAR Tracking (Sensor Fusion): Associating bounding box detections across consecutive video frames in autonomous driving (SORT tracker).
- Semiconductor Chip Floorplanning: Assigning logic cells to circuit pads with minimal total wire length.
Part 2: High-Performance String & Pattern Matching
flowchart LR
subgraph stringStrategies ["String Matching Trade-offs"]
RK["Rabin-Karp<br/>Rolling hash<br/>Multi-pattern and fingerprinting"]
KMP["KMP<br/>Prefix-function DFA<br/>Streams and linear guarantees"]
HO["Horspool<br/>Right-to-left bad-char skip<br/>Natural text and logs"]
end
4. Rabin-Karp Algorithm (Rolling Hash Substring Search)
Core Intuition & Mathematical Invariant
Rabin-Karp (1987) uses polynomial rolling hashing to test substring equality in per shift. Instead of checking characters sequentially, it maintains a sliding hash window:
Mathematical Invariant: If , the substrings are guaranteed to differ. If the hashes match, a full character equality check is performed to eliminate hash collisions.
Big-O Complexity
| Metric | Best Case | Average Case | Worst Case | Space Complexity |
|---|---|---|---|---|
| Bound |
Pros & Cons
| Pros | Cons |
|---|---|
| sliding window update cost | Vulnerable to worst-case under hash collisions |
| Naturally extends to searching multiple patterns simultaneously () | Requires arbitrary-precision integers (native in Python) |
| Excellent for 2D matrix matching and document fingerprinting | Slower than Horspool for standard single-needle text search |
Production Python Implementation
def rabin_karp_search(
text: str,
pattern: str,
*,
base: int = 256,
modulus: int = 1_000_000_007,
) -> list[int]:
"""Return start indices of all pattern occurrences using Rabin–Karp rolling hash."""
n, m = len(text), len(pattern)
if m == 0 or m > n:
return []
high_pow = pow(base, m - 1, modulus)
pattern_hash = 0
window_hash = 0
for i in range(m):
pattern_hash = (pattern_hash * base + ord(pattern[i])) % modulus
window_hash = (window_hash * base + ord(text[i])) % modulus
matches: list[int] = []
for i in range(n - m + 1):
if pattern_hash == window_hash and text[i : i + m] == pattern:
matches.append(i)
if i < n - m:
window_hash = (
(window_hash - ord(text[i]) * high_pow) % modulus
)
window_hash = (window_hash * base + ord(text[i + m])) % modulus
return matches
Real-World Production Use Cases
- Deep packet inspection (DPI): Rolling-hash signature scan across network payloads at line rate.
- Plagiarism and duplicate detection: Hash document shingles for near-duplicate discovery.
- Bioinformatics k-mer indexing: Seed-and-extend alignment pipelines over DNA streams.
Mermaid Flowchart
flowchart TD
A["Precompute pattern hash and B^m mod M"] --> B["Compute initial window hash"]
B --> C{"i at most n minus m?"}
C -- No --> Z["Return verified hits"]
C -- Yes --> D{"Window hash equals pattern hash?"}
D -- Yes --> E["Verify characters at i"]
E --> F{"Exact match?"}
F -- Yes --> G["Record hit at i"]
F -- No --> H["Slide window"]
D -- No --> H
G --> H
H --> I["Rolling update hash"]
I --> J["Increment i"]
J --> C
5. Knuth-Morris-Pratt (KMP) Algorithm (Prefix Function DFA)
Core Intuition & Mathematical Invariant
KMP (1977) eliminates text backtracking by analyzing the pattern’s self-similarity. It constructs a Prefix Function (or LPS table: Longest Proper Prefix that is also a Suffix). When a mismatch occurs after matching characters, the text cursor never retreats; the pattern cursor falls back to .
Mathematical Invariant: .
Big-O Complexity
| Metric | Best Case | Average Case | Worst Case | Space Complexity |
|---|---|---|---|---|
| Bound |
Pros & Cons
| Pros | Cons |
|---|---|
| Strictly deterministic worst-case guarantee | Requires preprocessing and table storage |
| Zero text stream backtracking (supports unbounded network streams) | Higher constant factor than Horspool on English text |
| Immune to pathological adversarial input | Not easily generalized to multi-pattern search without Aho-Corasick |
Production Python Implementation
def build_kmp_prefix_table(pattern: str) -> list[int]:
"""Build KMP longest proper prefix / suffix (pi) table."""
m = len(pattern)
pi = [0] * m
length = 0
for i in range(1, m):
while length > 0 and pattern[i] != pattern[length]:
length = pi[length - 1]
if pattern[i] == pattern[length]:
length += 1
pi[i] = length
return pi
def kmp_search(text: str, pattern: str) -> list[int]:
"""Search for pattern in text with zero stream backtracking."""
n, m = len(text), len(pattern)
if m == 0:
return []
pi = build_kmp_prefix_table(pattern)
matches: list[int] = []
j = 0
for i in range(n):
while j > 0 and text[i] != pattern[j]:
j = pi[j - 1]
if text[i] == pattern[j]:
j += 1
if j == m:
matches.append(i - m + 1)
j = pi[j - 1]
return matches
Real-World Production Use Cases
- Intrusion detection on live TCP streams: Match signatures without rewinding the byte stream when packets fragment across boundaries.
- DNA / protein motif scanning: Long alphabets and repetitive motifs where Rabin–Karp collision rates would dominate verification cost.
- Compiler lexer backtracking avoidance: Single-pattern scans over generated source where worst-case linear time is contractual.
Mermaid Flowchart
flowchart TD
A["Build pi table from pattern"] --> B["i = 0, j = 0 over text"]
B --> C{"i less than n?"}
C -- No --> Z["Return match indices"]
C -- Yes --> D{"text at i equals pattern at j?"}
D -- Yes --> E["Increment j"]
E --> F{"j equals m?"}
F -- Yes --> G["Record match; j = pi of j minus 1"]
F -- No --> H["Increment i"]
G --> H
D -- No --> I{"j greater than 0?"}
I -- Yes --> J["j = pi of j minus 1"]
I -- No --> K["Increment i"]
J --> B
K --> B
H --> B
6. Horspool’s Algorithm (Boyer-Moore-Horspool Bad-Character Shift)
Core Intuition & Mathematical Invariant
Horspool (1980) is a practical simplification of the Boyer–Moore algorithm. It matches the pattern against the text from right to left, but on mismatch, it shifts the entire window based solely on the character in the text aligned with the last position of the pattern.
Mathematical Invariant: The bad-character shift table records the distance from the last occurrence of character in to the end of the pattern. Any smaller shift would cause an immediate mismatch.
Big-O Complexity
| Metric | Best Case | Average Case | Worst Case | Space Complexity |
|---|---|---|---|---|
| Bound |
Note: is alphabet size (256 for ASCII/byte streams).
Pros & Cons
| Pros | Cons |
|---|---|
| Sublinear average case: skips characters without reading them | Quadratic worst case on pathological periodic strings |
| Trivial preprocessing (single 256-element array) | Requires random access to text buffer (not stream-friendly) |
| Outperforms KMP on natural language, code, and log files | Omits the Good-Suffix rule from full Boyer-Moore |
Production Python Implementation
def build_horspool_table(pattern: str) -> list[int]:
"""Build Boyer–Moore–Horspool bad-character shift table for byte strings."""
m = len(pattern)
table = [m] * 256
for i in range(m - 1):
table[ord(pattern[i]) & 0xFF] = m - 1 - i
return table
def horspool_search(text: str, pattern: str) -> list[int]:
"""Find all pattern occurrences using Boyer–Moore–Horspool."""
n, m = len(text), len(pattern)
if m == 0 or m > n:
return []
shift = build_horspool_table(pattern)
matches: list[int] = []
i = m - 1
while i < n:
k = 0
while k < m and pattern[m - 1 - k] == text[i - k]:
k += 1
if k == m:
matches.append(i - m + 1)
i += shift[ord(text[i]) & 0xFF]
return matches
Real-World Production Use Cases
- Log grep and SIEM needle search: High skip rate on ASCII logs and stack traces in observability pipelines.
- IDE “find in files” over local buffers: Random-access file slices where backward character comparison is cheap.
- Binary protocol field scanning: Fixed-width tokens in network captures when average case dominates SLA.
Mermaid Flowchart
flowchart TD
A["Build bad-character shift table"] --> B["Align pattern end at index i"]
B --> C{"i less than n?"}
C -- No --> Z["Return matches"]
C -- Yes --> D["Compare pattern right-to-left"]
D --> E{"Full match?"}
E -- Yes --> F["Record start index"]
E -- No --> G["Shift by bad-char table at text at i"]
F --> G
G --> H["Advance i by shift amount"]
H --> B
Part 3: Discrete & Combinatorial Optimization
7. 0/1 Knapsack Algorithm (Dynamic Programming with Space Optimization)
Core Intuition & Mathematical Invariant
Given items with weights and values , and a maximum capacity , select a subset to maximize total value without exceeding .
Recurrence Relation:
Invariant: Iterating backwards from to guarantees each item is included at most once ( constraint) while compressing 2D state into a single 1D array .
Big-O Complexity
| Metric | Best Case | Average Case | Worst Case | Space Complexity |
|---|---|---|---|---|
| Bound |
Note: Pseudo-polynomial time complexity dependent on numeric capacity .
Pros & Cons
| Pros | Cons |
|---|---|
| Exact optimal subset for 0/1 constraints | Pseudo-polynomial: unusable when capacity is huge |
| Space-optimized 1D DP with reconstruction | No polynomial-time guarantee for general NP-hard knapsack family |
| Transparent audit trail via chosen item IDs | Greedy fractional knapsack is faster but wrong for indivisible items |
Production Python Implementation
from dataclasses import dataclass
@dataclass(frozen=True, slots=True)
class KnapsackItem:
id: str
weight: int
value: float
@dataclass(frozen=True, slots=True)
class KnapsackResult:
max_value: float
selected_items: list[str]
def solve_01_knapsack(items: list[KnapsackItem], capacity: int) -> KnapsackResult:
"""0/1 knapsack with space-optimized DP and exact item reconstruction."""
if capacity <= 0 or not items:
return KnapsackResult(max_value=0.0, selected_items=[])
dp = [0.0] * (capacity + 1)
choice = [[0] * (capacity + 1) for _ in items]
for i, item in enumerate(items):
if item.weight > capacity:
continue
for w in range(capacity, item.weight - 1, -1):
with_item = dp[w - item.weight] + item.value
if with_item > dp[w]:
dp[w] = with_item
choice[i][w] = 1
selected: list[str] = []
remaining = capacity
for i in range(len(items) - 1, -1, -1):
if choice[i][remaining]:
selected.append(items[i].id)
remaining -= items[i].weight
return KnapsackResult(max_value=dp[capacity], selected_items=list(reversed(selected)))
Real-World Production Use Cases
- Cloud cost packing: Select feature bundles under CPU/RAM quotas for batch jobs or spot-instance fleets.
- Ad budget allocation: Discrete campaigns with fixed spend caps and predicted ROI scores.
- Cargo loading / fleet routing prep: Choose shipments under weight limits before solving continuous routing.
Mermaid Flowchart
flowchart TD
A["For each item i"] --> B["For w from W down to weight_i"]
B --> C{"dp at w minus weight plus value greater than dp at w?"}
C -- Yes --> D["Take item: update dp at w; mark choice"]
C -- No --> E["Skip at capacity w"]
D --> B
E --> B
B --> F["Next item or reconstruct from choice table"]
8. Ant Colony Optimization (ACO Metaheuristic)
Core Intuition & Mathematical Invariant
ACO (Dorigo 1992) is a bio-inspired population metaheuristic for NP-hard combinatorial problems (such as the Traveling Salesperson Problem). Artificial ants build solutions by stepping across graph edges probabilistically, influenced by:
- Pheromone trail intensity (): Memory of past high-quality solutions.
- Heuristic visibility (): Local greediness.
Transition Probability:
Pheromone Evaporation & Reinforcement:
Big-O Complexity
| Metric | Typical Behavior | Space |
|---|---|---|
| Per iteration | ants × cities for TSP-like construction | pheromone matrix |
| Convergence | No worst-case polynomial guarantee; anytime improving | Tunable via , , |
Pros & Cons
| Pros | Cons |
|---|---|
| Handles noisy, dynamic graphs without closed-form objective | No optimality certificate; parameter tuning is workload-specific |
| Naturally parallelizable ant constructions | Can stagnate on local optima without diversification |
| Strong on routing / scheduling when exact ILP is too slow | Slower than specialized solvers once problem structure is known |
flowchart TD
Init["Initialize pheromones and visibility"] --> StepLoop{"Iteration below max?"}
StepLoop -- No --> Done(["Return best tour found"])
StepLoop -- Yes --> AntConstruct["Ants construct tours via roulette selection"]
AntConstruct --> EvalTours["Evaluate tour costs L_k"]
EvalTours --> UpdateBest["Update global best tour"]
UpdateBest --> Evaporation["Evaporate pheromones: tau *= 1 - rho"]
Evaporation --> Deposit["Deposit reinforcement pheromones: delta tau = Q / L_k"]
Deposit --> StepLoop
Real-World Production Use Cases
- Last-mile delivery route shaping: Warm-start OR-Tools VRP with ACO tours on nightly graphs.
- Network design what-if: Explore fiber / WAN layouts when edge costs shift frequently.
- Factory floor scheduling: Sequence machine visits when exact CP-SAT timeouts at production scale.
Production Python Implementation (Illustrative ACO Step)
import random
def construct_ant_tour(
dist: list[list[float]],
pheromone: list[list[float]],
alpha: float,
beta: float,
rng: random.Random | None = None,
) -> list[int]:
"""One ant tour via roulette edge selection (symmetric TSP-style)."""
rng = rng or random.Random()
n = len(dist)
visited = [False] * n
tour = [0]
visited[0] = True
for _ in range(1, n):
from_city = tour[-1]
weights: list[tuple[int, float]] = []
total = 0.0
for to_city in range(n):
if visited[to_city]:
continue
tau = pheromone[from_city][to_city] ** alpha
eta = (1.0 / max(dist[from_city][to_city], 1e-9)) ** beta
weight = tau * eta
weights.append((to_city, weight))
total += weight
pick = rng.random() * total
for to_city, weight in weights:
pick -= weight
if pick <= 0:
tour.append(to_city)
visited[to_city] = True
break
return tour
Part 4: Advanced Graph & Network Flow Algorithms
9. Floyd-Warshall Algorithm (All-Pairs Shortest Paths)
Core Intuition & Mathematical Invariant
Floyd-Warshall (1962) computes shortest paths between all pairs of vertices in a weighted directed graph (allowing negative edges, provided no negative cycles exist).
Recurrence Formulation:
Inductive Invariant: After phase , holds the shortest path from to that uses only intermediate vertices from .
Big-O Complexity
| Metric | Best Case | Average Case | Worst Case | Space Complexity |
|---|---|---|---|---|
| Bound |
Pros & Cons
| Pros | Cons |
|---|---|
| Handles negative edges (no negative cycles) in one unified pass | even on sparse graphs |
| Simple triple loop — easy to vectorize / GPU batch for small | memory for dense distance matrix |
| Detects negative cycles via diagonal after closure | Poor fit when only one source–sink pair is needed |
Production Python Implementation
from dataclasses import dataclass
from math import inf
from typing import Callable
@dataclass(frozen=True, slots=True)
class AllPairsResult:
distance_matrix: list[list[float]]
has_negative_cycle: bool
reconstruct_path: Callable[[int, int], list[int] | None]
def floyd_warshall(
vertex_count: int,
edges: list[tuple[int, int, float]],
) -> AllPairsResult:
"""All-pairs shortest paths with path reconstruction pointers."""
n = vertex_count
dist = [[0.0 if i == j else inf for j in range(n)] for i in range(n)]
nxt = [[None if i != j else j for j in range(n)] for i in range(n)]
for u, v, weight in edges:
if weight < dist[u][v]:
dist[u][v] = weight
nxt[u][v] = v
for k in range(n):
for i in range(n):
if dist[i][k] == inf:
continue
for j in range(n):
if dist[k][j] == inf:
continue
candidate = dist[i][k] + dist[k][j]
if candidate < dist[i][j]:
dist[i][j] = candidate
nxt[i][j] = nxt[i][k]
has_negative_cycle = any(dist[i][i] < 0 for i in range(n))
def reconstruct_path(u: int, v: int) -> list[int] | None:
if has_negative_cycle or dist[u][v] == inf:
return None
path = [u]
current = u
while current != v:
step = nxt[current][v]
if step is None:
return None
current = step
path.append(current)
return path
return AllPairsResult(
distance_matrix=dist,
has_negative_cycle=has_negative_cycle,
reconstruct_path=reconstruct_path,
)
Real-World Production Use Cases
- Network POP / AS transit matrices: Precompute all-pairs latency for dense backbone graphs ().
- Game AI influence maps: Tile-grid reachability with teleporters when is modest.
- Policy validation: Detect negative cycles before publishing weighted routing tables.
Mermaid Flowchart
flowchart TD
A["Init dist and next from edges"] --> B["For each intermediate k"]
B --> C["Relax all pairs i,j through k"]
C --> D{"Improved distance?"}
D -- Yes --> E["Update dist and next pointer"]
D -- No --> F["Continue"]
E --> F
F --> G{"All k processed?"}
G -- No --> B
G -- Yes --> H["Check diagonal for negative cycles"]
10. Push-Relabel Algorithm (Goldberg-Tarjan Maximum Network Flow)
Core Intuition & Mathematical Invariant
Push-Relabel (Goldberg & Tarjan 1988) fundamentally diverges from Ford-Fulkerson and Edmonds-Karp by abandoning whole-path augmentations. Instead, it works locally via a preflow:
- Vertices maintain an excess balance: .
- Vertices maintain a height / distance label .
- Push Operation: Flow is pushed downhill along residual edges where .
- Relabel Operation: If vertex has excess but no downhill residual neighbor, its height is raised: .
Mathematical Invariant: Height validity: for all residual edges . Flow conservation is maintained everywhere once excess is drained back to source or sink.
stateDiagram-v2
[*] --> SaturateSourceEdges: Push capacity from source at height V
SaturateSourceEdges --> ActiveVerticesQueue: Excess at intermediate nodes
ActiveVerticesQueue --> PushFlow: Neighbor height equals h(u) minus 1 and residual positive
PushFlow --> ActiveVerticesQueue: Excess updated
ActiveVerticesQueue --> RelabelHeight: No downhill neighbor available
RelabelHeight --> ActiveVerticesQueue: Height raised to 1 plus min neighbor height
ActiveVerticesQueue --> [*]: No active excess remaining
Big-O Complexity
| Metric | Generic Push-Relabel | FIFO Active Selection | Highest-Label Heuristic |
|---|---|---|---|
| Worst-Case Time | |||
| Space |
Pros & Cons
| Pros | Cons |
|---|---|
| Excellent practical throughput on dense capitated networks | Height/excess bookkeeping is harder to debug than augmenting paths |
| Local operations map well to parallel GPU preflow variants | Worst-case bounds depend heavily on vertex selection heuristic |
| Foundation for min-cost flow and bipartite matching reductions | Edmonds–Karp or Dinic can be simpler for small teaching examples |
Real-World Production Use Cases
- Data-center egress shaping: Max-flow view of multi-path link capacities between regions.
- Ad allocation / b-matching: Capacitated supply to demand nodes in ad-tech graphs.
- OpenCV-style graph cuts: Vision pipelines reduce segmentation to s–t min-cut / max-flow.
Production Python Implementation (Preflow Push Sketch)
def push_flow(
u: int,
v: int,
excess: list[float],
capacity: list[list[float]],
flow: list[list[float]],
height: list[int],
) -> None:
"""Push excess along admissible residual edge (height[u] == height[v] + 1)."""
residual = capacity[u][v] - flow[u][v]
if residual <= 0 or height[u] != height[v] + 1:
return
delta = min(excess[u], residual)
flow[u][v] += delta
flow[v][u] -= delta
excess[u] -= delta
excess[v] += delta
11. Bron-Kerbosch Algorithm (Maximal Clique Enumeration)
Core Intuition & Mathematical Invariant
A clique is a complete subgraph where every pair of vertices is connected by an edge. Bron-Kerbosch (1973) is a recursive backtracking algorithm that enumerates all maximal cliques (cliques that cannot be extended) using three disjoint vertex sets:
- : Current clique under exploration.
- : Candidate vertices that can expand .
- : Already processed vertices (exclusion set to prevent duplicate reporting).
Pivoting Optimization (Tomita et al.): Select pivot . Iterate only over candidates not connected to the pivot: . This prunes redundant search branches.
Big-O Complexity
| Metric | Moon-Moser Theoretical Bound | Space Complexity |
|---|---|---|
| Worst-Case Time | stack depth |
Pros & Cons
| Pros | Cons |
|---|---|
| Enumerates all maximal cliques exactly | Exponential in worst-case dense graphs |
| Pivot rule (Tomita) cuts branches in practice | Memory blow-up if output clique count is huge |
| Natural fit for fraud-ring / community detection forensics | Approximation required at web scale |
Real-World Production Use Cases
- Financial fraud rings: Find fully connected account clusters in transaction graphs.
- Social graph moderation: Maximal cliques in mutual-follow subgraphs for bot detection.
- Bioinformatics: Protein interaction complexes as near-cliques (with noise filtering upstream).
Mermaid Flowchart
flowchart TD
A["Bron-Kerbosch with sets R, P, X"] --> B{"P and X empty?"}
B -- Yes --> C["Report maximal clique R"]
B -- No --> D["Choose pivot u in P union X"]
D --> E["For v in P minus neighbors of u"]
E --> F["Recurse with R plus v, P intersect N(v), X intersect N(v)"]
F --> G["Move v from P to X"]
G --> E
C --> H["Return"]
Production Python Implementation
def bron_kerbosch(
adj: list[set[int]],
r: list[int] | None = None,
p: list[int] | None = None,
x: list[int] | None = None,
out: list[list[int]] | None = None,
) -> list[list[int]]:
"""Enumerate maximal cliques with Bron–Kerbosch (Tomita pivot)."""
r = r or []
p = p if p is not None else list(range(len(adj)))
x = x or []
out = out if out is not None else []
if not p and not x:
out.append(r.copy())
return out
pivot = p[0] if p else x[0]
pivot_neighbors = adj[pivot]
for v in [node for node in p if node not in pivot_neighbors]:
neighbors = adj[v]
bron_kerbosch(
adj,
r + [v],
[node for node in p if node in neighbors],
[node for node in x if node in neighbors],
out,
)
p = [node for node in p if node != v]
x = x + [v]
return out
12. A* Search Algorithm (Heuristic Pathfinding)
Core Intuition & Mathematical Invariant
A* (Hart, Nilsson, Raphael 1968) guides Dijkstra’s uniform-cost search toward the target using an admissible heuristic:
- : Exact path cost from start to .
- : Estimated cost from to goal.
Admissibility & Consistency:
- Admissible: (never overestimates the true remaining cost). Guarantees optimal path.
- Consistent (Monotone): . Guarantees that the first time a node is popped from the Open Set, its is already optimal (no closed-set re-expansion).
Big-O Complexity
| Metric | With Consistent Heuristic | With Admissible Only | Space |
|---|---|---|---|
| Time | with binary heap | More re-expansions possible | open+closed |
| Optimality | Guaranteed if admissible | Same | — |
Pros & Cons
| Pros | Cons |
|---|---|
| Optimal paths when is admissible | Requires good heuristic design per domain |
| Expands far fewer nodes than Dijkstra toward a single goal | Memory for open set on huge grids |
| Industry default for games, robotics, maps | Weighted A* / inconsistent trades optimality for speed |
Real-World Production Use Cases
- Game navmeshes and NPC pathing: Manhattan / Euclidean heuristics on tile grids.
- Warehouse AMR routing: Time-dependent edge costs with static obstacle maps.
- Map APIs (walking/driving): Goal-directed search on contraction hierarchies’ local layers.
Mermaid Flowchart
flowchart TD
A["Push start with f = g + h"] --> B{"Open set empty?"}
B -- Yes --> Z["No path"]
B -- No --> C["Pop min-f node n"]
C --> D{"n equals goal?"}
D -- Yes --> Y["Reconstruct path"]
D -- No --> E["For each neighbor v"]
E --> F["Compute tentative g"]
F --> G{"Improve g score?"}
G -- Yes --> H["Set parent; push f = g + h"]
G -- No --> E
H --> B
Production Python Implementation
from heapq import heappop, heappush
from math import inf
from typing import Callable
def a_star(
neighbors: Callable[[int], list[tuple[int, float]]],
h: Callable[[int], float],
start: int,
goal: int,
) -> list[int] | None:
"""A* shortest path with admissible heuristic h."""
open_set: list[tuple[float, int]] = [(h(start), start)]
g_score = {start: 0.0}
came_from: dict[int, int] = {}
while open_set:
_, current = heappop(open_set)
if current == goal:
path = [goal]
node = goal
while node != start:
node = came_from[node]
path.append(node)
return list(reversed(path))
for nxt, cost in neighbors(current):
tentative = g_score[current] + cost
if tentative < g_score.get(nxt, inf):
came_from[nxt] = current
g_score[nxt] = tentative
heappush(open_set, (tentative + h(nxt), nxt))
return None
Part 5: Systems, Memory & Caching Algorithms
13. Adaptive Replacement Cache (ARC)
Core Intuition & Mathematical Invariant
Invented by Nimrod Megiddo and Dharmendra S. Modha (IBM Almaden, 2003), ARC solves the classic caching dilemma between Recency (LRU) and Frequency (LFU).
Pure LRU is vulnerable to single-pass sequential scans that wipe out the working set. Pure LFU suffers from cache pollution when historic items accumulate high counts and never evict.
ARC maintains four separate doubly-linked lists and a dynamic adaptation parameter :
- : Cache entries accessed once recently (Recency).
- : Cache entries accessed at least twice (Frequency).
- : Ghost list tracking keys recently evicted from (Metadata only, no payload).
- : Ghost list tracking keys recently evicted from (Metadata only, no payload).
flowchart LR
subgraph residentCache ["Physical Memory Cache (total size C)"]
T1["T1: Recent hits (size at most p)"]
T2["T2: Frequent hits (size at most C minus p)"]
end
subgraph ghostHistory ["Ghost metadata history (size at most C)"]
B1["B1: Ghost recency keys"]
B2["B2: Ghost frequency keys"]
end
Miss["Page miss"] --> T1
T1 -- "2nd hit" --> T2
T1 -- Eviction --> B1
T2 -- Eviction --> B2
B1 -- "Ghost hit: increase p" --> T2
B2 -- "Ghost hit: decrease p" --> T2
The Adaptation Rule:
- If a cache miss hits ghost list , it means the cache was penalized for lack of recency capacity. ARC increases :
- If a cache miss hits ghost list , ARC decreases to expand frequency capacity:
Big-O Complexity
| Operation | Time Complexity | Space Overhead |
|---|---|---|
get(key) | amortized | metadata |
set(key, val) | amortized | key tracker memory |
Pros & Cons
| Pros | Cons |
|---|---|
| Adapts between scan-heavy and hot-set workloads without manual tuning | ~2× metadata vs plain LRU (ghost lists) |
| Strong production pedigree (ZFS ARC, storage engines) | W-TinyLFU (Caffeine) often wins on skewed web caches in benchmarks |
| get/set with explicit recency/frequency semantics | Implementations differ on edge cases; test against your access pattern |
Production Python Implementation
from collections import OrderedDict
from typing import Callable, Generic, TypeVar
K = TypeVar("K")
V = TypeVar("V")
class AdaptiveReplacementCache(Generic[K, V]):
"""Adaptive Replacement Cache (Megiddo & Modha)."""
def __init__(
self,
capacity: int,
key_serializer: Callable[[K], str] | None = None,
) -> None:
self.c = max(1, capacity)
self.p = 0
self._serialize = key_serializer or str
self.t1: OrderedDict[str, tuple[K, V]] = OrderedDict()
self.t2: OrderedDict[str, tuple[K, V]] = OrderedDict()
self.b1: OrderedDict[str, None] = OrderedDict()
self.b2: OrderedDict[str, None] = OrderedDict()
def get(self, key: K) -> V | None:
k_str = self._serialize(key)
if k_str in self.t1:
k_obj, value = self.t1.pop(k_str)
self.t2[k_str] = (k_obj, value)
return value
if k_str in self.t2:
k_obj, value = self.t2.pop(k_str)
self.t2[k_str] = (k_obj, value)
return value
return None
def set(self, key: K, value: V) -> None:
k_str = self._serialize(key)
if k_str in self.t1:
self.t1.pop(k_str)
self.t2[k_str] = (key, value)
return
if k_str in self.t2:
self.t2.pop(k_str)
self.t2[k_str] = (key, value)
return
if k_str in self.b1:
delta = 1 if len(self.b1) >= len(self.b2) else len(self.b2) // max(len(self.b1), 1)
self.p = min(self.c, self.p + delta)
self._replace(k_str)
self.b1.pop(k_str, None)
self.t2[k_str] = (key, value)
return
if k_str in self.b2:
delta = 1 if len(self.b2) >= len(self.b1) else len(self.b1) // max(len(self.b2), 1)
self.p = max(0, self.p - delta)
self._replace(k_str)
self.b2.pop(k_str, None)
self.t2[k_str] = (key, value)
return
total_resident = len(self.t1) + len(self.t2)
total_tracking = total_resident + len(self.b1) + len(self.b2)
if len(self.t1) + len(self.b1) == self.c:
if len(self.t1) < self.c:
self._delete_oldest(self.b1)
self._replace(k_str)
else:
self._delete_oldest(self.t1)
elif total_tracking >= self.c:
if total_tracking == 2 * self.c:
self._delete_oldest(self.b2)
self._replace(k_str)
self.t1[k_str] = (key, value)
def _replace(self, incoming_key: str) -> None:
if self.t1 and (
len(self.t1) > self.p
or (incoming_key in self.b2 and len(self.t1) == self.p)
):
evicted = self._delete_oldest(self.t1)
if evicted is not None:
self.b1[evicted] = None
else:
evicted = self._delete_oldest(self.t2)
if evicted is not None:
self.b2[evicted] = None
@staticmethod
def _delete_oldest(mapping: OrderedDict) -> str | None:
if not mapping:
return None
key, _ = mapping.popitem(last=False)
return key
Real-World Production Use Cases
- Database buffer pools: Protect hot index pages from one-off sequential table scans.
- CDN / object metadata caches: Mixed read patterns on versioned keys.
- In-process service caches: When traffic alternates between batch and interactive modes.
Part 6: Modern Algorithmic Frontiers: Probabilistic Sketches & Vector Search
14. HyperLogLog (HLL Cardinality Estimation)
Core Intuition & Mathematical Invariant
HyperLogLog (Flajolet et al. 2007) solves the problem of counting billions of distinct items in a high-throughput stream using only 1.5 KB of RAM.
- Hashes elements to a 64-bit uniform space.
- Uses the first bits to select one of register buckets.
- Uses the remaining bits to observe the number of leading zeros ().
- Invariant: The position of the first 1-bit acts as an exponential probability estimator ().
- Combines all registers using a harmonic mean to eliminate outlier variance:
Standard error is tightly bounded: . For (), error is .
flowchart TD
Item["Stream item x"] --> Hash["64-bit uniform hash"]
Hash --> Split["Split bits: bucket index b and value suffix w"]
Split --> CountZeros["Compute rho = leading zeros plus 1"]
CountZeros --> UpdateRegister{"Rho greater than register M_b?"}
UpdateRegister -- Yes --> Write["Set M_b = rho"]
UpdateRegister -- No --> Ignore["No-op"]
Write --> HarmonicMean["On read: harmonic mean of all M registers"]
Big-O Complexity
| Operation | Time | Space | Error |
|---|---|---|---|
| Add | registers | — | |
| Estimate | ~1.5 KB at | SE |
Pros & Cons
| Pros | Cons |
|---|---|
| Mergeable across shards (union of registers) | Approximate cardinality only |
| Constant memory per sketch | Hash quality dominates accuracy |
| Standard in Redis / analytics stacks | Not for compliance-grade exact counts |
Real-World Production Use Cases
- Unique visitors / DAU: Merge per-shard sketches at query time.
- Security telemetry: Distinct IPs or fingerprints under high ingest.
- Experiment exposure: Cheap distinct assignment tracking.
Production Python Implementation
ALPHA_14 = 0.7213475204444817
class HyperLogLog:
"""HyperLogLog cardinality sketch (precision b => m = 2**b registers)."""
def __init__(self, precision: int = 14) -> None:
self.m = 1 << precision
self.precision = precision
self.registers = bytearray(self.m)
def add(self, h64: int) -> None:
idx = (h64 >> (64 - self.precision)) & (self.m - 1)
w = ((h64 << self.precision) | 1) & ((1 << 64) - 1)
rho = 1
while rho < 64 and ((w >> (63 - rho)) & 1) == 0:
rho += 1
if rho > self.registers[idx]:
self.registers[idx] = rho
def estimate(self) -> float:
inv_sum = sum(2.0 ** (-reg) for reg in self.registers)
return ALPHA_14 * self.m * self.m / inv_sum
15. HNSW: Hierarchical Navigable Small World (Vector Similarity Search)
Core Intuition & Mathematical Invariant
HNSW (Malkov & Yashunin 2018) is the industry standard indexing structure for vector embeddings powering AI Retrieval-Augmented Generation (RAG) and semantic search (Qdrant, Milvus, Weaviate).
It creates a multi-layer geometric skip-list graph:
- Top layers contain sparse vertices with long-range edges for high-speed routing ( hops).
- Bottom Layer 0 contains the dense proximity graph with all indexed vectors.
- Query traversal executes greedy search at the top layer, descends upon reaching local minima, and switches to beam search (
efSearch) on Layer 0.
flowchart TD
subgraph layer2 ["Layer 2: sparse long-range highway"]
L2_A(("Node A")) --- L2_B(("Node B"))
end
subgraph layer1 ["Layer 1: medium skip connections"]
L1_A(("Node A")) --- L1_C(("Node C")) --- L1_B(("Node B"))
end
subgraph layer0 ["Layer 0: dense full proximity graph"]
L0_A(("Node A")) --- L0_D(("Node D")) --- L0_C(("Node C")) --- L0_E(("Node E")) --- L0_B(("Node B"))
end
Query["Query vector Q"] --> L2_A
L2_A -->|"Greedy hop"| L2_B
L2_B -->|Descend| L1_B
L1_B -->|"Greedy hop"| L1_C
L1_C -->|"Descend to layer 0"| L0_C
L0_C -->|"Beam search efSearch"| TopK["Return top-K nearest neighbors"]
Big-O Complexity
| Operation | Average Case | Space Complexity | Real-World Scale |
|---|---|---|---|
| Query (-NN) | Billions of embeddings at p99 | ||
| Insert | Real-time incremental ingestion |
Pros & Cons
| Pros | Cons |
|---|---|
| Best-in-class ANN recall/latency on CPU for RAG | RAM-heavy (vectors + graph edges) |
| Supports incremental inserts | Requires tuning M, efConstruction, efSearch |
| Default in many vector databases | Approximate; exact k-NN tier may still be required |
Real-World Production Use Cases
- RAG retrieval: Top- embedded chunks for LLM context windows.
- Media similarity: Near-duplicate detection in image/audio catalogs.
- Risk / fraud embeddings: Similarity search over transaction or device vectors.
Production Python Implementation (Greedy Layer Sketch)
import math
from typing import Sequence
def cosine(a: Sequence[float], b: Sequence[float]) -> float:
dot = sum(x * y for x, y in zip(a, b))
na = math.sqrt(sum(x * x for x in a))
nb = math.sqrt(sum(x * x for x in b))
return dot / (na * nb + 1e-12)
def greedy_layer_search(
query: Sequence[float],
entry_id: int,
vectors: Sequence[Sequence[float]],
neighbors: Sequence[Sequence[int]],
) -> int:
"""Greedy search on one HNSW layer; production code stacks layers + efSearch on L0."""
best = entry_id
improved = True
while improved:
improved = False
for cand in neighbors[best]:
if cosine(query, vectors[cand]) > cosine(query, vectors[best]):
best = cand
improved = True
return best
Part 7: Meta-Synthesis: What Are All These Algorithms Really Teaching Us?
When you zoom out from individual algorithms, they collapse into six universal architectural primitives:
flowchart TB
subgraph primitives ["The 6 algorithmic primitives"]
P1["1. Amortization vs worst-case guarantees"]
P2["2. Dynamic programming and optimal substructure"]
P3["3. Dual bounds and relaxation"]
P4["4. Goal-directed heuristics vs exact search"]
P5["5. Probabilistic sketching vs exact state"]
P6["6. Online self-tuning and adaptive feedback"]
end
P1 --> Systems["High-performance systems design"]
P2 --> Systems
P3 --> Systems
P4 --> Systems
P5 --> Systems
P6 --> Systems
The 6 Core Primitives
- Amortization vs Worst-Case Guarantees: KMP and Push-Relabel protect strict p99 latency SLOs. Horspool and HNSW prioritize high average throughput.
- Dynamic Programming & Optimal Substructure: Solve overlapping subproblems once and compress state (0/1 Knapsack backward iteration, Floyd-Warshall intermediate closure).
- Dual Bounds & Relaxation: Maintain bounding invariants (Hungarian dual prices, Push-Relabel heights) to prune search spaces without sacrificing optimality proofs.
- Goal-Directed Heuristics vs Exact Search: Admissible heuristics (A*) preserve exact optimality while drastically cutting exploration; metaheuristics (ACO) trade proofs for tractability on NP-hard landscapes.
- Probabilistic Sketching vs Exact Storage: Trade negligible bounded error for orders-of-magnitude memory savings (HyperLogLog, rolling hashes).
- Online Self-Tuning & Feedback Loops: Structure systems to adapt automatically to non-stationary production traffic (ARC cache, ACO pheromone decay).
Algorithm Quick-Selection Matrix
| Problem Domain | Typical Inputs | Recommended Algorithm | Fallback / Alternative | Key Trade-off |
|---|---|---|---|---|
| Stable Two-Sided Matching | Preference rankings, two agent groups | Gale–Shapley | Integer Programming / Auction | Stability guarantee vs global social utility |
| Bipartite Min-Cost Assignment | cost matrix | Hungarian (Kuhn–Munkres) | Successive Shortest Path / Auction | exact optimality vs scaling limits |
| Multi-Pattern / Stream Fingerprint | Stream bytes, rolling window | Rabin–Karp | Aho–Corasick Automaton | rolling hash update vs hash collision verify |
| Single-Pattern Streaming Search | Single needle, unbounded stream | Knuth–Morris–Pratt | Suffix Automaton / Z-Algorithm | Strict worst-case vs Horspool average speed |
| Log Ingestion / Grep Needle Search | Natural text, log files | Horspool (BM-Horspool) | Full Boyer–Moore / Vectorized memchr | skip speed vs pathological worst-case |
| Capacity-Bounded Discrete Choice | Weights, values, capacity | 0/1 Knapsack DP (1D) | Branch and Bound / Greedy Fractional | Exact optimal allocation vs pseudo-polynomial |
| NP-Hard Routing / Scheduling | Combinatorial graph, TSP-like | Ant Colony Optimization | OR-Tools CP-SAT / Simulated Annealing | Anytime improving routes vs lack of optimality proof |
| All-Pairs Transit Matrix (Dense) | Network POP latency matrix | Floyd–Warshall | Dijkstra / Johnson’s Algorithm | Triple loop simplicity & cache locality vs |
| Maximum Flow / Min-Cut Routing | Capacitated directed graph | Push–Relabel (Goldberg–Tarjan) | Dinic’s Algorithm | High throughput on dense graphs vs complex height labels |
| Fraud Ring & Community Mining | Undirected graph | Bron–Kerbosch (with Pivot) | Approximate Clique Heuristics | Exact maximal enumeration vs exponential graph density |
| Navmesh & Robot Pathfinding | Graph + spatial coordinates | A* Search | Jump Point Search (JPS) / Contraction Hierarchies | Fast heuristic convergence vs admissible proof |
| Non-Stationary Memory Caching | Unknown mix of scan vs hot keys | Adaptive Replacement Cache (ARC) | W-TinyLFU (Caffeine) / 2Q | Automated adaptation vs ghost list memory |
| High-Scale Cardinality Counter | High-volume event stream | HyperLogLog (HLL) | Count-Min Sketch / Exact Set | standard error at 1.5 KB vs loss of exactness |
| Vector Embedding Similarity Search | High-dimensional float vectors | HNSW | IVF-PQ (Faiss) / ScaNN | Sub-millisecond ANN search vs RAM index footprint |
Sources
Matching & Assignment
- Gale, D., & Shapley, L. S. (1962). College Admissions and the Stability of Marriage. American Mathematical Monthly, 69(1), 9–15.
- Kuhn, H. W. (1955). The Hungarian Method for the Assignment Problem. Naval Research Logistics Quarterly, 2(1–2), 83–97.
- Munkres, J. (1957). Algorithms for the Assignment and Transportation Problems. Journal of the Society for Industrial and Applied Mathematics, 5(1), 32–38.
String & Search Algorithms
- Karp, R. M., & Rabin, M. O. (1987). Efficient Randomized Pattern-Matching Algorithms. IBM Journal of Research and Development, 31(2), 249–260.
- Knuth, D. E., Morris, J. H., & Pratt, V. R. (1977). Fast Pattern Matching in Strings. SIAM Journal on Computing, 6(2), 323–350.
- Horspool, R. N. (1980). Practical Fast Searching in Strings. Software: Practice and Experience, 10(6), 501–506.
Optimization & Graph Algorithms
- Bellman, R. (1957). Dynamic Programming. Princeton University Press.
- Dorigo, M., Maniezzo, V., & Colorni, A. (1996). Ant System: Optimization by a Colony of Cooperating Agents. IEEE Transactions on Systems, Man, and Cybernetics, Part B, 26(1), 29–41.
- Floyd, R. W. (1962). Algorithm 97: Shortest Path. Communications of the ACM, 5(6), 345.
- Goldberg, A. V., & Tarjan, R. E. (1988). A New Approach to the Maximum-Flow Problem. Journal of the ACM, 35(4), 921–940.
- Bron, C., & Kerbosch, J. (1973). Algorithm 457: Finding All Cliques of an Undirected Graph. Communications of the ACM, 16(9), 575–577.
- Hart, P. E., Nilsson, N. J., & Raphael, B. (1968). A Formal Basis for the Heuristic Determination of Minimum Cost Paths. IEEE Transactions on Systems Science and Cybernetics, 4(2), 100–107.
Systems, Sketches & Vector Search
- Megiddo, N., & Modha, D. S. (2003). ARC: A Self-Tuning, Low Overhead Replacement Cache. Proceedings of the 2nd USENIX Conference on File and Storage Technologies (FAST ‘03).
- Flajolet, P., Fusy, É., Gandouet, O., & Meunier, F. (2007). HyperLogLog: The Analysis of a Near-Optimal Cardinality Estimation Algorithm. AOFA ‘07.
- Malkov, Y. A., & Yashunin, D. A. (2018). Efficient and Robust Approximate Nearest Neighbor Search Using Hierarchical Navigable Small World Graphs. IEEE Transactions on Pattern Analysis and Machine Intelligence, 42(4), 824–836.