Feat: neighbor_list sort via unordered_map to get pos
This commit is contained in:
parent
33b6e58d32
commit
808d7e6dae
|
@ -12,6 +12,7 @@
|
|||
#include <climits>
|
||||
#include <random>
|
||||
#include <algorithm>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <ogdf/fileformats/GraphIO.h>
|
||||
|
||||
|
@ -60,8 +61,7 @@ public:
|
|||
void guided_DFS_visit(vector<node *> &dfsList,
|
||||
const vector<node *> &node_list,
|
||||
int &return_index,
|
||||
const vector<int> &rev_post_order,
|
||||
int prev_node);
|
||||
const unordered_map<int, int> &node_id_to_pos);
|
||||
void mutated_DFS_visit(vector<node*> &dfsList,
|
||||
vector<node*> &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<int>& vec_order, node* a, node* b);
|
||||
bool sortByOrder(const std::unordered_map<int, int>& node_id_to_pos, node* a, node* b);
|
||||
|
||||
//PARENT-CHILDREN
|
||||
void set_parent(node* n) ;
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
//-----------------------------------------------------------------------------------
|
||||
|
||||
#include "mps.h"
|
||||
#include <iterator>
|
||||
|
||||
// #define DEBUG
|
||||
|
||||
|
@ -52,30 +51,39 @@ void
|
|||
maximal_planar_subgraph_finder::guidedPostOrderTraversal(vector<int> post_order) {
|
||||
node::init_mark();
|
||||
|
||||
vector<int> rev_post_order;
|
||||
// // implementation 1: pass reversed vector
|
||||
// vector<int> 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<int, int> 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;
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
// #define DEBUG
|
||||
|
||||
// #define TIME
|
||||
#define TIME
|
||||
|
||||
//-----------------------------------------------------------------------------------
|
||||
// Finding MPS
|
||||
|
|
|
@ -70,19 +70,19 @@ void node::DFS_visit(vector<node*> &dfsList, int &index) {
|
|||
}
|
||||
|
||||
|
||||
bool node::sortByOrder(const std::vector<int>& 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<int, int>& 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<node *> &dfsList,
|
||||
const vector<node *> &node_list,
|
||||
int &return_index,
|
||||
const vector<int> &rev_post_order,
|
||||
int prev_node)
|
||||
const unordered_map<int, int> &node_id_to_pos)
|
||||
{
|
||||
|
||||
mark();
|
||||
|
@ -112,8 +112,8 @@ void node::guided_DFS_visit(vector<node *> &dfsList,
|
|||
|
||||
// implementation 2: sort elements of _adj_list
|
||||
vector<node*> 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<node *> &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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue