Topological Sort of a graph is a depth-first search on a directed graph. A topological sort of a dag (directed graph) G = (V, E) is a linear ordering of all its vertices such that if G contains an edge (u, v) then u appears before v in the ordering. (If the graph contains a cycle, then no linear ordering is possible.)
The algorithm for a topological sort is the following:
TOPOLOGICAL-SORT (G)
Lemma 22.11
A directed graph G is acyclic if and only if a depth-first search of G
yields no back edges.
Topological sort sorts directed acyclic graphs (basically posets), which have the property that if you can get from node A to node B, then there's no way to get from node B to node A. The sorting algorithm will place node A before node B if node B can be reached from node A.
The algorithm is really really simple: run a depth-first search. Each time we hit a dead end, insert that node in the front of a linked list. The end. Since inserting into the front of a linked list has O(1) and we insert V nodes, this adds an additional O(V) work and the algorithm still runs in linear time.
A topological sort is sort of a method of compressing the vertices of a directed graph with no cycles, meaning no loops, into a line. If there is a path from vertex u to vertex v, then u comes before v in the ordering. To do this, we first run a depth first search. As we discover each node, it is placed at the front of a linked list. Since the depth first search runs in Theta(vertices + edges), and inserting a node at the front of a linked list takes Theta(1), this sort will take Theta(vertices+edges).
Topological sort is an application of depth-first search. G=(V,E) is a linear ordering of all its vertices, which means v is before e. Majority of this sorting use directed acyclic graphs. The example from the reading explains this method very well.
Topological sort can be applied to a directed acyclic graph, or a DAG. It sorts the graph in a linear ordering so that all of its vertices, on an edge (u, v), u appears before v in the ordering. If there's a cycle in the graph, then this linear ordering is impossible. v would have to start before u, but we need u to finish before v, and we wouldn't even be able to visit u if that were the case, which leads us to the conclusion that topological sort will not be possible on a cyclic graph. A DAG and the topological sort are used to indicate what event takes precedence over another. A real world application of this would for example be job scheduling.
A topological sort is a depth-first search that orders the vertices "such that if G contains an edge (u,v), then u appears before v in the ordering". The algorithm goes: call depth first search for each vertex, when each DFS is finishes, put it on the front of a linked list, return that list. The performance of this sort is Omega(V+E).
In this section, we learn that we can use depth-first search to perform a topological sort of a directed acyclic graph. In topological search, you first call depth first search, and after each vertex is popped from the stack, you insert it in the front of a linked list and your return this sorted linked list of vertices. Topological takes O(V+E) time.
In this chapter we discuss the topographical sort. Which is a linerar ordering of all vertices of a directed acyclic graph such that if an element contains an esge (u, v), then u appears before v. Topographical sort produces a topological sort of the directed acyclic graph.
A topological sort of a dag(G=(V,E)) is a linear ordering of all its vertices such that if G contains an edge then u appears before v in the ordering.
Here is how to do Topological sort:
Here is the algorithm to find the strongly-connected components:
In topological sort, is there more than one possible solution when ordering the verticies (depending on starting point)?
If we have a cycle, do we just return a failure, or is there an accepted way of dealing with cycles in topological sorting?
Would you mind going over exactly why topological sort only takes time theta(V+E)?
Would you mind going over the proofs on page 614?
[Overall, this makes a lot more sense after Prof. Walker's explanations on Wednesday.]
I don't quite understand what "topological" means.
Topological sorting seems pretty straightforward but I have a question about cyclic graphs. Can we topologically sort part of a graph if only some subset of its vertices are cyclical?
What is a practical use for topological sort?
Could you go over the proofs on page 614?
Why are strongly connected components important?
What is the transpose of a graph?
What happens if a strongly connected component has a sub-graph containing a smaller strongly connected component?
Could you go over the proofs?
This document is available on the World Wide Web as
http://www.walker.cs.grinnell.edu/courses/301.fa13/student-comments/student-comments-clrs-sec22.4.shtml
created November 23, 2013 last revised November 23, 2013 |
![]() ![]() |
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |