DSA Notes
Master all graph representation methods — adjacency list with dictionary and array, adjacency matrix for dense graphs, edge list for algorithms like Kruskal
Overview of Representations
Given a graph G = (V, E), we need an efficient way to store it in memory. The choice of representation affects the time and space complexity of every graph algorithm you run.
The three main representations:
- Adjacency List — list of neighbors per vertex (most common)
- Adjacency Matrix — 2D boolean/weight array
- Edge List — list of all (u, v) or (u, v, w) pairs
Representation 2 — Adjacency Matrix
A V×V boolean (or weight) matrix where matrix[i][j] = 1 (or weight) if edge (i,j) exists.
Space and Time Complexity
| Space | O(V²) — always, regardless of number of edges |
| For V=10,000: 10^8 entries — 800MB for 64-bit floats | impractical! |
| Check if edge (u,v) exists | O(1) ← direct matrix lookup |
| Get all neighbors of u | O(V) ← scan entire row |
| Add edge | O(1) |
| Remove edge | O(1) |
| Iterate all edges | O(V²) |
When to use adjacency matrix:
- Dense graphs (E close to V²)
- Need O(1) edge existence check
- Floyd-Warshall algorithm (needs to check any pair quickly)
- Small graphs where V² is manageable
Representation 3 — Edge List
Simply a list of all edges in the graph.
Space: O(E) When to use: Kruskal's MST (sort edges by weight), graph construction, dense edge iteration
Converting Between Representations
Choosing the Right Representation
Decision tree
Is E close to V² (dense graph)?
YES → Adjacency Matrix (space is O(V²) but acceptable; O(1) edge check)
NO → Adjacency List (O(V+E) space — efficient for sparse graphs)
Do you need to sort edges by weight (Kruskal's)?
YES → Edge List (sort all edges, then process)
Otherwise → Adjacency List
Specific algorithm requirements
BFS/DFS: Adjacency List O(V+E)
Dijkstra: Adjacency List O(V+E)
Bellman-Ford: Edge List O(VE)
Floyd-Warshall: Adjacency Matrix O(V³)
Kruskal's MST: Edge List O(E log E)
Prim's MST: Adjacency List + Priority Queue
Real-World Graph Sizes
| World Wide Web (crawl): V ≈ 10^9 pages, E ≈ 10^11 links | Very sparse |
| Social Network (Facebook): V ≈ 3×10^9 users, avg degree ≈ 200 | Sparse |
| Road Network (US): V ≈ 10^7 intersections, E ≈ 3×10^7 roads | Sparse |
| Adjacency Matrix for V=10^9: 10^18 bytes | Impossible |
Summary
| Representation | Space | Edge Check | Neighbors | Add Edge | Best For |
|---|---|---|---|---|---|
| Adjacency List | O(V+E) | O(degree) | O(degree) | O(1) | Most algorithms |
| Adjacency Matrix | O(V²) | O(1) | O(V) | O(1) | Dense graphs, Floyd-Warshall |
| Edge List | O(E) | O(E) | O(E) | O(1) | Kruskal's, edge sorting |
For competitive programming and interviews, adjacency list is the default choice 90% of the time. Use adjacency matrix when you specifically need O(1) edge checking or are running Floyd-Warshall.
*Next Lesson: BFS Algorithm*
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Graph Representation — Adjacency List, Matrix, Edge List with Complete Analysis.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, graphs, graph, representation
Related Data Structures & Algorithms Topics