Algorithms, Data Structures, and Computational Complexity (Big-O & P vs NP)
Algorithms and data structures form the mathematical core of computer science—governing whether a computation over ten million records completes in two milliseconds or runs longer than the age of the universe. Faster hardware never rescues an accidental quadratic O(n²) or exponential O(2ⁿ) bottleneck at scale; selecting the right asymptotic algorithm and memory-layout data structure routinely delivers thousand-fold speedups in databases, compilers, search engines, and AI systems.
Key Highlights
- Big-O notation characterizes worst-case or amortized growth of operations and memory as input size n scales, abstracting away constant clock-speed factors.
- Hash tables provide O(1) expected key lookup; B+ Trees optimize read-heavy disk/SSD range queries; Log-Structured Merge (LSM) Trees optimize write-intensive analytical and event stores.
- Graph search (BFS, Dijkstra, A*), Divide-and-Conquer, Greedy matroid algorithms, and Dynamic Programming solve structured combinatorial problems in polynomial time.
- Recognizing NP-hard problems (Traveling Salesperson, Integer Scheduling, Boolean 3-SAT) prevents engineers from wasting weeks chasing impossible exact fast algorithms—steering straight to MILP/SAT solvers or bounded approximations.
Core Data Structures Driving Databases and Systems Software
Textbook pointer-heavy binary search trees perform poorly on modern CPUs and SSDs because every comparison chases a random memory address. Production systems use open-addressed SIMD Hash Tables (such as Abseil SwissTables or Rust `hashbrown`) for O(1) in-memory point lookups, wide page-aligned B+ Trees (branching factor 100–500) in PostgreSQL and SQLite so a billion rows require only 3 or 4 disk page reads, and append-only Log-Structured Merge (LSM) Trees paired with probabilistic Bloom Filters in RocksDB, Cassandra, and ClickHouse for blazing sequential write throughput.
At web scale where exact sets overflow RAM, sub-linear probabilistic sketches—HyperLogLog counting billions of unique visitors in 12 kilobytes at ~0.8% error—trade tiny bounded tolerances for massive memory savings.
| Data Structure | Point Lookup | Range Scan [a..b] | Primary Systems Role |
|---|---|---|---|
| Dynamic Contiguous Array (Vec) | O(1) by index | O(k) blazing cache scan | Default in-memory sequence container |
| Open-Addressed Hash Table | O(1) average | O(n) unordered | Caches, symbol tables, hash joins, deduplication |
| B+ Tree | O(log_B n) | O(log_B n + k) ordered leaf links | Relational OLTP indexes (PostgreSQL, MySQL, SQLite) |
| LSM-Tree + Bloom Filter | O(L) w/ Bloom skip | O(L log n + k) merge iterator | Write-heavy KV & time-series engines (RocksDB, Pebble) |
Graph Search, Dynamic Programming, and Navigating NP-Hardness
Dijkstra’s priority-queue shortest path and heuristic-guided A* power map routing and game/robot pathfinding; Tarjan’s topological sort and cycle detection schedule build graphs (`make`, Bazel, Airflow DAGs); and Dynamic Programming eliminates redundant exponential recursion on overlapping subproblems—powering everything from Levenshtein edit distance (`git diff`) and Viterbi sequence decoding to biological sequence alignment.
Meanwhile, Stephen Cook and Richard Karp’s theory of NP-completeness distinguishes problems verifiable quickly (NP) from those solvable quickly in worst-case polynomial time (P). Whenever a logistics or chip-placement requirement reduces to an NP-hard problem, mature engineers formulate Mixed-Integer Linear Programs, invoke CDCL SAT solvers, or apply provable approximation heuristics.
- Never place a hidden database query or O(n) `list.contains()` linear scan inside a loop—turning an O(n) pass into an O(n²) outage at 50,000 items.
- Use Binary Heaps (`heapq` / `PriorityQueue`) to extract Top-K items from n elements in O(n log K) time rather than sorting all n elements in O(n log n).
- Combine asymptotic Big-O analysis with real flamegraph benchmarks—at n < 64, a flat contiguous array scan often beats a hash map due to CPU L1 cache locality.
References & Sources
- Cormen, Thomas H.; Leiserson, Charles E.; Rivest, Ronald L.; Stein, Clifford — Introduction to Algorithms (CLRS, 4th ed.), MIT Press.
- Skiena, Steven S. — The Algorithm Design Manual (3rd ed.), Springer.
- Kleppmann, Martin — Designing Data-Intensive Applications, O’Reilly Media.