Algorithms and Data Structures · Senior · Question 46 of 75

Which algorithm should you use to detect a cycle in module imports at build time?

Explanation

Imports form a directed graph; a cycle is a back edge in DFS, or leftover vertices after Kahn's topological sort. Union-find detects cycles only in undirected graphs, so it would report false cycles for diamonds.

Further reading

Problem with a question

Algorithms and Data Structures · Senior · question 46

Test yourself

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

Take this quiz