Feat/Fix: add guided post traversal to rotate the tree to the correct

orientation before mutation
This commit is contained in:
Richard Wong 2024-02-23 09:59:18 +09:00
parent 8794837369
commit be49c6cfe0
Signed by: richard
GPG Key ID: 5BD36BA2E9EE33D0
4 changed files with 47 additions and 20 deletions

View File

@ -164,6 +164,7 @@ public:
int compute_removed_edge_size(string input_file, vector<int> post_order, int mutate_point); int compute_removed_edge_size(string input_file, vector<int> post_order, int mutate_point);
void print_removed_edge_size(string input_file, vector<int> post_order, int mutate_point); void print_removed_edge_size(string input_file, vector<int> post_order, int mutate_point);
vector<int> generate_post_order(string input_file); vector<int> generate_post_order(string input_file);
vector<int> generate_guided_post_order(string input_file, vector<int> post_order);
vector<int> generate_mutated_post_order(string input_file, vector<int> post_order, int mutate_point); vector<int> generate_mutated_post_order(string input_file, vector<int> post_order, int mutate_point);
void set_mutate_point(int mutate_point); void set_mutate_point(int mutate_point);
int get_mutate_point(); int get_mutate_point();

View File

@ -21,6 +21,8 @@ void print_removed_edge_size(string input_file, vector<int> post_order, int muta
vector<int> generate_post_order(string input_file); vector<int> generate_post_order(string input_file);
vector<int> generate_guided_post_order(string input_file, vector<int> post_order);
vector<int> generate_mutated_post_order_at_x(string input_file, vector<int> post_order, int mutate_point); vector<int> generate_mutated_post_order_at_x(string input_file, vector<int> post_order, int mutate_point);
@ -30,29 +32,36 @@ void measure_removed_edges(string input_file, int k_max, int mutate_point) {
vector<int> state_old = generate_post_order(input_file); vector<int> state_old = generate_post_order(input_file);
vector<int> state_new; vector<int> state_new;
std::cout << "first generated order" << std::endl; // std::cout << "first generated order" << std::endl;
for (int i = 0; i < state_old.size(); i++) { // for (int i = 0; i < state_old.size(); i++) {
std::cout << state_old[i] << ", "; // std::cout << state_old[i] << ", ";
} // }
std::cout << std::endl; // std::cout << std::endl;
int removed_old; int removed_old = compute_removed_edge_size(input_file, state_old, mutate_point);
int removed_new; int removed_new;
for (int k = 0; k < k_max; ++k) { for (int k = 0; k < k_max; ++k) {
std::cout << std::endl; // std::cout << std::endl;
std::cout << "new generated order" << std::endl; // std::cout << "new generated order" << std::endl;
state_new = generate_mutated_post_order_at_x(input_file, state_old, mutate_point); // rotate to prep for next run
removed_old = compute_removed_edge_size(input_file, state_old, mutate_point); state_new = generate_guided_post_order(input_file, state_old);
// rotate to prep for next run
// tree produced should be the same as earlier given tree, except from the mutate point
state_new = generate_mutated_post_order_at_x(input_file, state_new, mutate_point);
// for (int i = 0; i < state_new.size(); i++) {
// std::cout << state_new[i] << ", ";
// }
// std::cout << std::endl;
// rotate to prep for next run
state_new = generate_guided_post_order(input_file, state_new);
// tree produced should be the same as tree from mutation
removed_new = compute_removed_edge_size(input_file, state_new, mutate_point); removed_new = compute_removed_edge_size(input_file, state_new, mutate_point);
for (int i = 0; i < state_new.size(); i++) {
std::cout << state_new[i] << ", "; // std::cout << "removed edges in old: " << removed_old << std::endl;
} // std::cout << "removed edges in new: " << removed_new << std::endl;
std::cout << std::endl;
std::cout << "removed edges in old: " << removed_old << std::endl;
std::cout << "removed edges in new: " << removed_new << std::endl;
print_removed_edge_size(input_file, state_new, mutate_point); print_removed_edge_size(input_file, state_new, mutate_point);

View File

@ -180,14 +180,18 @@ maximal_planar_subgraph_finder::determine_edges() {
//The main part of the whole algorithm: Back-edge-traversal //The main part of the whole algorithm: Back-edge-traversal
void void
maximal_planar_subgraph_finder::back_edge_traversal() { maximal_planar_subgraph_finder::back_edge_traversal() {
node* i_node = 0; node* i_node = nullptr;
node* current_node = 0; node* current_node = nullptr;
int dfs_mutate_point = get_mutate_point(); int dfs_mutate_point = get_mutate_point();
int node_list_last_index = _node_list.size() - 1; int node_list_last_index = _node_list.size() - 1;
int post_order_mutate_point = node_list_last_index - dfs_mutate_point - 1; // dfs_mutate_point starts counting from the DFS head
// post_order starts from the last leaf of the DFS tree
// hence we start counting from the post_order last index
int post_order_mutate_point = node_list_last_index - dfs_mutate_point;
std::cout << "post_order_mutate_point: " << post_order_mutate_point << std::endl; std::cout << "post_order_mutate_point: " << post_order_mutate_point << std::endl;
// back_edge first node is higher than the second // back_edge first node is higher than the second
for (int i = 0; i < _back_edge_list.size(); ++i) { for (int i = 0; i < _back_edge_list.size(); ++i) {
// current node is the lower
current_node = _back_edge_list[i].second; current_node = _back_edge_list[i].second;
i_node = _back_edge_list[i].first; i_node = _back_edge_list[i].first;
// back_edge_traversal returns true if it should be included // back_edge_traversal returns true if it should be included

View File

@ -34,6 +34,11 @@ vector<int> generate_post_order(string input_file) {
return m.generate_post_order(input_file); return m.generate_post_order(input_file);
} }
vector<int> generate_guided_post_order(string input_file, vector<int> post_order) {
maximal_planar_subgraph_finder m;
return m.generate_guided_post_order(input_file, post_order);
}
vector<int> generate_mutated_post_order_at_x(string input_file, vector<int> post_order, int mutate_point) { vector<int> generate_mutated_post_order_at_x(string input_file, vector<int> post_order, int mutate_point) {
maximal_planar_subgraph_finder m; maximal_planar_subgraph_finder m;
return m.generate_mutated_post_order(input_file, post_order, mutate_point); return m.generate_mutated_post_order(input_file, post_order, mutate_point);
@ -58,6 +63,14 @@ vector<int> maximal_planar_subgraph_finder::generate_post_order(string input_fil
return return_post_order(); return return_post_order();
} }
vector<int> maximal_planar_subgraph_finder::generate_guided_post_order(string input_file, vector<int> post_order) {
read_from_gml(input_file);
set_mutate_point(INT_MAX); // essentially remove mutate point
guidedPostOrderTraversal(post_order);
return return_post_order();
}
vector<int> maximal_planar_subgraph_finder::generate_mutated_post_order(string input_file, vector<int> post_order, int mutate_point) { vector<int> maximal_planar_subgraph_finder::generate_mutated_post_order(string input_file, vector<int> post_order, int mutate_point) {
read_from_gml(input_file); read_from_gml(input_file);
set_mutate_point(INT_MAX); set_mutate_point(INT_MAX);