DSA Crash Course
Foundation Data Structures

1.9 Graphs

Mark when done:

Deep Dive: Graph Traversal Visualized

Beginner tip: A graph is a set of dots (nodes) connected by lines (edges). Think of a social network: each person is a node, friendships are edges. Trees are special graphs with no loops. Most graph problems boil down to: "Visit every reachable node" (DFS/BFS), "Find the shortest path" (BFS), or "Detect a loop" (DFS with coloring).

BFS vs DFS on Number of Islands

Grid (1=land, 0=water):

1 1 0 0 0
1 1 0 0 0
0 0 1 0 0
0 0 0 1 1

DFS (island 1): Start at (0,0). Go deep first:

(0,0) -> (0,1) -> (1,1) -> (1,0) -> backtrack, backtrack...
Marked: all four '1's in top-left as visited. Island count = 1.

BFS (island 1): Start at (0,0). Go wide first:

Queue: [(0,0)]
  Process (0,0) -> enqueue neighbors (0,1), (1,0)
Queue: [(0,1), (1,0)]
  Process (0,1) -> enqueue (1,1)
  Process (1,0) -> (1,1) already queued
Queue: [(1,1)]
  Process (1,1) -> no new neighbors
Island count = 1. Then find (2,2) -> island 2. Then (3,3) -> island 3.

Both give the same answer (3 islands). BFS finds shortest paths; DFS uses less memory on narrow graphs.

Cycle Detection: The 3-State Trick

For directed graphs (Course Schedule), use 3 states:

State 0 = UNVISITED (white)
State 1 = VISITING  (gray)  -- currently in recursion stack
State 2 = VISITED   (black) -- fully processed

If you reach a GRAY node -> CYCLE! (you looped back to an ancestor)
If you reach a BLACK node -> safe (already processed, no cycle through here)

BFS = Shortest Path (Unweighted)

Why BFS gives shortest path but DFS does not:

BFS explores ALL nodes at distance 1, then ALL at distance 2, etc.
The first time BFS reaches a node, that IS the shortest path.

DFS might reach a node via a long winding path first.

Quick Reference (Cheat Sheet)

Before You Start -- How to Think About Graphs

Why Graphs Exist

Need: Model arbitrary relationships (friends, roads, dependencies). Key insight: Trees are special graphs (connected, no cycles). Graphs are the general case.

The Graph Mindset

Every graph problem is about TRAVERSAL. The question is: BFS or DFS? And what do I track during traversal?

Step 1: How Is the Graph Given?

Before solving, figure out the representation:

Given AsConvert ToExample
Edge list [[0,1],[1,2]]Adjacency list{0:[1], 1:[0,2], 2:[1]}
Adjacency matrixUsually use as-ismatrix[i][j] == 1 means edge
Grid/MatrixImplicit graphNeighbors = 4 directions (up/down/left/right)
Prerequisites [[1,0]]Directed adjacency list{0:[1]} means 0->1

Step 2: BFS or DFS?

Use BFS When...Use DFS When...
Shortest path (unweighted)Explore all paths
Level-by-level processingDeep dive first
"Minimum steps to reach""Does a path exist?"
Multi-source spread (rotten oranges)Connected components

Step 3: What Extra State Do I Track?

- VISITED set -> Always. Prevents infinite loops in cycles.
- DISTANCE / LEVEL -> For shortest path / level-order
- PARENT map -> To reconstruct the actual path
- COLOR / STATE -> For cycle detection (white/gray/black)
- COMPONENT ID -> For counting connected components

Graph Problem Categories

CategorySignalTechnique
Traversal"Visit all", "connected"BFS or DFS
Shortest Path"Minimum steps", "closest"BFS (unweighted), Dijkstra (weighted)
Cycle Detection"Is valid tree?", "redundant"DFS (back edge) or Union-Find
Topological Sort"Prerequisites", "ordering"Kahn's BFS or DFS
Connected Components"Number of islands", "groups"DFS/BFS or Union-Find

Common Mistakes

  • Forgetting the visited set -> infinite loop on cycles
  • For grids: forgetting to mark visited BEFORE adding to queue (causes duplicates)
  • Directed vs undirected: adding edges in both directions when it should be one-way
  • Topological sort: forgetting to check for cycles (if cycle exists, no valid ordering)

Problems

#ProblemDifficultyPatternStatus
1Number of IslandsMediumDFS/BFS flood fill[ ]
2Clone GraphMediumBFS + HashMap[ ]
3Course Schedule I & IIMediumTopological Sort[ ]
4Pacific Atlantic Water FlowMediumMulti-source BFS[ ]
5Graph Valid TreeMediumUnion-Find / DFS[ ]
6Number of Connected ComponentsMediumDFS / Union-Find[ ]
7Word LadderHardBFS shortest path[ ]

Notes