Topological SortingA 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 CodeNow 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++ CodeLet'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 SortingTopological 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.
Next TopicTernary Search Tree |