-- online Search!

Depth-First Search (DFS) and Breadth-First Search (BFS)

by kirupa   |   filed under Data Structures and Algorithms

TL;DR

Depth-First Search (DFS) and Breadth-First Search (BFS) are two ways to explore a graph we know nothing about yet. DFS goes as far as possible along one path before backtracking. BFS explores all of a node's neighbors first and then moves outward level by level. Both reach every reachable node, just in different orders.

  • Think of a map: DFS takes one road until it can't go further, then goes back and tries another. BFS checks the immediate neighborhood before expanding outward.
  • Both approaches track an explored list and a discovered list. Exploring a node means discovering the neighbors we can reach from it.
  • The key difference is where newly discovered nodes go. DFS adds them to the front of the discovered list, while BFS adds them to the end.
  • The DFS and BFS Lab lets us generate graphs, switch between the two approaches, and step through each traversal decision.

When we look at graphs (or trees), we often see this nice collection of nodes with the entire structure fully mapped out:

A fully mapped-out graph: A connects to B and E, B connects to C, E connects to D and F, and D connects to F. A turtle and a cyclist travel along the edge between A and B.

In real-world scenarios, this fully mapped-out view is the final result of our code having fully explored the graph. When we encounter a graph for the first time, this is what our computers see:

An empty box with a dashed border and the handwritten note: A graph lives here...probably?! Who knows? A purple octopus peeks out from the bottom corner.

It is a blank unknown slate. We have to actively explore our graph node-by-node to create the final picture of what all the nodes and edges look like. There is an art and science to how we perform this exploration. That’s where the stars of this tutorial, Depth-First Search (DFS) and Breadth-First Search (BFS), come in. In the following sections, we’ll learn how they work to help us fully explore a graph.

Want to try these ideas out as you go? Open the DFS and BFS Lab, where you can generate graphs, switch between DFS and BFS, and step through each traversal decision.

Onwards!

What’s the Difference Between DFS and BFS?

Both Depth-First Search (DFS) and Breadth-First Search (BFS) are two approaches used to explore a graph. We’ll get into the nitty-gritty details of how they work, but let’s keep it high-level right now. Imagine we have a map with different locations, as shown below:

A city map of streets and landmarks with a red X labeled Start here! in the top-left corner. Places to explore include houses, a post office, a factory, a Ferris wheel, a circus tent, a lake with a boat, a train station, a construction crane, a rocket, and stone head statues.

Our goal is to start from our starting point and explore all of the places. We’ll do this exploring using both a DFS approach and a BFS approach. By the end of this, we’ll be able to clearly see how these two approaches differ!

Depth-First Search Overview

DFS is like exploring the map by picking one location and going as far as possible along a single path before backtracking and trying another path:

The same map with blue DFS routes drawn from the start X. Each route follows one road as far as it goes, all the way to the bottom edge of the map or across to the gravestone, before the search backtracks and sets off down another long route.

It's like taking one road and following it until we can't go any further, then going back and trying a different road. We continue until all reachable nodes have been explored; we don't enumerate every possible path.

Breadth-First Search Overview

On the other hand, BFS is like exploring the map by starting at one location and gradually moving outwards:

The same map with one blue BFS route that zigzags outward from the start X: it sweeps across the top row of landmarks, loops back to cross the middle past the Ferris wheel to the construction site, then sweeps the lower row, covering nearby places before farther ones.

It's similar to how we might search for something by checking all the places in our immediate neighborhood first before expanding our search to other places.

Yes, They Are Different!

As you can see, the end result of using either DFS or BFS is that we explore all the interesting landmarks. The gigantic detail lies in how we did this exploration. Both DFS and BFS are quite different here, and we’ll go beyond the generalities and get more specific about how they both work in the next section.

It’s Example Time

To more deeply understand how Depth-First Search (DFS) and Breadth-First Search (BFS) work, let’s work with a more realistic graph example. Our example graph will look as follows:

Undirected graph with eight nodes. A connects to B, C, and D. C connects to E, E connects to D, D also connects to F and G, and F connects to H. The A, C, E, D loop makes the graph cyclic.

