7.3. Topological Sorting
This section discusses two algorithms for topological sorting.
The task of topological sorting is to sort vertices in a linear order such that for each vertex, all target vertices associated with its incoming edges must appear prior to it.
By Incoming Scores
The followings demonstrate how to perform a topological sort by incoming scores, where the incoming score of a vertex is define as the sum of all source vertices:
The followings demonstrate how to perform a topological sort by depth-first traversing:
Let us define the topological_sort() method under the Graph class that performs topological sort by either incoming scores or depth-first search:
L2: starts with vertices with no incoming edges.
L3: retrieves outgoing edges of each source vertex.
What is the worst-case complexity of the topological_sort() method?