我得到cygwin_exception :: open_stackdumpfile试图运行我的代码。我猜这是关于记忆的错误。我几乎100%肯定我得到这个错误,因为没有正确创建,发送到函数或处理maximalSets和/或compsub向量。 请帮我解决这个错误,谢谢你的所有答案! 更新:解决了Paul Evans指出的问题。现在我不时得到例外,但仍然得到它。
class Graph {
private:
int size; // the number of node
vector<vector<bool> > connected;
vector<vector<int> > bkv2(vector<int> oldSet, int ne, int ce,
vector<int> &compsub, vector<vector<int> > maximalSets);
public:
Graph(int size);
vector<vector<int> > findMaximalSets();
}
Graph::Graph(int n) {
int i, j;
srand((unsigned int) time( NULL));
size = n;
connected.resize(size);
for (int i=0; i<size; i++) {
connected[i].resize(size);
}
for (i = 0; i < size; i++) { // the graph is randomly generated
connected[i][i] = 1;
for (j = i + 1; j < size; j++) {
if (rand() % 2 == 1) {
connected[i][j] = 1;
connected[j][i] = 1;
} else {
connected[i][j] = 0;
connected[j][i] = 0;
}
}
}
}
vector<vector<int> > Graph::findMaximalSets() {
int i;
vector<int> all(size);
vector<int> compsub;
vector<vector<int> > maximalSets;
for (i = 0; i < size; i++) {
all[i] = i;
}
return bkv2(all, 0, size, compsub, maximalSets);
}
vector<vector<int> > Graph::bkv2(vector<int> oldSet, int ne, int ce,
vector<int> &compsub, vector<vector<int> > maximalSets) {
vector<int> newSet(ce);
int nod, fixp;
int newne, newce, i, j, count, pos, p, s, sel, minnod;
minnod = ce;
nod = 0;
for (i = 0; i < ce && minnod != 0; i++) {
p = oldSet[i];
count = 0;
for (j = ne; j < ce && count < minnod; j++)
if (connected[p][oldSet[j]]) {
count++;
pos = j;
}
if (count < minnod) {
fixp = p;
minnod = count;
if (i < ne) {
s = pos;
} else {
s = i;
nod = 1;
}
}
}
for (nod = minnod + nod; nod >= 1; nod--) {
p = oldSet[s];
oldSet[s] = oldSet[ne];
sel = oldSet[ne] = p;
newne = 0;
for (i = 0; i < ne; i++) {
if (!connected[sel][oldSet[i]]) {
newSet[newne++] = oldSet[i];
}
}
newce = newne;
for (i = ne + 1; i < ce; i++) {
if (!connected[sel][oldSet[i]]) {
newSet[newce++] = oldSet[i];
}
}
compsub.push_back(sel);
if (newce == 0) {
vector<int> copy = compsub;
maximalSets.push_back(copy);
} else if (newne < newce) {
maximalSets = bkv2(newSet, newne, newce, compsub, maximalSets);
}
compsub.pop_back();
ne++;
if (nod > 1) {
for (s = ne; !connected[fixp][oldSet[s]]; s++) {
}
}
}
return maximalSets;
}
答案 0 :(得分:1)
这是因为你没有在任何地方初始化size
。