diff --git a/bnb/ibfs_subtree.cpp b/bnb/ibfs_subtree.cpp index 854da4b95627b842c181fa154a262c2931a49cb6..daf451f77ea7c95ab0d871851043408b926c618f 100644 --- a/bnb/ibfs_subtree.cpp +++ b/bnb/ibfs_subtree.cpp @@ -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); diff --git a/bnb/incremental_bfs.cpp b/bnb/incremental_bfs.cpp index 8ae56a25d388921c9e3dfb89e9f418e0a5c34d8c..d88a3696016ce488e2fc02fd104577aac3e3bf4c 100644 --- a/bnb/incremental_bfs.cpp +++ b/bnb/incremental_bfs.cpp @@ -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; diff --git a/include/gp-bnb/incremental_bfs.hpp b/include/gp-bnb/incremental_bfs.hpp index f2be707ecb41cd6f566e2df6e6c4cbc476db1583..14fb2623692306a3b60100dd01767ba01affe44c 100644 --- a/include/gp-bnb/incremental_bfs.hpp +++ b/include/gp-bnb/incremental_bfs.hpp @@ -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_;