diff --git a/deferred_planarity_test/include/mps.h b/deferred_planarity_test/include/mps.h index b1f788c..899e624 100644 --- a/deferred_planarity_test/include/mps.h +++ b/deferred_planarity_test/include/mps.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -60,8 +61,7 @@ public: void guided_DFS_visit(vector &dfsList, const vector &node_list, int &return_index, - const vector &rev_post_order, - int prev_node); + const unordered_map &node_id_to_pos); void mutated_DFS_visit(vector &dfsList, vector &node_list, int &index, @@ -70,7 +70,7 @@ public: int mutate_point); // custom comparator function to sort nodes according to order in given vector - bool sortByOrder(const std::vector& vec_order, node* a, node* b); + bool sortByOrder(const std::unordered_map& node_id_to_pos, node* a, node* b); //PARENT-CHILDREN void set_parent(node* n) ; diff --git a/deferred_planarity_test/src/mps.cpp b/deferred_planarity_test/src/mps.cpp index 0c07fbd..bdeba63 100644 --- a/deferred_planarity_test/src/mps.cpp +++ b/deferred_planarity_test/src/mps.cpp @@ -3,7 +3,6 @@ //----------------------------------------------------------------------------------- #include "mps.h" -#include // #define DEBUG @@ -52,30 +51,39 @@ void maximal_planar_subgraph_finder::guidedPostOrderTraversal(vector post_order) { node::init_mark(); - vector rev_post_order; + // // implementation 1: pass reversed vector + // vector rev_post_order; + // for (int i = post_order.size() - 1; i >= 0; --i) { + // rev_post_order.push_back(post_order[i]); + // } + // int start = rev_post_order[0]; + + + unordered_map node_id_to_pos; + int j = 0; + // we flip the post_order vector around for (int i = post_order.size() - 1; i >= 0; --i) { - rev_post_order.push_back(post_order[i]); + node_id_to_pos[post_order[i]] = j++; } int postOrderID = 0; - int end_condition = _node_list.size(); - int start = rev_post_order[0]; + // we start from the end of the post_order, which is the root node + int start = post_order[post_order.size() - 1]; int i = start; - int prev_node = INT_MAX; while (true) { if (((start > 0) && (i == (start - 1))) || ((start == 0 ) && (i == end_condition - 1))) { if (!_node_list[i]->is_marked()) { - _node_list[i]->guided_DFS_visit(_post_order_list, _node_list, postOrderID, rev_post_order, prev_node); + _node_list[i]->guided_DFS_visit(_post_order_list, _node_list, postOrderID, node_id_to_pos); } break; } if (!_node_list[i]->is_marked()) { - _node_list[i]->guided_DFS_visit(_post_order_list, _node_list, postOrderID, rev_post_order, prev_node); + _node_list[i]->guided_DFS_visit(_post_order_list, _node_list, postOrderID, node_id_to_pos); } i = (i + 1) % end_condition; } diff --git a/deferred_planarity_test/src/mps_test.cpp b/deferred_planarity_test/src/mps_test.cpp index 437570d..29d1819 100644 --- a/deferred_planarity_test/src/mps_test.cpp +++ b/deferred_planarity_test/src/mps_test.cpp @@ -9,7 +9,7 @@ // #define DEBUG -// #define TIME +#define TIME //----------------------------------------------------------------------------------- // Finding MPS diff --git a/deferred_planarity_test/src/node.cpp b/deferred_planarity_test/src/node.cpp index 412d6eb..965285b 100644 --- a/deferred_planarity_test/src/node.cpp +++ b/deferred_planarity_test/src/node.cpp @@ -70,19 +70,19 @@ void node::DFS_visit(vector &dfsList, int &index) { } -bool node::sortByOrder(const std::vector& vec_order, node* a, node* b) { - auto itA = std::find(vec_order.begin(), vec_order.end(), a->node_id()); - auto itB = std::find(vec_order.begin(), vec_order.end(), b->node_id()); +bool node::sortByOrder(const std::unordered_map& node_id_to_pos, node* a, node* b) { + auto iter_a = node_id_to_pos.find(a->node_id()); + auto iter_b = node_id_to_pos.find(b->node_id()); - return (std::distance(vec_order.begin(), itA) < std::distance(vec_order.begin(), itB)); + // second yields the position + return iter_a->second < iter_b->second; } void node::guided_DFS_visit(vector &dfsList, const vector &node_list, int &return_index, - const vector &rev_post_order, - int prev_node) + const unordered_map &node_id_to_pos) { mark(); @@ -112,8 +112,8 @@ void node::guided_DFS_visit(vector &dfsList, // implementation 2: sort elements of _adj_list vector neighbor_list = _adj_list; - std::sort(neighbor_list.begin(), neighbor_list.end(), [this, &rev_post_order](node* a, node* b) { - return sortByOrder(rev_post_order, a, b); + std::sort(neighbor_list.begin(), neighbor_list.end(), [this, &node_id_to_pos](node* a, node* b) { + return sortByOrder(node_id_to_pos, a, b); }); @@ -131,7 +131,7 @@ void node::guided_DFS_visit(vector &dfsList, for (int i = 0; i < neighbor_list.size(); ++i) { if (!neighbor_list[i]->is_marked()) { neighbor_list[i]->_parent = this; - neighbor_list[i]->guided_DFS_visit(dfsList, node_list, return_index, rev_post_order, this->node_id()); + neighbor_list[i]->guided_DFS_visit(dfsList, node_list, return_index, node_id_to_pos); } }