Q. In which order does BFS visit nodes in a graph?
-
A.
Pre-order
-
B.
In-order
-
C.
Post-order
-
D.
Level-order
Solution
BFS visits nodes in level-order, exploring all neighbors at the present depth prior to moving on to nodes at the next depth level.
Correct Answer:
D
— Level-order
Learn More →
Q. What happens if you apply DFS on a graph with cycles without tracking visited nodes?
-
A.
It will terminate successfully.
-
B.
It will enter an infinite loop.
-
C.
It will throw an error.
-
D.
It will only visit some nodes.
Solution
If DFS is applied on a graph with cycles without tracking visited nodes, it will enter an infinite loop, revisiting the same nodes.
Correct Answer:
B
— It will enter an infinite loop.
Learn More →
Q. Which of the following scenarios is BFS preferred over DFS?
-
A.
Finding the shortest path in an unweighted graph
-
B.
Exploring all possible paths
-
C.
Finding a cycle in a graph
-
D.
Topological sorting
Solution
BFS is preferred for finding the shortest path in an unweighted graph because it explores all nodes at the present depth before moving deeper.
Correct Answer:
A
— Finding the shortest path in an unweighted graph
Learn More →
Q. Which traversal method uses a stack to explore nodes?
-
A.
BFS
-
B.
DFS
-
C.
Dijkstra's Algorithm
-
D.
Prim's Algorithm
Solution
Depth-First Search (DFS) uses a stack to explore as far as possible along each branch before backtracking.
Correct Answer:
B
— DFS
Learn More →
Showing 1 to 4 of 4 (1 Pages)