โ† Competitive Programming learning path
Competitive Programming ยท Module 9 of 10

Graphs: BFS, DFS and shortest paths

Model connections and travel them efficiently.

๐Ÿ“– Key ideas

  1. Store graphs as adjacency lists.
  2. BFS explores level by level: shortest path in unweighted graphs.
  3. DFS goes deep: components, cycles, topological sort.
  4. Dijkstra finds shortest paths with non-negative weights in O((V+E) log V).
  5. Union-Find (DSU) merges sets and checks connectivity fast.
๐Ÿ’ก Real-life example: BFS is ripples spreading in a pond; DFS is exploring one corridor to its end before turning back.
BFSLevel-by-level search
DFSDepth-first search
DijkstraShortest paths with non-negative weights
DSUDisjoint set union for connectivity