Graphs
Also Known As
directed graph
undirected graph
DAG
adjacency list
TL;DR
A collection of nodes (vertices) connected by edges — directed or undirected, weighted or unweighted. The most general data structure, modelling networks, dependencies, and relationships.
Explanation
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.
Common 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.
Code Examples
✗ Vulnerable
// Adjacency matrix for sparse social graph — 1M users = 1TB memory:
$matrix = array_fill(0, 1000000, array_fill(0, 1000000, 0));
// 10^12 entries for a graph with ~1 billion edges
✓ Fixed
// Adjacency list — O(V+E) space:
$graph = [
'A' => ['B', 'C'],
'B' => ['D'],
'C' => ['D', 'E'],
'D' => [],
'E' => [],
];
// Only stores actual edges — efficient for sparse graphs
// 5 vertices, 5 edges — exactly 10 entries total
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
16 Mar 2026
Edited
22 Mar 2026
Views
38
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Amazonbot 13
Perplexity 11
Ahrefs 3
Unknown AI 2
Google 2
Majestic 1
Also referenced
How they use it
crawler 30
crawler_json 1
pre-tracking 1
Related categories
⚡
DEV INTEL
Tools & Severity
🟢 Low
⚙ Fix effort: Medium
⚡ 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
📦 Applies To
any
web
cli
🔗 Prerequisites
🔍 Detection Hints
V×V adjacency matrix for sparse graph wasting O(V²) memory; no graph representation for dependency resolution or social network features
Auto-detectable:
✗ No
⚠ Related Problems
🤖 AI Agent
Confidence: Low
False Positives: High
✗ Manual fix
Fix: Medium
Context: Class
Tests: Update