LeetEye LeetEye
Pattern

Graphs

Model relationships and connections between entities.

8 Problems
0 Easy
8 Medium
0 Hard

How Graphs Works

Graphs pattern visualization

Graph problems are solved using BFS (breadth-first search) for shortest paths and level exploration, or DFS (depth-first search) for exhaustive exploration and connectivity. Build an adjacency list from edges, then traverse. BFS uses a queue and processes nodes level by level, guaranteeing shortest path in unweighted graphs. DFS uses recursion or a stack, going as deep as possible before backtracking. Track visited nodes to avoid cycles. Union-Find is used for connectivity queries and cycle detection in undirected graphs.

When to Use Graphs

Pattern Recognition

Look for these trigger words in problem statements:

number of islands graphs clone graph pacific atlantic water flow course schedule course schedule ii rotting oranges surrounded regions graph valid tree

Common Mistakes

  • Not marking nodes as visited, causing infinite loops in cyclic graphs
  • Using DFS when BFS is needed for shortest path (DFS doesn't guarantee shortest path)
  • Building the adjacency list incorrectly for undirected graphs (add edges both ways)
  • Not handling disconnected components (run BFS/DFS from every unvisited node)

When NOT to Use Graphs

  • When the problem is about sequences without relationships (use arrays/DP)
  • When the graph is actually a tree (simpler tree algorithms suffice)
  • When the data is grid-based with simple traversal (might be simpler to index directly)

Practice Problems

Master Graphs

Build pattern recognition with interactive MCQs. Understand why to use Graphs, not just how.

Download LeetEye Free
Practice in LeetEye