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

Graph Algorithms

Algorithms PHP 5.0+ Advanced
debt(d9/e5/b3/t7)
d9 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'silent in production until users hit it' (d9). The detection_hints field explicitly states 'automated: no' and the code_pattern describes hand-rolled implementations of cycle detection, shortest path, etc. No tool in the ecosystem automatically detects wrong algorithm selection (e.g. DFS used instead of BFS for shortest path, or Dijkstra on negative-weight graphs). These bugs only surface at runtime when wrong results are returned or infinite loops are hit on cyclic graphs in production.

e5 Effort Remediation debt — work required to fix once spotted

Closest to 'touches multiple files / significant refactor in one component' (e5). The quick_fix outlines multiple algorithm swaps (BFS↔DFS, Dijkstra↔Bellman-Ford, adjacency matrix→adjacency list). Swapping a core graph traversal implementation typically requires reworking the data structure representation and the traversal logic together, touching graph construction, traversal, and any downstream consumers of results — more than a one-line patch but generally contained within one component rather than a full cross-cutting refactor.

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

Closest to 'localised tax' (b3). Graph algorithms typically live in a bounded component (dependency resolver, social feature, route planner). While the choice of algorithm and representation affects that component's correctness and performance, it rarely imposes a tax on the broader codebase. The applies_to scope (web, cli) is wide but the actual graph code tends to be isolated.

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

Closest to 'serious trap (contradicts how a similar concept works elsewhere)' (t7). The canonical misconception is that 'BFS always finds the shortest path' — which is true for unweighted graphs but false for weighted graphs where Dijkstra is required. Additionally, Dijkstra with negative edge weights produces silently wrong results. These are not minor edge cases: a competent developer familiar with BFS in unweighted contexts will confidently apply it in weighted contexts and get wrong answers, and will apply Dijkstra with negative weights trusting it because the algorithm 'looks right'. Multiple serious traps across the algorithm family justify t7.

About DEBT scoring →

Also Known As

BFS DFS Dijkstra graph traversal

TL;DR

Algorithms for traversing, searching, and finding paths in graphs — BFS for shortest hops, DFS for exploration, Dijkstra for weighted shortest paths.

Explanation

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.

Diagram

flowchart LR
    subgraph BFS_shortest_path
        A1[A] -->|1| B1[B] & C1[C]
        B1 -->|1| D1[D]
        C1 -->|1| D1
        D1 -->|1| E1[E]
    end
    subgraph Dijkstra_weighted
        N1[N1] -->|4| N2[N2]
        N1 -->|2| N3[N3]
        N3 -->|1| N2
        N2 -->|3| N4[N4]
        N3 -->|5| N4
    end
    style A1 fill:#238636,color:#fff
    style E1 fill:#f85149,color:#fff
    style N1 fill:#238636,color:#fff
    style N4 fill:#f85149,color:#fff

Common 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.

Code Examples

✗ Vulnerable
// DFS for shortest path — wrong result on unweighted graph:
function findPath(array $graph, string $start, string $end): array {
    $visited = [];
    $stack = [[$start, [$start]]];
    while ($stack) {
        [$node, $path] = array_pop($stack); // DFS — may not find shortest path
        if ($node === $end) return $path;
        foreach ($graph[$node] as $neighbour) {
            if (!in_array($neighbour, $visited)) {
                $visited[] = $neighbour;
                $stack[] = [$neighbour, [...$path, $neighbour]];
            }
        }
    }
    return [];
}
✓ Fixed
// BFS — guaranteed shortest path in unweighted graph:
function shortestPath(array $graph, string $start, string $end): array {
    $queue = [[$start, [$start]]];
    $visited = [$start => true];
    while ($queue) {
        [$node, $path] = array_shift($queue); // BFS uses queue, not stack
        if ($node === $end) return $path;
        foreach ($graph[$node] ?? [] as $neighbour) {
            if (!isset($visited[$neighbour])) {
                $visited[$neighbour] = true;
                $queue[] = [$neighbour, [...$path, $neighbour]];
            }
        }
    }
    return [];
}

Added 15 Mar 2026
Edited 22 Mar 2026
Views 78
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
1 ping T 0 pings W 1 ping T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 2 pings F 2 pings 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 1 ping M 1 ping T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W
No pings yet today
No pings yesterday
Perplexity 13 Amazonbot 13 Scrapy 9 Google 7 Ahrefs 5 ChatGPT 4 Unknown AI 2 Claude 2 Bing 2 Majestic 1 Meta AI 1
crawler 56 crawler_json 3
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: High
⚡ 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
📦 Applies To
PHP 5.0+ web cli
🔗 Prerequisites
🔍 Detection Hints
Dependency resolution cycle detection social graph shortest path implemented without graph algorithm library
Auto-detectable: ✗ No
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: High Context: Function Tests: Update


✓ schema.org compliant