Feat: re-organized code to accept an order and return edges removed

Issues: order validity checking is not yet implemented
Note: compute_removed_edge_size assumes order is valid
This commit is contained in:
Richard Wong 2023-09-10 13:48:33 +09:00
parent 1e86ec5b1e
commit 46a3810af5
Signed by: richard
GPG Key ID: 5BD36BA2E9EE33D0
5 changed files with 64 additions and 3 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@ make_graphs/bin
make_graphs/test_graphs make_graphs/test_graphs
make_planar/bin make_planar/bin
test_area test_area
dpt

View File

@ -2,15 +2,32 @@
// A simple code that test the MPS algorighm. // A simple code that test the MPS algorighm.
//----------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------
#include "mps.h"
#include <iostream> #include <iostream>
#include <cstdlib> #include <cstdlib>
#include <climits> #include <climits>
#include <string> #include <string>
#include "mps.h" #include <algorithm>
#include <iterator>
#include <random>
#include <vector>
#include <ogdf/fileformats/GraphIO.h>
using namespace std; using namespace std;
int find_mps(string input_file); int compute_removed_edge_size(string input_file, vector<int> post_order);
int get_graph_size(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;
}
return G.numberOfNodes();
}
//----------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------
// Main function. // Main function.
@ -19,7 +36,25 @@ int find_mps(string input_file);
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
string input_file = argv[1]; string input_file = argv[1];
std::cout << "Number of removed edges: " << find_mps(input_file) << std::endl; int num_runs = stoi(argv[2]);
// find the size of the graph here
int node_size = get_graph_size(input_file);
// generate order here
vector<int> post_order;
for (int i=0; i < node_size; ++i) {
post_order.push_back(i);
}
for (int i=0; i< num_runs; ++i) {
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(post_order.begin(), post_order.end(), g);
// print order
std::copy(post_order.begin(), post_order.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
std::cout << "Number of removed edges: " << compute_removed_edge_size(input_file, post_order) << std::endl;
}
return 0; return 0;
} }

View File

@ -31,6 +31,14 @@ maximal_planar_subgraph_finder::postOrderTraversal() {
} }
} }
//Set the post-order-list via given list
void
maximal_planar_subgraph_finder::set_post_order(vector<int> post_order) {
for (int i = 0; i < _node_list.size(); ++i) {
_node_list[i]->set_post_order_index(post_order[i]);
}
}
//Sort the adj-list of every node increasingly according to post-order-index. //Sort the adj-list of every node increasingly according to post-order-index.
void void
maximal_planar_subgraph_finder::sort_adj_list() { maximal_planar_subgraph_finder::sort_adj_list() {

View File

@ -142,10 +142,12 @@ public:
maximal_planar_subgraph_finder(); maximal_planar_subgraph_finder();
~maximal_planar_subgraph_finder(); ~maximal_planar_subgraph_finder();
int find_mps(string input_file); int find_mps(string input_file);
int compute_removed_edge_size(string input_file, vector<int> post_order);
node* get_new_node(node_type t); node* get_new_node(node_type t);
void read_from_gml(string input_file); void read_from_gml(string input_file);
int output_removed_edge_size(); int output_removed_edge_size();
void postOrderTraversal(); void postOrderTraversal();
void set_post_order(vector<int> post_order);
void sort_adj_list(); void sort_adj_list();
void determine_edges(); void determine_edges();
void back_edge_traversal(); void back_edge_traversal();

View File

@ -13,6 +13,11 @@ int find_mps(string input_file) {
return m.find_mps(input_file); return m.find_mps(input_file);
} }
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);
}
int maximal_planar_subgraph_finder::find_mps(string input_file) { int maximal_planar_subgraph_finder::find_mps(string input_file) {
read_from_gml(input_file); read_from_gml(input_file);
postOrderTraversal(); postOrderTraversal();
@ -22,6 +27,16 @@ int maximal_planar_subgraph_finder::find_mps(string input_file) {
return output_removed_edge_size(); return output_removed_edge_size();
} }
int maximal_planar_subgraph_finder::compute_removed_edge_size(string input_file, vector<int> post_order) {
read_from_gml(input_file);
set_post_order(post_order);
sort_adj_list();
determine_edges();
back_edge_traversal();
return output_removed_edge_size();
}
//----------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------
// Imput, output // Imput, output
//----------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------