Practice free →
HomeGATE CSEcomputerscienceAlgorithms › Choose BFS over DFS when

Choose BFS over DFS when

Athe graph is a tree, not a general graph
Byou need cycle detection in a directed graph
Cyou need the shortest hop count from a source to other vertices
Dyou want to compute discovery and finish timestamps
Answer & Solution
Correct answer: C. you need the shortest hop count from a source to other vertices
BFS reaches vertices by increasing edge distance, so it naturally gives shortest hop counts in unweighted graphs. Cycle detection and timestamps are DFS specialities (back edges flag cycles, discovery/finish times power topological sort and SCC). Trees can use either; the choice depends on the question, not the graph type.
Solve this in the app — GATE CSE practice & 24k+ MCQs →
Related questions