maximum_planar_subgraph/deferred_planarity_test/src/mps_test.cpp

177 lines
5.9 KiB
C++
Raw Normal View History

//-----------------------------------------------------------------------------------
// Implementation of a MPS algorithm via PC-tree.
//-----------------------------------------------------------------------------------
#include "mps.h"
#include <ogdf/fileformats/GraphIO.h>
#include <algorithm>
#include <vector>
//-----------------------------------------------------------------------------------
// Finding MPS
//-----------------------------------------------------------------------------------
// functions to call from main:
int find_mps(string input_file) {
maximal_planar_subgraph_finder m;
return m.find_mps(input_file);
}
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) {
maximal_planar_subgraph_finder m;
m.print_removed_edge_size(input_file, post_order, mutate_point);
}
vector<int> generate_post_order(string input_file) {
maximal_planar_subgraph_finder m;
return m.generate_post_order(input_file);
}
vector<int> generate_mutated_post_order_at_x(string input_file, vector<int> post_order, int mutate_point) {
maximal_planar_subgraph_finder m;
return m.generate_mutated_post_order(input_file, post_order, mutate_point);
}
// immediate functions called by functions called from main
int maximal_planar_subgraph_finder::find_mps(string input_file) {
read_from_gml(input_file);
postOrderTraversal();
sort_adj_list();
determine_edges();
back_edge_traversal();
return output_int_removed_edge_size();
}
vector<int> maximal_planar_subgraph_finder::generate_post_order(string input_file) {
read_from_gml(input_file);
set_mutate_point(INT_MAX); // essentially removed mutate_point
postOrderTraversal();
return return_post_order();
}
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);
set_mutate_point(INT_MAX);
mutatedPostOrderTraversal(post_order, mutate_point);
return return_post_order();
}
int maximal_planar_subgraph_finder::compute_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();
return output_int_removed_edge_size();
}
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();
}
//-----------------------------------------------------------------------------------
// Input, output
//-----------------------------------------------------------------------------------
// 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) {
_node_list.push_back(new node(P_NODE));
_node_list[i]->set_id(i);
}
// 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);
// }
// 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()]);
}
}
// count the number of removed edges
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() {
int sum = 0;
for (int i = 0; i < _back_edge_list.size(); ++i) {
if (_is_back_edge_eliminate[i] != RETAINED) ++sum;
}
return sum;
}