Sponsored
Sponsored
This approach involves using Kruskal's algorithm to find the Minimum Spanning Tree. First, sort all edges by weight. Then, use a Union-Find data structure to maintain and check connectivity of the nodes as edges are added to the MST. Critical edges are those whose exclusion from the MST will increase its total weight, whereas pseudo-critical edges are those that can be in any MST when forced to be present.
The time complexity is O(E log E), where E is the number of edges, mainly due to sorting edges. Space complexity is O(V), where V is the number of vertices, due to the Union-Find structure.
1from collections import defaultdict
2
3class UnionFind:
4 def __init__(self, size):
5 self.parent = list(range(size))
6 self.rank = [1] * size
7
8 def find(self, u):
9 if self.parent[u] != u:
10 self.parent[u] = self.find(self.parent[u])
11 return self.parent[u]
12
13 def union(self, u, v):
14 rootU = self.find(u)
15 rootV = self.find(v)
16 if rootU != rootV:
17 if self.rank[rootU] > self.rank[rootV]:
18 self.parent[rootV] = rootU
19 elif self.rank[rootU] < self.rank[rootV]:
20 self.parent[rootU] = rootV
21 else:
22 self.parent[rootV] = rootU
23 self.rank[rootU] += 1
24 return rootU != rootV
25
26 def connected(self, u, v):
27 return self.find(u) == self.find(v)
28
29
30class Solution:
31 def findCriticalAndPseudoCriticalEdges(self, n: int, edges: List[List[int]]) -> List[List[int]]:
32 edges_with_index = [(wt, u, v, i) for i, (u, v, wt) in enumerate(edges)]
33 edges_with_index.sort() # Sort by weight
34
35 def kruskal(n, edges, include=-1, exclude=-1):
36 uf = UnionFind(n)
37 mst_weight = 0
38 edges_used = 0
39 if include != -1:
40 wt, u, v, i = edges[include]
41 if uf.union(u, v):
42 mst_weight += wt
43 edges_used += 1
44 for i, (wt, u, v, idx) in enumerate(edges):
45 if i == exclude:
46 continue
47 if uf.union(u, v):
48 mst_weight += wt
49 edges_used += 1
50 if edges_used == n - 1:
51 break
52 return mst_weight if edges_used == n - 1 else float('inf')
53
54 min_mst_weight = kruskal(n, edges_with_index)
55 critical = []
56 pseudo_critical = []
57
58 for i in range(len(edges_with_index)):
59 if kruskal(n, edges_with_index, exclude=i) > min_mst_weight:
60 critical.append(edges_with_index[i][3])
61 elif kruskal(n, edges_with_index, include=i) == min_mst_weight:
62 pseudo_critical.append(edges_with_index[i][3])
63 return [critical, pseudo_critical]
This solution uses Kruskal's algorithm for MST construction. We first sort edges by weight, then use a Union-Find data structure to track connected components as we build the MST. Each edge is checked to see if its exclusion increases the MST weight, which determines if it's critical. For pseudo-critical edges, we include them initially and check if they can form an MST of the same weight. The complexity involves sorting and iterating over the edges.
This approach leverages Kruskal's algorithm to build MSTs while testing each edge's criticality and pseudo-criticality. We initially use Kruskal's algorithm to determine the MST and its weight without any edge removal. We then experiment with excluding and mandatorily including each edge to ascertain its nature: critical or pseudo-critical.
Steps:
Time Complexity: O(E^2 * log(E)) where E is the number of edges, due to sorting and constructing MST for each edge.
Space Complexity: O(E + V) where E is the number of edges and V is the number of vertices used for storing edges and union-find data structure.
1class
This approach focuses on constructing subsets of edges that definitely belong or don't belong in the MST. By manipulating combinations of edges and recalculating the MST weight, this approach checks the necessity of each edge.
Steps:
Time Complexity: O(E^2 * log(E)) due to sorting edges and iterating for each edge's condition.
Space Complexity: O(E + V) for storing edges and union-find structure during operations.
1#include <vector>
2#include <algorithm>
3
class UnionFind {
public:
std::vector<int> parent, rank;
UnionFind(int n) : parent(n), rank(n, 0) {
for (int i = 0; i < n; ++i) parent[i] = i;
}
int find(int u) {
if (parent[u] != u) parent[u] = find(parent[u]);
return parent[u];
}
bool unionSets(int u, int v) {
int rootU = find(u), rootV = find(v);
if (rootU != rootV) {
if (rank[rootU] > rank[rootV])
parent[rootV] = rootU;
else if (rank[rootU] < rank[rootV])
parent[rootU] = rootV;
else {
parent[rootV] = rootU;
rank[rootU]++;
}
return true;
}
return false;
}
};
class Solution {
public:
std::vector<std::vector<int>> findCriticalAndPseudoCriticalEdges(int n, std::vector<std::vector<int>>& edges) {
std::vector<std::tuple<int, int, int, int>> indexedEdges;
for (int i = 0; i < edges.size(); ++i) {
indexedEdges.emplace_back(edges[i][0], edges[i][1], edges[i][2], i);
}
std::sort(indexedEdges.begin(), indexedEdges.end(), [](auto& e1, auto& e2) {
return std::get<2>(e1) < std::get<2>(e2);
});
auto kruskal = [&](int excludeEdge, int forceEdge) {
UnionFind uf(n);
int totalWeight = 0, edgesUsed = 0;
if (forceEdge != -1) {
auto [u, v, w, idx] = indexedEdges[forceEdge];
if (uf.unionSets(u, v)) {
totalWeight += w;
edgesUsed++;
}
}
for (auto& [u, v, w, idx] : indexedEdges) {
if (idx != excludeEdge && uf.unionSets(u, v)) {
totalWeight += w;
edgesUsed++;
}
}
return (edgesUsed == n - 1) ? totalWeight : INT_MAX;
};
int mstWeight = kruskal(-1, -1);
std::vector<int> critical, pseudo_critical;
for (int i = 0; i < edges.size(); ++i) {
if (kruskal(i, -1) > mstWeight) {
critical.push_back(i);
} else if (kruskal(-1, i) == mstWeight) {
pseudo_critical.push_back(i);
}
}
return {critical, pseudo_critical};
}
};
This JavaScript solution defines a Union-Find class for performing union and find operations, essential for Kruskal's algorithm. Two checks are performed for each edge: one to ascertain if its exclusion raises the MST weight (indicative of criticality), and another to observe if a forced inclusion keeps the MST weight constant (demonstrating pseudo-criticality). Each of these operations helps distinguish between critical and pseudo-critical edges effectively.
This C++ solution effectively uses the Union-Find data structure for Kruskal's algorithm to determine edge connectivity. It manages edge inclusion/exclusion flexibly, ensuring optimal MST weights are computed to ascertain the critical or pseudo-critical nature of edges. Comparison of MST weight with and without specific edges indicates their status. This solution demonstrates high proficiency in handling complex graph structures using classical algorithms.