forked from striver79/StriversGraphSeries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
articulationJava
73 lines (58 loc) · 1.67 KB
/
articulationJava
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
import java.util.*;
class Main
{
private void dfs(int node, int parent, int vis[], int tin[], int low[], ArrayList<ArrayList<Integer>> adj, int timer, int isArticulation[]) {
vis[node] = 1;
tin[node] = low[node] = timer++;
int child = 0;
for(Integer it: adj.get(node)) {
if(it == parent) continue;
if(vis[it] == 0) {
dfs(it, node, vis, tin, low, adj, timer, isArticulation);
low[node] = Math.min(low[node], low[it]);
if(low[it] >= tin[node] && parent != -1) {
isArticulation[node] = 1;
}
child++;
} else {
low[node] = Math.min(low[node], tin[it]);
}
}
if(parent != -1 && child > 1) isArticulation[node] = 1;
}
void printBridges(ArrayList<ArrayList<Integer>> adj, int n)
{
int vis[] = new int[n];
int tin[] = new int[n];
int low[] = new int[n];
int isArticulation[] = new int[n];
int timer = 0;
for(int i = 0;i<n;i++) {
if(vis[i] == 0) {
dfs(i, -1, vis, tin, low, adj, timer, isArticulation);
}
}
for(int i = 0;i<n;i++) {
if(isArticulation[i] == 1) System.out.println(i);
}
}
public static void main(String args[])
{
int n = 5;
ArrayList<ArrayList<Integer> > adj = new ArrayList<ArrayList<Integer> >();
for (int i = 0; i < n; i++)
adj.add(new ArrayList<Integer>());
adj.get(0).add(1);
adj.get(1).add(0);
adj.get(0).add(2);
adj.get(2).add(0);
adj.get(1).add(2);
adj.get(2).add(1);
adj.get(1).add(3);
adj.get(3).add(1);
adj.get(3).add(4);
adj.get(4).add(3);
Main obj = new Main();
obj.printBridges(adj, n);
}
}