2023-09-09 20:41:49 +09:00
|
|
|
//-----------------------------------------------------------------------------------
|
|
|
|
// Implementation of a MPS algorithm via PC-tree.
|
|
|
|
//-----------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "mps.h"
|
2023-09-09 21:29:57 +09:00
|
|
|
#include <ogdf/fileformats/GraphIO.h>
|
2024-02-06 10:58:38 +09:00
|
|
|
#include <algorithm>
|
|
|
|
#include <vector>
|
|
|
|
|
2023-09-09 20:41:49 +09:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------------
|
|
|
|
// Finding MPS
|
|
|
|
//-----------------------------------------------------------------------------------
|
2023-09-11 12:21:02 +09:00
|
|
|
|
2024-02-06 10:58:38 +09:00
|
|
|
// functions to call from main:
|
2023-09-11 12:21:02 +09:00
|
|
|
|
2023-09-09 21:29:57 +09:00
|
|
|
int find_mps(string input_file) {
|
2023-09-09 20:41:49 +09:00
|
|
|
maximal_planar_subgraph_finder m;
|
2023-09-09 21:29:57 +09:00
|
|
|
return m.find_mps(input_file);
|
2023-09-09 20:41:49 +09:00
|
|
|
}
|
|
|
|
|
2024-02-06 10:58:38 +09:00
|
|
|
int compute_removed_edge_size(string input_file, vector<int> post_order, int mutate_point) {
|
|
|
|
maximal_planar_subgraph_finder m;
|
|
|
|
return m.compute_removed_edge_size(input_file, post_order, mutate_point);
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_removed_edge_size(string input_file, vector<int> post_order, int mutate_point) {
|
2023-09-10 13:48:33 +09:00
|
|
|
maximal_planar_subgraph_finder m;
|
2024-02-06 10:58:38 +09:00
|
|
|
m.print_removed_edge_size(input_file, post_order, mutate_point);
|
2023-09-10 13:48:33 +09:00
|
|
|
}
|
|
|
|
|
2023-09-11 12:21:02 +09:00
|
|
|
vector<int> generate_post_order(string input_file) {
|
|
|
|
maximal_planar_subgraph_finder m;
|
|
|
|
return m.generate_post_order(input_file);
|
|
|
|
}
|
|
|
|
|
2024-02-06 10:58:38 +09:00
|
|
|
vector<int> generate_mutated_post_order_at_x(string input_file, vector<int> post_order, int mutate_point) {
|
2023-09-11 12:21:02 +09:00
|
|
|
maximal_planar_subgraph_finder m;
|
2024-02-06 10:58:38 +09:00
|
|
|
return m.generate_mutated_post_order(input_file, post_order, mutate_point);
|
2023-09-11 12:21:02 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-02-06 10:58:38 +09:00
|
|
|
// immediate functions called by functions called from main
|
2023-09-11 12:21:02 +09:00
|
|
|
|
2023-09-09 21:29:57 +09:00
|
|
|
int maximal_planar_subgraph_finder::find_mps(string input_file) {
|
|
|
|
read_from_gml(input_file);
|
2023-09-09 20:41:49 +09:00
|
|
|
postOrderTraversal();
|
|
|
|
sort_adj_list();
|
|
|
|
determine_edges();
|
|
|
|
back_edge_traversal();
|
2024-02-06 10:58:38 +09:00
|
|
|
return output_int_removed_edge_size();
|
2023-09-09 20:41:49 +09:00
|
|
|
}
|
|
|
|
|
2023-09-11 12:21:02 +09:00
|
|
|
vector<int> maximal_planar_subgraph_finder::generate_post_order(string input_file) {
|
|
|
|
read_from_gml(input_file);
|
2024-02-06 10:58:38 +09:00
|
|
|
set_mutate_point(INT_MAX); // essentially removed mutate_point
|
2024-02-21 21:23:19 +09:00
|
|
|
postOrderTraversal();
|
|
|
|
return return_post_order();
|
2023-09-11 12:21:02 +09:00
|
|
|
}
|
|
|
|
|
2024-02-06 10:58:38 +09:00
|
|
|
vector<int> maximal_planar_subgraph_finder::generate_mutated_post_order(string input_file, vector<int> post_order, int mutate_point) {
|
2023-09-11 12:21:02 +09:00
|
|
|
read_from_gml(input_file);
|
2024-02-06 10:58:38 +09:00
|
|
|
set_mutate_point(INT_MAX);
|
2024-02-21 21:23:19 +09:00
|
|
|
mutatedPostOrderTraversal(post_order, mutate_point);
|
|
|
|
return return_post_order();
|
2023-09-11 12:21:02 +09:00
|
|
|
}
|
|
|
|
|
2023-09-10 13:48:33 +09:00
|
|
|
|
2024-02-06 10:58:38 +09:00
|
|
|
int maximal_planar_subgraph_finder::compute_removed_edge_size(string input_file, vector<int> post_order, int mutate_point) {
|
2023-09-10 13:48:33 +09:00
|
|
|
read_from_gml(input_file);
|
2024-02-06 10:58:38 +09:00
|
|
|
set_mutate_point(mutate_point);
|
2023-09-11 12:21:02 +09:00
|
|
|
guidedPostOrderTraversal(post_order);
|
|
|
|
|
2023-09-10 13:48:33 +09:00
|
|
|
sort_adj_list();
|
|
|
|
determine_edges();
|
|
|
|
back_edge_traversal();
|
2024-02-06 10:58:38 +09:00
|
|
|
return output_int_removed_edge_size();
|
2023-09-10 13:48:33 +09:00
|
|
|
}
|
|
|
|
|
2024-02-06 10:58:38 +09:00
|
|
|
void maximal_planar_subgraph_finder::print_removed_edge_size(string input_file, vector<int> post_order, int mutate_point) {
|
|
|
|
read_from_gml(input_file);
|
|
|
|
set_mutate_point(mutate_point);
|
|
|
|
guidedPostOrderTraversal(post_order);
|
|
|
|
|
|
|
|
sort_adj_list();
|
|
|
|
determine_edges();
|
|
|
|
back_edge_traversal();
|
|
|
|
output_print_removed_edge_size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-09-09 20:41:49 +09:00
|
|
|
//-----------------------------------------------------------------------------------
|
2024-02-06 10:58:38 +09:00
|
|
|
// Input, output
|
2023-09-09 20:41:49 +09:00
|
|
|
//-----------------------------------------------------------------------------------
|
2023-09-09 21:29:57 +09:00
|
|
|
|
|
|
|
|
|
|
|
// read input file of gml format
|
|
|
|
void maximal_planar_subgraph_finder::read_from_gml(string input_file) {
|
|
|
|
ogdf::Graph G;
|
|
|
|
|
|
|
|
// utilize OGDF readGML
|
|
|
|
if (!ogdf::GraphIO::read(G, input_file, ogdf::GraphIO::readGML)) {
|
|
|
|
std::cerr << "Could not read " << input_file << ".gml" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create nodes
|
|
|
|
for (int i = 0; i < G.numberOfNodes(); ++i) {
|
2023-09-09 20:41:49 +09:00
|
|
|
_node_list.push_back(new node(P_NODE));
|
|
|
|
_node_list[i]->set_id(i);
|
|
|
|
}
|
|
|
|
|
2024-02-06 10:58:38 +09:00
|
|
|
// vector<ogdf::edge> unique_edges;
|
|
|
|
// // we want to get unique edge only
|
|
|
|
// for (ogdf::edge e : G.edges) {
|
|
|
|
// // Check if the edge is already in the array
|
|
|
|
// if (std::find(unique_edges.begin(), unique_edges.end(), e) == unique_edges.end())
|
|
|
|
// unique_edges.push_back(e);
|
|
|
|
// }
|
|
|
|
|
2023-09-09 21:29:57 +09:00
|
|
|
// create edges
|
|
|
|
for (ogdf::edge e : G.edges) {
|
|
|
|
ogdf::node source = e->source();
|
|
|
|
ogdf::node target = e->target();
|
|
|
|
_node_list[source->index()]->add_adj(_node_list[target->index()]);
|
|
|
|
_node_list[target->index()]->add_adj(_node_list[source->index()]);
|
|
|
|
}
|
2023-09-09 20:41:49 +09:00
|
|
|
}
|
|
|
|
|
2023-09-09 21:29:57 +09:00
|
|
|
|
|
|
|
|
2024-02-05 11:02:44 +09:00
|
|
|
// count the number of removed edges
|
2024-02-06 10:58:38 +09:00
|
|
|
void maximal_planar_subgraph_finder::output_print_removed_edge_size() {
|
|
|
|
int mutated_sum = 0;
|
|
|
|
int preserved_sum = 0;
|
|
|
|
|
|
|
|
vector<pair<node*, node*>> mutated_removed_edges;
|
|
|
|
vector<pair<node*, node*>> preserved_removed_edges;
|
|
|
|
|
|
|
|
for (int i = 0; i < _back_edge_list.size(); ++i) {
|
|
|
|
if (_is_back_edge_eliminate[i] == MUTATED_REMOVE) {
|
|
|
|
mutated_removed_edges.push_back(_back_edge_list[i]);
|
|
|
|
++mutated_sum;
|
|
|
|
}
|
|
|
|
if (_is_back_edge_eliminate[i] == NON_MUTATED_REMOVE) {
|
|
|
|
preserved_removed_edges.push_back(_back_edge_list[i]);
|
|
|
|
++preserved_sum;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// print the edges
|
|
|
|
// std::cout << "mutated removed edges: " << std::endl;
|
|
|
|
// for (int i = 0; i < mutated_removed_edges.size(); ++i) {
|
|
|
|
// std::cout << mutated_removed_edges[i].first->node_id() << ", " <<
|
|
|
|
// mutated_removed_edges[i].second->node_id() << std::endl;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// std::cout << "preserved removed edges: " << std::endl;
|
|
|
|
// for (int i = 0; i < preserved_removed_edges.size(); ++i) {
|
|
|
|
// std::cout << preserved_removed_edges[i].first->node_id() << ", " <<
|
|
|
|
// preserved_removed_edges[i].second->node_id() << std::endl;
|
|
|
|
// }
|
|
|
|
|
|
|
|
std::cout << "<- sum of removed edges -> " << std::endl;
|
|
|
|
std::cout << "mutated portion: " << mutated_sum << std::endl;
|
|
|
|
std::cout << "preserved portion: " << preserved_sum << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
int maximal_planar_subgraph_finder::output_int_removed_edge_size() {
|
2023-09-09 21:29:57 +09:00
|
|
|
int sum = 0;
|
|
|
|
for (int i = 0; i < _back_edge_list.size(); ++i) {
|
2024-02-06 10:58:38 +09:00
|
|
|
if (_is_back_edge_eliminate[i] != RETAINED) ++sum;
|
2023-09-09 21:29:57 +09:00
|
|
|
}
|
2024-02-06 10:58:38 +09:00
|
|
|
|
2023-09-09 21:29:57 +09:00
|
|
|
return sum;
|
|
|
|
}
|