Data Structures & Algorithms · Revision notes
Graphs & Graph Algorithms
A graph G=(V,E) models pairwise relationships. Choose adjacency list (O(V+E) space) or matrix (O(V^2)); traverse with BFS (shortest path in unweighted graphs) or DFS (cycles, components, topological order); use Dijkstra for non-negative weighted shortest paths.
Quick revision
- List O(V+E) space; matrix O(V^2) O(1) edge check; sum deg = 2E
- BFS: queue, mark on enqueue, shortest path (unweighted), O(V+E)
- DFS: back edge = cycle (directed); parent check (undirected); components
- Dijkstra: non-negative only, O((V+E) log V), skip stale heap entries; negatives -> Bellman-Ford O(VE)
- Topological: Kahn in-degree queue; processed < V means cycle