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 <climits>
|
||||||
#include <random>
|
#include <random>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <unordered_map>
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
#include <ogdf/fileformats/GraphIO.h>
|
#include <ogdf/fileformats/GraphIO.h>
|
||||||
|
|
||||||
|
@ -60,8 +61,7 @@ public:
|
||||||
void guided_DFS_visit(vector<node *> &dfsList,
|
void guided_DFS_visit(vector<node *> &dfsList,
|
||||||
const vector<node *> &node_list,
|
const vector<node *> &node_list,
|
||||||
int &return_index,
|
int &return_index,
|
||||||
const vector<int> &rev_post_order,
|
const unordered_map<int, int> &node_id_to_pos);
|
||||||
int prev_node);
|
|
||||||
void mutated_DFS_visit(vector<node*> &dfsList,
|
void mutated_DFS_visit(vector<node*> &dfsList,
|
||||||
vector<node*> &node_list,
|
vector<node*> &node_list,
|
||||||
int &index,
|
int &index,
|
||||||
|
@ -70,7 +70,7 @@ public:
|
||||||
int mutate_point);
|
int mutate_point);
|
||||||
|
|
||||||
// custom comparator function to sort nodes according to order in given vector
|
// 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
|
//PARENT-CHILDREN
|
||||||
void set_parent(node* n) ;
|
void set_parent(node* n) ;
|
||||||
|
|
|
@ -3,7 +3,6 @@
|
||||||
//-----------------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------------
|
||||||
|
|
||||||
#include "mps.h"
|
#include "mps.h"
|
||||||
#include <iterator>
|
|
||||||
|
|
||||||
// #define DEBUG
|
// #define DEBUG
|
||||||
|
|
||||||
|
@ -52,30 +51,39 @@ void
|
||||||
maximal_planar_subgraph_finder::guidedPostOrderTraversal(vector<int> post_order) {
|
maximal_planar_subgraph_finder::guidedPostOrderTraversal(vector<int> post_order) {
|
||||||
node::init_mark();
|
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) {
|
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 postOrderID = 0;
|
||||||
|
|
||||||
int end_condition = _node_list.size();
|
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 i = start;
|
||||||
|
|
||||||
int prev_node = INT_MAX;
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
if (((start > 0) && (i == (start - 1))) || ((start == 0 ) && (i == end_condition - 1)))
|
if (((start > 0) && (i == (start - 1))) || ((start == 0 ) && (i == end_condition - 1)))
|
||||||
{
|
{
|
||||||
if (!_node_list[i]->is_marked())
|
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;
|
break;
|
||||||
}
|
}
|
||||||
if (!_node_list[i]->is_marked())
|
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;
|
i = (i + 1) % end_condition;
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
|
|
||||||
// #define DEBUG
|
// #define DEBUG
|
||||||
|
|
||||||
// #define TIME
|
#define TIME
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------------
|
||||||
// Finding MPS
|
// 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) {
|
bool node::sortByOrder(const std::unordered_map<int, int>& node_id_to_pos, node* a, node* b) {
|
||||||
auto itA = std::find(vec_order.begin(), vec_order.end(), a->node_id());
|
auto iter_a = node_id_to_pos.find(a->node_id());
|
||||||
auto itB = std::find(vec_order.begin(), vec_order.end(), b->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,
|
void node::guided_DFS_visit(vector<node *> &dfsList,
|
||||||
const vector<node *> &node_list,
|
const vector<node *> &node_list,
|
||||||
int &return_index,
|
int &return_index,
|
||||||
const vector<int> &rev_post_order,
|
const unordered_map<int, int> &node_id_to_pos)
|
||||||
int prev_node)
|
|
||||||
{
|
{
|
||||||
|
|
||||||
mark();
|
mark();
|
||||||
|
@ -112,8 +112,8 @@ void node::guided_DFS_visit(vector<node *> &dfsList,
|
||||||
|
|
||||||
// implementation 2: sort elements of _adj_list
|
// implementation 2: sort elements of _adj_list
|
||||||
vector<node*> neighbor_list = _adj_list;
|
vector<node*> neighbor_list = _adj_list;
|
||||||
std::sort(neighbor_list.begin(), neighbor_list.end(), [this, &rev_post_order](node* a, node* b) {
|
std::sort(neighbor_list.begin(), neighbor_list.end(), [this, &node_id_to_pos](node* a, node* b) {
|
||||||
return sortByOrder(rev_post_order, a, 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) {
|
for (int i = 0; i < neighbor_list.size(); ++i) {
|
||||||
if (!neighbor_list[i]->is_marked()) {
|
if (!neighbor_list[i]->is_marked()) {
|
||||||
neighbor_list[i]->_parent = this;
|
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