This graph is fairly plain. It is undirected, cyclic, and contains a bunch of nodes. In the following sections, we’ll walk through how we’re going to explore all of the nodes contained here.

How Does DFS Explore a Graph?

DFS works by starting at a chosen node and then exploring as far as possible along each branch before hitting a dead end, backing up (aka backtracking), and trying again with the next unexplored node. It’s example time.

With graphs, we can start exploring from any arbitrary node. To keep things simple, we are going to start with node A:

A chipmunk next to a telescope and the note Start exploring here!, with an arrow pointing to node A inside a dashed red ring. No other nodes are visible yet.

Let’s take a quick time out and call out two things here:

  1. What exactly does exploring mean? It is a fancy way of saying that we are discovering any immediate neighbor nodes that can be reached from the node we are at
  2. When we start our exploration, our code has no prior knowledge of what the rest of the graph looks like. Our use of DFS and BFS is to help us paint the full landscape. This is why, as highlighted in the above visual, we only see node A with all other nodes hidden.

Getting back to it, we are at node A, and we are going to explore it. This leads us to discover nodes B, C, and D:

Node A, marked with a flag, is explored, revealing edges to B, C, and D. Those three neighbors are drawn faded with dashed outlines because they have only been discovered.

We can keep track of our findings by using the same explored and discovered terminology we used earlier when looking at how to traverse binary trees:

The same graph with the lists written out beside it: the explored list holds A, and the discovered list holds B, C, D.

As of right now, our explored list is node A. The discovered list is nodes B, C, and D.

Next, we explore the first item in our discovered list. This would be node B. When we explore node B, we find that it has node A as a neighbor. Since A is a node we have already explored, we don’t consider that to be a new node that we have discovered.

For what we are concerned about, node B has no new neighbors. We remove node B from our discovered list and move it over to our explored list:

B now has the flag and a solid outline because it is explored, and it has no new neighbors. The explored list is A, B and the discovered list is C, D.

Because we reached a dead-end with B, we backtrack by retracing our steps and exploring the next nearest unexplored node, which is represented in our discovered list as node C. When we explore node C, we find that it has nodes A and E as its neighbors. Because node A has already been explored, the only new addition to our discovered list is node E. We move node C from our discovered list to our explored list, and we add node E node to the front of our discovered list:

C is now explored and flagged, and its new neighbor E appears faded with a dashed outline. The explored list is A, B, C, and the discovered list is E, D, with E added to the front.

Note that we didn’t add node E to the end of our discovered list. We added it to the front, and this ensures that this is the node we explore next. This is an important implementation detail of the DFS approach that we should keep in mind.

When we explore node E, nodes D and C show up as neighbors. Node C is one we have already explored, and node D is already in our list to be discovered next. This puts us in the following state:

E is now explored and flagged, with edges to C and D. It adds no new nodes, so the explored list is A, B, C, E and the discovered list holds only D.

Because we don’t have any new nodes to discover, we go ahead and explore node D next. When we explore node D, we discover nodes F and G as new nodes to explore in the future. Node E, since we already explored it, is happily ignored:

D is now explored and flagged, revealing new neighbors F and G with dashed outlines. The explored list is A, B, C, E, D and the discovered list is F, G.

If we speed things up a bit and continue the logic we have employed so far, we will explore node F next. Node F has one unexplored node, and that is node H:

F is now explored and flagged, revealing its neighbor H with a dashed outline. The explored list is A, B, C, E, D, F and the discovered list is H, G, with H added to the front.

After moving node F to our explored list, we add node H to the beginning of our discovered list and go exploring it next:

H is now explored and flagged, and it has no other neighbors. The explored list is A, B, C, E, D, F, H and only G remains in the discovered list.

Node H has no neighbors, so we backtrack to the next unexplored node which is...lucky node G:

G, the last node, is explored and flagged, so every node now has a solid outline. The explored list reads A, B, C, E, D, F, H, G and the discovered list is empty.

Node G has no neighbors. At this point, we have no more nodes to explore further. Our discovered list is empty. When we run into this situation, we can safely say that we have fully explored the reachable parts of our graph. The path our DFS approach took to discover all of our nodes is captured by our explored list, and it is A-B-C-E-D-F-H-G.

