Topological Sorting

A topological sort or topological ordering of a directed graph is a linear ordering of its vertices in which u occurs before v in the ordering for every directed edge uv from vertex u to vertex v. For example, the graph's vertices could represent jobs to be completed, and the edges could reflect requirements that one work must be completed before another.

In this case, a topological ordering is just a legitimate task sequence. A topological sort is a graph traversal in which each node v is only visited after all of its dependencies have been visited. If the graph contains no directed cycles, then it is a directed acyclic graph. Any DAG has at least one topological ordering, and there exist techniques for building topological orderings in linear time for any DAG.

Topological sorting has many applications, particularly in ranking issues like the feedback arc set. Even if the DAG includes disconnected components, topological sorting is possible.

Java Code

Now let's write a sample java code for topological sorting.

Output

The above Java code gives the following output.

Please Choose one of the Operations::
1. To create a Directed Acyclic Graph (DAG) for topological sort.
2. To print the result of the topological sort.
1
Directed Acyclic Graph (DAG) created successfully.
Do you want to continue (Type y or n)
y
Please Choose one of the Operations::
1. To create a Directed Acyclic Graph (DAG) for topological sort.
2. To print the result of the topological sort.
2
The result after topological sort of the Directed Acyclic Graph
25  20 15 10 5  0 
Do you want to continue (Type y or n)
n

So, in the above Java code, we have created a class named Graph that will represent a directed graph using adjacency list representation. Different member functions to perform various operations like adding a new node in the graph are also written. A function named topologicalSort() is written to perform the actual task of the topological sort of the graph. The topologicalSort() function internally calls a recursive function named topologicalSortUtil() that consists of the actual logic of the topological sorting of the graph. Once the topological sorting is performed on the graph, the graph's nodes are printed due to the topological operation.

C++ Code

Let's see the C++ code,

Output

The above C++ code gives the following output.

Directed Acyclic Graph (DAG) created successfully.
The result of topological Sort is
5 4 2 3 1 0 

So, in the above C++ code, we created a class named Graph that will represent a directed graph using adjacency list representation. Different member functions to perform various operations like adding a new node in the Graph are also written. Then a function named topologicalSort() is written to perform the actual task of the topological sort of the graph. The topologicalSort() function internally calls a recursive function named topologicalSortUtil() that consists of the actual logic of the topological sorting of the graph. Once the topological sorting is performed on the graph, the graph's nodes are printed due to the topological operation.

Advantages of Topological Sorting

Topological Sorting is mostly used to schedule jobs based on their dependencies. Instruction scheduling, ordering formula cell evaluation when recomputing formula values in spreadsheets, logic synthesis, determining the order of compilation tasks to perform in make files, data serialization, and resolving symbol dependencies in linker are all examples of applications of this type in computer science.

  • Finding cycle in a graph: Only directed acyclic graphs may be ordered topologically (DAG). It is impossible to arrange a circular graph topologically.
  • Operation System deadlock detection: A deadlock occurs when one process is waiting while another holds the requested resource.
  • Dependency resolution: Topological Sorting has been proved to be very helpful in Dependency resolution.
  • Sentence Ordering: A set of n documents D={d1,d2...,dn} and the number of sentences in a document is vi, where ∀i, vi>=1. Suppose a random order o=[o1,....ovi] and a set of vi sentences in random order are {So1, So2,..., Sovi}. Then the task is to find the right order of the sentences o*={o*1,...o*vi}. A set of constraints Ci represents the relative ordering between every pair of sentences in di where |Ci|=(vi�(vi-1))/2. For example, if a document has three sentences in the correct order s1 < s2 < s3, then we have three set of constraints {s1 < s2, s1 < s3, s2 < s3}
    The order of the sentences can be represented using a DAG. Here the sentences (Si) represent the vertices, and the edges represent the ordering between sentences. For example, if we have a directed edge between S1 to S2, then S1 must come before S2. Topological sort can produce an ordering of these sentences (Sentence ordering).
  • Critical Path Analysis: A project management approach known as critical route analysis. It's used to figure out how long a project should take and how dependent each action is on the others. There may be some preceding actions before an activity. Before beginning a new activity, all previous actions must be completed.
  • Course Schedule problem: Topological Sorting has been proved to be very helpful in solving the Course Schedule problem.
  • Other applications like manufacturing workflows, data serialization, and context-free grammar.