Graph

31 articles

dsa2 min read

Clone Graph — BFS/DFS with HashMap

Deep copy an undirected graph. BFS/DFS with a HashMap from original node to cloned node prevents revisiting and handles cycles.

Read →
dsa1 min read

Open the Lock — BFS State Space

Minimum turns to reach target combination avoiding deadends. BFS over all 4-digit wheel states with bidirectional BFS optimization.

Read →
dsa1 min read

Course Schedule II — Topological Order

Return a valid course order given prerequisites. Kahn's BFS topological sort outputs nodes in processing order — that is the valid schedule.

Read →
dsa2 min read

Word Ladder — BFS Shortest Transformation

Find minimum transformations from beginWord to endWord changing one letter at a time (each intermediate must be in wordList). Classic BFS on word states.

Read →
dsa1 min read

Bus Routes — BFS on Route Graph

Find minimum buses to get from source to target stop. BFS where nodes are routes (buses), not stops. Jump to all stops of a route, then to all routes passing through each stop.

Read →
dsa2 min read

Reconstruct Itinerary — Hierholzer's Eulerian Path

Find the itinerary using all flight tickets exactly once (Eulerian path). Uses Hierholzer's algorithm: DFS with post-order insertion into result — the only graph algorithm that uses post-order for path construction.

Read →