gpt4 book ai didi

c++ - 实现 union 查找 C++ 的段错误(核心已转储)

转载 作者:搜寻专家 更新时间:2023-10-31 01:47:36 24 4
gpt4 key购买 nike

我正在做 Kruskal 最小生成树并编写了一些 union 查找函数来执行集合操作。以前它工作正常,直到我将 KruskalMST() 函数从按值传递更改为按引用传递。现在它看起来像这样:

vector<Edge*> KruskalMST(vector<Edge>& EV, vector<Node>& mynodelist){
int setcount = NVAR;
vector<Edge*> mst; //mst
vector<int> umark; //setmark
for(int i=0;i<NVAR;i++)
{
umark.push_back(i);
}


/*Kruskal*/
while(setcount>1){
cout<<"# USER DEFINED EDGES = "<<USER_DEFINED_EDGES<<endl;
cout<<"Threshold = "<<THRESHOLD<<endl;
cout<<"# edges = "<<EV.size()<<endl;

for(int j = 0; j < EV.size(); j++){
cout<<"Find(v1) = "<<Find(&mynodelist[EV[j].readv1()->readi()])->readi()<<endl;
cout<<"Find(v2) = "<<Find(&mynodelist[EV[j].readv2()->readi()])->readi()<<endl;
if(Find(&mynodelist[EV[j].readv1()->readi()])->readi()!= Find(&mynodelist[EV[j].readv2()->readi()])->readi()){

mst.push_back(&EV[j]); //add this edge to mst
cout<<"mst.size() = "<<mst.size()<<endl;
Union(&mynodelist[EV[j].readv1()->readi()], &mynodelist[EV[j].readv2()->readi()]); //
cout<<"after Find(v1) = "<<Find(&mynodelist[EV[j].readv1()->readi()])->readi()<<endl;
cout<<"after Find(v2) = "<<Find(&mynodelist[EV[j].readv2()->readi()])->readi()<<endl;
}
}
cout<<"it never reaches here"<<endl;
std::list<int> listmark;
for(int i=0;i<NVAR;i++){
mynodelist[i].setmark(Find(&mynodelist[i]));
listmark.push_back(mynodelist[i].readmark());
}
listmark.sort();
listmark.unique();
setcount = listmark.size();

umark.clear();
umark.insert(umark.begin(), listmark.begin(),listmark.end()); //assign list to vector (clear first)


cout << "# disjoint components: " << setcount << endl;
cout << endl;

}
return mst;
}

奇怪的是它运行了一段时间就崩溃了,yield

All Vertices: #100
All Edges: #4950
# USER DEFINED EDGES = 0
Threshold = 15
# edges = 4950
Find(v1) = 96
Find(v2) = 36
mst.size() = 1
after Find(v1) = 96
after Find(v2) = 96
Find(v1) = 81
Find(v2) = 48
mst.size() = 2
after Find(v1) = 81
after Find(v2) = 81
...
...
Find(v1) = 57
Find(v2) = 84
mst.size() = 26
after Find(v1) = 84
after Find(v2) = 84
Find(v1) = 29
Segmentation fault (core dumped)

它似乎在随机迭代次数时崩溃,但从未脱离 for 循环。可能的原因是什么?请帮助我!

编辑

查找和 union 函数:

Node* Find(Node* node) {          //FINDING THE REPRESENTATIVE FOR EACH SET
Node* temp;
Node* root = node;

while (root->readp() != NULL)
root = root->readp(); //readp() reads its parent

/* Updates the parent pointers */
while (node->readp() != NULL) {
temp = node->readp();
node->setp(root); //setparent
node = temp;
}

return root;
}


/* Merges two nodes based on their rank */
void Union(Node* node1, Node* node2) {
Node* root1 = Find(node1);
Node* root2 = Find(node2);

if (root1->readrank() > root2->readrank()) { //WHOSE RANK IS LARGER, WHO'S THE PARENT. RANK IS THE NUMBER OF CHILDREN FOR THE VERTEX
root2->setp(root1);
} else if (root2->readrank() > root1->readrank()) {
root1->setp(root2);
} else { //EQUAL, ROOT1'S RANK INCREMENTS AND IS THE PARENT
root2->setp(root1);
root1->setrank((root1->readrank())+1);
}
}

我没有更改 Union-find 部分,只是更改了 KruskalMST(vector<Edge> EV, vector<Node> mynodelist)KruskalMST(vector<Edge>& EV, vector<Node>& mynodelist)

最佳答案

您的返回 vector 是指针,但传递的 vector 按值获取项目。当您构建结果时,您正在插入指向传递的 EV vector 中的值的指针。这不是保险箱。如果您在任何时候更改传递的 EV vector ——在函数执行期间或什至在 KrustalMST() 返回之后——该 vector 中值的地址将会更改。因此访问返回的指针将是无效的。

如果 KruskalMST() 方法按值获取参数,那会使情况变得更糟,因为您将返回指向已销毁 vector 中元素的指针。

关于c++ - 实现 union 查找 C++ 的段错误(核心已转储),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18996654/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com