-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tarjan_Algorithm.java
127 lines (117 loc) · 3.76 KB
/
Tarjan_Algorithm.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
import java.util.*;
public class Tarjan_Algorithm {
public static class edge{
int src;
int dest;
edge(int s,int d){
this.src = s;
this.dest = d;
}
}
public static void createGraph(ArrayList<edge>graph[]){
for(int i = 0;i<graph.length;i++){
graph[i] = new ArrayList<>();
}
graph[0].add(new edge(0,2));
graph[0].add(new edge(0,3));
graph[1].add(new edge(1,0));
graph[1].add(new edge(1,2));
graph[2].add(new edge(2,0));
graph[2].add(new edge(2,1));
graph[3].add(new edge(3,0));
graph[3].add(new edge(3,4));
graph[3].add(new edge(3,5));
graph[4].add(new edge(4,3));
graph[4].add(new edge(4,5));
graph[5].add(new edge(5,3));
graph[5].add(new edge(5,4));
}
public static void dfs(ArrayList<edge>graph[],boolean vis[],int dt[],int low[],int curr,int parent,int time){
vis[curr] = true;
dt[curr]=low[curr] = ++time;
for(int i=0;i<graph[curr].size();i++){
edge e = graph[curr].get(i);
int neigh = e.dest;
if(neigh == parent){
continue;
}
else if(!vis[neigh]){
dfs(graph, vis, dt, low, neigh, curr, time);
low[curr] = Math.min(low[curr],low[neigh]);
if(dt[curr]<low[neigh]){
System.out.println("bright: "+curr+" <-----> "+neigh);
}
}
else if(vis[neigh]){
low[curr] = Math.min(low[curr],dt[neigh]);
}
}
}
public static void trajanAlgo_Bridge(ArrayList<edge>graph[],int v){
int dt[] = new int[v];
int low[] = new int [v];
boolean vist[] = new boolean [v];
int time = 0;
for(int i = 0;i<graph.length;i++){
if(!vist[i]){
dfs(graph, vist, dt, low, i, -1, time);
}
}
}
public static void dfsAP(ArrayList<edge>graph[],
boolean vis[],
boolean ap[],
int dt[],
int low[],
int curr,
int parent,
int time)
{
vis[curr] = true;
dt[curr] = low[curr] = ++time;
int children = 0;
for(int i = 0;i<graph[curr].size();i++){
edge e = graph[curr].get(i);
int neigh = e.dest;
if(neigh == parent){
continue;
}
else if(vis[neigh]){
low[curr] = Math.min(low[curr],dt[neigh]);
}else{
dfsAP(graph, vis, ap, dt, low, neigh, curr, time);
low[curr] = Math.min(low[curr],low[neigh]);
if(parent !=-1 && dt[curr]<=low[neigh]){
ap[curr] = true;
}
}
children++;
}
if(parent == -1 && children>1){
ap[curr]=true;
}
}
public static void articulationPoint(ArrayList<edge>graph[],int v){
int dt[] = new int[v];
int low[] = new int[v];
boolean visted[] = new boolean[v];
boolean ap[] = new boolean[v];
int time = 0;
for(int i = 0;i<graph.length;i++){
if(!visted[i]){
dfsAP(graph, visted, ap, dt, low, i, -1, time);
}
}
for(int i = 0;i<ap.length;i++){
if(ap[i]){
System.out.println("AP : "+ i);
}
}
}
public static void main(String arg[]){
int v = 6;
ArrayList<edge>graph[] = new ArrayList[v];
createGraph(graph);
articulationPoint(graph, v);
}
}