- Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathTopologicalSortingBFS_KahnAlgorithm.java
More file actions
Latest commit
81 lines (67 loc) · 3.39 KB
/
Copy pathTopologicalSortingBFS_KahnAlgorithm.java
File metadata and controls
81 lines (67 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
packageGraph;
importjava.util.ArrayList;
importjava.util.LinkedList;
importjava.util.Queue;
importjava.util.Scanner;
publicclassTopologicalSortingBFS_KahnAlgorithm {
publicstaticvoidmain(String[] args) {
Scannersc = newScanner(System.in);
ArrayList<ArrayList<Integer>> adjacencyList = newArrayList<>();
// Taking input for number of nodes
System.out.println("Enter the number of nodes:");
intnumberOfNodes = sc.nextInt();
// initializing adjacencyList with new ArrayList<>()
for(inti=1 ; i<=numberOfNodes+1 ; i++){
adjacencyList.add(newArrayList<>());
}
// taking input of adjacent nodes of particular node
for(inti=1 ; i<=numberOfNodes ; i++){
System.out.println("Enter number of adjacent nodes to the node : " + i);
intcountOfAdjacentNodes = sc.nextInt();
// taking input of the particular adjacent nodes to the node i
System.out.println("Enter adjacent nodes:");
for(intj=0 ; j<countOfAdjacentNodes ; j++){
// taking input of adjacent node
intadjacentNode = sc.nextInt();
// adding the adjacent node to the particular node i
adjacencyList.get(i).add(adjacentNode);
}
}
// This is a helper method which is used to call to topologicalSort method
// for printing the topological sort of the given graph
helperMethod(adjacencyList, numberOfNodes);
}
publicstaticvoidhelperMethod(ArrayList<ArrayList<Integer>> adjacencyList, intnumberOfNodes){
int[] inDegree = newint[numberOfNodes+1]; // for storing indegree of the nodes
// calculating indegree of the nodes
for(ArrayList<Integer> adjacentNodesList : adjacencyList){
for(intadjacentNode : adjacentNodesList){
inDegree[adjacentNode]++;
}
}
Queue<Integer> queue = newLinkedList<>(); // for storing final topological sorting of the graph nodes
// adding the elements in to the queue data structure whose indegree is 0
for(inti=1 ; i<=numberOfNodes ; i++){
if(inDegree[i] == 0){
queue.offer(i);
}
}
// for storing the topological sorting of the graph
ArrayList<Integer> topologicalSorting = newArrayList<>();
// While queue is not empty
while(!queue.isEmpty()){
intcurrentNode = queue.poll(); // current visiting node
// now check for adjacent node : decrease the indegree of the adjacent nodes of the current node
// if the indegree becomes 0 then add that adjacent node in to the queue data structure
for(intadjacentNode : adjacencyList.get(currentNode)){
inDegree[adjacentNode]--; // decreasing the indegree of the adjacent node
// if the indegree of adjacent node becomes 0 then add it to the queue
if(inDegree[adjacentNode] == 0){
queue.offer(adjacentNode);
}
}
topologicalSorting.add(currentNode); // finally add the current node into the final topological sorting list
}
System.out.println("Topological sorting of the given graph is : " + topologicalSorting);
}
}