PATTERN 14A: Graph Traversal & Properties
D
Qubits of DPK
April 11, 2026
Core DSA
Core Intent
Model relationships, reason about structure, ordering, cycles, and optimization.
EASY: Graph Modeling & Reachability
- #Graph Representation – Adjacency List — GFG
- #DFS Traversal of a Graph — GFG
- #BFS Traversal of a Graph — GFG
- #Number of Connected Components — LC 323
- #Find if Path Exists in Graph — LC 1971
- #Number of Provinces — LC 547
- #Clone Graph — LC 133
- #Graph Valid Tree — LC 261
- #Keys and Rooms — LC 841
- #Find the Town Judge — LC 997
Now EASY locks:
- DFS vs BFS intuition
- Adjacency list usage
- Visited tracking
- Undirected graph logic
- Basic property reasoning
️ MEDIUM: Cycles, Ordering, Graph Properties
Now reasoning starts.
- #Course Schedule — LC 207
- #Course Schedule II — LC 210
- #Topological Sort (DFS) — GFG
- #Bipartite Graph — LC 785
- #Alien Dictionary — LC 269
- #Evaluate Division — LC 399
- #Find Eventual Safe States — LC 802
- #Reorder Routes to Make All Paths Lead to City Zero — LC 1466
- #All Paths From Source to Target — LC 797
- #Minimum Height Trees — LC 310
This section locks:
- Directed vs undirected thinking
- Topological ordering
- Coloring logic
- Reverse graph reasoning
- Graph pruning
HARD: Optimization & Advanced Graph Theory
Now weighted, structural, and deep reasoning.
- #Dijkstra’s Algorithm — GFG
- #Network Delay Time — LC 743
- #Cheapest Flights Within K Stops — LC 787
- #Minimum Spanning Tree (Kruskal) — GFG
- #Minimum Spanning Tree (Prim) — GFG
- #Number of Ways to Arrive at Destination — LC 1976
- #Critical Connections (Bridges) — LC 1192
- #Strongly Connected Components (Kosaraju) — GFG
- #Longest Path in DAG — GFG
- #Shortest Path in a DAG — GFG
Now this section cleanly covers:
- Weighted shortest path
- MST
- Bridge detection
- SCC
- DAG DP-style logic
INTERVIEW REVISION CARD — Pattern 14A
1️⃣ Pattern in One Line
"Model relationships as a graph — then reason about reachability, cycles, ordering, and connectivity."
2️⃣ Recognize It When...
- Problem has entities with relationships/dependencies
- Trigger words: "connected", "path exists", "cycle", "order", "prerequisite", "bipartite"
- Can be modeled as nodes + edges
- Need to visit/classify all nodes
3️⃣ Don't Confuse With...
- BFS (P14B) → use BFS for shortest path/minimum steps; DFS for reachability/cycles
- Union-Find (P15) → DSU for dynamic connectivity; Graph DFS for static traversal
- Tree (P11) → trees are acyclic graphs; general graphs can have cycles
4️⃣ Approach in 3 Steps
- #Build adjacency list from input
- #Choose DFS (cycle/path/ordering) or BFS (shortest path/levels)
- #Track visited array — mark when entering, unmark if backtracking needed
5️⃣ Invariant to Maintain
"Every visited node is never re-processed — the visited array ensures each node is handled exactly once."
6️⃣ Complexity to Justify
- Time: O(V + E) — visit each vertex and edge once
- Space: O(V + E) — adjacency list + visited array
- Why: Each node and edge processed exactly once
7️⃣ Edge Cases to Always Check
- Disconnected graph — must start DFS/BFS from all unvisited nodes
- Self-loops
- No edges (isolated nodes)
- Directed vs undirected — handle accordingly
- Cycle detection differs for directed vs undirected
8️⃣ One Line to Say in Interview
"I'll model this as a graph, build an adjacency list, then apply DFS/BFS — O(V+E) time and space."