Kahn's algorithm for Topological Sorting in JavaKahn's algorithm is a popular method used to perform topological sorting on a directed acyclic graph (DAG). Topological sorting is ordering the vertices in a DAG, such that for every directed edge (u, v), vertex u comes before vertex v in the ordering. In other words, there are no cycles in the sorted graph. Multiple topological sortings are possible for a graph. For instance, the following graph can have two valid topological sortings: "5 4 2 3 1 0" and "4 5 2 0 3 1". In any valid topological sorting, the first vertex is always a vertex with an indegree of 0, indicating it has no incoming edges from other vertices in the graph. Example: Input: Output: 5 4 2 3 1 0 Explanation: Topological sorting of a directed acyclic graph (DAG) is a linear ordering of its vertices. For every directed edge uv, vertex u comes before v in the ordering. It means that for any vertex v, all its dependencies (vertices with edges directed into v) must come before v in the ordering. In the example given, vertices 5 and 4 have no incoming edges, so they can be placed in any order in the topological ordering. The vertices 2 and 0 have incoming edges from 4 and 5, respectively, so they must come after 4 and 5 in the topological ordering. Finally, vertex 1 has no dependencies, so it must last in the topological ordering. Example: Input: Output: 0 3 4 1 2 Explanation: In the directed acyclic graph (DAG), 0 and 3 have no incoming edges, 4 and 1 have incoming edges from 0 and 3, and 2 are placed at last. Intuition: The intuition behind topological sorting is that we can start with the vertices that have no dependencies and then add the vertices that depend on them. It ensures that we never add a vertex before its dependencies, which would create a cycle in the graph.
The topological sorting algorithm is similar to a breadthfirst search (BFS) but only adds vertices to the queue if they have indegree 0. It ensures that the vertices are added to the topological ordering in a dependencyordered fashion. Algorithm
How do we find the indegree of each node? The indegree of a vertex is the number of edges that point to that vertex. In the code you provided, the indegree of each vertex is calculated in the following for loop: The for loop iterates through all of the adjacency lists in the graph. For each adjacency list, it iterates through all of the destinations of the edges in the adjacency list. For each destination, it increments the indegree of the destination vertex. It ensures that the indegree of each vertex is correct after we have finished iterating through all of the adjacency lists. Implementation:Filename: TopologicalSort.java Output: Topological Sorting: 4 5 2 0 3 1 Time Complexity: The time complexity of the code is O(V + E), where V is the number of vertices and E is the number of edges in the graph. Auxiliary Space: The auxiliary space complexity is O(V), which comes from the additional data structures used in the algorithm. Application of Kahn's algorithm for Topological Sort:
Next TopicMost Popular Java Backend Tools
