SOARDO
Software Engineer (SDE)dsaintermediate3 interactive MCQs in app

BFS & DFS on Graphs

mark visited on BFS enqueue, not pophandling cycles and disconnected componentsBFS unweighted shortest path vs DFS flood-fill

Graphs can contain CYCLES and disconnected components, so both BFS and DFS require a `visited` set and an outer `for node in range(n)` loop! For **BFS**, **mark nodes `visited` AT ENQUEUE TIME** (right when pushing into `deque`), NEVER only after popping—otherwise multiple neighbors push the same node repeatedly into the queue causing exponential blowups!

BFS counts unweighted shortest path distances (`dist[v] = dist[u] + 1`) cleanly, while DFS excels at connected-component flood-fill (`Number of Islands`) and backtracking paths.
marking `visited` on pop instead of enqueue in BFS, or skipping outer component loops.
BFS on Graphs — GeeksforGeeksread ↗BFS & DFS on Graphs — Applied Practicepractice ↗VisuAlgo Interactive Visualizationsvisualize ↗NeetCode DSA Roadmappractice ↗

Practice BFS & DFS on Graphs with 3 graded MCQs & written feedback.

Take SOARDO’s free 12-question diagnostic to see how strong you are across your target role and unlock adaptive daily missions.

Test yourself free on SOARDO →View full Software Engineer roadmap

More Software Engineer Study Guides