Algorithms and Data Structures · Middle · Question 29 of 75

Why do graph traversals keep a visited set?

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).

Further reading

Problem with a question

Algorithms and Data Structures · Middle · question 29

Test yourself

Take the Algorithms and Data Structures · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz