← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Adjacency Matrix vs Adjacency List

data_structures Intermediate

Also Known As

adjacency matrix adjacency list graph representation

TL;DR

Two ways to represent a graph — adjacency matrix (2D array, O(1) edge lookup, O(V²) space) vs adjacency list (array of lists, O(V+E) space, better for sparse graphs).

Explanation

Adjacency matrix: V×V boolean array where matrix[i][j]=true means edge from i to j. O(1) edge existence check, O(V²) space — wasteful for sparse graphs (few edges). Adjacency list: array of V lists, each containing neighbours. O(V+E) space — efficient for sparse graphs. Edge existence: O(degree). Most real-world graphs are sparse (social networks, road maps, dependency trees) — adjacency lists are the default. Adjacency matrices suit: dense graphs, fast edge lookup in algorithms like Floyd-Warshall, and small graphs.

Common Misconception

Adjacency matrix is always faster than adjacency list — matrix has O(1) edge lookup but O(V²) space; for a social network with 1 billion users, a matrix would require 10^18 bytes — physically impossible.

Why It Matters

Choosing the wrong graph representation can make an algorithm O(V²) instead of O(V+E) — for a sparse graph with 1M vertices and 5M edges, adjacency list uses 5M space while matrix uses 1 trillion.

Common Mistakes

  • Adjacency matrix for a sparse social graph — O(V²) memory makes it infeasible at scale.
  • Adjacency list for a dense graph needing frequent edge queries — O(degree) lookup is slow for dense graphs.
  • Not considering directed vs undirected — directed graphs need asymmetric representation.
  • Forgetting to handle disconnected graphs — not all vertices have edges; the list may be empty.

Avoid When

  • Avoid adjacency matrices for graphs with more than a few thousand vertices — memory blows out quadratically.
  • Avoid adjacency lists when repeated random edge lookups dominate; a matrix is faster for those access patterns.

When To Use

  • Use an adjacency list for sparse graphs (social networks, dependency trees) where edges ≪ V².
  • Use an adjacency matrix when the graph is dense or you need O(1) edge-existence checks (e.g. game board adjacency).
  • Prefer adjacency lists in PHP where memory is a concern — a list uses O(V+E) vs O(V²) for a matrix.

Code Examples

💡 Note
A 1,000,000-node matrix would require ~8 TB of memory; the list for the same social graph with 200 friends per user uses a few hundred MB.
✗ Vulnerable
// Adjacency matrix for 1M user social graph:
$matrix = array_fill(0, 1000000, array_fill(0, 1000000, false));
// Memory: 1M * 1M * 1 byte = 1 terabyte — impossible
// Even with bits: 125 GB
✓ Fixed
// Adjacency list for sparse social graph:
$graph = [];
// Average user has 200 friends:
$graph[42] = [7, 99, 156, 203]; // User 42 follows these users
// Memory: 1M users * 200 friends avg * 8 bytes = 1.6 GB — feasible

// Edge check — O(degree):
function hasEdge(array $graph, int $from, int $to): bool {
    return in_array($to, $graph[$from] ?? []);
}
// For faster lookup use hash set per vertex:
$graph[42] = array_flip([7, 99, 156, 203]); // O(1) lookup

Added 16 Mar 2026
Edited 31 Mar 2026
Views 44
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 1 ping T 1 ping W 0 pings T 2 pings F 0 pings S 4 pings S 0 pings M 0 pings T 1 ping W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 1 ping W 0 pings T
No pings yet today
Amazonbot 13 Perplexity 9 ChatGPT 7 Ahrefs 3 Google 2 Unknown AI 1 Meta AI 1
crawler 36
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Medium
⚡ Quick Fix
Use adjacency lists (array of arrays) for sparse graphs — most real-world graphs (social networks, dependency trees) are sparse; adjacency matrices waste O(V²) memory
📦 Applies To
any web cli
🔗 Prerequisites
🔍 Detection Hints
V×V matrix for sparse graph with few edges; nested loop over all vertices when only neighbours are needed
Auto-detectable: ✗ No
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: Medium Context: Function Tests: Update

✓ schema.org compliant