Algorithms and Data Structures · Middle · Question 29 of 75
Why do graph traversals keep a visited set?
- 1To keep the vertices sorted during the traversal
- ✓Correct answer: To avoid processing vertices twice or looping on cycles
- 3To store the final answer the algorithm returns
Explanation
Unlike trees, graphs can have cycles and several paths to the same vertex. Without visited a traversal can revisit vertices endlessly; with it every vertex is processed once, O(V + E).