{
    "slug": "adjacency_matrix_list",
    "term": "Adjacency Matrix vs Adjacency List",
    "category": "data_structures",
    "difficulty": "intermediate",
    "short": "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).",
    "long": "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.",
    "aliases": [
        "adjacency matrix",
        "adjacency list",
        "graph representation"
    ],
    "tags": [
        "data-structures",
        "algorithms",
        "graphs"
    ],
    "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."
    ],
    "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."
    ],
    "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."
    ],
    "related": [
        "graph_data_structure",
        "graph_algorithms",
        "big_o_notation"
    ],
    "prerequisites": [
        "graph_data_structure",
        "big_o_notation",
        "hash_table"
    ],
    "refs": [
        "https://en.wikipedia.org/wiki/Adjacency_list"
    ],
    "bad_code": "// Adjacency matrix for 1M user social graph:\n$matrix = array_fill(0, 1000000, array_fill(0, 1000000, false));\n// Memory: 1M * 1M * 1 byte = 1 terabyte — impossible\n// Even with bits: 125 GB",
    "good_code": "// Adjacency list for sparse social graph:\n$graph = [];\n// Average user has 200 friends:\n$graph[42] = [7, 99, 156, 203]; // User 42 follows these users\n// Memory: 1M users * 200 friends avg * 8 bytes = 1.6 GB — feasible\n\n// Edge check — O(degree):\nfunction hasEdge(array $graph, int $from, int $to): bool {\n    return in_array($to, $graph[$from] ?? []);\n}\n// For faster lookup use hash set per vertex:\n$graph[42] = array_flip([7, 99, 156, 203]); // O(1) lookup",
    "example_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.",
    "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",
    "severity": "low",
    "effort": "medium",
    "created": "2026-03-16",
    "updated": "2026-03-31",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/adjacency_matrix_list",
        "html_url": "https://codeclaritylab.com/glossary/adjacency_matrix_list",
        "json_url": "https://codeclaritylab.com/glossary/adjacency_matrix_list.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Adjacency Matrix vs Adjacency List](https://codeclaritylab.com/glossary/adjacency_matrix_list) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/adjacency_matrix_list"
            }
        }
    }
}