Compare commits

..

No commits in common. "reverse_traversal" and "test" have entirely different histories.

6 changed files with 60 additions and 70 deletions

View File

@ -33,12 +33,6 @@ enum node_type {
AE_VIRTUAL_ROOT = 3 AE_VIRTUAL_ROOT = 3
}; };
enum mark_type {
UNMARKED = 0,
MARKED = 1,
RESERVED = 2,
};
class node class node
{ {
public: public:
@ -122,6 +116,7 @@ public:
//MARK //MARK
void mark(); void mark();
static void init_mark();
void un_mark(); void un_mark();
bool is_marked(); bool is_marked();
@ -151,7 +146,8 @@ private:
vector<node*> _essential_list; vector<node*> _essential_list;
//Mark //Mark
mark_type _mark; int _mark;
static int _ref_mark;
}; };
class maximal_planar_subgraph_finder class maximal_planar_subgraph_finder
@ -162,7 +158,7 @@ public:
int find_mps(string input_file); int find_mps(string input_file);
int compute_removed_edge_size(string input_file, vector<int> post_order); int compute_removed_edge_size(string input_file, vector<int> post_order);
vector<int> generate_post_order(string input_file); vector<int> generate_post_order(string input_file);
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);
vector<int> generate_guided_post_order(string input_file, vector<int> post_order); vector<int> generate_guided_post_order(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);
@ -170,7 +166,7 @@ public:
vector<int> return_post_order(); vector<int> return_post_order();
void postOrderTraversal(); void postOrderTraversal();
void guidedPostOrderTraversal(vector<int> post_order); void guidedPostOrderTraversal(vector<int> post_order);
void mutatedPostOrderTraversal(vector<int> post_order, int mutate_point); void mutatedPostOrderTraversal(vector<int> post_order);
// void set_post_order(vector<int> post_order); // void set_post_order(vector<int> post_order);
void print_post_order(); void print_post_order();
void sort_adj_list(); void sort_adj_list();

View File

@ -31,4 +31,4 @@ $(BIN_DIR) $(OBJ_DIR):
.PHONY: clean .PHONY: clean
clean: clean:
rm -r $(OBJ_DIR) rm -r $(BIN_DIR) $(OBJ_DIR)

View File

