Graphs problems, grouped by pattern

Graphs show up as grids, dependencies and networks. BFS finds shortest paths in unweighted graphs, DFS explores components, topological sort orders dependencies, and Dijkstra handles weighted edges.

When to reach for it

  • A grid where you move between neighbours
  • Tasks with prerequisites
  • Shortest path, connected components or cycles

Mistakes to watch for

  • Marking visited on pop instead of on push in BFS
  • Missing disconnected components
  • Using BFS on weighted edges

The 18 problems

1BFS / DFS Basics

2Matrix Graphs

3Connected Components

4BFS for shortest path

5DFS - Complement Trick

6Multi source BFS

7Cycle Detection

8Topological Sort