Engrain by ByteRush

DSA.11.01 Mid-levelCore — asked often

Pattern 14: Graph traversal (BFS/DFS on graphs and grids)

Comes up in: DSA

This concept is being written. It gets the full five steps — explanation, predict, interview angle, practice with tests, and review cards. Here is exactly what it will cover, so you can study it elsewhere in the meantime.

What it covers

  • visited sets
  • grid neighbours
  • multi-source BFS
  • BFS for shortest path in unweighted graphs
  • connected components

Words you will know

visited setmulti-source BFSconnected component

What you will practise

  • flood-fill (E)
  • number-of-islands (M)
  • clone-graph (M)
  • rotting-oranges (M)
  • pacific-atlantic (M)
  • word-ladder (H)

Interview questions this answers

  • Count the islands in a grid. What is the complexity?
  • How do you detect a cycle in a directed graph?
  • Why does BFS give the shortest path in an unweighted graph?