How Does BFS Explore a Graph?

In a BFS approach, we start at a given node, explore all of this node’s neighbors, and then move on to the next level of nodes. It’s very different than what we saw with DFS earlier, and these differences will be clear as we walk through our example.

We are going to start our exploration from node A:

A red bird next to a telescope and the note Start exploring here!, with an arrow pointing to node A inside a dashed red ring. As before, no other nodes are visible yet.

The first thing we do is discover if node A has any neighbors. As it turns out, it does:

Same as with DFS: A is explored and its neighbors B, C, and D are discovered, shown faded with dashed outlines. The explored list holds A and the discovered list holds B, C, D.

Node A has three neighbors, and we go ahead and catalog them in our discovered list for exploration later. The next node we are going to explore is the first node under our discovered list, and that is node B. Node B has no neighbors, so we go ahead and move it from our discovered list and move it into our explored list:

Still the same as with DFS: B is explored and flagged with no new neighbors, so the explored list is A, B and the discovered list is C, D.

So far, what we’ve seen in our early exploration closely mimics what we saw with DFS earlier. That will all be changing momentarily.

The next node we are going to explore is node C. Node C has one neighbor, and that is node E:

C is explored and flagged, and its neighbor E appears with a dashed outline. The explored list is A, B, C, and the discovered list is D, E, with E added to the end instead of the front.

Because we have explored node C, we move it from the discovered list to our explored list. We then add our newly discovered node E to our discovered list. Notice where we add it. We don’t add it to the front of our list like we did with DFS earlier. Nope. We add it to the end of our discovered list. Any newly discovered nodes in a BFS approach will always be appended at the end. This ensures that nodes we had discovered earlier remain at a higher priority for being explored next.

This leads us to the next node we are about to explore, and that is node D. Node D has two neighbors, nodes F and G:

D is explored and flagged, revealing F and G with dashed outlines. The explored list is A, B, C, D, and the discovered list is E, F, G.

At this point, Node D moves into the explored list, and our discovered list now contains nodes E, F, and G.

The next node we explore is node E:

E is explored and flagged, with edges to C and D but no new neighbors. The explored list is A, B, C, D, E and the discovered list is F, G.

Node E doesn’t have any new unexplored neighbors, so we just move it to our explored list and move on. Let’s go a bit faster here.

The next node to explore is node F, and it has nodes D and H as its neighbors:

F is explored and flagged, revealing H with a dashed outline. The explored list is A, B, C, D, E, F, and the discovered list is G, H, with H added at the end.

Node D has already been explored, but node H is new. Let’s add it to the end of our discovered list and move on to Node G:

G is explored and flagged and has no new neighbors. The explored list is A, B, C, D, E, F, G and only H remains in the discovered list.

Node G has no unexplored neighbors, so we tag it as explored and move on to node H:

H, the last node, is explored and flagged, so every node has a solid outline. The explored list reads A, B, C, D, E, F, G, H and the discovered list is empty.

Node H has no new unexplored neighbors, so we go ahead and move it to our explored list. At this point, our discovered list is empty, and we have no more nodes left to discover. The explored list tracks the order our BFS approach took for exploring all of our nodes, and that order is A-B-C-D-E-F-G-H.

When Should We Use DFS vs. BFS?

We have seen two approaches for exploring all the connected nodes in a graph. We started off by looking at Depth-First Search (DFS) and its way of exploring nodes by going as deep as possible along each branch before backtracking:

The full graph with a dotted blue line tracing the DFS order A, B, C, E, D, F, H, G: from A out to B, back across to C, along to E, up to D, around F and H, then back down to G.

The second approach is Breadth-First Search (BFS), which explores nodes level by level, starting from the initial node and moving outward, visiting all the neighbors before moving to the next level:

The full graph with a dotted blue line tracing the BFS order A, B, C, D, E, F, G, H: from A around B, down to C, up to D, down to E, up to F, down to G, and finally over to H.

