Time and Space Complexity
Lesson 7 of 9 in Coddy's Topological Sort - Graph Algorithms course.
Time Complexity:
- O(V2 + E) as written here, because each of the V rounds scans all vertices to find the smallest in-degree-0 one. With a priority queue it improves to O((V + E) log V), and with a plain queue (any valid order) to O(V + E).
Space Complexity:
- O(V + E) for the adjacency list, in-degree array, and output.
Summary:
- Topological Sort orders a DAG so every edge points forward, using in-degrees and Kahn's algorithm.
- If not all vertices can be placed, the graph contains a cycle.
Try it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Topological Sort - Graph Algorithms
2The Algorithm
How it works?Pseudo CodeImplementation (Part 1)Implementation (Part 2)