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

Adjacency Matrix vs Adjacency List

Data Structures Intermediate
debt(d7/e5/b5/t5)
d7 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'only careful code review or runtime testing' (d7). The detection_hints indicate automated detection is 'no' — there are no tools listed that catch this. Identifying that someone used an adjacency matrix for a sparse graph requires manual code review or runtime profiling to notice the O(V²) memory blowup. A profiler might surface it at runtime, but it won't flag the representation choice itself, so d7 fits.

e5 Effort Remediation debt — work required to fix once spotted

Closest to 'touches multiple files / significant refactor in one component' (e5). The quick_fix says 'use adjacency lists (array of arrays)' which sounds simple, but swapping from a matrix to a list changes the data structure's API — all code that does O(1) edge lookups via matrix[i][j] must be rewritten to iterate neighbor lists, and graph traversal algorithms may need adjustment. This typically touches multiple functions/files within the graph component, warranting e5.

b5 Burden Structural debt — long-term weight of choosing wrong

Closest to 'persistent productivity tax' (b5). The graph representation choice is foundational to any graph-processing code. It applies across web and CLI contexts and shapes how every graph algorithm is written — traversal, shortest path, connectivity checks all depend on the representation. It's not quite system-defining (b7/b9) since graphs are usually one component, but it imposes a persistent tax on all graph-related work streams.

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'notable trap (a documented gotcha most devs eventually learn)' (t5). The misconception is 'adjacency matrix is always faster than adjacency list' — developers see O(1) edge lookup and assume matrix is universally superior, not realizing the O(V²) space cost makes it infeasible for sparse graphs. This is a well-documented gotcha taught in algorithms courses that most developers eventually learn, but it genuinely catches intermediate developers off guard.

About DEBT scoring →

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 81
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
1 ping T 1 ping W 1 ping T 0 pings F 0 pings S 0 pings S 1 ping M 1 ping T 0 pings W 1 ping T 1 ping F 1 ping S 1 ping S 3 pings M 0 pings T 1 ping 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 1 ping S 0 pings M 2 pings T 0 pings W
No pings yet today
Ahrefs 1 PetalBot 1
ChatGPT 17 Amazonbot 14 Perplexity 9 Ahrefs 6 Scrapy 5 Google 2 Meta AI 2 Claude 2 SEMrush 2 Unknown AI 1 Bing 1 Qwen 1 PetalBot 1
crawler 60 crawler_json 3
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