All Patterns

Graphs

Model relationships as nodes and edges, then traverse with BFS (shortest path) or DFS (all paths, connectivity).

When to use

  • Connected components
  • Shortest path
  • Dependency ordering
  • Cycle detection
  • Minimum spanning tree

When NOT to use

  • Simple linear data
  • No relationships between elements

Common traps

  • Forgetting visited set
  • Confusing directed vs undirected
  • Not handling disconnected components

Key Invariant

BFS finds shortest unweighted paths; DFS explores all paths — always track visited nodes

Problems (29)

#37Number of Islands
mediumFREE
#84Clone Graph
medium
#85Pacific Atlantic Water Flow
medium
#86Surrounded Regions
medium
#169Network Delay Time (Dijkstra)
medium
#170Cheapest Flights Within K Stops
medium
#171Minimum Spanning Tree (Prim's)
medium
#172Kruskal's MST
medium
#173Topological Sort
medium
#174Is Graph Bipartite
medium
#175Graph Valid Tree
medium
#176Minimum Height Trees
medium
#178Evaluate Division
medium
#179Open the Lock
medium
#180Shortest Path in Binary Matrix
medium
#181Rotting Oranges
medium
#182Shortest Bridge
medium
#183All Paths From Source to Target
medium
#184Keys and Rooms
medium
#279Minimum Cost to Connect All Points
medium
#280Path with Maximum Probability
medium
#281Find City With Smallest Number of Neighbors at a Threshold Distance
medium
#40Course Schedule
hardFREE
#105Word Ladder
hard
#106Alien Dictionary
hard
#107Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree
hard
#109Course Schedule II
hard
#177Reconstruct Itinerary
hard
#285Bus Routes
hard