Repository navigation
Expand file tree
/
Copy pathMainClass.java
More file actions
126 lines (98 loc) · 2.1 KB
/
Copy pathMainClass.java
File metadata and controls
126 lines (98 loc) · 2.1 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package graphs3;
import java.util.*;
class Graph3 {
ArrayList<ArrayList<Integer>> a = new ArrayList<ArrayList<Integer>>();
int v;
int indeg[];
// directed graph
public Graph3(int v) {
this.v = v;
for(int i = 0; i<v; i++) {
a.add(new ArrayList<Integer>());
}
indeg = new int[v];
}
public void addEdge(int src, int dest) {
a.get(src).add(dest);
}
void indegree() {
for(ArrayList<Integer> adjacent: a) {
for(Integer neighbor: adjacent) {
indeg[neighbor]++;
}
}
}
void topologicalSortUsingDFS() {
ArrayDeque<Integer> stack = new ArrayDeque<>();
boolean vis[] = new boolean[v];
for(int i = 0; i<v; i++) {
if(!vis[i]) {
dfs(i, vis, stack);
}
}
while(!stack.isEmpty()) {
System.out.print(stack.pop()+" ");
}
}
void dfs(int src, boolean vis[], ArrayDeque<Integer> stack) {
vis[src] = true;
for(Integer neighbor: a.get(src)) {
if(!vis[neighbor]) {
dfs(neighbor, vis, stack);
}
}
stack.push(src);
}
// Kahn's Algorithm
void topologicalSortUsingBFS() {
indegree();
int count = 0;
ArrayDeque<Integer> q = new ArrayDeque<>();
for(int i = 0; i<v; i++) {
if(indeg[i] == 0) {
q.add(i);
count++;
}
}
ArrayList<Integer> ans = new ArrayList<>();
while(!q.isEmpty()) {
int cur = q.poll();
ans.add(cur);
for(Integer neighbor: a.get(cur)) {
indeg[neighbor]--;
if(indeg[neighbor] == 0) {
q.add(neighbor);
count++;
}
}
}
if(count < v) {
System.out.println("cycle is present, no topological sort possible");
} else {
for(Integer e: ans) {
System.out.print(e+" ");
}
}
}
}
public class MainClass {
public static void main(String[] args) {
int v = 6;
Graph3 graph = new Graph3(v);
graph.addEdge(0, 1);
graph.addEdge(1, 3);
graph.addEdge(1, 2);
graph.addEdge(3, 4);
graph.addEdge(0, 2);
// graph.addEdge(3, 5);
graph.addEdge(5, 3);
graph.addEdge(4, 5);
// graph.indegree();
//
// for(int e: graph.indeg) {
// System.out.println(e);
// }
// graph.topologicalSortUsingDFS();
graph.topologicalSortUsingBFS();
}
}