BFS DFS
BFS DFS
BFS DFS
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.