Difference between BFS and DFS
# Difference between BFS and DFS
Breadth First Search (BFS) and Depth First Search (DFS) are two popular algorithms used for traversing or searching a tree or graph data structure. Both algorithms have the same goal: to traverse the entire graph, visiting each node, and finding the shortest path between two nodes.
The main difference between BFS and DFS is in the strategy used to traverse the graph. BFS is an algorithm for traversing or searching a graph in a systematic manner, starting from a certain node (the source node) and visiting each node exactly once. BFS uses a queue to store the nodes that have been visited but not yet explored. The algorithm continues until all nodes have been explored or the target node is found.
On the other hand, DFS is an algorithm for traversing or searching a graph in a depth-first manner. It begins at the root node and explores each branch as much as possible before backtracking. DFS uses a stack to store the nodes that have been visited but not yet explored. The algorithm continues until all nodes have been explored or the target node is found.
Both algorithms have their own advantages and disadvantages. BFS is generally more efficient when the target node is close to the source node, as it can quickly find the shortest path between the two nodes. However, it is not suitable for graphs with large numbers of nodes, as it can be time-consuming to traverse the entire graph. DFS is more suitable for graphs with large numbers of nodes, as it can quickly find the target node without having to traverse the entire graph. However, it is not suitable for graphs with many branches, as it can be inefficient in finding the shortest path between the source and target nodes.
Worried About Failing Tech Interviews?
Attend our webinar on
"How to nail your next tech interview" and learn
.png)
Hosted By
Ryan Valles
Founder, Interview Kickstart

Our tried & tested strategy for cracking interviews

How FAANG hiring process works

The 4 areas you must prepare for

How you can accelerate your learnings
Register for Webinar
### Breadth-First Search (BFS)
BFS is a graph traversal algorithm that starts at a given node and explores all of its neighbors before exploring any of its deeper level neighbors. It visits all of the nodes on the same level before proceeding to the next level.
BFS is often used to find the shortest path between two nodes.
### Depth-First Search (DFS)
DFS is a graph traversal algorithm that starts at a given node and explores as far as possible along each branch before backtracking. It visits each node only once and does not guarantee the shortest path.
DFS is often used to find all possible solutions to a problem.
### Sample Code
//BFS
function BFS(node) {
let queue = [];
queue.push(node);
while (queue.length > 0) {
let currNode = queue.shift();
console.log(currNode);
for (let neighbor of currNode.neighbors) {
if (!neighbor.visited) {
neighbor.visited = true;
queue.push(neighbor);
}
}
}
}
//DFS
function DFS(node) {
if (node === null) return;
console.log(node);
node.visited = true;
for (let neighbor of node.neighbors) {
if (!neighbor.visited) {
DFS(neighbor);
}
}
}