Graph algorithms, step by step

20 interview patterns, each animated next to its C++ code. Press Space to play or pause, and ← → to step. Companion to the tree algorithms page.

:

C++

          
Unweighted graphs use vector<vector<int>> adj; weighted ones use vector<vector<pair<int,int>>> adj where adj[u] holds {v, w}. The animations visit neighbors in ascending order.