Skip to main content

πŸ•ΈοΈ Graph

Graphs unify trees, grids, social networks, and dependency systems. The two workhorses are BFS and DFS; everything else (shortest path, MST, SCC) is a variation.

This category contains 21 problems. Use the patterns below to recognize what's being asked, then jump to the problem list at the bottom.


🧠 Key Patterns​

  • BFS β€” Shortest path in unweighted graph. Level-by-level traversal.
  • DFS β€” Connectivity, cycle detection, topological order, path enumeration.
  • Topological Sort β€” DAG dependency resolution: Kahn (BFS) or DFS post-order.
  • Union-Find (DSU) β€” Dynamic connectivity, MST (Kruskal), grouping.
  • Dijkstra / Bellman-Ford β€” Shortest path with non-negative / negative weights.
  • Bipartite Check β€” Two-color BFS. Equivalent to: no odd cycle.
  • Grid-as-Graph β€” Islands, flood fill, walls-and-gates. 4 or 8 directions.

⚠️ Common Pitfalls​

  • Forgetting to mark visited before enqueueing β†’ exponential blowup.
  • Using Dijkstra on graphs with negative weights β€” undefined behavior.
  • Union-find without path compression OR union-by-rank is O(n)O(n) per op.

πŸ“š Study Resources​

πŸ“Ί Videos​

πŸ“– Books​

  • Introduction to Algorithms (CLRS) β€” Ch. 22 (Elementary), Ch. 23–26 (MST, SSSP, APSP)
  • Algorithm Design β€” Kleinberg & Tardos β€” Ch. 3

🌐 Articles & References​


πŸ’» All Graph Problems​