adjacency list
0
1
2
3
adjacency list
- 0:[]
- 1:[]
- 2:[]
- 3:[]
Build & read an adjacency list
▸1adj = map node -> [neighbours]2for each node u:3 adj[u] = []4 for each edge (u, v): adj[u].append(v)5// traverse:6for v in adj[u]: visit(v) // read neighbours directly7// matrix alternative costs O(V^2) space
state
- nodes4
- edges5
- listO(V+E)
- matrixO(V²)