-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbridgeinagraph.java
52 lines (42 loc) · 1.14 KB
/
bridgeinagraph.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
package graphs;
import java.util.Arrays;
public class bridgeinagraph {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] adj = { { 0, 1, 1, 0 }, { 1, 0, 1, 1 }, { 1, 1, 0, 0 }, { 0, 1, 0, 0 } };
bridge(adj);
}
public static void bridge(int[][] adj) {
int[] vis = new int[adj.length];
int[] par = new int[adj.length];
int[] rt = new int[adj.length];
int[] lt = new int[adj.length];
Arrays.fill(par, -1);
Arrays.fill(rt, -1);
Arrays.fill(lt, -1);
for (int i = 0; i < adj.length; i++) {
if (vis[i] == 0) {
dfs(adj, vis, par, rt, lt, i, 0);
}
}
}
// tarjans algo
public static void dfs(int[][] adj, int[] vis, int[] par, int[] rt, int[] lt, int src, int t) {
vis[src] = 1;
rt[src] = t;
lt[src] = t;
int[] nbr = adj[src];
for (int i = 0; i < nbr.length; i++) {
if (nbr[i] == 1 && vis[i] == 0) {
par[i] = src;
dfs(adj, vis, par, rt, lt, i, t + 1);
lt[src] = Math.min(lt[src], lt[i]);
if (lt[i] > rt[src]) {
System.out.println(src + " - " + i);
}
} else if (nbr[i] == 1 && i != par[src]) {
lt[src] = Math.min(lt[src], rt[i]);
}
}
}
}