-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphs_Supplemental.java
263 lines (211 loc) · 7.91 KB
/
Graphs_Supplemental.java
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import java.util.ArrayList;
import java.util.Stack;
public class Graphs_Supplemental {
/*
Topics Covered:
* Strongly Connected Components (Kosaraju Algorithm)
* Bridges in the Graph (Tarjan's Algorithm)
* Articulation Point in the Graph (Tarjan's Algorithm)
*/
public static void main(String[] args) {
Graph g = new Graph(5);
g.add_edge(1, 0);
g.add_edge(0, 2);
g.add_edge(2, 1);
g.add_edge(0, 3);
g.add_edge(3, 4);
System.out.println("Strongly Connected Components (Kosaraju Algorithm): "); kosaraju_algorithm(g.graph);
Graph g2 = new Graph(6);
g2.add_edge(1, 0);
g2.add_edge(0, 1);
g2.add_edge(1, 2);
g2.add_edge(2, 1);
g2.add_edge(2, 0);
g2.add_edge(0, 2);
g2.add_edge(3, 0);
g2.add_edge(0, 3);
g2.add_edge(3, 4);
g2.add_edge(4, 3);
g2.add_edge(5, 4);
g2.add_edge(4, 5);
g2.add_edge(5, 3);
g2.add_edge(3, 5);
System.out.println("Bridges in the Graph: "); bridges_in_graph(g2.graph);
Graph g3 = new Graph(5);
g3.add_edge(0, 1);
g3.add_edge(0, 2);
g3.add_edge(0, 3);
g3.add_edge(1, 0);
g3.add_edge(1, 2);
g3.add_edge(2, 0);
g3.add_edge(2, 1);
g3.add_edge(3, 0);
g3.add_edge(3, 4);
g3.add_edge(4, 3);
System.out.println("Articulation Points in the Graph: "); articulation_points(g3.graph);
}
// Strongly Connected Component
public static void kosaraju_algorithm(ArrayList<Graph.Edge> graph[]){
// step 1: Get nodes in stack
Stack<Integer> stack = new Stack<>();
boolean visited[] = new boolean[graph.length];
for (int i = 0; i < visited.length; i++) {
if(visited[i] == false){
topological_sort(graph, i, visited, stack);
}
}
// step 2: Transpose the graph
Graph transpose = new Graph(graph.length);
// reverse all edges
for(int i=0; i<graph.length; i++){
for(int j=0; j<graph[i].size(); j++){
Graph.Edge edge = graph[i].get(j);
transpose.add_edge(edge.dest, edge.src);
}
}
// step 3: Perform DFS on transposed graph according to Topological Sort order
visited = new boolean[graph.length];
while(!stack.isEmpty()){
int curr = stack.pop();
if(visited[curr] == false){
System.out.print("\tSCC -> ");
dfs(transpose.graph, visited, curr);
System.out.println();
}
}
}
public static void topological_sort(ArrayList<Graph.Edge> graph[], int curr, boolean visited[], Stack<Integer> stack){
visited[curr] = true;
for(int i=0; i<graph[curr].size(); i++){
Graph.Edge edge = graph[curr].get(i);
if(visited[edge.dest] == false){
topological_sort(graph, edge.dest, visited, stack);
}
}
stack.push(curr);
}
public static void dfs(ArrayList<Graph.Edge> graph[], boolean visited[], int curr){
visited[curr] = true;
System.out.print(curr + " ");
for(int i=0; i<graph[curr].size(); i++){
Graph.Edge edge = graph[curr].get(i);
if(visited[edge.dest] == false){
dfs(graph, visited, edge.dest);
}
}
}
public static void bridges_in_graph(ArrayList<Graph.Edge> graph[]){
boolean visited[] = new boolean[graph.length];
int discoveryTime[] = new int[graph.length];
int low[] = new int[graph.length];
for(int i=0; i<graph.length; i++){
if(visited[i] == false){
bridges_in_graph_helper(graph, visited, i, -1, discoveryTime, low, 0);
}
}
}
// Bridge in Graph (Modified DFS)
public static void bridges_in_graph_helper(ArrayList<Graph.Edge> graph[], boolean visited[], int curr, int parent, int discoveryTime[], int low[], int time){
visited[curr] = true;
discoveryTime[curr] = low[curr] = ++time; // start from 1
for(int i=0; i<graph[curr].size(); i++){
Graph.Edge edge = graph[curr].get(i);
int neighbour = edge.dest;
if(neighbour != parent){ // neighbour not parent
if(visited[neighbour] == false){
bridges_in_graph_helper(graph, visited, neighbour, curr, discoveryTime, low, time);
low[curr] = Math.min(low[curr], low[neighbour]); // low = lowest of low of neighbour
if(low[neighbour] > discoveryTime[curr]){ // if low[curr] < low[neightbour] // no other way to reach
System.out.println("\t"+curr+" -> "+neighbour);
}
}else{
// already visited
low[curr] = Math.min(low[curr], discoveryTime[neighbour]);
}
}
}
}
public static void articulation_points(ArrayList<Graph.Edge> graph[]){
int discoveryTime[] = new int[graph.length];
int low[] = new int[graph.length]; // lowest discovery time
int time = 0;
boolean visited[] = new boolean[graph.length];
boolean ap[] = new boolean[graph.length];
for(int i=0; i<graph.length; i++){
if(visited[i] == false){
articulation_points_helper(graph, i, -1, time, discoveryTime, low, visited, ap);
}
}
System.out.print("\t");
for(int i=0; i<graph.length; i++){
if(ap[i] == true){
System.out.print(i + ", ");
}
}
System.out.println();
}
public static void articulation_points_helper(ArrayList<Graph.Edge> graph[], int curr, int parent, int time,
int discoveryTime[], int low[], boolean visited[], boolean ap[]){
visited[curr] = true;
discoveryTime[curr] = low[curr] = ++time;
int children = 0;
for(int i=0; i<graph[curr].size(); i++){
Graph.Edge e = graph[curr].get(i);
int neighbour = e.dest;
if(parent == neighbour){ // ignore
continue;
} else if(visited[neighbour] == true){
low[curr] = Math.min(low[curr], discoveryTime[neighbour]);
} else {
articulation_points_helper(graph, neighbour, curr, time, discoveryTime, low, visited, ap);
low[curr] = Math.min(low[curr], low[neighbour]);
// possibility of articulation point
if(parent != -1 && discoveryTime[curr] <= low[neighbour]){
// System.out.print(curr + ", ");
ap[curr] = true; // curr is A.P.
}
children++;
}
}
if(parent == -1 && children > 1){
ap[curr] = true;
}
}
}
class Graph {
int V;
ArrayList<Edge>[] graph;
@SuppressWarnings("unchecked")
public Graph(int V){
this.V = V;
graph = new ArrayList[V];
for(int i=0; i<V; i++){
this.graph[i] = new ArrayList<>();
}
}
class Edge {
int src;
int dest;
int weight;
public Edge(int src, int dest, int weight){
this.src = src;
this.dest = dest;
this.weight = weight;
}
public Edge(int src, int dest){
this.src = src;
this.dest = dest;
this.weight = 1;
}
@Override
public String toString() {
return "("+src+", "+dest+")";
}
}
void add_edge(int src, int dest, int weight){
graph[src].add(new Edge(src, dest, weight));
}
void add_edge(int src, int dest){
graph[src].add(new Edge(src, dest));
}
}