Open In App

Kahn’s algorithm for Topological Sorting

Last Updated : 07 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

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=" ")


Output

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)

Application of Kahn’s algorithm for Topological Sort:   

  • Course sequencing: Courses at universities frequently have prerequisites for other courses. The courses can be scheduled using Kahn’s algorithm so that the prerequisites are taken before the courses that call for them.
  • Management of software dependencies: When developing software, libraries and modules frequently rely on other libraries and modules. The dependencies can be installed in the proper order by using Kahn’s approach.
  • Scheduling tasks: In project management, activities frequently depend on one another. The tasks can be scheduled using Kahn’s method so that the dependent tasks are finished before the tasks that depend on them.
  • Data processing: In data processing pipelines, the outcomes of some processes may be dependent. The stages can be carried out in the right order by using Kahn’s algorithm.
  • Circuit design: In the creation of an electronic circuit, some components may be dependent on the output of others. The components can be joined in the right order by using Kahn’s technique.
     


Previous Article
Next Article

Similar Reads

Kahn's Algorithm vs DFS Approach: A Comparative Analysis
Topological sorting is a common problem in computer science that involves arranging the vertices of a directed acyclic graph (DAG) in a linear order such that for every directed edge (u, v), vertex u comes before vertex v in the ordering. Two important methods to solve are: Kahn's AlgorithmDepth-First Search (DFS) AlgorithmLet us take the following
13 min read
Kahn's Algorithm in Python
Kahn's Algorithm is used for topological sorting of a directed acyclic graph (DAG). The algorithm works by repeatedly removing nodes with no incoming edges and recording them in a topological order. Here's a step-by-step implementation of Kahn's Algorithm in Python. Example: Input: V=6 , E = {{2, 3}, {3, 1}, {4, 0}, {4, 1}, {5, 0}, {5, 2}} Output:
4 min read
What is the stupidest sorting algorithm? (Worst Sorting Algorithm)
Bogo sort stands out as the undisputed champion of stupidity. Unlike other sorting algorithms that follow a structured approach, Bogo sort relies on sheer luck and randomness to achieve its goal. How Bogo Sort Works?Bogo sort operates on the following principle: Randomly shuffle the elements in the list.Check if the list is sorted.If the list is no
2 min read
Topological Sorting
Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of vertices such that for every directed edge u-v, vertex u comes before v in the ordering. Note: Topological Sorting for a graph is not possible if the graph is not a DAG. Example: Input: Graph : Output: 5 4 2 3 1 0Explanation: The first vertex in topological sorting is alwa
15+ min read
Sorting objects using In-Place sorting algorithm
Given an array of red, blue and yellow objects, the task is to use an in-place sorting algorithm to sort the array in such a way that all the blue objects appear before all the red objects and all the red objects appear before all the yellow objects.Examples: Input: arr[] = {"blue", "red", "yellow", "blue", "yellow"} Output: blue blue red yellow ye
7 min read
Know Your Sorting Algorithm | Set 1 (Sorting Weapons used by Programming Languages)
Ever wondered how sort() function we use in C++/Java or sorted() in Python work internally? Here is a list of all the inbuilt sorting algorithms of different programming languages and the algorithm they use internally. C’s qsort() – QuicksortBest Case Time Complexity- O(NlogN)Average Case Time Complexity- O(NlogN)Worse Case Time Complexity- O(N2)Au
2 min read
Detect cycle in Directed Graph using Topological Sort
Given a Directed Graph consisting of N vertices and M edges and a set of Edges[][], the task is to check whether the graph contains a cycle or not using Topological sort. Topological sort of directed graph is a linear ordering of its vertices such that, for every directed edge U -&gt; V from vertex U to vertex V, U comes before V in the ordering. E
9 min read
All Topological Sorts of a Directed Acyclic Graph
Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of vertices such that for every directed edge uv, vertex u comes before v in the ordering. Topological Sorting for a graph is not possible if the graph is not a DAG. Given a DAG, print all topological sorts of the graph. For example, consider the below graph. All topological
11 min read
Topological Sort of a graph using departure time of vertex
Given a Directed Acyclic Graph (DAG), find Topological Sort of the graph. Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of vertices such that for every directed edge uv, vertex u comes before v in the ordering. Topological Sorting for a graph is not possible if the graph is not a DAG. For example, a topological sorting o
9 min read
Lexicographically Smallest Topological Ordering
Given a directed graph with N vertices and M edges that may contain cycles, the task is to find the lexicographically smallest topological ordering of the graph if it exists otherwise print -1 (if the graph has cycles). Lexicographically smallest topological ordering means that if two vertices in a graph do not have any incoming edge then the verte
8 min read