Master graph fundamentals from representations to traversals. Learn BFS and DFS on graphs, grid-based graph problems, and classic interview patterns like flood fill, number of islands, and word ladder.
Graphs are one of the most versatile data structures in computer science. Unlike trees, which are hierarchical and acyclic, graphs can represent arbitrary relationships between entities --- social networks, road maps, web pages, dependency chains, and much more.
A graph consists of vertices (nodes) and edges (connections between nodes). Edges can be directed or undirected, weighted or unweighted. The way you represent and traverse a graph determines the efficiency of your solution.
This chapter builds your graph intuition from the ground up:
Click any topic to jump in
Vertices, edges, adjacency lists, and matrices — choosing the right representation for your data.
Queue or stack
Queue-based traversal with a visited set — handling cycles and disconnected components.
Recursive or stack-based deep exploration — finding connected components and cycles.
BFS gives optimal distances in unweighted graphs — plus parent tracking for full path reconstruction.
Grids and adjacency
Grids as implicit graphs — each cell is a node, neighbors are the 4 or 8 adjacent cells.
Classic connectivity problem — BFS or DFS from a seed cell to fill a region.
Islands and word ladders
Count connected components in a grid — iterate, trigger BFS/DFS, mark visited.
Words as nodes, one-letter changes as edges — BFS finds the shortest transformation sequence.
A graph consists of a set of vertices and a set of edges . Each edge connects two vertices. Graphs come in many flavors --- directed vs undirected, weighted vs unweighted, cyclic vs acyclic --- and picking the right representation is the first step to solving any graph problem.
The two primary representations are the adjacency list (space-efficient for sparse graphs) and the adjacency matrix (fast edge lookups for dense graphs). Most interview problems use adjacency lists because real-world graphs tend to be sparse.
A vertex (or node) represents an entity, and an edge represents a relationship between two entities. In an undirected graph, edges have no direction --- if A connects to B, then B connects to A. In a directed graph (digraph), edges point from one vertex to another --- A pointing to B does not imply B points to A.
The degree of a vertex is the number of edges connected to it. In directed graphs, we distinguish in-degree (edges coming in) and out-degree (edges going out).
A graph has vertices and edges. In an undirected graph, each edge contributes to both and , giving the handshaking lemma . In a directed graph, . The maximum edge count is for a simple undirected graph (complete graph ) or for a directed one. Sparse graphs have ; dense graphs have .
Given 4 cities connected as: A-B, B-C, C-D, A-D. How many vertices, edges, and what is the degree of each vertex?
An adjacency list stores, for each vertex, a list of its neighbors. This is typically implemented as a dictionary (hash map) where each key is a vertex and the value is a list of adjacent vertices.
Space complexity: --- stores each vertex and each edge once (twice for undirected). Edge lookup: --- must scan the neighbor list. Best for: Sparse graphs where .
An adjacency list stores vertex entries, each mapping to a list of neighbors. Total space is : each vertex contributes overhead, and each edge appears in 1 or 2 lists (directed or undirected). Checking whether edge exists requires scanning 's neighbor list — worst case. Iterating all neighbors of is , which is optimal. For sparse graphs with , total space is , making adjacency lists dramatically more efficient than matrices.
Represent this undirected graph as an adjacency list: edges are (0,1), (0,2), (1,3), (2,3).
An adjacency matrix is a 2D array of size where entry if there is an edge from vertex to , and otherwise. For weighted graphs, the entry stores the edge weight.
Space complexity: regardless of edge count. Edge lookup: --- just check the matrix cell. Best for: Dense graphs where , or when you need fast edge existence checks.
An adjacency matrix has if edge exists. Space is always regardless of . Edge existence queries are , but iterating the neighbors of vertex is — you must scan the entire row. The matrix is symmetric () for undirected graphs. Matrix powers have elegant meaning: counts walks of length from to . For dense graphs with , matrices and lists use the same space, but matrices give edge lookups.
Build an adjacency matrix for 4 vertices with edges: (0,1), (0,2), (1,3), (2,3).