|  | 
|  | 1 | +//Reference - https://www.geeksforgeeks.org/topological-sorting-indegree-based-solution/ | 
|  | 2 | + | 
|  | 3 | +/*Algorithm -  | 
|  | 4 | +Step-1: Compute in-degree (number of incoming edges) for each of the vertex present in the DAG and initialize the count of visited nodes as 0. | 
|  | 5 | +
 | 
|  | 6 | +Step-2: Pick all the vertices with in-degree as 0 and add them into a queue (Enqueue operation) | 
|  | 7 | +
 | 
|  | 8 | +Step-3: Remove a vertex from the queue (Dequeue operation) and then. | 
|  | 9 | +
 | 
|  | 10 | +    Increment count of visited nodes by 1. | 
|  | 11 | +    Decrease in-degree by 1 for all its neighboring nodes. | 
|  | 12 | +    If in-degree of a neighboring nodes is reduced to zero, then add it to the queue. | 
|  | 13 | +
 | 
|  | 14 | +Step 5: Repeat Step 3 until the queue is empty. | 
|  | 15 | +
 | 
|  | 16 | +Step 5: If count of visited nodes is not equal to the number of nodes in the graph then the topological sort is not possible for the given graph. | 
|  | 17 | +*/ | 
|  | 18 | + | 
|  | 19 | +class TopologicalSort{ | 
|  | 20 | +    int vertices; | 
|  | 21 | +    ArrayList<Integer> adjList[]; | 
|  | 22 | +    //adjList[2] signifies all the neighbours of vertex 2. The edge is directed from 2 ==> (all elements in adjList[2]) | 
|  | 23 | +     | 
|  | 24 | +    topologicalSort(int n){ | 
|  | 25 | +        vertices = n; | 
|  | 26 | +        adjList = new ArrayList[n]; | 
|  | 27 | +        for(int i=0;i<n;i++) adjList[i] = new ArrayList<Integer>(); | 
|  | 28 | +    } | 
|  | 29 | +     | 
|  | 30 | +    void topologicalSort(){ | 
|  | 31 | +        int inDegree[] = new int[vertices];  | 
|  | 32 | +        ArrayList<Integer> sortOrder = new ArrayList<>(); | 
|  | 33 | +        Arrays.fill(inDegree,0); | 
|  | 34 | +         | 
|  | 35 | +        for(int i=0 ; i<vertices ; i++){ | 
|  | 36 | +            ArrayList<Integer> neighbours = adjList[i]; | 
|  | 37 | +            for(int neighbour : neighbours){ | 
|  | 38 | +                inDegree[neighbour]++; | 
|  | 39 | +            } | 
|  | 40 | +        } | 
|  | 41 | +         | 
|  | 42 | +        Queue<Integer> inDegree0 = new LinkedList<>(); | 
|  | 43 | +        for(int i=0;i<vertices;i++){ | 
|  | 44 | +            if(inDegree[i]==0) inDegree0.add(i); | 
|  | 45 | +        } | 
|  | 46 | +         | 
|  | 47 | +        int vertexCnt = 0; | 
|  | 48 | +        while(!inDegree0.isEmpty()){ | 
|  | 49 | +            int curr = inDegree0.poll(); | 
|  | 50 | +            sortOrder.add(curr); | 
|  | 51 | +            ArrayList<Integer> neighbours = adjList[curr]; | 
|  | 52 | +            for(int neighbour : neighbours){ | 
|  | 53 | +                if(--inDegree[neighbour]==0) inDegree0.add(neighbour); | 
|  | 54 | +            } | 
|  | 55 | +            vertexCnt++; | 
|  | 56 | +        } | 
|  | 57 | +         | 
|  | 58 | +        if(vertexCnt!=vertices) System.out.println("Cycles in the graph"); | 
|  | 59 | +        else{ | 
|  | 60 | +            for(int i=0;i<vertices;i++) System.out.println(sortOrder.get(i)); | 
|  | 61 | +        } | 
|  | 62 | +    } | 
|  | 63 | +     | 
|  | 64 | +} | 
0 commit comments