Vertices + edges. No root, cycles allowed. How you store it sets the cost of everything after.
| Adjacency list | Adjacency matrix | |
|---|---|---|
| Space | O(V+E) | O(V²) |
| has_edge | O(degree) | O(1) |
| neighbors | O(degree) | O(V) |
| Best for | sparse (most) | dense / edge tests |
Each edge appends to both endpoints' neighbor lists.
Sparse graph, 4000 vertices: 437 KB (list) vs 125 MB (matrix) → 286×. Matrix also makes every traversal O(V²) instead of O(V+E).
Default to the adjacency list — real graphs are sparse. Every algorithm ahead is O(V+E) because of it. Next: BFS and DFS.