πΈοΈ Graph Data Structures: Adjacency List vs Matrix, BFS & DFS
Graphs from the ground up β vertices and edges, why there's no single Big O for a graph, adjacency list vs adjacency matrix tradeoffs, and why BFS and DFS traversal cost is the foundation every graph algorithm builds on.
π² Trees Deep Dive: BST, AVL Rotations, Red-Black Trees, B-Trees & B+ Trees
π’ Algorithmic Complexity: Big O From First Principles
πΈοΈ Graph
A graph is a set of vertices () connected by edges () β the general case, with no hierarchy or root. Edges may be directed or undirected, weighted or not.
flowchart LR
A --- B
A --- C
B --- D
C --- D
D --- E
Every other non-linear structure in this series β trees included β is really a graph with an extra constraint bolted on (a tree is a connected, acyclic graph with a designated root). A plain graph drops all of those constraints, which is exactly why it's the most general and the most common shape for representing real-world relationships: social networks, road maps, dependency graphs, call graphs.
Adjacency List vs Matrix
There's no single Big O for a graph because it isn't one structure β the representation is the design decision:
| Representation | Space | Edge lookup | Best when |
|---|---|---|---|
| Adjacency list | Sparse graphs (most real graphs) | ||
| Adjacency matrix | Dense graphs, frequent edge checks |
flowchart LR
subgraph Matrix["Adjacency Matrix β O(V^2) space"]
direction TB
M[" A B C<br/>A [ 0 1 1 ]<br/>B [ 1 0 0 ]<br/>C [ 1 0 0 ]"]
end
subgraph List["Adjacency List β O(V+E) space"]
direction TB
LA["A β [B, C]"]
LB["B β [A]"]
LC["C β [A]"]
end
An adjacency matrix answers "are A and B connected?" in β just index into the grid β at the cost of space even if the graph barely has any edges. An adjacency list only stores edges that actually exist, so it's space, but checking a specific edge means scanning that node's neighbor list. This is the exact same space-vs-lookup tradeoff a hash table's chaining makes at the bucket level, just applied at the whole-graph level: dense connectivity favors the matrix, sparse connectivity favors the list β and most real-world graphs are sparse, which is why adjacency lists dominate in practice.
Traversal: BFS and DFS
Traversal is where the actual work lives β BFS and DFS both visit every vertex and edge once, so they run in on a list and on a matrix.
| Breadth-First Search (BFS) | Depth-First Search (DFS) | |
|---|---|---|
| Explores | Level by level, all neighbors before going deeper | One path fully before backtracking |
| Data structure | Queue | Stack (or recursion) |
| Finds | Shortest path in an unweighted graph | Any path; good for exhaustive exploration |
| Typical use | Shortest path, level-order processing | Cycle detection, topological sort, connected components |
Everything downstream builds on these two walks: shortest paths, connectivity, topological sort, and cycle detection are all BFS or DFS with extra bookkeeping layered on top β there is no graph algorithm that avoids visiting vertices and edges at least once, so (or on a matrix) is the practical floor for anything that needs to reason about the whole graph.
Key Takeaways
| Concept | Summary |
|---|---|
| No single Big O | A graph's complexity depends entirely on its representation, not the abstract structure |
| Adjacency list | space, edge lookup β wins for sparse graphs (most real ones) |
| Adjacency matrix | space, edge lookup β wins for dense graphs or frequent edge checks |
| BFS / DFS | Both visit every vertex and edge once β on a list, on a matrix β the floor every graph algorithm builds on |
| Trees are graphs | A tree is just a connected, acyclic graph with a root β every tree structure in this series is a constrained special case of a graph |
The representation choice isn't a detail β it's the entire performance profile of every algorithm that runs on the graph afterward. Picking adjacency list vs matrix before writing a single traversal is the graph equivalent of picking the right data structure before writing the rest of the program.
Related Posts
- π§± Data Structures β linear structures and the Heap/Hash Table/Trie that round out the non-tree, non-graph structures
- π² Non-Linear Data Structures β trees are graphs with a root and no cycles; see how BST/AVL/Red-Black build on that constraint
- π’ Algorithmic Complexity β the Big O notation used throughout this post
