Algorithms and Data Structures · Middle · Question 32 of 75

How does Kahn's algorithm compute a topological order?

Explanation

Keep in-degrees and a queue of vertices with in-degree 0. If the queue empties before all vertices are output, the graph has a cycle. The DFS version outputs vertices in reverse order of finishing, not of discovery.

Further reading

Problem with a question

Algorithms and Data Structures · Middle · question 32

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