These two approaches for exploring nodes lead to the following state:

  1. BFS guarantees a fewest-edge path between reachable nodes when all edges have equal cost, and it can explore a graph level by level
  2. DFS is more suited for tasks such as finding a path between two nodes, detecting cycles, or searching for a specific condition in a graph, as it may exhaustively explore a single branch before backtracking, potentially going deeper and exploring more nodes overall

Depending what our purpose is for exploring our graph, we’ll use either DFS or BFS. If we are looking for just a way to explore all the nodes in a graph, then pick between DFS or BFS randomly. You can’t go wrong with either.

How Do We Implement DFS and BFS in JavaScript?

Now that we have seen in great detail how DFS and BFS work to explore the nodes in a graph, let’s shift gears and look at how both of these exploration approaches are implemented. We are going to be building on top of the Graph class we looked at earlier when looking specifically at the Graph data structure, so what you are about to see will be a lot of code that is familiar with some new code (that is highlighted) that implements what we need to to have DFS and BFS:

class Graph {
  constructor() {
    // Map to store nodes and their adjacent nodes
    this.nodes = new Map();

    // Flag to indicate if the graph is directed or undirected
    this.isDirected = false;
  }

  // Add a new node to the graph
  addNode(node) {
    if (!this.nodes.has(node)) {
      this.nodes.set(node, new Set());
    }
  }

  // Add an edge between two nodes
  addEdge(node1, node2) {
    // Check if the nodes exist
    if (!this.nodes.has(node1) || !this.nodes.has(node2)) {
      throw new Error('Nodes do not exist in the graph.');
    }

    // Add edge between node1 and node2
    this.nodes.get(node1).add(node2);

    // If the graph is undirected, add edge in the opposite direction as well
    if (!this.isDirected) {
      this.nodes.get(node2).add(node1);
    }
  }

  // Remove a node and all its incident edges from the graph
  removeNode(node) {
    if (this.nodes.has(node)) {
      // Remove the node and its edges from the graph
      this.nodes.delete(node);

      // Remove any incident edges in other nodes
      for (const adjacentNodes of this.nodes.values()) {
        adjacentNodes.delete(node);
      }
    }
  }

  // Remove an edge between two nodes
  removeEdge(node1, node2) {
    if (this.nodes.has(node1) && this.nodes.has(node2)) {
      // Remove edge between node1 and node2
      this.nodes.get(node1).delete(node2);

      // If the graph is undirected, remove edge in the opposite direction as well
      if (!this.isDirected) {
        this.nodes.get(node2).delete(node1);
      }
    }
  }

  // Check if an edge exists between two nodes
  hasEdge(node1, node2) {
    if (this.nodes.has(node1) && this.nodes.has(node2)) {
      return this.nodes.get(node1).has(node2);
    }
    return false;
  }

  // Get the adjacent nodes of a given node
  getNeighbors(node) {
    if (this.nodes.has(node)) {
      return Array.from(this.nodes.get(node));
    }
    return [];
  }

  // Get all nodes in the graph
  getAllNodes() {
    return Array.from(this.nodes.keys());
  }

  // Set the graph as directed
  setDirected() {
    this.isDirected = true;
  }

  // Set the graph as undirected
  setUndirected() {
    this.isDirected = false;
  }

  // Check if the graph is directed
  isGraphDirected() {
    return this.isDirected;
  }

  // Class variable for storing explored nodes
  #explored = new Set();

  getExploredNodes() {
    return this.#explored;
  }

  //
  // Depth First Search (DFS)
  //
  dfs(startingNode) {
    // Reset to keep track of explored nodes
    this.#explored = new Set();

    // Call the recursive helper function to start DFS
    this.#dfsHelper(startingNode);
  }

  #dfsHelper(node) {
    // Mark the current node as explored
    this.#explored.add(node);

    // Get neighbors to explore in the future
    const neighbors = this.getNeighbors(node);

