forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCritical Connections in a Network.java
80 lines (59 loc) · 2.86 KB
/
Critical Connections in a Network.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
class Solution {
private Map<Integer, List<Integer>> graph;
private Map<Integer, Integer> rank;
private Map<Pair<Integer, Integer>, Boolean> connDict;
public List<List<Integer>> criticalConnections(int n, List<List<Integer>> connections) {
this.formGraph(n, connections);
this.dfs(0, 0);
List<List<Integer>> result = new ArrayList<List<Integer>>();
for (Pair<Integer, Integer> criticalConnection : this.connDict.keySet()) {
result.add(new ArrayList<Integer>(Arrays.asList(criticalConnection.getKey(), criticalConnection.getValue())));
}
return result;
}
private int dfs(int node, int discoveryRank) {
// That means this node is already visited. We simply return the rank.
if (this.rank.get(node) != null) {
return this.rank.get(node);
}
// Update the rank of this node.
this.rank.put(node, discoveryRank);
// This is the max we have seen till now. So we start with this instead of INT_MAX or something.
int minRank = discoveryRank + 1;
for (Integer neighbor : this.graph.get(node)) {
// Skip the parent.
Integer neighRank = this.rank.get(neighbor);
if (neighRank != null && neighRank == discoveryRank - 1) {
continue;
}
// Recurse on the neighbor.
int recursiveRank = this.dfs(neighbor, discoveryRank + 1);
// Step 1, check if this edge needs to be discarded.
if (recursiveRank <= discoveryRank) {
int sortedU = Math.min(node, neighbor), sortedV = Math.max(node, neighbor);
this.connDict.remove(new Pair<Integer, Integer>(sortedU, sortedV));
}
// Step 2, update the minRank if needed.
minRank = Math.min(minRank, recursiveRank);
}
return minRank;
}
private void formGraph(int n, List<List<Integer>> connections) {
this.graph = new HashMap<Integer, List<Integer>>();
this.rank = new HashMap<Integer, Integer>();
this.connDict = new HashMap<Pair<Integer, Integer>, Boolean>();
// Default rank for unvisited nodes is "null"
for (int i = 0; i < n; i++) {
this.graph.put(i, new ArrayList<Integer>());
this.rank.put(i, null);
}
for (List<Integer> edge : connections) {
// Bidirectional edges
int u = edge.get(0), v = edge.get(1);
this.graph.get(u).add(v);
this.graph.get(v).add(u);
int sortedU = Math.min(u, v), sortedV = Math.max(u, v);
connDict.put(new Pair<Integer, Integer>(sortedU, sortedV), true);
}
}
}