Stage 1 · Code
Graph Algorithms
Graph Representation
Adjacency matrix, adjacency list, and edge list in Go.
Adjacency Matrix
V×V matrix where matrix[u][v] = 1 (or weight) if edge u→v exists. O(V²) space, O(1) edge check.
Adjacency List
Array of lists where list[u] contains all neighbors of u. O(V+E) space, O(deg(u)) to iterate neighbors.
Edge List
Simple list of all edges (u, v, w). O(E) space. Good for algorithms that process all edges (Kruskal, Bellman-Ford).
Comparison
| Operation | Matrix | List | Edge List |
|---|---|---|---|
| Space | O(V²) | O(V+E) | O(E) |
| Add edge | O(1) | O(1) | O(1) |
| Check edge | O(1) | O(deg(u)) | O(E) |
| Iterate neighbors | O(V) | O(deg(u)) | O(E) |
| Best for | Dense, small V | Sparse, general | Edge-centric algos |
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.