Skip to content
Snippets Groups Projects
Commit 12f721e8 authored by p-hamann's avatar p-hamann
Browse files

Fix segfault and assertion failures in IBFS algorithm

parent 3efe3d99
No related merge requests found
......@@ -25,21 +25,23 @@ std::vector<node_id> ibfs_subtree::reduce_path(node_id leaf) {
for (label i = current_max_; i > 0; --i) {
last_flow_.push_back(current);
labels_[i].erase(current);
// searches and adopts orphans iteratively
std::vector<std::unordered_set<node_id>> orphans;
orphans.push_back(succ_[current]);
for (label j = i + 1; j < current_max_; ++j) {
for (label j = i + 1; j <= current_max_; ++j) {
std::unordered_set<node_id> next_back;
for (node_id n : orphans.back()) {
for (node_id m : succ_[n]) {
labels_[j].erase(m);
pred_.erase(j);
next_back.insert(m);
}
labels_[j].erase(n);
pred_.erase(n);
succ_.erase(n);
}
orphans.push_back(next_back);
if (!next_back.empty()) {
orphans.push_back(next_back);
}
}
for (label j = 0; j < orphans.size(); ++j) {
for (node_id n : orphans[j]) {
......@@ -51,7 +53,7 @@ std::vector<node_id> ibfs_subtree::reduce_path(node_id leaf) {
}
}
}
node_id pred = pred_[current];
succ_[pred].erase(current);
......
......@@ -6,17 +6,18 @@
incremental_bfs::incremental_bfs(const graph& g, std::vector<node_id> sources, std::vector<node_id> sinks)
: g_(g), sources_(sources), sinks_(sinks), s_(ibfs_subtree(sources, subtree::s, g)), t_(ibfs_subtree(sinks, subtree::t, g)) {
assert(!sources.empty());
/* assert(!sources.empty());
assert(!sinks.empty());
assert(sources.size() <= g.num_nodes()/2);
assert(sinks.size() <= g.num_nodes()/2);
assert(sinks.size() <= g.num_nodes()/2); */
node_assignments_ = std::vector<subtree>(g.num_nodes(), none);
for (node_id node : sources) {
node_assignments_[node-1] = s;
node_assignments_[node-1] = s_root;
}
for (node_id node : sinks) {
node_assignments_[node-1] = t;
node_assignments_[node-1] = t_root;
}
};
......@@ -24,7 +25,7 @@ void incremental_bfs::run() {
// increments the flow value to the number of pairwise neighbors of sources and sinks
for (node_id node : s_.get_front()) {
for (node_id neighbor : g_.get_adjacency(node)) {
if (node_assignments_[neighbor-1] == t) {
if (node_assignments_[neighbor-1] == t_root) {
flow_++;
}
}
......@@ -32,9 +33,10 @@ void incremental_bfs::run() {
// grows the subtrees alternating until one subtree cannot grow anymore
for (int i = 0; ; ++i) {
ibfs_subtree& current = (i%2 == 0) ? s_ : t_;
if (!grow(current)) {
break;
if (i%2 == 0) {
if (!grow(s_)) break;
} else {
if (!grow(t_)) break;
}
}
};
......@@ -81,6 +83,7 @@ bool incremental_bfs::grow(ibfs_subtree& st) {
}
flow_++;
break;
// ... or extends st
} else if (node_assignments_[neighbor-1] == none) {
node_assignments_[neighbor-1] = (st.get_id() == subtree::s) ? s : t;
......
......@@ -26,7 +26,7 @@ public:
};
private:
enum subtree { s = -1, t = 1, none, flow };
enum subtree { s = -1, t = 1, none, flow, s_root, t_root };
const graph& g_;
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment