Kahn’s Algorithm for Topological Sorting
Kahn’s Algorithm for Topological Sorting is a method used to order the vertices of a directed graph in a linear order such that for every directed edge from vertex A to vertex B, A comes before B in the order. The algorithm works by repeatedly finding vertices with no incoming edges, removing them from the graph, and updating the incoming edges of the remaining vertices. This process continues until all vertices have been ordered.
Algorithm:
- Add all nodes with in-degree 0 to a queue.
- While the queue is not empty:
- Remove a node from the queue.
- For each outgoing edge from the removed node, decrement the in-degree of the destination node by 1.
- If the in-degree of a destination node becomes 0, add it to the queue.
- If the queue is empty and there are still nodes in the graph, the graph contains a cycle and cannot be topologically sorted.
- The nodes in the queue represent the topological ordering of the graph.
How to find the in-degree of each node?
To find the in-degree of each node by initially calculating the number of incoming edges to each node. Iterate through all the edges in the graph and increment the in-degree of the destination node for each edge. This way, you can determine the in-degree of each node before starting the sorting process.
Below is the implementation of the above algorithm.
C++
// Including necessary header file #include <bits/stdc++.h> using namespace std; // Function to return list containing vertices in // Topological order. vector< int > topologicalSort(vector<vector< int > >& adj, int V) { // Vector to store indegree of each vertex vector< int > indegree(V); for ( int i = 0; i < V; i++) { for ( auto it : adj[i]) { indegree[it]++; } } // Queue to store vertices with indegree 0 queue< int > q; for ( int i = 0; i < V; i++) { if (indegree[i] == 0) { q.push(i); } } vector< int > result; while (!q.empty()) { int node = q.front(); q.pop(); result.push_back(node); // Decrease indegree of adjacent vertices as the // current node is in topological order for ( auto it : adj[node]) { indegree[it]--; // If indegree becomes 0, push it to the queue if (indegree[it] == 0) q.push(it); } } // Check for cycle if (result.size() != V) { cout << "Graph contains cycle!" << endl; return {}; } return result; } int main() { // Number of nodes int n = 4; // Edges vector<vector< int > > edges = { { 0, 1 }, { 1, 2 }, { 3, 1 }, { 3, 2 } }; // Graph represented as an adjacency list vector<vector< int > > adj(n); // Constructing adjacency list for ( auto i : edges) { adj[i[0]].push_back(i[1]); } // Performing topological sort cout << "Topological sorting of the graph: " ; vector< int > result = topologicalSort(adj, n); // Displaying result for ( auto i : result) { cout << i << " " ; } return 0; } |
Java
import java.util.*; public class TopologicalSort { // Function to return list containing vertices in // Topological order. public static List<Integer> topologicalSort(List<List<Integer> > adj, int V) { // Vector to store indegree of each vertex int [] indegree = new int [V]; for (List<Integer> list : adj) { for ( int vertex : list) { indegree[vertex]++; } } // Queue to store vertices with indegree 0 Queue<Integer> q = new LinkedList<>(); for ( int i = 0 ; i < V; i++) { if (indegree[i] == 0 ) { q.add(i); } } List<Integer> result = new ArrayList<>(); while (!q.isEmpty()) { int node = q.poll(); result.add(node); // Decrease indegree of adjacent vertices as the // current node is in topological order for ( int adjacent : adj.get(node)) { indegree[adjacent]--; // If indegree becomes 0, push it to the // queue if (indegree[adjacent] == 0 ) q.add(adjacent); } } // Check for cycle if (result.size() != V) { System.out.println( "Graph contains cycle!" ); return new ArrayList<>(); } return result; } public static void main(String[] args) { int n = 4 ; // Number of nodes // Edges List<List<Integer> > edges = Arrays.asList( Arrays.asList( 0 , 1 ), Arrays.asList( 1 , 2 ), Arrays.asList( 3 , 1 ), Arrays.asList( 3 , 2 )); // Graph represented as an adjacency list List<List<Integer> > adj = new ArrayList<>(); for ( int i = 0 ; i < n; i++) { adj.add( new ArrayList<>()); } // Constructing adjacency list for (List<Integer> edge : edges) { adj.get(edge.get( 0 )).add(edge.get( 1 )); } // Performing topological sort System.out.print( "Topological sorting of the graph: " ); List<Integer> result = topologicalSort(adj, n); // Displaying result for ( int vertex : result) { System.out.print(vertex + " " ); } } } |
C#
using System; using System.Collections.Generic; class Program { // Function to return list containing vertices in // Topological order. static List< int > TopologicalSort(List<List< int > > adj, int V) { // Vector to store indegree of each vertex int [] indegree = new int [V]; foreach ( var list in adj) { foreach ( var vertex in list) { indegree[vertex]++; } } // Queue to store vertices with indegree 0 Queue< int > q = new Queue< int >(); for ( int i = 0; i < V; i++) { if (indegree[i] == 0) { q.Enqueue(i); } } List< int > result = new List< int >(); while (q.Count > 0) { int node = q.Dequeue(); result.Add(node); // Decrease indegree of adjacent vertices as the // current node is in topological order foreach ( var adjacent in adj[node]) { indegree[adjacent]--; // If indegree becomes 0, push it to the // queue if (indegree[adjacent] == 0) q.Enqueue(adjacent); } } // Check for cycle if (result.Count != V) { Console.WriteLine( "Graph contains cycle!" ); return new List< int >(); } return result; } static void Main( string [] args) { int n = 4; // Number of nodes // Edges List<List< int > > edges = new List<List< int > >{ new List< int >{ 0, 1 }, new List< int >{ 1, 2 }, new List< int >{ 3, 1 }, new List< int >{ 3, 2 } }; // Graph represented as an adjacency list List<List< int > > adj = new List<List< int > >(); for ( int i = 0; i < n; i++) { adj.Add( new List< int >()); } // Constructing adjacency list foreach ( var edge in edges) { adj[edge[0]].Add(edge[1]); } // Performing topological sort Console.Write( "Topological sorting of the graph: " ); List< int > result = TopologicalSort(adj, n); // Displaying result foreach ( var vertex in result) { Console.Write(vertex + " " ); } } } |
Javascript
// Function to return list containing vertices in Topological order. function topologicalSort(adj, V) { // Vector to store indegree of each vertex const indegree = new Array(V).fill(0); for (let i = 0; i < V; i++) { for (const vertex of adj[i]) { indegree[vertex]++; } } // Queue to store vertices with indegree 0 const q = []; for (let i = 0; i < V; i++) { if (indegree[i] === 0) { q.push(i); } } const result = []; while (q.length > 0) { const node = q.shift(); result.push(node); // Decrease indegree of adjacent vertices as the current node is in topological order for (const adjacent of adj[node]) { indegree[adjacent]--; // If indegree becomes 0, push it to the queue if (indegree[adjacent] === 0) q.push(adjacent); } } // Check for cycle if (result.length !== V) { console.log( "Graph contains cycle!" ); return []; } return result; } const n = 4; // Number of nodes // Edges const edges = [[0, 1], [1, 2], [3, 1], [3, 2]]; // Graph represented as an adjacency list const adj = Array.from({ length: n }, () => []); // Constructing adjacency list for (const edge of edges) { adj[edge[0]].push(edge[1]); } // Performing topological sort console.log( "Topological sorting of the graph: " ); const result = topologicalSort(adj, n); // Displaying result for (const vertex of result) { console.log(vertex + " " ); } |
Python3
from collections import deque # Function to return list containing vertices in Topological order. def topological_sort(adj, V): # Vector to store indegree of each vertex indegree = [ 0 ] * V for i in range (V): for vertex in adj[i]: indegree[vertex] + = 1 # Queue to store vertices with indegree 0 q = deque() for i in range (V): if indegree[i] = = 0 : q.append(i) result = [] while q: node = q.popleft() result.append(node) # Decrease indegree of adjacent vertices as the current node is in topological order for adjacent in adj[node]: indegree[adjacent] - = 1 # If indegree becomes 0, push it to the queue if indegree[adjacent] = = 0 : q.append(adjacent) # Check for cycle if len (result) ! = V: print ( "Graph contains cycle!" ) return [] return result if __name__ = = "__main__" : n = 4 # Number of nodes # Edges edges = [[ 0 , 1 ], [ 1 , 2 ], [ 3 , 1 ], [ 3 , 2 ]] # Graph represented as an adjacency list adj = [[] for _ in range (n)] # Constructing adjacency list for edge in edges: adj[edge[ 0 ]].append(edge[ 1 ]) # Performing topological sort print ( "Topological sorting of the graph:" , end = " " ) result = topological_sort(adj, n) # Displaying result for vertex in result: print (vertex, end = " " ) |
Topological sorting of the graph: 0 3 1 2
Complexity Analysis:
- Time Complexity: O(V+E).
The outer for loop will be executed V number of times and the inner for loop will be executed E number of times. - Auxiliary Space: O(V).
The queue needs to store all the vertices of the graph. So the space required is O(V)
Kahn’s algorithm for Topological Sorting
Given a Directed Acyclic Graph having V vertices and E edges, your task is to find any Topological Sorted order of the graph.
Topological Sorted order: It is a linear ordering of vertices such that for every directed edge u -> v, where vertex u comes before v in the ordering.
Example:
Input: V=6 , E = {{2,3},{3,1},{4,0},{4,1},{5,0},{5,2}}
Output: 5 4 2 3 1 0
Explanation: In the above output, each dependent vertex is printed after the vertices it depends upon.Input: V=5 , E={{0,1},{1,2},{3,2},{3,4}}
Output: 0 3 4 1 2
Explanation: In the above output, each dependent vertex is printed after the vertices it depends upon.
Contact Us