Introduction to Beam Search Algorithm

Last Updated : 3 Sep, 2026

Beam Search is a heuristic search algorithm used in Artificial Intelligence to efficiently explore large search spaces by selecting only the most promising nodes at each level. Instead of expanding every possible path like Breadth-First Search, it keeps a limited number of best nodes based on heuristic values, making the search faster and more memory-efficient.

  • Uses a fixed beam width to limit node expansion
  • Balances efficiency and solution quality
  • Widely used in NLP, speech recognition and pathfinding tasks
  • Width of the Beam (W): This parameter defines the number of nodes considered at each level. The beam width W directly influences the number of nodes evaluated and hence the breadth of the search.
  • Branching Factor (B): If B is the branching factor, the algorithm evaluates W \times B nodes at every depth but selects only W for further expansion.
  • Completeness and Optimality: The restrictive nature of beam search, due to a limited beam width, can compromise its ability to find the best solution as it may prune potentially optimal paths.
  • Memory Efficiency: The beam width bounds the memory required for the search, making beam search suitable for resource-constrained environments.

Working

Beam Search works by selecting only a limited number of the best nodes at each level based on heuristic values. Suppose the beam width \displaystyle W = 2, which means only the 2 most promising nodes are selected for further expansion at every level.

start
Beam Search Algorithm

Step 1: Start from the initial node Start.

Step 2: Generate all successor nodes: A, B and C.

Step 3: Evaluate the nodes using heuristic values and select the best \displaystyle W = 2 nodes. Suppose A and B are selected, while C is discarded.

Step 4: Expand the selected nodes:

  • A → D, E
  • B → F, G

Step 5: Again evaluate the generated nodes (D, E, F, G) and keep only the best 2 nodes for further exploration.

Step 6: Repeat the process until the goal node is reached.

1. Beam Search Algorithm

The following function implements Beam Search. It maintains a fixed number of the highest-scoring candidates at each stage, determined by the beam_width.

Python
def beam_search(graph, start, goal, beam_width):
    beam = [(start, [start], 0)]

    while beam:
        candidates = []

        for node, path, score in beam:
            if node == goal:
                return path, score

            for neighbor, edge_score in graph.get(node, []):
                new_score = score + edge_score
                candidates.append(
                    (neighbor, path + [neighbor], new_score)
                )

        if not candidates:
            break

        candidates.sort(key=lambda x: x[2], reverse=True)
        beam = candidates[:beam_width]

        for node, path, score in beam:
            if node == goal:
                return path, score

    return None

The algorithm starts with the initial node and generates possible paths. It then scores these paths, sorts them by their scores and retains only the top candidates according to the specified beam_width. This process continues until the goal is reached or no more candidates are available.

2. Example

Now, define a graph and use the beam_search() function to find a path from A to G.

Python
graph = {
    'A': [('B', 8), ('C', 6), ('D', 4)],
    'B': [('E', 7), ('F', 5)],
    'C': [('G', 9), ('H', 3)],
    'D': [('I', 6)],
    'E': [],
    'F': [],
    'G': [],
    'H': [],
    'I': []
}

result = beam_search(graph, 'A', 'G', 2)

print("Best Path:", result[0])
print("Score:", result[1])

Output:

Best Path: ['A', 'C', 'G']

Score: 15

Here, beam_width = 2, so the algorithm keeps only the two highest-scoring candidates at each stage. The resulting path from A to G is A → C → G, with a total score of 15.

you can download the complete code from here.

Applications

  • Used in machine translation, text generation and speech recognition to predict the most likely sequence of words.
  • Helps robots find efficient paths and navigate through environments.
  • Used in strategic games where exploring every possible move is computationally expensive.
  • Efficient for large search spaces because it expands only limited nodes
  • Flexible since beam width and heuristic functions can be adjusted
  • Scalable for complex problems with many possible solution paths
  • Requires less memory than exhaustive search method
  • May miss the optimal solution because some paths are discarded early
  • Performance depends heavily on the quality of the heuristic function
  • Small beam width can remove potentially useful solutions
  • Large beam width increases computation time and memory usage
Comment