Algorithms and Data Structures · Middle · Question 33 of 75

How do you detect a cycle in a directed graph with DFS?

Explanation

Use three states: unvisited, in progress (on the stack) and done. Only an edge to an "in progress" vertex is a back edge. An edge to a "done" vertex is fine: it's just another path to the same place.

Further reading

Problem with a question

Algorithms and Data Structures · Middle · question 33

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