@ -13,46 +13,38 @@
#include <vector> #include <vector>
#include <ogdf/fileformats/GraphIO.h> #include <ogdf/fileformats/GraphIO.h>
#define START_TEMP 100
using namespace std; using namespace std;
int compute_removed_edge_size(string input_file, vector<int> post_order); int compute_removed_edge_size(string input_file, vector<int> post_order);
// these functions are defined in mps_test.cpp
// but their signatures are not in mps.h, hence they are declared here
vector<int> generate_post_order(string input_file); vector<int> generate_post_order(string input_file);
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);
vector<int> generate_guided_post_order(string input_file, vector<int> post_order); vector<int> generate_guided_post_order(string input_file, vector<int> post_order);
void vector_printer(vector<int> state) {
for (int i = 0; i < state.size(); ++i) {
std::cout << state[i] << ",";
}
std::cout << std::endl;
}
vector<int> repeated_mutation(string input_file, int k_max) {
vector<int> repeated_mutation(string input_file, int k_max, int mutate_point) {
// generate first post order // generate first post order
std::cout << "generate first post order" << std::endl; std::cout << "generate first post order" << std::endl;
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;
int num_removed_edges;
for (int k = 0; k < k_max; ++k) { for (int k = 0; k < k_max; ++k) {
// rotate it first
std::cout << "cycle:" << k << std::endl; std::cout << "cycle:" << k << std::endl;
std::cout << "rotate the dfs tree" << std::endl;
vector_printer(state_old); state_new = generate_guided_post_order(input_file, state_old);
// then the next traversal will rotate it back
std::cout << "mutate the dfs tree" << std::endl;
// mutation produces rotated view state_new = generate_mutated_post_order(input_file, state_new);
state_new = generate_mutated_post_order(input_file, state_old, mutate_point); // num_removed_edges = compute_removed_edge_size(input_file, state_new);
vector_printer(state_new); // first time will rotate the tree
std::cout << "rotate the dfs tree" << std::endl;
// another round of guided post order gives canonical representation state_new = generate_guided_post_order(input_file, state_new);
// second time will rotate back the rotated tree
std::cout << "print the mutated tree again" << std::endl;
state_new = generate_guided_post_order(input_file, state_new); state_new = generate_guided_post_order(input_file, state_new);
vector_printer(state_new);
std::cout << std::endl; std::cout << std::endl;
} }
return state_new; return state_new;
@ -84,10 +76,9 @@ int get_graph_size(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];
int k_max = std::stoi(argv[2]); int k_max = std::stoi(argv[2]);
int mutate_point = std::stoi(argv[3]);
// generate order here // generate order here
vector<int> post_order = repeated_mutation(input_file, k_max, mutate_point); vector<int> post_order = repeated_mutation(input_file, k_max);
// test_correctness(input_file); // test_correctness(input_file);
// // print final order and number of edges // // print final order and number of edges

View File

@ -34,13 +34,7 @@ maximal_planar_subgraph_finder::return_post_order() {
//Determine the post-order-list by a DFS-traversal. //Determine the post-order-list by a DFS-traversal.
void void
maximal_planar_subgraph_finder::postOrderTraversal() { maximal_planar_subgraph_finder::postOrderTraversal() {
// node::init_mark(); // set ref_mark node::init_mark();
// print out the ref_mark
// std::cout << "normal post order traversal" << std::endl;
// std::cout << "the ref mark is initialized to be:" << std::endl;
// _node_list[0]->print_ref_mark();
// always start with node 0 // always start with node 0
int postOrderID = 0; int postOrderID = 0;
for (int i = 0; i < _node_list.size(); ++i) { for (int i = 0; i < _node_list.size(); ++i) {
@ -56,7 +50,7 @@ maximal_planar_subgraph_finder::postOrderTraversal() {
// return is by reference via _post_order_list // return is by reference via _post_order_list
void void
maximal_planar_subgraph_finder::guidedPostOrderTraversal(vector<int> post_order) { maximal_planar_subgraph_finder::guidedPostOrderTraversal(vector<int> post_order) {
// node::init_mark(); node::init_mark();
vector<int> rev_post_order; vector<int> rev_post_order;
for (int i = post_order.size() - 1; i >= 0; --i) { for (int i = post_order.size() - 1; i >= 0; --i) {
@ -91,8 +85,8 @@ maximal_planar_subgraph_finder::guidedPostOrderTraversal(vector<int> post_order)
// take in a post-order argument then traces the graph in the same order // take in a post-order argument then traces the graph in the same order
// return is by reference via _post_order_list // return is by reference via _post_order_list
void void
maximal_planar_subgraph_finder::mutatedPostOrderTraversal(vector<int> post_order, int mutate_point) { maximal_planar_subgraph_finder::mutatedPostOrderTraversal(vector<int> post_order) {
// node::init_mark(); node::init_mark();
vector<int> rev_post_order; vector<int> rev_post_order;
for (int i = post_order.size() - 1; i >= 0; --i) { for (int i = post_order.size() - 1; i >= 0; --i) {
@ -101,13 +95,27 @@ maximal_planar_subgraph_finder::mutatedPostOrderTraversal(vector<int> post_order
int postOrderID = 0; int postOrderID = 0;
int traversal_index = 0; int traversal_index = 0;
// introduce random selection
std::random_device rd;
std::mt19937 rng(rd());
// Define the range [0, n] // Define the range [0, n]
int n = _node_list.size() - 1; // Change 'n' to your desired upper bound int n = _node_list.size() - 1; // Change 'n' to your desired upper bound
// Create a uniform distribution for the range [0, n]
std::uniform_int_distribution<int> distribution(0, n);
// Generate a random number between 0 and n (inclusive)
int mutate_point = distribution(rng);
// std::cout << "the mutate point: " << mutate_point << std::endl;
// set loop variables // set loop variables
int start = rev_post_order[0]; int start = rev_post_order[0];
int i = start; int i = start;
// if mutate_point = 0
if (mutate_point == 0) {
// generate another point
start = distribution(rng);
}
int end_condition = _node_list.size(); int end_condition = _node_list.size();
// this loop assumes start is not from 0 // this loop assumes start is not from 0

View File

@ -5,7 +5,7 @@
#include "mps.h" #include "mps.h"
#include <ogdf/fileformats/GraphIO.h> #include <ogdf/fileformats/GraphIO.h>
// #define DEBUG #define DEBUG
//----------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------
// Finding MPS // Finding MPS
@ -28,9 +28,9 @@ 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_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) {
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);
} }
vector<int> generate_guided_post_order(string input_file, vector<int> post_order) { vector<int> generate_guided_post_order(string input_file, vector<int> post_order) {
@ -68,9 +68,9 @@ vector<int> maximal_planar_subgraph_finder::generate_post_order(string input_fil
} }
// result of this will be used as input to "compute_removed_edge_size" // result of this will be used as input to "compute_removed_edge_size"
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) {
read_from_gml(input_file); read_from_gml(input_file);
mutatedPostOrderTraversal(post_order, mutate_point); mutatedPostOrderTraversal(post_order);
#ifdef DEBUG #ifdef DEBUG
std::cout << "mutated post order traversal" << std::endl; std::cout << "mutated post order traversal" << std::endl;
@ -85,10 +85,10 @@ vector<int> maximal_planar_subgraph_finder::generate_guided_post_order(string in
read_from_gml(input_file); read_from_gml(input_file);
guidedPostOrderTraversal(post_order); guidedPostOrderTraversal(post_order);
// #ifdef DEBUG #ifdef DEBUG
// std::cout << "guided post order traversal" << std::endl; std::cout << "guided post order traversal" << std::endl;
// print_post_order(); print_post_order();
// #endif #endif
return return_post_order(); return return_post_order();
} }

View File

@ -5,7 +5,6 @@
#include "mps.h" #include "mps.h"
// #define DEBUG // #define DEBUG
// #define DEBUG_MUTATION
//----------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------
// CONSTRUCTOR // CONSTRUCTOR
@ -20,7 +19,7 @@ node::node(node_type t) {
_parent = 0; _parent = 0;
_post_order_index = INT_MAX; _post_order_index = INT_MAX;
_node_id = INT_MAX; _node_id = INT_MAX;
_mark = UNMARKED; _mark = 0;
} }
//----------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------
@ -57,7 +56,7 @@ node* node::adj(int i) {return _adj_list[i];}
void node::set_adj_list(vector<node*> vec) {_adj_list = vec;} void node::set_adj_list(vector<node*> vec) {_adj_list = vec;}
void node::DFS_visit(vector<node*> &dfsList, int &index) { void node::DFS_visit(vector<node*> &dfsList, int &index) {
mark(); // set _mark to _ref_mark mark();
for (int i = 0; i < _adj_list.size(); ++i) { for (int i = 0; i < _adj_list.size(); ++i) {
if (!_adj_list[i]->is_marked()) { if (!_adj_list[i]->is_marked()) {
_adj_list[i]->_parent = this; _adj_list[i]->_parent = this;
@ -166,14 +165,6 @@ void node::mutated_DFS_visit(vector<node*> &dfsList,
std::mt19937 rng(rd()); std::mt19937 rng(rd());
// Use std::shuffle to shuffle the elements in the vector // Use std::shuffle to shuffle the elements in the vector
std::shuffle(neighbor_list.begin(), neighbor_list.end(), rng); std::shuffle(neighbor_list.begin(), neighbor_list.end(), rng);
#ifdef DEBUG_MUTATION
std::cout << "current node:" << this->node_id() << std::endl;
for (int i = 0; i < neighbor_list.size(); ++i) {
std::cout << neighbor_list[i]->node_id() << "(" << neighbor_list[i]->is_marked() << ")" << ",";
}
std::cout << std::endl;
#endif
} }
// increment traversal index after checking // increment traversal index after checking
@ -358,8 +349,12 @@ void node::add_essential(node* u) {_essential_list.push_back(u);}
//----------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------
// MARK // MARK
//----------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------
void node::mark() {_mark = MARKED;} void node::mark() {_mark = _ref_mark;}
void node::un_mark() {_mark = UNMARKED;} void node::init_mark() {++_ref_mark;}
bool node::is_marked() {return _mark == MARKED;} void node::un_mark() {_mark = 0;}
bool node::is_marked() {return _mark == _ref_mark;}
int node::_ref_mark = 1;