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>
|
2023-09-09 20:41:49 +09:00
|
|
|
|
2024-02-20 17:00:51 +09:00
|
|
|
#define DEBUG
|
|
|
|
|
2023-09-09 20:41:49 +09:00
|
|
|
//-----------------------------------------------------------------------------------
|
|
|
|
// Finding MPS
|
|
|
|
//-----------------------------------------------------------------------------------
|
2023-09-11 12:21:02 +09:00
|
|
|
|
|
|
|
// programs to call from main:
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-09-10 13:48:33 +09:00
|
|
|
int compute_removed_edge_size(string input_file, vector<int> post_order) {
|
|
|
|
maximal_planar_subgraph_finder m;
|
|
|
|
return m.compute_removed_edge_size(input_file, post_order);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<int> generate_mutated_post_order(string input_file, vector<int> post_order) {
|
|
|
|
maximal_planar_subgraph_finder m;
|
|
|
|
return m.generate_mutated_post_order(input_file, post_order);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ---------
|
|
|
|
|
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();
|
2024-02-20 17:00:51 +09:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
print_post_order();
|
|
|
|
#endif
|
|
|
|
|
2023-09-09 20:41:49 +09:00
|
|
|
sort_adj_list();
|
|
|
|
determine_edges();
|
|
|
|
back_edge_traversal();
|
2023-09-09 21:29:57 +09:00
|
|
|
return output_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-20 17:00:51 +09:00
|
|
|
postOrderTraversal();
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
print_post_order();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return return_post_order();
|
2023-09-11 12:21:02 +09:00
|
|
|
}
|
|
|
|
|
2024-02-20 17:00:51 +09:00
|
|
|
// result of this will be used as input to "compute_removed_edge_size"
|
2023-09-11 12:21:02 +09:00
|
|
|
vector<int> maximal_planar_subgraph_finder::generate_mutated_post_order(string input_file, vector<int> post_order) {
|
|
|
|
read_from_gml(input_file);
|
2024-02-20 17:00:51 +09:00
|
|
|
mutatedPostOrderTraversal(post_order);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
print_post_order();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return return_post_order();
|
2023-09-11 12:21:02 +09:00
|
|
|
}
|
|
|
|
|
2023-09-10 13:48:33 +09:00
|
|
|
|
|
|
|
int maximal_planar_subgraph_finder::compute_removed_edge_size(string input_file, vector<int> post_order) {
|
|
|
|
read_from_gml(input_file);
|
2023-09-11 12:21:02 +09:00
|
|
|
guidedPostOrderTraversal(post_order);
|
|
|
|
|
2024-02-20 17:00:51 +09:00
|
|
|
// set post_order_index
|
2023-09-11 12:21:02 +09:00
|
|
|
for (int i = 0; i < _post_order_list.size(); ++i) {
|
|
|
|
_node_list[_post_order_list[i]->node_id()]->set_post_order_index(i);
|
|
|
|
}
|
2024-02-20 17:00:51 +09:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
print_post_order();
|
|
|
|
#endif
|
2023-09-11 12:21:02 +09:00
|
|
|
|
2023-09-10 13:48:33 +09:00
|
|
|
sort_adj_list();
|
|
|
|
determine_edges();
|
|
|
|
back_edge_traversal();
|
|
|
|
return output_removed_edge_size();
|
|
|
|
}
|
|
|
|
|
2023-09-09 20:41:49 +09:00
|
|
|
//-----------------------------------------------------------------------------------
|
|
|
|
// Imput, output
|
|
|
|
//-----------------------------------------------------------------------------------
|
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);
|
|
|
|
}
|
|
|
|
|
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
|
2023-09-09 21:29:57 +09:00
|
|
|
int maximal_planar_subgraph_finder::output_removed_edge_size() {
|
|
|
|
int sum = 0;
|
|
|
|
for (int i = 0; i < _back_edge_list.size(); ++i) {
|
|
|
|
if (_is_back_edge_eliminate[i]) ++sum;
|
|
|
|
}
|
|
|
|
return sum;
|
|
|
|
}
|