-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCriticalConnections.java
More file actions
58 lines (45 loc) · 1.7 KB
/
CriticalConnections.java
File metadata and controls
58 lines (45 loc) · 1.7 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
package graph;
import java.util.ArrayList;
import java.util.Collections;
public class CriticalConnections {
static int time = 0;
public static ArrayList<ArrayList<Integer>> criticalConnections(int v, ArrayList<ArrayList<Integer>> adj) {
return getBridges(adj, v);
}
public static ArrayList<ArrayList<Integer>> getBridges(ArrayList<ArrayList<Integer>> adj, int v) {
int[] dt = new int[v];
int[] low = new int[v];
boolean[] vis = new boolean[v];
ArrayList<ArrayList<Integer>> ans = new ArrayList<>();
dfs(adj, vis, dt, low, 0, -1, ans);
ans.sort((a, b) -> {
if (a.get(0).equals(b.get(0))) {
return a.get(1) - b.get(1);
} else {
return a.getFirst() - b.getFirst();
}
});
return ans;
}
public static void dfs(ArrayList<ArrayList<Integer>> adj, boolean[] vis, int[] dt, int[] low, int curr, int parent, ArrayList<ArrayList<Integer>> ans) {
vis[curr] = true;
dt[curr] = low[curr] = ++time;
for (int dest : adj.get(curr)) {
if (dest == parent) continue;
if (!vis[dest]) {
dfs(adj, vis, dt, low, dest, curr, ans);
low[curr] = Math.min(low[curr], low[dest]);
// Bridge condition.
if (dt[curr] < low[dest]) {
ArrayList<Integer> bridge = new ArrayList<>();
bridge.add(curr);
bridge.add(dest);
Collections.sort(bridge);
ans.add(bridge);
}
} else {
low[curr] = Math.min(low[curr], dt[dest]);
}
}
}
}