โ Coding Interview Prep (LeetCode) learning pathCoding Interview Prep (LeetCode) ยท Module 8 of 10
Graphs: BFS and DFS
Graphs model connections; BFS and DFS explore them.
๐ Key ideas
- Represent graphs with adjacency lists.
- BFS explores level by level and finds the shortest path in unweighted graphs.
- DFS goes deep first, using recursion or a stack.
- Grid problems (islands, flood fill) are graphs of cells.
- Track visited nodes to avoid infinite loops.
๐ก Real-life example: BFS is ripples spreading in a pond; DFS is exploring one cave tunnel to its end before trying the next.
Adjacency listEach node lists its neighbours
BFSBreadth-first search, level by level
DFSDepth-first search, go deep first
Visited setNodes already explored