Computer Science I / Graph Algorithms and Traversal
Practice question · Sort into groups

Sort each task by which traversal is the natural fit.

Groups: Use BFS · Use DFS

Hints
  1. Shortest-by-edges and nearest-first are BFS strengths.
  2. Cycle detection and topological sort come naturally from DFS.
Show the answer

Use BFS: Find the shortest route in an unweighted maze, Find the nearest reachable vertex by edge count

Use DFS: Detect a cycle in a dependency graph, Topologically sort tasks with dependencies

Why

BFS suits shortest-edge and nearest-first questions; DFS suits cycle detection and topological sorting. Matching the traversal's exploration order to the task is the whole skill.

Read the lesson: Graph Algorithms and Traversal →

Practise Graph Algorithms and Traversal

The app has 6 more questions on this lesson, and keeps your place in the course. Computer Science I is free to start.

More questions on Graph Algorithms and Traversal