# Topological Sort | Algorithm, Time Complexity

By Priyanshu Vaish|Updated : May 30th, 2022

Suppose we have a set of tasks, but certain tasks have to be performed before others. Only one task can be performed at a time, and each task 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.

The 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.

## 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:

## 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 the nodes of G 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: The main idea is that at each step, we exclude a node that does not depend on any other. 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 we subtract the source from the graph, new sources may appear because the edges of the source are excluded from the graph, or there is more than one source in the graph. 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.

write a comment

## FAQs

• 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 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 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. GradeStack Learning Pvt. Ltd.Windsor IT Park, Tower - A, 2nd Floor, Sector 125, Noida, Uttar Pradesh 201303 help@byjusexamprep.com