What is Topological Sort?

By Priyanshu Vaish|Updated : August 26th, 2022

Topological Sort exists if and only if the graph does not contain any cycle (that is, no self-contradict constraints). These precedence constraints form the directed acyclic graph. Any topological sort (also known as the linear extension) defines an order to do these tasks such that each is performed only after all of its constraints are satisfied.

Suppose we have a set of tasks, but specific tasks must be performed before others. Only one task can be performed at a time, and each must be completed before the next task begins. Such a relationship can be represented as a directed graph, and topological sort can be used to schedule tasks under precedence constraints. We will determine if an order exists such that this set of tasks can be completed under the given constraints. Such an order is called a topological sort of graph G, where G is the graph containing all tasks, the vertices are tasks, and the edges are constraints. Here, we will discuss the definition of topological sort and various topological sorting methods.

Definition of Topological Sort

The topological sort of a DAG 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.

Mathematically, this ordering is described by a function t that maps each vertex to a number
t: V → (1, 2,..., n): t(v) i such that t(u) < t(v) < t(w) for the vertices u, v, w of the following diagram:

Topological Sort

In the above diagram, we see that the node u has no incoming edge, and u comes first as above said, so the node u is removed and added to the topological sort order as u now the node v comes, it also not has incoming edges so, node v is removed and update the topological sort order as uv. Similarly, for the last node, the topological sort order is uvw.

Topological Sort using DFS

The topological sort time complexity using DFS takes θ(m+n) time, and the insertion of each vertex to the linked list takes O(1) time. Thus, topological sort using DFS takes time θ(m + n). The following algorithm is as follows:

1. Perform DFS to compute finishing times f[v] for each vertex v
2. As each vertex is finished, insert it to the front of a linked list
3. Return the linked list of vertices

Topological Sort using Bucket Sorting

This algorithm computes a topological sort of a DAG beginning from a source and uses bucket sorting to arrange G nodes by their in-degree (# incoming edges).
An array of lists of nodes forms the bucket structure. Each node also maintains links to the vertices (nodes) to which its outgoing edges lead. The lists are sorted so that all vertices with n incoming edges lay on the list at the nth position of the table, as shown in the figure:

Topological Sort 2

The main idea is to exclude a node that does not depend on any other at each step. That maps to removing an entry v1 from the 0th index of the bucket structure (which contains the sources of G) and reducing by one the in-degree of the entry's adjacent nodes and reallocating them in the bucket. This means that node v2 is moved to the list at the 0th index and thus becomes a source. v3 is moved to the list at the 1st index, and v4 is moved to the list at the 2nd index. After subtracting the source from the graph, new sources may appear because the source edges are excluded from the graph, or there is more than one source. Thus the algorithm subtracts a source at every step and inserts it into a list until no sources (and consequently no nodes) exist. The resulting list represents the topological sort of the graph.

Important Topics for Gate Exam
Responsivity Types Of Governors
SR Flip-FlopTypes Of Vibration
Norton's TheoremWelded Joints
P N Junction DiodeRound Robin Scheduling
Classless AddressingScaling In Computer Graphics

Comments

write a comment

FAQs on Topological Sort

  • The graph traversal in which each node x is visited only after all of its dependencies has been visited is known as the topological sort. It is a directed acyclic graph if the graph contains no directed cycles

  • The topological sort is used to schedule the jobs from given dependencies among Jobs. The topological sort is used to determine the order of compilation tasks to perform in resolving symbol dependencies in linkers, data serializations, and making files.

  • The linearization of the DAG that ranges in the linearization where the topological order does not matter is sorted lexicographically and is said to be a "stable topological sort".

  • The steps of topological sorting are:

    • Step 1: First, identify the node with no incoming edges that is indegree of the node is zero.
    • Step 2: After finding the node, deletes the node from the graph and adds it to the topological sort order.
    • Step 3: After deleting the node, remove the outgoing edges from the graph.
    • Step 4: Reduce the indegree by the number of related edges that were removed.
  • Instead of recursion, we can use a queue to implement topological sort. To begin, go around all of the edges and count how many edges lead to each vertex (i.e., count the number of prerequisites for each vertex). The queue is filled with all vertices that do not have any precondition. The queue is then started to be processed.

Follow us for latest updates