[Leetcode] Problem 1489 - Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree

Given a weighted undirected connected graph with n vertices numbered from 0 to n-1, and an array edges where edges[i] = [fromi, toi, weighti] represents a bidirectional and weighted edge between nodes fromi and toi. A minimum spanning tree (MST) is a subset of the edges of the graph that connects all vertices without cycles and with the minimum possible total edge weight.

Find all the critical and pseudo-critical edges in the minimum spanning tree (MST) of the given graph. An MST edge whose deletion from the graph would cause the MST weight to increase is called a critical edge. A pseudo-critical edge, on the other hand, is that which can appear in some MSTs but not all.

Note that you can return the indices of the edges in any order.

Example

No.1

0pbph8.png

Input: n = 5, edges = [[0,1,1],[1,2,1],[2,3,2],[0,3,2],[0,4,3],[3,4,3],[1,4,6]]

Output: [[0,1],[2,3,4,5]]

Explanation: The figure above describes the graph.
The following figure shows all the possible MSTs:

Notice that the two edges 0 and 1 appear in all MSTs, therefore they are critical edges, so we return them in the first list of the output.
The edges 2, 3, 4, and 5 are only part of some MSTs, therefore they are considered pseudo-critical edges. We add them to the second list of the output.

No.2

0pHXnA.png

Input: n = 4, edges = [[0,1,1],[1,2,1],[2,3,1],[0,3,1]]

Output: [[],[0,1,2,3]]

Explanation: We can observe that since all 4 edges have equal weight, choosing any 3 edges from the given 4 will yield an MST. Therefore all 4 edges are pseudo-critical.

Constraints

  • 2 <= n <= 100
  • 1 <= edges.length <= min(200, n * (n - 1) / 2)
  • edges[i].length == 3
  • 0 <= fromi < toi < n
  • 1 <= weighti <= 1000
  • All pairs (fromi, toi) are distinct.

Code

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
public class UnionFind {
private int[] id;
private int[] size;
private int count;

public UnionFind(int n) {
this.id = new int[n];
this.size = new int[n];
count = n;

for (int i = 0; i < n; i++) {
id[i] = i;
size[i] = 1;
}
}

public int count() {
return count;
}

public boolean union(int p, int q) {
int pRoot = find(p);
int qRoot = find(q);

if (pRoot == qRoot)
return false;

if (size[pRoot] < size[qRoot]) {
id[pRoot] = qRoot;
size[qRoot] += size[pRoot];
} else {
id[qRoot] = pRoot;
size[pRoot] += size[qRoot];
}

count--;

return true;
}

public int find(int p) {
while (p != id[p])
p = id[p];

return p;
}
}

public List<List<Integer>> findCriticalAndPseudoCriticalEdges(int n, int[][] edges) {
List<Integer> critical = new ArrayList<>();
List<Integer> pseudo = new ArrayList<>();
Map<int[], Integer> map = new HashMap<>();

for (int i = 0; i < edges.length; i++)
map.put(edges[i], i);

Arrays.sort(edges, (a, b) -> Integer.compare(a[2], b[2]));

int minCost = buildMST(n, edges, null, null);

for (int i = 0; i < edges.length; i++) {
int[] edge = edges[i];

if (buildMST(n, edges, edge, null) > minCost)
critical.add(map.get(edge));
else if (buildMST(n, edges, null, edge) == minCost)
pseudo.add(map.get(edge));
}

return Arrays.asList(critical, pseudo);
}

private int buildMST(int n, int[][] edges, int[] exclude, int[] include) {
UnionFind uf = new UnionFind(n);
int cost = 0;

if (include != null) {
uf.union(include[0], include[1]);
cost += include[2];
}

for (int[] edge : edges) {
if (edge == exclude)
continue;

if (uf.union(edge[0], edge[1]))
cost += edge[2];
}

return uf.count == 1 ? cost : Integer.MAX_VALUE;
}