Compare commits

..

2 Commits

6 changed files with 70 additions and 60 deletions

View File

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

View File

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

View File

@ -13,38 +13,46 @@
#include <vector>
#include <ogdf/fileformats/GraphIO.h>
#define START_TEMP 100
using namespace std;
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_mutated_post_order(string input_file, vector<int> post_order);
vector<int> generate_mutated_post_order(string input_file, vector<int> post_order, int mutate_point);
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
std::cout << "generate first post order" << std::endl;
vector<int> state_old = generate_post_order(input_file);
vector<int> state_new;
int num_removed_edges;
for (int k = 0; k < k_max; ++k) {
// rotate it first
std::cout << "cycle:" << k << std::endl;
std::cout << "rotate the dfs tree" << std::endl;
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;
state_new = generate_mutated_post_order(input_file, state_new);
// num_removed_edges = compute_removed_edge_size(input_file, state_new);
// first time will rotate the tree
std::cout << "rotate the dfs tree" << std::endl;
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;
vector_printer(state_old);
// mutation produces rotated view
state_new = generate_mutated_post_order(input_file, state_old, mutate_point);
vector_printer(state_new);
// another round of guided post order gives canonical representation
state_new = generate_guided_post_order(input_file, state_new);
vector_printer(state_new);
std::cout << std::endl;
}
return state_new;
@ -76,9 +84,10 @@ int get_graph_size(string input_file) {
int main(int argc, char* argv[]) {
string input_file = argv[1];
int k_max = std::stoi(argv[2]);
int mutate_point = std::stoi(argv[3]);
// generate order here
vector<int> post_order = repeated_mutation(input_file, k_max);
vector<int> post_order = repeated_mutation(input_file, k_max, mutate_point);
// test_correctness(input_file);
// // print final order and number of edges

View File

@ -34,7 +34,13 @@ maximal_planar_subgraph_finder::return_post_order() {
//Determine the post-order-list by a DFS-traversal.
void
maximal_planar_subgraph_finder::postOrderTraversal() {
node::init_mark();
// node::init_mark(); // set ref_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
int postOrderID = 0;
for (int i = 0; i < _node_list.size(); ++i) {
@ -50,7 +56,7 @@ maximal_planar_subgraph_finder::postOrderTraversal() {
// return is by reference via _post_order_list
void
maximal_planar_subgraph_finder::guidedPostOrderTraversal(vector<int> post_order) {
node::init_mark();
// node::init_mark();
vector<int> rev_post_order;
for (int i = post_order.size() - 1; i >= 0; --i) {
@ -85,8 +91,8 @@ maximal_planar_subgraph_finder::guidedPostOrderTraversal(vector<int> post_order)
// take in a post-order argument then traces the graph in the same order
// return is by reference via _post_order_list
void
maximal_planar_subgraph_finder::mutatedPostOrderTraversal(vector<int> post_order) {
node::init_mark();
maximal_planar_subgraph_finder::mutatedPostOrderTraversal(vector<int> post_order, int mutate_point) {
// node::init_mark();
vector<int> rev_post_order;
for (int i = post_order.size() - 1; i >= 0; --i) {
@ -95,27 +101,13 @@ maximal_planar_subgraph_finder::mutatedPostOrderTraversal(vector<int> post_order
int postOrderID = 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
// 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
int start = rev_post_order[0];
int i = start;
// if mutate_point = 0
if (mutate_point == 0) {
// generate another point
start = distribution(rng);
}
int end_condition = _node_list.size();
// this loop assumes start is not from 0

View File

@ -5,7 +5,7 @@
#include "mps.h"
#include <ogdf/fileformats/GraphIO.h>
#define DEBUG
// #define DEBUG
//-----------------------------------------------------------------------------------
// Finding MPS
@ -28,9 +28,9 @@ vector<int> generate_post_order(string input_file) {
return m.generate_post_order(input_file);
}
vector<int> generate_mutated_post_order(string input_file, vector<int> post_order) {
vector<int> generate_mutated_post_order(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);
return m.generate_mutated_post_order(input_file, post_order, mutate_point);
}
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"
vector<int> maximal_planar_subgraph_finder::generate_mutated_post_order(string input_file, vector<int> 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);
mutatedPostOrderTraversal(post_order);
mutatedPostOrderTraversal(post_order, mutate_point);
#ifdef DEBUG
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);
guidedPostOrderTraversal(post_order);
#ifdef DEBUG
std::cout << "guided post order traversal" << std::endl;
print_post_order();
#endif
// #ifdef DEBUG
// std::cout << "guided post order traversal" << std::endl;
// print_post_order();
// #endif
return return_post_order();
}

View File

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