{
    "slug": "graph_algorithms",
    "term": "Graph Algorithms",
    "category": "algorithms",
    "difficulty": "advanced",
    "short": "Algorithms for traversing, searching, and finding paths in graphs — BFS for shortest hops, DFS for exploration, Dijkstra for weighted shortest paths.",
    "long": "Breadth-First Search (BFS): explores level by level using a queue — finds shortest path in unweighted graphs. Depth-First Search (DFS): explores depth-first using a stack/recursion — detects cycles, topological sort. Dijkstra: shortest path in weighted graphs with non-negative edges. A*: Dijkstra with a heuristic — faster for geographic routing. Topological sort: ordering of nodes with no cycles — dependency resolution, build systems. Applications: social network connections, dependency graphs, route finding, link analysis.",
    "aliases": [
        "BFS",
        "DFS",
        "Dijkstra",
        "graph traversal"
    ],
    "tags": [
        "algorithms",
        "graphs",
        "data-structures"
    ],
    "misconception": "BFS always finds the shortest path — BFS finds the path with fewest edges; in weighted graphs, Dijkstra finds the path with minimum total weight.",
    "why_it_matters": "Graph algorithms appear in dependency resolution (Composer), social network features, route planning, and detecting circular dependencies in code — practical applications in everyday PHP development.",
    "common_mistakes": [
        "Not tracking visited nodes in BFS/DFS — infinite loops on cyclic graphs.",
        "Using DFS for shortest path in unweighted graphs — BFS guarantees shortest; DFS does not.",
        "Dijkstra with negative edge weights — use Bellman-Ford for negative weights; Dijkstra produces wrong results.",
        "Adjacency matrix for sparse graphs — wastes O(V²) memory; adjacency list is O(V+E) and usually correct."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "big_o_notation",
        "dynamic_programming",
        "hash_table",
        "spl_data_structures"
    ],
    "prerequisites": [
        "graph_data_structure",
        "big_o_notation",
        "recursion_patterns"
    ],
    "refs": [
        "https://en.wikipedia.org/wiki/Graph_traversal"
    ],
    "bad_code": "// DFS for shortest path — wrong result on unweighted graph:\nfunction findPath(array $graph, string $start, string $end): array {\n    $visited = [];\n    $stack = [[$start, [$start]]];\n    while ($stack) {\n        [$node, $path] = array_pop($stack); // DFS — may not find shortest path\n        if ($node === $end) return $path;\n        foreach ($graph[$node] as $neighbour) {\n            if (!in_array($neighbour, $visited)) {\n                $visited[] = $neighbour;\n                $stack[] = [$neighbour, [...$path, $neighbour]];\n            }\n        }\n    }\n    return [];\n}",
    "good_code": "// BFS — guaranteed shortest path in unweighted graph:\nfunction shortestPath(array $graph, string $start, string $end): array {\n    $queue = [[$start, [$start]]];\n    $visited = [$start => true];\n    while ($queue) {\n        [$node, $path] = array_shift($queue); // BFS uses queue, not stack\n        if ($node === $end) return $path;\n        foreach ($graph[$node] ?? [] as $neighbour) {\n            if (!isset($visited[$neighbour])) {\n                $visited[$neighbour] = true;\n                $queue[] = [$neighbour, [...$path, $neighbour]];\n            }\n        }\n    }\n    return [];\n}",
    "quick_fix": "Use BFS for shortest path in unweighted graphs, Dijkstra for weighted, and DFS for cycle detection and topological sort — represent sparse graphs as adjacency lists not matrices",
    "severity": "medium",
    "effort": "high",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/graph_algorithms",
        "html_url": "https://codeclaritylab.com/glossary/graph_algorithms",
        "json_url": "https://codeclaritylab.com/glossary/graph_algorithms.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": "[Graph Algorithms](https://codeclaritylab.com/glossary/graph_algorithms) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/graph_algorithms"
            }
        }
    }
}