  # What is Topological Sort?

By BYJU'S Exam Prep

Updated on: September 25th, 2023 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.

Download Formulas for GATE Computer Science Engineering – Programming & Data Structures

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