From 12f721e8270d45327abe3ef1bf3ae46a0dbb22f7 Mon Sep 17 00:00:00 2001
From: p-hamann <p.hamann@dareit.de>
Date: Mon, 27 May 2019 13:35:08 +0200
Subject: [PATCH] Fix segfault and assertion failures in IBFS algorithm

---
 bnb/ibfs_subtree.cpp               | 14 ++++++++------
 bnb/incremental_bfs.cpp            | 19 +++++++++++--------
 include/gp-bnb/incremental_bfs.hpp |  2 +-
 3 files changed, 20 insertions(+), 15 deletions(-)

diff --git a/bnb/ibfs_subtree.cpp b/bnb/ibfs_subtree.cpp
index 854da4b..daf451f 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 8ae56a2..d88a369 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 f2be707..14fb262 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_;
     
-- 
GitLab