-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1489-Find_Critical_and_Pseudo_Critical_Edges_in_Minimum_Spanning_Tree.cpp
209 lines (192 loc) · 5.49 KB
/
1489-Find_Critical_and_Pseudo_Critical_Edges_in_Minimum_Spanning_Tree.cpp
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*******************************************************************************
* 1489-Find_Critical_and_Pseudo_Critical_Edges_in_Minimum_Spanning_Tree.cpp
* Billy.Ljm
* 19 August 2023
*
* =======
* Problem
* =======
* https://leetcode.com/problems/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] = [ai, bi, weighti] represents a
* bidirectional and weighted edge between nodes ai and bi. A minimum spanning
* tree (MST) is a subset of the graph's edges that connects all vertices
* without cycles and with the minimum possible total edge weight.
*
* Find all the critical and pseudo-critical edges in the given graph's minimum
* spanning tree (MST). An MST edge whose deletion from the graph would cause
* the MST weight to increase is called a critical edge. On the other hand, a
* pseudo-critical edge is that which can appear in some MSTs but not all.
*
* Note that you can return the indices of the edges in any order.
*
* ===========
* My Approach
* ===========
* We'll use Kruskal's algorithm to find the minimum spanning trees (MST). First,
* we'll find the weight of all the MSTs. Then, we'll try removing each edge and
* finding the MST again, if the weight is larger, then the edge is critical.
* Lastly, we'll try forcing the edge into the MST, if the weight is the same,
* then it is pseudo-critical
*
* This has a time complexity of O(n * e), and a space complexity of O(e^2 log e)
* ,where n is the number of nodes and e is the number of edges.
******************************************************************************/
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
using namespace std;
/**
* << operator for vectors
*/
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
os << "[";
for (const auto elem : v) {
os << elem << ",";
}
if (v.size() > 0) os << "\b";
os << "]";
return os;
}
/**
* Union-find/Disjoint-set data structure
*/
class UnionFind {
private:
std::vector<int> parent, rank;
public:
/**
* Class Constructor
*
* @param size total number of nodes
*/
UnionFind(int size) {
parent = std::vector<int>(size);
std::iota(std::begin(parent), std::end(parent), 0);
rank = std::vector<int>(size, 0);
}
/**
* Find set of node. Uses path compression.
*
* @param i node to find parent of
*
* @return parent of node[i]
*/
int find(int i) {
if (parent[i] != i) {
parent[i] = find(parent[i]);
}
return parent[i];
}
/**
* Union of connected cities. Uses union by rank.
*
* @param x node to union with y
* @param y node to union with x
*/
void unionn(int x, int y) {
int xroot = find(x);
int yroot = find(y);
if (rank[xroot] < rank[yroot]) {
parent[xroot] = yroot;
}
else if (rank[xroot] > rank[yroot]) {
parent[yroot] = xroot;
}
else {
parent[yroot] = xroot;
rank[xroot]++;
}
}
};
/**
* Solution
*/
class Solution {
public:
vector<vector<int>> findCriticalAndPseudoCriticalEdges(
int n, vector<vector<int>>& edges) {
// sort edges, remembering original index
for (int i = 0; i < edges.size(); i++) edges[i].push_back(i);
sort(edges.begin(), edges.end(), [](auto& left, auto& right) {
return left[2] < right[2];
});
// find weight of the MST's
UnionFind uf(n);
int nedges = 0; // number of edges already added
int mstweight = 0; // weight of MST
for (vector<int> edge : edges) {
if (nedges == n - 1) break;
else if (uf.find(edge[0]) == uf.find(edge[1])) continue;
else {
uf.unionn(edge[0], edge[1]);
nedges++;
mstweight = mstweight + edge[2];
}
}
if (nedges != n - 1) return { {}, {} }; // no mst
// check if each edge is critical or pseudo critical
vector<int> crit, pcrit;
int currweight;
for (int i = 0; i < edges.size(); i++) {
// check if critical edge, by removing it
uf = UnionFind(n);
nedges = 0;
currweight = 0;
for (vector<int> edge : edges) {
if (nedges == n - 1) break;
else if (edge == edges[i]) continue; // don't add current edge
else if (uf.find(edge[0]) == uf.find(edge[1])) continue;
else {
uf.unionn(edge[0], edge[1]);
nedges++;
currweight = currweight + edge[2];
}
}
if (nedges != n - 1 or currweight > mstweight) {
crit.push_back(edges[i][3]);
continue;
}
// check if pseudo-critical edge, by adding it
uf = UnionFind(n);
uf.unionn(edges[i][0], edges[i][1]); // add current edge
nedges = 1;
currweight = edges[i][2];
for (vector<int> edge : edges) {
if (nedges == n) break;
else if (uf.find(edge[0]) == uf.find(edge[1])) continue;
else {
uf.unionn(edge[0], edge[1]);
nedges++;
currweight = currweight + edge[2];
}
}
if (nedges == n - 1 and currweight == mstweight) {
pcrit.push_back(edges[i][3]);
}
}
return { crit, pcrit };
}
};
/**
* Test cases
*/
int main(void) {
Solution sol;
int n;
vector<vector<int>> edges;
// test case 1
n = 5;
edges = { {0,1,1},{1,2,1},{2,3,2},{0,3,2},{0,4,3},{3,4,3},{1,4,6} };
std::cout << "findCriticalAndPseudoCriticalEdges(" << n << "," << edges << ") = ";
std::cout << sol.findCriticalAndPseudoCriticalEdges(n, edges) << std::endl;
// test case 2
n = 4;
edges = { {0,1,1},{1,2,1},{2,3,1},{0,3,1} };
std::cout << "findCriticalAndPseudoCriticalEdges(" << n << "," << edges << ") = ";
std::cout << sol.findCriticalAndPseudoCriticalEdges(n, edges) << std::endl;
return 0;
}