# Introduction to Beam Search Algorithm

**Introduction :**

A heuristic technique is a set of criteria for determining which of multiple options will be the most effective in achieving a particular goal. This strategy increases the efficiency of a search process by surrendering claims of systematic and completeness of the best.

We can hope to achieve a good solution to difficult problems (such as the traveling salesman problem) in less than exponent time if we use appropriate heuristics.

**Beam Search :**

A heuristic search algorithm that examines a graph by extending the most promising node in a limited set is known as beam search.

Beam search is a heuristic search technique that always expands the W number of the best nodes at each level. It progresses level by level and moves downwards only from the best W nodes at each level. Beam Search uses breadth-first search to build its search tree. Beam Search constructs its search tree using breadth-first search. It generates all the successors of the current level’s state at each level of the tree. However, at each level, it only evaluates a W number of states. Other nodes are not taken into account.

The heuristic cost associated with the node is used to choose the best nodes. The width of the beam search is denoted by W. If B is the branching factor, at every depth, there will always be W × B nodes under consideration, but only W will be chosen. More states are trimmed when the beam width is reduced.

When W = 1, the search becomes a hill-climbing search in which the best node is always chosen from the successor nodes. No states are pruned if the beam width is unlimited, and the beam search is identified as a breadth-first search.

The beamwidth bounds the amount of memory needed to complete the search, but it comes at the cost of completeness and optimality (possibly that it will not find the best solution). The reason for this danger is that the desired state could have been pruned.

Example: The search tree generated using this algorithm with *W = 2 & B = 3* is given below :

Attention reader! Don’t stop learning now. Get hold of all the important Machine Learning Concepts with the **Machine Learning Foundation Course** at a student-friendly price and become industry ready.

The black nodes are selected based on their heuristic values for further expansion.

The algorithm for beam search is given as :

**Input:** Start & Goal States.**Local Variables:** OPEN, NODE, SUCCS, W_OPEN, FOUND**Output:** Yes or No (yes if the search is successfully done)

Start Take the inputs NODE = Root_Node & Found = False If : Node is the Goal Node, Then Found = True, Else : Find SUCCs of NODE if any, with its estimated cost& store it in OPEN List While (Found == false & not able to proceed further), do { Sort OPEN List Select top W elements from OPEN list and put it in W_OPEN list and empty the OPEN list. for each NODE from W_OPEN list { if NODE = Goal, then FOUND = true else Find SUCCs of NODE. If any with its estimated cost & Store it in OPEN list } } If FOUND = True, then return Yes else return No Stop