**Definition:**

Breadth-First Search Algorithm is a graph search algorithm. It begins from root node and continues to expand all neighbour node 1- level below.

In artificial intelligence, it is categorized as Uninformed (Blind) Search.

In worst case, the time complexity of this algorithm is O ( b ^d ). ( b: maximum branching factor of the search tree ; d: depth of the least-cost solution )

In worst case, the space complexity of this algorithm is O ( b ^d ).

Properties of BFS:

- Completed,
- Finds the shallowest path,
- Generates b + b^2 + b^3 + … + b^d = O( b^d ) nodes,
- Space complexity (frontier size) is O (b ^ d ) .

**How Does It Work?**

- The root node is expanded.
- Then, all successors of the root node are expanded,
- Then all their successors.

In detail: (from Wikipedia)

- Enqueue the root node,
- Dequeue a node
- If the element sought is found in this node, quit the search and return a result.
- Otherwise enqueue any successors (the direct child nodes) that have not yet been discovered.

- If the queue is empty, every node on the graph has been examined – quit the search and return “not found”.
- If the queue is not empty, repeat from Step 2.

In the figure below, you can see the animated figure of BFS.

Animated Figure of BFS

In the figure below, you can see the order of nodes expanded (visited)

Order of Nodes Expandend in BFS

**Animation:**

For better understanding, go to this animation page.

**Pseudocode:**

1procedureBFS(G,v)is2 create a queue Q 3 create a vector set V 4 enqueue v onto Q 5 add v to V 6whileQ is not emptyloop7 t ← Q.dequeue() 8ift is what we are looking for then 9 returnt10end if11for alledges e in G.adjacentEdges(t)loop12u← G.adjacentVertex(t,e) 13ifuis not inVthen14 addutoV15 enqueueuontoQ16end if17end loop18end loop19 returnnone20endBFS

**References:**

Artificial Intelligence A Modern Approach, S.Russell, P. Norvig, Third Edition,

Figures from: http://en.wikipedia.org