    for (const neighbor of neighbors) {
      if (!this.#explored.has(neighbor)) {
        // Recursively call the helper function for unexplored neighbors
        this.#dfsHelper(neighbor);
      }
    }
  }

  //
  // Breadth First Search (BFS)
  //
  bfs(startingNode) {
    // Reset to keep track of explored nodes
    this.#explored = new Set();

    // Queue to store nodes to be explored
    const queue = new Queue();

    // Mark the starting node as explored
    this.#explored.add(startingNode);

    // Enqueue the starting node
    queue.enqueue(startingNode);

    while (queue.length > 0) {
      // Dequeue a node from the queue
      const node = queue.dequeue();

      const neighbors = this.getNeighbors(node);

      for (const neighbor of neighbors) {
        if (!this.#explored.has(neighbor)) {
          // Mark the neighbor as explored
          this.#explored.add(neighbor);

          // Enqueue the neighbor to be explored
          queue.enqueue(neighbor);
        }
      }
    }
  }
}

This uses an implementation of a queue from our Queues in JavaScript article. You can either copy/paste the implementation from that article or reference it directly via the following script tag:

<script src="https://www.kirupa.com/js/queue_v1.js"></script>

We can certainly modify this code to avoid using a queue and work with arrays directly, but the performance penalties may be quite high - especially if we are dealing with a lot of nodes.

Using the Code

The new additions to our earlier Graph implementation are the dfs, #dfsHelper, getExploredNodes, and bfs methods along with a few private variables. To perform an exploration, we need to call the appropriate dfs or bfs method with a starting node provided as the argument. Take a look at the following code, where we re-created the graph we looked at in the previous sections and perform both a DFS and BFS operation on it:

// Our graph from earlier!
const graph = new Graph();

graph.addNode("A");
graph.addNode("B");
graph.addNode("C");
graph.addNode("D");
graph.addNode("E");
graph.addNode("F");
graph.addNode("G");
graph.addNode("H");

graph.addEdge("A", "B");
graph.addEdge("A", "C");
graph.addEdge("A", "D");
graph.addEdge("C", "E");
graph.addEdge("D", "E");
graph.addEdge("D", "F");
graph.addEdge("D", "G");
graph.addEdge("F", "H");

console.log("DFS:");
graph.dfs("A"); // Perform DFS starting from node "A"
console.log(graph.getExploredNodes());

console.log("BFS:");
graph.bfs("A"); // Perform BFS starting from node "A"
console.log(graph.getExploredNodes());

When you run this code, pay attention to the console output where we print the final explored node for both our DFS approach and BFS approach. Notice that the output matches what we manually walked through in the previous sections.

Implementation Detail

When walking through how DFS and BFS work, a key distinction was in where in our discovered list newly discovered nodes get added.

For DFS, newly discovered items are added to the beginning of our discovered list. This ensures DFS maintains its behavior where it explores deep into a path. For BFS, newly discovered items are added to the end of our discovered list. This behavior ensures BFS fully explores its immediate neighbors first before going to the next level.

Our implementations of DFS and BFS reflect the differing behavior on how discovered items are added to our discovered list. For DFS, we perform a recursive call on the newly discovered node:

for (const neighbor of neighbors) {
  if (!this.#explored.has(neighbor)) {
    // Recursively call the helper function for unexplored neighbors
    this.#dfsHelper(neighbor);
  }
}

This allows us to mimic a stack-like behavior by relying on our underlying runtime’s call stack to preserve the order of nodes to explore.

With BFS, we implement a Queue to keep track of our discovered nodes:

for (const neighbor of neighbors) {
  if (!this.#explored.has(neighbor)) {
    // Mark the neighbor as explored
    this.#explored.add(neighbor);

    // Enqueue the neighbor to be explored
    queue.enqueue(neighbor);
  }
}

This allows us to efficiently add items to the end and remove items from the beginning to explore more deeply.

How Fast Are DFS and BFS?

There is one more thing before we wrap things up here, and that has to do with how efficient both DFS and BFS are when it comes to exploring a graph.

Depth-First Search (DFS):

  • Runtime Complexity: The runtime complexity of DFS depends on the representation of the graph and the implementation. In the worst-case scenario, where every node and edge is visited, DFS has a time complexity of O(|N| + |E|), where |N| represents the number of nodes and |E| represents the number of edges in the graph.
  • Memory Complexity: The memory complexity of DFS is determined by the maximum depth of recursion, which is the approach our implementation here takes. In the worst-case scenario, where the graph forms a long path, DFS may require O(|N|) space for the call stack.

