You are on page 1of 3

Difference between BFS and DFS

Breadth-First Search:
BFS, Breadth-First Search, is a vertex-based technique for finding the shortest
path in the graph. It uses a Queue data structure that follows first in first out. In BFS,
one vertex is selected at a time when it is visited and marked then its adjacent are
visited and stored in the queue. It is slower than DFS.
Example:
Input:
A
/\
B C
/ /\
D E F
Output:
A, B, C, D, E, F
Depth First Search:
DFS, Depth First Search, is an edge-based technique. It uses the Stack data
structure and performs two stages, first visited vertices are pushed into the stack,
and second if there are no vertices then visited vertices are popped.
Example:
Input:
A
/\
B D
/ /\
C E F
Output:
A, B, C, D, E, F

BFS vs DFS
S. No. Parameters BFS DFS
BFS stands for Breadth DFS stands for Depth First
1. Stands for First Search. Search.
BFS(Breadth First Search)
uses Queue data structure
for finding the shortest DFS(Depth First Search)
2. Data Structure path. uses Stack data structure.
3. Definition BFS is a traversal approach DFS is also a traversal
approach in which the
traverse begins at the root
node and proceeds through
in which we first walk the nodes as far as possible
through all nodes on the until we reach the node
same level before moving with no unvisited nearby
on to the next level. nodes.
BFS can be used to find a
single source shortest path
in an unweighted graph
because, in BFS, we reach a In DFS, we might traverse
vertex with a minimum through more edges to
number of edges from a reach a destination vertex
4. Technique source vertex. from a source.
Conceptual BFS builds the tree level by DFS builds the tree sub-tree
5. Difference level. by sub-tree.
It works on the concept of It works on the concept of
6. Approach used FIFO (First In First Out). LIFO (Last In First Out).
BFS is more suitable for DFS is more suitable when
searching vertices closer to there are solutions away
7. Suitable for the given source. from source.
DFS is more suitable for
game or puzzle problems.
BFS considers all neighbors We make a decision, and the
first and therefore not then explore all paths
Suitable for suitable for decision- through this decision. And if
Decision making trees used in games this decision leads to win
8. Treestheirwinning or puzzles. situation, we stop.
The Time complexity of The Time complexity of DFS
BFS is O(V + E) when is also O(V + E) when
Adjacency List is used and Adjacency List is used and
O(V^2) when Adjacency O(V^2) when Adjacency
Matrix is used, where V Matrix is used, where V
stands for vertices and E stands for vertices and E
9. Time Complexity stands for edges. stands for edges.
Visiting of Siblings/ Here, siblings are visited Here, children are visited
10. Children before the children. before the siblings.
The visited nodes are added
Nodes that are traversed to the stack and then
Removal of several times are deleted removed when there are no
11. Traversed Nodes from the queue. more nodes to visit.
12. Backtracking In BFS there is no concept DFS algorithm is a recursive
algorithm that uses the idea
of backtracking. of backtracking
BFS is used in various DFS is used in various
applications such as applications such as acyclic
bipartite graphs, shortest graphs and topological
13. Applications paths, etc. order etc.
BFS requires more
14. Memory memory. DFS requires less memory.
BFS is optimal for finding DFS is not optimal for
15. Optimality the shortest path. finding the shortest path.
DFS has lesser space
In BFS, the space complexity because at a
complexity is more critical time it needs to store only a
as compared to time single path from the root to
16. Space complexity complexity. the leaf node.
BFS is slow as compared to DFS is fast as compared to
17. Speed DFS. BFS.
When the target is close to When the target is far from
the source, BFS performs the source, DFS is
18. When to use? better. preferable.

You might also like