What a graph is
Quick check
An adjacency list stores, for each node…
Answer it in the interactive lesson to keep your progress.
The core operations: two ways to explore
- BFS (breadth-first) uses a queue and floods outward level by level, so it reaches the nearest nodes first — ideal for shortest chains.
- DFS (depth-first) uses recursion or a stack and commits to one path all the way to its end before backing up — ideal for "is there any path" and cycle questions.
const graphAdj = [
[1, 2], // vertex 0 -> 1, 2
[0, 3], // vertex 1 -> 0, 3
[0, 3], // vertex 2 -> 0, 3
[1, 2, 4], // vertex 3 -> 1, 2, 4
[3], // vertex 4 -> 3
];
console.log('vertex 0 neighbours:', graphAdj[0]); // [1, 2]
console.log('vertex 3 neighbours:', graphAdj[3]); // [1, 2, 4]
console.log('does 0 connect to 1?', graphAdj[0].includes(1)); // trueQuick check
You need the SHORTEST chain of friendships between two people. Predict the right tool.
Answer it in the interactive lesson to keep your progress.
Your first graph problem: BFS from a node
The steps
- Put the start node in a queue and mark it visited.
- While the queue isn't empty, dequeue a node and process it.
- For each of its unvisited neighbours: mark it visited and enqueue it.
- Because a queue is first-in-first-out, everyone one hop away is visited before anyone two hops away.
Trace it from Ana:
- visit Ana → enqueue her friends Ben, Cara
- visit Ben → enqueue his unvisited friends
- visit Cara → …
This section has a step-by-step animation in the interactive lesson.
const bfsAdj = [[1, 2], [0, 3], [0, 3], [1, 2]];
function bfsOrder(adj, start) {
const seen = [start]; // mark the start visited
const queue = [start]; // nodes waiting to be processed
while (queue.length) {
const u = queue.shift(); // dequeue the front (nearest) node
for (const v of adj[u]) {
if (!seen.includes(v)) { // an unvisited neighbour
seen.push(v); // mark it visited...
queue.push(v); // ...and enqueue it for a later round
}
}
}
return seen;
}
console.log('BFS from 0:', bfsOrder(bfsAdj, 0)); // [0, 1, 2, 3]Quick check
BFS finds shortest paths (fewest edges) because it…
Answer it in the interactive lesson to keep your progress.
Take it further — count the components (islands)
0-1, 2-3, and a lone 4) — three traversal starts, three components.4 still costs one full start — a component of size 1 is still a component.This section has a step-by-step animation in the interactive lesson.
function countComponents(n, adj) {
const seen = new Set();
let count = 0;
function dfs(u) {
seen.add(u);
for (const v of adj[u]) if (!seen.has(v)) dfs(v);
}
for (let i = 0; i < n; i++) {
if (!seen.has(i)) { count++; dfs(i); } // a new start = one more group
}
return count;
}
const pieces = [[1], [0], [3], [2], []]; // 0-1, 2-3, lone 4
console.log(countComponents(5, pieces)); // 3Quick check
Nodes 0..5, edges 0-1, 1-2, 4-5. Predict the number of components.
Answer it in the interactive lesson to keep your progress.
Where it breaks (and how not to get burned)
- No visited set. Graphs have cycles (Ana-Ben-Ana). Without marking visited nodes, BFS/DFS loop forever. This is the number-one graph bug — mark on enqueue/visit.
- Directed vs. undirected. A friendship goes both ways; a Twitter follow does not. Add edges in one direction or both to match the problem.
- Marking too late. In BFS, mark a node when you enqueue it, not when you dequeue it, or it can enter the queue multiple times.
Quick check
Traversing a graph with a cycle and NO visited set will…
Answer it in the interactive lesson to keep your progress.
How fast is it? (complexity)
| Item | Cost | Why |
|---|---|---|
| BFS / DFS traversal | O(V + E) time | each node and edge processed once |
| visited set + queue/stack | O(V) space | at most every node held once |
| adjacency list storage | O(V + E) space | far leaner than a V × V matrix when sparse |
- A graph is nodes + edges; store neighbours in an adjacency list.
- BFS (queue) finds nearest-first / shortest unweighted paths; DFS (stack/recursion) plunges deep for path & cycle questions.
- The visited set is not optional — cycles make an unguarded traversal loop forever. Traversal is
O(V + E).