{
    "slug": "graph_data_structure",
    "term": "Graphs",
    "category": "data_structures",
    "difficulty": "intermediate",
    "short": "A collection of nodes (vertices) connected by edges — directed or undirected, weighted or unweighted. The most general data structure, modelling networks, dependencies, and relationships.",
    "long": "A graph G = (V, E) has vertices V and edges E. Directed graphs have one-way edges; undirected have two-way. Weighted graphs assign costs to edges. Representations: adjacency matrix (O(V²) space, O(1) edge lookup), adjacency list (O(V+E) space, better for sparse graphs). Cycles: DAGs (Directed Acyclic Graphs) have no cycles — used for dependency resolution (Composer), build systems, and topological sorting. Trees are DAGs with one path between any two nodes.",
    "aliases": [
        "directed graph",
        "undirected graph",
        "DAG",
        "adjacency list"
    ],
    "tags": [
        "data-structures",
        "algorithms",
        "graphs"
    ],
    "misconception": "Trees are not graphs — trees are a special case of graphs (connected DAG with no cycles); all trees are graphs but not all graphs are trees.",
    "why_it_matters": "Composer's dependency resolution, social network connections, route finding, and task scheduling are all graph problems — knowing the data structure enables choosing the right algorithm.",
    "common_mistakes": [
        "Adjacency matrix for sparse graphs — wastes O(V²) memory when most edges don't exist.",
        "Not detecting cycles before topological sort — Kahn's algorithm or DFS detects cycles.",
        "Treating directed edges as undirected — in a dependency graph, A depends on B is not the same as B depends on A.",
        "Infinite loops without a visited set in BFS/DFS on cyclic graphs."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "graph_algorithms",
        "binary_tree",
        "hash_table",
        "dynamic_programming"
    ],
    "prerequisites": [
        "graph_algorithms",
        "adjacency_matrix_list",
        "big_o_notation"
    ],
    "refs": [
        "https://en.wikipedia.org/wiki/Graph_(discrete_mathematics)"
    ],
    "bad_code": "// Adjacency matrix for sparse social graph — 1M users = 1TB memory:\n$matrix = array_fill(0, 1000000, array_fill(0, 1000000, 0));\n// 10^12 entries for a graph with ~1 billion edges",
    "good_code": "// Adjacency list — O(V+E) space:\n$graph = [\n    'A' => ['B', 'C'],\n    'B' => ['D'],\n    'C' => ['D', 'E'],\n    'D' => [],\n    'E' => [],\n];\n// Only stores actual edges — efficient for sparse graphs\n// 5 vertices, 5 edges — exactly 10 entries total",
    "quick_fix": "Represent sparse graphs (social networks, dependency trees) as adjacency lists ($graph[$node][] = $neighbour) — use adjacency matrices only for dense graphs where every node connects to many others",
    "severity": "low",
    "effort": "medium",
    "created": "2026-03-16",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/graph_data_structure",
        "html_url": "https://codeclaritylab.com/glossary/graph_data_structure",
        "json_url": "https://codeclaritylab.com/glossary/graph_data_structure.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": "[Graphs](https://codeclaritylab.com/glossary/graph_data_structure) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/graph_data_structure"
            }
        }
    }
}