2. • Indtroduction
• Directed Acylic
Graph(DAG)
• Comparison of Algorithms
• Pseudocode of Top. Sort.
• Example of Topological
Sort
• Shortest Path in DAG with
Single Source
• Pseudocode
• Example
• Applications
• References
OUTLINE
3. WHAT IS TOPOLOGICAL SORT?
Topological sorting for Directed Acyclic Graph
(DAG) is a linear ordering of vertices such that
for every directed edge uv, vertex u comes
before v in the ordering. Topological Sorting
for a graph is not possible if the graph is not a
DAG.[1]
Introduction
4. In mathematics, particularly graph theory, and computer science, a
directed acyclic graph is a finite directed graph with no directed cycles.
That is, it consists of finitely many vertices and edges with each edge
directed from one vertex to another, such that there is no way to start
at any vertex v and follow a consistently-directed sequence of edges
that eventually loops back to v again. Equivalently, a DAG is a directed
graph that has a topological ordering, a sequence of the vertices such
that every edge is directed from earlier to later in the sequence.[2]
Directed Acylic Graph
6. DFS vs Kahn’s Algorithm vs
Topological Sort
In topological sorting DFS algorithm can be used as Kahn’s Algorithm . In DFS, a vertex is
printed and then DFS is called recursively for its adjacent vertices. In topological sorting, a
vertex is need to be printed before its adjacent vertices For an instance, in the given graph
below, the vertex ‘F’ should be printed before ‘A’, but unlie DFS, the vertex ‘E’ should also be
printed before vertex ‘A’. So Topological sorting is different from DFS. Or an instance, a DFS of
the shown graph is «F C D B A E», but it’s not a topological sorting.
Kahn Algorithm works by choosing vertices in the same order as the eventual topological sort
[3]. First, find a list of "start nodes" which have no incoming edges and insert them into a set S;
at least one such node must exist in an acyclic graph. [4]
Both Kahn’s algorithm’s and DFS’s complexity is O(E+V). Also there is a new algorithm is
available for topological sorting.
16. Shortest Path in DAG
with Single Source
In a general graph which is wieghted, single source
shortest distances can be calculated by using Bellman-
Ford Algorithm. If the graph has no negative weight, it
can be calculated more efficient by using Djikstra’s
Algorithm. But in DAG by using Topological Sorting as I
explained with an example before is better. Time
complexity of topological sorting is O(V+E). After finding
topological order, the algorithm process all vertices and
for every vertex, it runs a loop for all adjacent vertices.
Total adjacent vertices in a graph is O(E). So the inner
loop runs O(V+E) times. Therefore, overall time
complexity of this algorithm is O(V+E).[5]
17. How It Works
At the beginning, distances to all vertices as
infinite and distance to source as zero should
be initialized.
Then topological sorting of graph should be
found. Once topological order is found, we
should process all vertices one by one. For
each vertex processed, distances should be
updated.
26. IMPORTANT NOTE
Result of sorting may change for every node. For an
instance in the previous example if we choose C first, the
result will be [Inf, 0, 4, 10, 3, 4].
27. Applications
• While creating complex database tables, some tables
have interdependencies . Topological sort can determine
the order in which we create them.
• Determining what order to take courses and their pre-
requisites in to complete a degree.
• Formulating a lesson plan for a course
• Topological sort algorithm in UNIX rearranges source files to
define all the methods before they are used in the file.
• It is used to detect cycles in a graph.
28. REFERENCES
[1] : https://www.geeksforgeeks.org/topological-sorting/
[2]: https://en.wikipedia.org/wiki/Directed_acyclic_graph
[3]: A. B. Kahn, “Topological sorting of large
networks,”Communications of the ACM, vol. 5, no. 11,
pp. 558–562, 1962, doi: 10.1145/368996.369025.
[4]: http://www.ijmlc.org/papers/411-LC039.pdf
[5]: https://www.geeksforgeeks.org/shortest-path-for-
directed-acyclic-graphs/?ref=rp