Part II: Discovery Through Software Engineering and Vibe Coding
Chapter 15: Discovery of Algorithms

Discovery of Algorithms

"I generated forty-seven sorting algorithms, benchmarked them all, and the winner was the one I wrote by accident while testing the benchmark harness."

A Fitness Function With Impeccable Taste

Overview

Every software system rests on algorithmic choices. Should you use a hash table or a balanced tree? A brute-force search or a branch-and-bound solver? A greedy heuristic or a dynamic programming formulation? These decisions determine whether your system responds in milliseconds or minutes, whether it handles a thousand records or a billion, whether it returns correct results always or only almost always. Yet algorithm selection is rarely treated as a systematic search problem. Developers choose algorithms based on intuition, textbook familiarity, or whatever the LLM suggests first. The result is software whose performance characteristics are discovered after deployment (usually by the on-call engineer at 3 AM) rather than during design.

This chapter applies the discovery-as-search framework from Chapter 1 to algorithm selection and synthesis. The search space $S$ contains all candidate algorithms for a given task, characterized by their time complexity, space complexity, and correctness guarantees. The actions $A$ include proposing algorithm variants, synthesizing implementations from specifications, and selecting data structures. The objective function $f$ measures wall-clock performance, memory consumption, and statistical confidence in the measurements. The constraints $C$ encode correctness requirements, memory budgets, and latency SLAs.

We build three capabilities in this chapter. First, a systematic approach to navigating the complexity-correctness trade-off space when multiple algorithmic strategies are viable. Second, program synthesis techniques that let AI propose and refine algorithm implementations from high-level specifications. Third, a rigorous benchmarking methodology that uses statistical hypothesis testing to distinguish genuine performance differences from measurement noise. These capabilities feed into the Discovery Workbench as the Algorithm Benchmark component, connecting to the architecture decisions from Chapter 14 and informing the AI-assisted implementation strategies in Chapter 16.

Prerequisites

You should have read Chapter 14: Discovery of Architectures for the context of how algorithmic choices fit within system design decisions. Familiarity with basic algorithm analysis (Big-O notation, common data structures) is assumed. Chapter 9: Vibe Coding provides the LLM-steering techniques used for program synthesis, and Chapter 10: Prompting to Programming covers the structured output patterns needed to extract algorithm specifications from natural language. Some background in statistics (hypothesis testing, p-values) is helpful for Section 15.2 but is developed from first principles where needed.

Learning Outcomes

Sections

15.1 Algorithm Search and Complexity

Algorithm selection as exploration of the complexity-correctness trade-off space. Data structure taxonomies and their algorithmic implications. Asymptotic analysis, amortized bounds, and the gap between theoretical complexity and real-world performance. Systematic candidate enumeration for common problem classes.

15.2 Program Synthesis and Benchmarking

AI-assisted program synthesis from specifications. Property-based testing with Hypothesis for correctness verification. Benchmarking methodology: warmup, GC control, input distributions. Statistical comparison with Mann-Whitney U and bootstrap confidence intervals. The timeit and memory_profiler toolchain.

15.3 Building an Algorithm Benchmark

Recipe: a complete pipeline that proposes three algorithm candidates for a problem, synthesizes implementations, verifies correctness with property-based tests, benchmarks under controlled conditions, and selects the winner using statistical hypothesis testing. Integration with the Discovery Workbench.

What's Next

Algorithms live inside implementations. Chapter 16: AI-Assisted Implementation at Repository Scale takes the algorithmic decisions from this chapter and addresses the next challenge: writing, refactoring, and maintaining the code that embodies those decisions across large codebases. The benchmarking methodology you build here becomes a quality gate in Chapter 16's implementation workflow, ensuring that AI-generated code not only compiles and passes tests but also meets the performance targets established by your algorithm benchmarks. Together, Chapters 14, 15, and 16 form the "architecture, algorithm, implementation" triad of AI-assisted software design, connecting the high-level design decisions from Chapter 14 through the algorithmic choices here to the concrete code in Chapter 16.

Bibliography

Foundational Papers

Jain, N., et al. (2022). Jigsaw: Large language models meet program synthesis. ICSE 2022.

Demonstrates how LLMs can synthesize correct programs from natural language specifications and test cases. Introduces the multi-modal synthesis approach that combines intent descriptions with input-output examples.

Li, Y., et al. (2022). Competition-level code generation with AlphaCode. Science, 378(6624), 1092-1097.

AlphaCode generates competitive programming solutions by sampling millions of candidates and filtering with test cases. The generate-and-filter paradigm directly parallels our propose-benchmark-select methodology.

Chen, M., et al. (2021). Evaluating large language models trained on code. arXiv:2108.07732.

The Codex paper that established pass@k as a metric for code generation. Foundational for understanding how many candidate implementations you need to sample before finding a correct one.

Books

Cormen, T. H., et al. (2022). Introduction to Algorithms, 4th Edition. MIT Press.

The standard reference for algorithm analysis and design. The complexity classes and data structure trade-offs in Section 15.1 follow CLRS conventions. Essential background for any algorithm discovery effort.

Gorelick, M. & Ozsvald, I. (2020). High Performance Python, 2nd Edition. O'Reilly.

Practical guide to profiling and optimizing Python code. Covers timeit, memory_profiler, and the measurement methodology that our benchmarking framework builds upon.

Tools & Libraries

MacIver, D. R. (2019). Hypothesis: Property-based testing for Python.

The property-based testing framework used throughout this chapter. Generates random test inputs, finds minimal failing cases, and provides the correctness oracle for synthesized algorithms.

Python Software Foundation. (2024). timeit: Measure execution time of small code snippets.

The standard library module for microbenchmarking. Handles timer selection, loop counting, and garbage collection control. Our benchmarking framework wraps timeit with statistical analysis.

Pedregosa, F. & Gervais, P. (2024). memory_profiler: Monitor memory usage of Python programs.

Line-by-line memory profiling for Python. Used in Section 15.2 to measure peak memory consumption alongside wall-clock time, enabling multi-objective algorithm comparison.

Virtanen, P., et al. (2020). SciPy: Fundamental algorithms for scientific computing in Python. Nature Methods, 17(3), 261-272.

Provides the Mann-Whitney U test implementation used for non-parametric comparison of benchmark timing distributions. Also supplies bootstrap and other statistical testing utilities.

Tutorials & Benchmarks

Austin, J., et al. (2021). Program synthesis with large language models. arXiv:2107.03374.

Introduces the MBPP benchmark for evaluating program synthesis from natural language. The methodology for measuring synthesis quality (functional correctness, test pass rate) informs our pipeline design.

Mankowitz, D. J., et al. (2023). Faster sorting algorithms discovered using deep reinforcement learning. Nature, 618, 257-263.

AlphaDev discovered novel sorting algorithms that outperform human-designed ones at the assembly level. Demonstrates that algorithm discovery is a legitimate search problem amenable to automated exploration.

Romera-Paredes, B., et al. (2024). Mathematical discoveries from program search with large language models. Nature, 625, 468-475.

FunSearch uses LLMs to discover new mathematical constructions by iteratively improving program candidates. Shows that LLM-guided search over the space of programs can yield genuinely novel algorithms.