I have written this code which implements the Kruskal's algorithm for Minimum Spanning Tree, which yields segmentation fault when I submit it to an online judge. I think I have narrowed the cause of segmentation fault to the part where I sort the edges. Still, I can't find the exact reason why this code fails.
struct DisjointSet{
private:
struct Node{
int size;
int repr;
Node(int rep){
size = 1;
repr = rep;
}
};
vector<Node> nodes;
public:
int makeSet(){
nodes.push_back(Node(nodes.size()));
return nodes.size() - 1;
}
int findRepresentative(int ind){
if(nodes[nodes[ind].repr].size == -1){
nodes[ind].repr = findRepresentative(nodes[ind].repr);
}
return nodes[ind].repr;
}
void unionSets(int ind1, int ind2){
int rep1 = findRepresentative(ind1), rep2 = findRepresentative(ind2);
if(rep1 != rep2){
if(nodes[rep1].size < nodes[rep2].size){
int t = rep1;
rep1 = rep2;
rep2 = t;
}
nodes[rep1].size += nodes[rep2].size;
nodes[rep2].size = -1;
nodes[rep2].repr = rep1;
}
}
};
bool compare(const pair<pair<int, int>, int> &p1, const pair<pair<int, int>, int> &p2){
return (p1.second <= p2.second);
}
int spanningTree(int V, int E, vector<vector<int>> &graph) {
vector<pair<pair<int, int>, int>> edges;
for(int i = 0;i < V;i ++){
for(int j = 0;j < V;j ++){
if(i <= j){
if(graph[i][j] != INT_MAX){
edges.push_back(make_pair(make_pair(i, j), graph[i][j]));
}
}
}
}
sort(edges.begin(), edges.end(), compare);
DisjointSet d;
for(int i = 0;i < V;i ++){
d.makeSet();
}
int weight = 0;
for(int i = 0;i < edges.size();i ++){
int u = edges[i].first.first, v = edges[i].first.second;
if(d.findRepresentative(u) != d.findRepresentative(v)){
d.unionSets(u, v);
weight += edges[i].second;
}
}
return weight;
}
Your compare function:
bool compare(const pair<pair<int, int>, int> &p1, const pair<pair<int, int>, int> &p2){
return (p1.second <= p2.second);
}
does not provide a strict-weak ordering, since comparing 2 elements e1
and e2
with the same .second
will return true for both e1 < e2
and e2 < e1
, which is not allowed. Using this compare
function in sort invokes undefined behavior, potentially causing a segfault.
You need to do something like:
bool compare(const pair<pair<int, int>, int> &p1, const pair<pair<int, int>, int> &p2){
return p1.second < p2.second;
}