Inferensys

Glossary

Beam Search

Beam Search is a heuristic search algorithm used in graph-based ANN methods and sequence generation that explores paths by maintaining a fixed-width 'beam' of the most promising candidates at each step.
Stylish WeWork-like workspace with hot desks and document wall, professional searching through enterprise knowledge base on a mounted ultrawide display, warm industrial pendants overhead.
VECTOR QUERY OPTIMIZATION

What is Beam Search?

A heuristic graph traversal algorithm that balances exploration and efficiency in approximate nearest neighbor search.

Beam Search is a heuristic search algorithm used in graph-based Approximate Nearest Neighbor (ANN) methods like Hierarchical Navigable Small World (HNSW). It explores a graph by maintaining a fixed-size priority queue, or 'beam,' of the most promising candidate nodes at each traversal step. This beam width parameter directly trades off between search accuracy (recall) and computational efficiency, preventing the exponential growth of a naive breadth-first search while being more thorough than a greedy best-first approach.

During a k-NN search query, the algorithm starts from entry points and iteratively evaluates the neighbors of the current beam's nodes, expanding only the most promising candidates based on their distance to the query vector. This process of dynamic pruning ensures low query latency and high throughput. In vector databases, beam search is a core component for executing fast, accurate similarity searches over high-dimensional embeddings, enabling scalable semantic retrieval.

GRAPH-BASED ANN ALGORITHM

Key Features of Beam Search

Beam Search is a heuristic graph traversal algorithm that balances exploration and computational efficiency by maintaining a fixed-width 'beam' of the most promising candidate nodes at each step. It is a core component of high-performance Approximate Nearest Neighbor (ANN) search in vector databases.

01

Beam Width Parameter

The beam width (B) is the algorithm's central hyperparameter, defining the maximum number of candidate nodes retained at each search step. A larger beam width explores more paths, increasing recall and computational cost, while a smaller width prioritizes speed at the risk of missing the optimal path. This parameter directly trades off between search accuracy and query latency.

02

Pruned Graph Exploration

Unlike exhaustive breadth-first search, Beam Search prunes the search frontier at each step, keeping only the top-B nodes based on a distance metric (e.g., Euclidean, cosine). This controlled exploration prevents combinatorial explosion in dense graphs, making it practical for high-dimensional vector search in systems like HNSW. It is a form of dynamic pruning where only the most promising paths are extended.

03

Integration with HNSW

Beam Search is the primary query algorithm for the Hierarchical Navigable Small World (HNSW) index. It traverses the hierarchical graph by:

  • Starting at a high-level entry point.
  • Using Beam Search at each layer to find a local minimum.
  • Passing the beam's top candidates as the entry points to the next, more granular layer. This multi-layer application is key to HNSW's logarithmic-time search complexity.
04

Candidate List Management (efSearch)

In HNSW implementations, the beam width is often controlled by the efSearch (or ef) parameter. This defines the size of a dynamic candidate list that is maintained during traversal. The algorithm:

  1. Inserts newly discovered neighbors into the list.
  2. Sorts the list by distance.
  3. Truncates it to efSearch size before proceeding. A higher efSearch increases Recall@K and memory usage during the query.
05

Greedy Search vs. Beam Search

Greedy Search (beam width = 1) always extends the single closest node, risking convergence to a local minimum. Beam Search (B > 1) maintains multiple hypotheses, providing a hedge against poor initial steps. This makes it more robust for approximate nearest neighbor search where the graph may have deceptive local minima. Beam Search generalizes greedy search.

06

Application in Multi-Stage Retrieval

Beam Search often functions as the candidate generation stage in a multi-stage retrieval pipeline. A fast, wide beam can quickly produce a broad set of candidate vectors (e.g., 1000). This candidate set is then passed to a more computationally expensive, precise re-ranking model or a filtered search stage. This pipeline architecture optimizes the overall trade-off between throughput (QPS) and final result quality.

SEARCH ALGORITHM COMPARISON

Beam Search vs. Other Search Methods

A comparison of heuristic search algorithms used in graph-based Approximate Nearest Neighbor (ANN) search, focusing on their trade-offs between exploration, efficiency, and result quality.

Feature / MetricBeam SearchGreedy SearchExhaustive Search (Brute-Force)

Algorithm Type

Heuristic, best-first

Heuristic, local optimum

Exact, global optimum

Search Strategy

Maintains a fixed-width 'beam' of top candidates

Always expands the single best immediate neighbor

Evaluates all nodes in the search space

Time Complexity

O(b * d * w), where w is beam width

O(b * d)

O(n * d), where n is total vectors

Space Complexity (during search)

O(w)

O(1)

O(n)

Guarantees Optimal Path?

Primary Use Case

Balancing exploration & efficiency in HNSW graphs

Fast, low-overhead traversal in simple graphs

Ground truth calculation for small datasets or evaluation

Risk of Local Optima

Low (mitigated by beam width)

High

None

Typical Recall@10 (on standard benchmarks)

95-99%

70-85%

100%

Query Latency (relative)

Medium

Low

Very High

Indexing Overhead

None (search-time parameter)

None

None

Key Parameter

Beam width (w)

None

None

Integration with ANN Graphs

Core algorithm for HNSW traversal

Used in naive graph implementations

Not applicable

BEAM SEARCH

Frequently Asked Questions

Beam Search is a heuristic search algorithm central to optimizing vector database queries, particularly within graph-based Approximate Nearest Neighbor (ANN) methods. It balances exploration and computational efficiency by maintaining a fixed-width 'beam' of the most promising candidates.

Beam Search is a heuristic graph traversal algorithm used in vector databases to efficiently find the k-nearest neighbors (k-NN) by exploring only the most promising paths. It works by maintaining a priority queue, or 'beam', of a fixed width B (the beam width). Starting from entry points, the algorithm explores the neighbors of the current best candidates, adds them to a candidate pool, and then selects only the top-B most promising nodes (based on distance to the query) to carry forward to the next search step. This process repeats until no better candidates are found, ensuring a balance between thorough exploration and low query latency.

In the context of graph-based ANN indexes like Hierarchical Navigable Small World (HNSW), Beam Search is the core subroutine for traversing the graph's layers. It systematically prunes less promising paths, preventing the search from degenerating into a costly breadth-first exploration of the entire graph.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.