Difference Between BFS and DFS

Soyoung Chung
1 min readMay 29, 2020

--

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 an 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

--

--

Soyoung Chung

The passionate individual on coding, programming and software development with Quality Assurance specialty.