Chapter 8: Graphs
Graph problems boil down to two questions: how do I traverse, and what do I track? Breadth-first search gives shortest paths in unweighted graphs. Depth-first search explores connected components and detects cycles. Most “hard” graph problems turn out to be BFS or DFS with clever state encoding: the nodes are board positions or words, the edges are moves or one-letter changes, and the visited set records more than a node id.
Concepts from other sections (7)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- Breadth-first search (BFS): traversal in order of hop distance using a FIFO queue. Read more: 18.6 Graph as an adjacency list
- Depth-first search (DFS): traversal that follows one path as far as possible before backtracking, using a stack. Read more: 18.6 Graph as an adjacency list
- DAG: a directed graph with no directed cycle. Read more: 12.1 Graphs, DAGs, and an iterative topological sort in Mathematics
- Kahn’s algorithm: topological sort by repeatedly emitting vertices whose in-degree has dropped to zero. Read more: 18.6 Graph as an adjacency list
- Directed graph: a set of vertices and a set of ordered pairs of vertices called edges. Read more: 12.1 Graphs, DAGs, and an iterative topological sort in Mathematics
- Adjacency list: a per-vertex list of outgoing edges; O(V + E) space for the whole graph. Read more: 18.6 Graph as an adjacency list
- In-degree: the number of edges entering a vertex, counting parallel edges separately. Read more: 18.6 Graph as an adjacency list
This chapter builds the traversal toolkit first (BFS for shortest paths, DFS for flood fill), then turns to dependencies with topological sort and cycle detection, weighted shortest paths with Dijkstra’s algorithm, dynamic connectivity with union-find, and multi-source BFS. It closes with a comparison of graph representations and five solutions in JavaScript, TypeScript, and C.
Learning objectives
- Find shortest paths in unweighted graphs with BFS and count connected regions with DFS flood fill.
- Order a DAG with Kahn’s algorithm and detect cycles in directed and undirected graphs.
- Compute weighted shortest paths with Dijkstra’s algorithm and explain its stale-entry check.
- Implement union-find with path compression and union by rank.
- Choose a graph representation by density and by the operations you need.
Before you begin
Section titled “Before you begin”You should know queues, stacks, and recursion, and be comfortable with the tree traversals in Trees, since a tree is a graph without cycles. Graphs, DAGs, and topological sort in Mathematics covers the definitions and proofs. Dijkstra’s algorithm uses a priority queue; the binary heap for top-k builds one.
BFS for shortest unweighted paths
Section titled “BFS for shortest unweighted paths”To find the minimum number of steps, moves, or transformations, explore outward in rings with a queue. Mark a node visited when you enqueue it, not when you dequeue it, or the same node is queued many times:
from collections import dequedef bfs(start, target): queue = deque([(start, 0)]) visited = {start} while queue: node, dist = queue.popleft() if node == target: return dist for neighbor in graph[node]: if neighbor not in visited: visited.add(neighbor) queue.append((neighbor, dist + 1)) return -1Shortest Path in Binary Matrix, Word Ladder, Rotting Oranges, and Open the Lock are BFS problems in disguise.
DFS for connected components and flood fill
Section titled “DFS for connected components and flood fill”To count islands or connected regions, or to fill or mark a region, start a DFS from every unvisited cell and let it mark everything it reaches. Each start is one component:
def dfs(r, c): if r < 0 or r >= rows or c < 0 or c >= cols: return if grid[r][c] != target: return grid[r][c] = visited_marker # mark in-place for dr, dc in [(0,1),(0,-1),(1,0),(-1,0)]: dfs(r + dr, c + dc)
count = 0for r in range(rows): for c in range(cols): if grid[r][c] == target: dfs(r, c) count += 1Marking the grid in place doubles as the visited set. Number of Islands, Flood Fill, Surrounded Regions, and Pacific Atlantic Water Flow are the standard problems.
Topological sort
Section titled “Topological sort”Dependencies, prerequisites, and build orders are all directed acyclic graphs that must be processed in dependency order. Kahn’s algorithm counts each node’s incoming edges, starts from the nodes with none, and releases a node once all its prerequisites have been processed. If some nodes are never released, the graph has a cycle:
from collections import dequeindegree = {u: 0 for u in graph}for u in graph: for v in graph[u]: indegree[v] += 1queue = deque([u for u in indegree if indegree[u] == 0])order = []while queue: u = queue.popleft() order.append(u) for v in graph[u]: indegree[v] -= 1 if indegree[v] == 0: queue.append(v)if len(order) != len(graph): return "cycle detected"Course Schedule, Course Schedule II, Alien Dictionary, and Task Scheduler use it.
Cycle detection
Section titled “Cycle detection”Cycles can also be found directly with DFS, and the method depends on whether edges have a direction. In a directed graph, track three states per node: unvisited, in progress, and visited. An edge back to an in-progress node is a back edge, and a back edge means a cycle. In an undirected graph, every edge looks like it leads back, so the rule is different: reaching a neighbor that is already visited and is not the node you came from means a cycle. Course Schedule and the Detect Cycle problems for directed and undirected graphs exercise both.
Dijkstra’s algorithm for weighted shortest paths
Section titled “Dijkstra’s algorithm for weighted shortest paths”With non-negative edge weights, BFS’s rings become a priority queue ordered by distance. Each pop settles the closest unsettled node. Because a node can be pushed several times with improving distances, skip any popped entry whose distance is stale:
import heapqdef dijkstra(graph, start): dist = {start: 0} heap = [(0, start)] while heap: d, u = heapq.heappop(heap) if d > dist.get(u, float('inf')): continue for v, w in graph[u]: nd = d + w if nd < dist.get(v, float('inf')): dist[v] = nd heapq.heappush(heap, (nd, v)) return distNetwork Delay Time, Cheapest Flights Within K Stops, Path With Minimum Effort, and Swim in Rising Water are the standard problems. Negative weights break the algorithm, because a settled node could later be reached more cheaply.
Union-find
Section titled “Union-find”For dynamic connectivity (“are these two nodes connected?” as edges keep arriving), a disjoint-set structure answers each query in nearly constant amortized time. Path compression flattens the trees during find, and union by rank keeps them shallow:
parent = list(range(n))rank = [0] * n
def find(x): while parent[x] != x: parent[x] = parent[parent[x]] # path compression x = parent[x] return x
def union(x, y): px, py = find(x), find(y) if px == py: return False if rank[px] < rank[py]: px, py = py, px parent[py] = px if rank[px] == rank[py]: rank[px] += 1 return Trueunion returning False means the two nodes were already connected, which is exactly how Redundant Connection finds the extra edge. Number of Connected Components in an Undirected Graph, Accounts Merge, and Smallest String With Swaps also use it.
Multi-source BFS
Section titled “Multi-source BFS”When there are several starting points at once (every rotten orange, every gate), initialize the queue with all of them and then run a standard BFS. The distances that come out are distances to the nearest source. Rotting Oranges, 01 Matrix, Walls and Gates, and Pacific Atlantic Water Flow (which starts from every cell on each ocean’s border) use this pattern.
Representing a graph
Section titled “Representing a graph”How you store the graph decides what is cheap. The four common choices:
| Format | Best for | Space |
|---|---|---|
| adjacency list | sparse graphs (most interviews) | |
| adjacency matrix | dense graphs, quick edge lookup | |
| edge list | union-find problems | |
| implicit graph | grid and matrix problems | extra |
Interviewers favor different variants. As a rough guide:
| Company | Favorite variant | Difficulty |
|---|---|---|
| Dijkstra variants, topological sort | Hard | |
| Meta | BFS and DFS on grids, clone graph | Medium |
| Amazon | course schedule (topological sort) | Medium |
| Anthropic | graphs as distributed-system models | Hard |
| NVIDIA | parallel BFS, graph partitioning | Hard |
| Palantir | entity relationship graphs | Medium-Hard |
Key terms
Section titled “Key terms”- Flood fill: a DFS or BFS from one cell that marks every cell reachable through matching neighbors; starting one from each unvisited cell counts connected regions, such as the islands in a grid.
- Back edge: an edge found during depth-first search that points to a node still in progress on the current path; in a directed graph, tracked with three node states (unvisited, in progress, visited), a back edge is exactly a cycle.
- Dijkstra’s algorithm: single-source shortest paths for non-negative edge weights that repeatedly pops the closest unsettled node from a priority queue, relaxes its edges, and skips popped entries whose distance is stale; with a binary heap.
- Union-find / disjoint-set union (DSU): a forest that keeps elements in disjoint sets, with
findreturning a set’s root andunionmerging two sets; it answers “are these connected?” as edges arrive in nearly constant amortized time per operation. - Path compression: a union-find optimization in which
findpoints the nodes it passes closer to, or directly at, their root, flattening the trees so later finds are nearly constant time. - Union by rank: a union-find rule that attaches the root of the shallower tree under the root of the deeper one so the trees stay shallow; with path compression it gives nearly constant amortized time per operation.
- Multi-source BFS: a breadth-first search whose queue starts with every source at once, so the distance it assigns each node is the distance to the nearest source.
- Adjacency matrix: a table whose entry records whether, or with what weight, an edge joins and ; it gives edge lookup at space, which suits dense graphs.
- Implicit graph: a graph whose nodes and edges are computed on demand rather than stored, such as grid cells and their four neighbors or words that differ by one letter; it needs no extra space for edges.
In this chapter
Section titled “In this chapter”The JavaScript files are solution functions; run them with Node and a small driver, or in an online judge. The TypeScript code can be run with ts-node, or compiled with tsc and run with Node.
- Number of Islands: DFS flood fill on a grid of
"1"and"0"cells. Each unvisited land cell starts a DFS that sinks its whole island to"0", so the number of starts is the number of islands. The function is written with TypeScript type annotations, so run it throughts-nodeortsc. - Clone Graph: a DFS that copies each node once, keeping a map from value to clone so that cycles and shared neighbors point at the same copy instead of recursing forever.
- Course Schedule: Kahn’s algorithm. It builds an adjacency map from each prerequisite to the courses it unlocks and an in-degree count per course, processes courses with no remaining prerequisites, and reports that all courses can be finished only if every course with prerequisites is eventually released.
- Pacific Atlantic Water Flow: reverses the direction of flow and runs multi-source DFS uphill from each ocean’s border cells, one visited set per ocean. A cell that ends up in both sets can drain to both oceans.
- Charles and the Corgi Conundrum: a graph problem stated in C, left for you to implement. Each corgi’s “social reach” is plus the degree of each of its friends, and the program prints the average over all corgis per test case to three decimals. In the sample, the path gives every corgi a reach of , so the output is
Pond #1: 3.000. Summing over all corgis, each vertex contributes its degree once per neighbor, so the total is , computable from an array of degrees in .