Skip to content

Graphs, BFS & DFS

Nodes connected in any pattern at all, social networks, maps, dependencies, and the two traversals that explore them.

Class 11 to 12intermediate45 min2 lessons1 interactive lab
By the end you will be able to
  • Represent a graph as an adjacency list
  • Trace breadth-first and depth-first traversal over the same graph
  • Explain why BFS finds the shortest path in an unweighted graph

Cost at a glance

bfs
O(V + E)
dfs
O(V + E)
space
O(V)

Lesson 1 of 2

Connections without hierarchy

A tree is a graph with rules. Drop the rules, allow any node to connect to any other, allow cycles, allow a node to have many parents, and you have a graph. Friend networks, road maps, package dependencies and web links are all graphs.

Because cycles are allowed, traversal needs something trees never needed: a record of what has already been visited. Skip that and BFS will happily loop forever between two mutually connected nodes.

Interactive lab

Graph traversal: BFS and DFS

Watch the frontier grow level by level under BFS, then dive and backtrack under DFS.

Graph traversal, BFS

O(V + E) time · O(V) space
from
ABCDEFG
Queue: Avisited order: -
1 / 25

Start

Breadth-first search starts at A, which goes into the queue and is marked visited straight away. Marking on discovery, not on visit, is what stops a cycle from re-adding it.

Continue

Lesson 2 of 2

Same graph, two orders

Run BFS and then DFS from the same starting node in the visualizer and compare the order of visits. BFS finishes all of a node's immediate neighbours before going deeper, which is why it finds the shortest path in an unweighted graph: the first time it reaches a node, it arrived by the fewest possible hops.

DFS commits to one path until it dead-ends, then backtracks. That makes it the natural fit for questions about reachability, cycle detection and topological ordering.

Continue

Worked examples

Read the code, then change it

Copy any example into the playground and break it on purpose. That is the fastest way to learn what each line is holding up.

BFS with a visited setJavaScript
function bfs(graph, start) {
  const visited = new Set([start]);
  const queue = [start];
  const order = [];
  while (queue.length) {
    const node = queue.shift();
    order.push(node);
    for (const neighbour of graph[node]) {
      if (!visited.has(neighbour)) {
        visited.add(neighbour); // guard against cycles
        queue.push(neighbour);
      }
    }
  }
  return order;
}

const graph = { A: ['B', 'C'], B: ['A', 'D'], C: ['A', 'D'], D: ['B', 'C'] };
console.log(bfs(graph, 'A'));

Practice

Work these out yourself

No answer key here on purpose: these are the questions worth thinking through before you move on. Open one and work it out.

0/3 attempted

Assessment

Check your understanding

Answer each question, then read the explanation. That is where the learning is.

0/5
  1. Question 1: Which data structure does breadth-first search rely on?
    Question 1 / 5

    Which data structure does breadth-first search rely on?

    Select an option first
  2. Question 2: What happens if you omit the visited set during BFS on a cyclic graph?
    Question 2 / 5

    What happens if you omit the visited set during BFS on a cyclic graph?

    Select an option first
  3. Question 3: Why does BFS find the shortest path in an unweighted graph while DFS does not?
    Question 3 / 5

    Why does BFS find the shortest path in an unweighted graph while DFS does not?

    Select an option first
  4. Question 4: What is the time complexity of BFS or DFS over a graph with V vertices and E edges?
    Question 4 / 5

    What is the time complexity of BFS or DFS over a graph with V vertices and E edges?

    Select an option first
  5. Question 5: A graph has 10,000 vertices and only 20,000 edges. Which representation should you choose?
    Question 5 / 5

    A graph has 10,000 vertices and only 20,000 edges. Which representation should you choose?

    Select an option first
5 of 5 questions left.

Finished this topic?

0 of 2 lessons marked done. Marking the topic complete ticks the rest.