You are on page 1of 10

Breadth First search

• Breadth first search is a simple strategy in which the root node is


expanded first, then all the BREADTH-FIRST SEARCH successors of
the root node are expanded next, then their successors, and so on.
• In general, all the nodes are expanded at a given depth in the
search tree before any nodes at the next level are expanded.
• Breadth-first search (BFS) is an algorithm for traversing or
searching tree or graph data structures.
• It starts at the tree root (or some arbitrary node of a graph,
sometimes referred to as a ‘search key’) and explores all of the
neighbour nodes at the present depth prior to moving on to the
nodes at the next depth level.
• It is implemented using a queue.
Initialization:
• Create the initial node with the state set to the initial state of the problem and the path cost set to
0.
• Check if the initial state satisfies the goal test. If yes, return a solution with the initial node.
Frontier and Explored Sets:
• Create a FIFO (First-In-First-Out) queue called frontier and enqueue the initial node.
• Create an empty set called explored to keep track of explored states.
BFS Loop:
• Enter a loop that continues until the frontier is empty.
• Dequeue a node from the frontier (choosing the shallowest node).
• Add the state of the dequeued node to the explored set.
Goal Test:
• For each action available in the current state, generate child nodes using the CHILD-NODE function.
• Check if the state of the child node is not in the explored set or the frontier.
Goal Test for Child Nodes:
• If the goal test is satisfied for a child node, return a solution with that node.
Enqueue Child Nodes:
• Enqueue the child node into the frontier.
• The algorithm continues to explore nodes in a breadth-first manner, ensuring that all nodes at the
current depth level are expanded before moving on to the next depth level. This ensures that the
shallowest solution is found first, making BFS complete and optimal for searching in a tree or graph.
Breadth-first search tree
In the above figure, it is seen that the nodes are expanded level by level starting
from the root node A till the last node I in the tree. Therefore, the BFS sequence
followed is: A->B->C->D->E->F->G->I.
BFS Algorithm
• Set a variable NODE to the initial state, i.e., the root node.
• Set a variable GOAL which contains the value of the goal state.
• Loop each node by traversing level by level until the goal state is not found.
• While performing the looping, start removing the elements from the queue in FIFO
order.
• If the goal state is found, return goal state otherwise continue the search.

The performance measure of BFS is as follows:


• Completeness: It is a complete strategy as it definitely finds the goal state.
• Optimality: It gives an optimal solution if the cost of each node is same.
• Space Complexity: The space complexity of BFS is O(bd), i.e., it requires a huge amount
of memory. Here, b is the branching factor and d denotes the depth/level of the tree
• Time Complexity: BFS consumes much time to reach the goal node for large instances.

Disadvantages of BFS
• The biggest disadvantage of BFS is that it requires a lot of memory space, therefore it is
a memory bounded strategy.
• BFS is time taking search strategy because it expands the nodes breadthwise.
Depth First Search
• Depth-first search always expands the deepest node in the current frontier of the search tree.
DEPTH-FIRST SEARCH.
• The search proceeds immediately to the deepest level of the search tree, where the nodes have
no successors. As those nodes are expanded, they are dropped from the frontier, so then the
search “backs up” to the next deepest node that still has unexplored successors.
• Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures.
• The algorithm starts at the root node (selecting some arbitrary node as the root node in the case
of a graph) and explores as far as possible along each branch before backtracking.
• It uses last in- first-out strategy and hence it is implemented using a stack.
• The properties of depth-first search depend strongly on whether the graph-search or tree-search
version is used.
• The graph-search version, which avoids repeated states and redundant paths, is complete in
finite state spaces because it will eventually expand every node.
• The time complexity of depth-first graph search is bounded by the size of the state space (which
may be infinite, of course). A depth-first tree search, on the other hand, may generate all of the
O(bm) nodes in the search tree, where m is the maximum depth of any node, this can be much
greater than the size of the state space. Note that m itself can be much larger than d (the depth
of the shallowest solution) and is infinite if the tree is unbounded.
• It uses LIFO (Last in First Out) order, which is based on the stack, in order
to expand the unexpanded nodes in the search tree. The search proceeds
to the deepest level of the tree where it has no successors. This search
expands nodes till infinity, i.e., the depth of the tree.
In the above figure, DFS works starting from the initial node A (root node) and
traversing in one direction deeply till node I and then backtrack to B and so on.
Therefore, the sequence will be A->B->D->I->E->C->F->G.
The performance measure of DFS
• Completeness: DFS does not guarantee to reach the goal state.
• Optimality: It does not give an optimal solution as it expands nodes in
one direction deeply.
• Space complexity: It needs to store only a single path from the root
node to the leaf node. Therefore, DFS has O(bm) space complexity
where b is the branching factor(i.e., total no. of child nodes, a parent
node have) and m is the maximum length of any path.
• Time complexity: DFS has O(bm) time complexity.

Disadvantages of DFS
• It may get trapped in an infinite loop.
• It is also possible that it may not reach the goal state.
• DFS does not give an optimal solution.

You might also like