Difference between BFS and DFS
Breadth First Search
BFS stands for Breadth First Search is a vertex based technique for finding a shortest path in graph. It uses a Queue data structure which 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.
Ex-
A
/ \
B C
/ / \
D E F
Output is:
A, B, C, D, E, F
Depth First Search
DFS stands for Depth First Search is a edge based technique. It uses the Stack data structure, performs two stages, first visited vertices are pushed into stack and second if there is no vertices then visited vertices are popped.
Ex-
A
/ \
B C
/ / \
D E F
Output is:
A, B, D, C, E, F
BFS vs DFS
|