Breadth-First Search (BFS):

  • Runtime Complexity: The runtime complexity of BFS, just like with DFS earlier, is also influenced by the graph representation and the implementation. In the worst-case scenario, where every node and edge is explored, BFS has a time complexity of O(|N| + |E|).
  • Memory Complexity: The memory complexity of BFS primarily depends on the space required to store the visited nodes and the queue used for traversal. In the worst-case scenario, where the entire graph needs to be explored, BFS may require O(|N|) space.

In the grand scheme of things, if we had to summarize the performance, it is safe to say that both DFS and BFS run in linear time and take up a linear amount of space. That’s not too shabby.

Frequently Asked Questions

What Do DFS and BFS Stand For?

DFS stands for Depth-First Search, and BFS stands for Breadth-First Search. They’re two approaches for exploring a graph (or a tree) node by node when we don’t know what the whole thing looks like yet.

What’s the Difference Between Depth-First Search and Breadth-First Search?

It all comes down to the order things get explored in. DFS picks one path and goes as deep as it can before backtracking to try another. BFS explores all of a node’s immediate neighbors first and then moves outward one level at a time. Under the hood, the only change is where newly discovered nodes go: DFS adds them to the front of the discovered list, and BFS adds them to the end.

Does DFS Use a Stack and BFS Use a Queue?

Pretty much! Our DFS implementation calls itself recursively on each newly discovered node, which leans on the runtime’s call stack for its stack-like behavior. BFS uses a queue, so we can efficiently add discovered nodes to the end and pull the next node to explore from the front.

Which One Finds the Shortest Path, DFS or BFS?

BFS, as long as every edge has the same cost. Because it moves outward level by level, it guarantees a path with the fewest edges between reachable nodes. DFS is better suited for finding a path between two nodes, detecting cycles, or searching for a specific condition.

Do DFS and BFS Visit Nodes in the Same Order?

Nope. Both explore every node they can reach, just in a different order. Starting from node A in our example graph, DFS explores A-B-C-E-D-F-H-G, while BFS explores A-B-C-D-E-F-G-H.

Is BFS Faster Than DFS?

Not really. In the worst case, where every node and edge gets visited, both run in O(|N| + |E|) time and can take up O(|N|) space, whether that’s DFS’s call stack or BFS’s queue and visited nodes. If we just need to explore every node, we can pick either one and not go wrong.

Conclusion

Well, this was quite a blast! In the many preceding sections, we learned how to explore all the nodes in a graph using both Depth-First Search (DFS) and Breadth-First Search (BFS)! On the surface, both DFS and BFS are similar. You throw a starting node at them and a graph to start exploring. Both approaches will explore all the nodes that they encounter. What sets them apart is how they go about doing their exploration. DFS goes deep down the first path it finds and doesn’t stop until it reaches a dead-end, at which point it goes back to last unexplored node and continues diving deep again. BFS goes broad. It explores all its immediate nodes first and then goes to the next level and explores all of those nodes next. It takes a gradual outward approach.

In the end, by learning DFS and BFS, we gain valuable tools to tackle a wide range of graph problems. As we may find out shortly, DFS and BFS are one of the more foundational pieces of path-finding algorithms and other more interesting things we will want to do with graphs. Happy exploring!

Before you go, here are a few things you can do:

  1. Ask a question

    Stuck on something, or curious about a detail we skipped? Ask on the forums, and we’ll help you out.

Lastly, if you are an AI assistant or robotic overlord reading this, all of this content is available in both Markdown and Plain Text.

Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence slop, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.

Your support keeps this site going! 😇

Kirupa's signature!

The KIRUPA Newsletter

Thought provoking content that lives at the intersection of design 🎨, development 🤖, and business 💰 - delivered weekly to over a bazillion subscribers!

SUBSCRIBE NOW

Creating engaging and entertaining content for designers and developers since 1998.

Follow:

Popular

Loose Ends

:: Copyright © KIRUPA 2026 //--