Topic 6 of 8

Graphs for Coding Interviews

Graphs explained with pictures: BFS, DFS, grids, cycle detection, topological sort and Dijkstra, with diagrams, a quiz and practice problems.

  • 3 min read
  • 5-question quiz
  • 3 practice problems

A graph is a set of nodes (also called vertices) connected by edges. Road maps, social networks, course prerequisites, and even a grid of cells are all graphs. Most graph interview questions come down to one of a few searches, so the work is usually in spotting the graph and choosing the search.

change here: a nodewith many edgesstation = nodetrack = edge
A subway map is a graph: stations are nodes and tracks are edges. Interchange stations are nodes with many edges.

Words you'll need

  • Directed edges go one way (A follows B); undirected edges go both ways (A and B are friends).
  • Weighted edges have a cost (minutes, distance); unweighted edges all cost the same.
  • A cycle is a path that returns to where it started.
  • A connected component is a group of nodes that can all reach each other.
An undirected graph
ABCDEF

Six nodes and seven edges. Every node can reach every other, so it's one connected component.

Representing a graph

RepresentationSpaceGood for
Adjacency list: a map from each node to its neighboursO(V + E)Almost every interview problem
Adjacency matrix: a V × V table of true/falseO(V²)Dense graphs, "is there an edge?" in O(1)
Edge list: a list of pairsO(E)Input format; usually convert it to an adjacency list first
python
1from collections import defaultdict
2graph = defaultdict(list)

node -> list of its neighbours

3for a, b in edges:
4    graph[a].append(b)

an edge from a to b...

5    graph[b].append(a)   # leave this out for a directed graph

...and back, if it's undirected

The two searches

Depth-first search (DFS) follows one path as far as it goes, then backs up. Write it with recursion or a stack. Use it to explore everything reachable, count components, and detect cycles.

Breadth-first search (BFS) visits all nodes one step away, then two steps, and so on, using a queue. In an unweighted graph, BFS finds the shortest path (fewest edges).

Breadth-first search from A1 / 4
A0BCDEF

Start at A: distance 0.

Both are O(V + E): every node and edge is handled once, as long as you mark nodes as visited. Without a visited set, a graph with a cycle sends you round forever.

!

Watch out

Forget the visited set and a single cycle sends your search round and round forever.

Grids are graphs

A grid of cells is a graph where each cell's neighbours are the cells up, down, left and right. You don't need to build an adjacency list: compute the neighbours on the fly, and check they're inside the grid.

A cell's four neighbours
↑←r,c→↓

Up, down, left and right: (r−1, c), (r+1, c), (r, c−1), (r, c+1).

Ordering with prerequisites

If edges mean "must come before", a valid order exists only if there's no cycle. Two ways to find one (a topological order):

  • Kahn's algorithm (BFS): repeatedly take a node with no remaining prerequisites.
  • DFS with three colours: unvisited, in progress, done. Reaching an "in progress" node again means you've found a cycle.

Shortest paths with weights

When edges have different non-negative costs, BFS isn't enough. Dijkstra's algorithm always expands the closest unfinished node next, using a min-heap (heapq in Python). It runs in O(E log V).

Common mistakes

  • Forgetting the visited set, or marking a node visited too late (mark it when you add it to the queue, not when you take it out, in BFS).
  • Treating a directed graph as undirected, or the other way round.
  • Recursion depth: a DFS over a long chain of 100,000 nodes can overflow Python's call stack. Use an explicit stack.

Nice work!

You've got the big idea. Try the quick quiz below, then put it to work on the practice problems.

Quick quiz

5 questions. Pick an answer to see the explanation.

0/5
  1. 1In an unweighted graph, which search finds the path with the fewest edges?

  2. 2What's the time complexity of BFS or DFS on a graph with V nodes and E edges, using an adjacency list and a visited set?

  3. 3Courses have prerequisites. When is it impossible to take them all?

  4. 4Why should BFS mark a node as visited when it's added to the queue, not when it's taken out?

  5. 5Dijkstra's algorithm gives wrong answers when...

Practise what you learned

Solve them in your browser, in Python or JavaScript.

  1. Problem 1Count the parks on a city mapmedium
  2. Problem 2Can every course be completed?medium
  3. Problem 3How long until news reaches everyone?hard