Fix: bug when decrementing with size_t

This commit is contained in:
Richard Wong 2024-03-04 09:32:01 +09:00
parent fbbc957fda
commit 0e80061f44
Signed by: richard
GPG Key ID: 5BD36BA2E9EE33D0
5 changed files with 33 additions and 16 deletions

View File

@ -10,6 +10,7 @@
#include <vector>
#include <utility>
#include <climits>
#include <limits>
#include <random>
#include <algorithm>
#include <unordered_map>

View File

@ -27,7 +27,7 @@ vector<int> generate_mutated_post_order(const ogdf::Graph &G, vector<int> post_o
vector<int> generate_guided_post_order(const ogdf::Graph &G, vector<int> post_order);
void vector_printer(const vector<int>& state) {
for (int i = 0; i < static_cast<int>(state.size()); ++i) {
for (size_t i = 0; i < state.size(); ++i) {
std::cout << state[i] << ",";
}
std::cout << std::endl;
@ -49,20 +49,20 @@ vector<int> repeated_mutation(const ogdf::Graph &G, int k_max, int mutate_point)
// mutation produces rotated view
// state_new = generate_mutated_post_order(G, state_old, mutate_point);
// simulate a mutation
state_new = generate_guided_post_order(G, state_old);
// state_new = generate_guided_post_order(G, state_old);
// another round of guided post order gives canonical representation
state_new = generate_guided_post_order(G, state_new);
new_edge_size = compute_removed_edge_size(G, state_new);
// state_new = generate_guided_post_order(G, state_new);
new_edge_size = compute_removed_edge_size(G, state_old);
if (new_edge_size < old_edge_size) {
state_old = state_new;
}
// if (new_edge_size < old_edge_size) {
// state_old = state_new;
// }
}
vector_printer(state_new);
return state_new;
// vector_printer(state_new);
return state_old;
}
void test_correctness(const ogdf::Graph &G) {

View File

@ -59,18 +59,20 @@ maximal_planar_subgraph_finder::guidedPostOrderTraversal(vector<int> post_order)
// int start = rev_post_order[0];
// implementation 2: use unordered_map to map node_id to position in reversed post_order
unordered_map<int, int> node_id_to_pos;
int j = 0;
// we flip the post_order vector around
for (size_t i = post_order.size() - 1; i >= 0; --i) {
for (size_t i = post_order.size() - 1; i != std::numeric_limits<size_t>::max(); --i) {
node_id_to_pos[post_order[i]] = j++;
}
int postOrderID = 0;
int end_condition = _node_list.size();
// we start from the end of the post_order, which is the root node
int start = post_order[post_order.size() - 1];
int i = start;
while (true)
{
if (((start > 0) && (i == (start - 1))) || ((start == 0 ) && (i == end_condition - 1)))

View File

@ -8,8 +8,7 @@
#include <ogdf/fileformats/GraphIO.h>
// #define DEBUG
#define TIME
// #define TIME
//-----------------------------------------------------------------------------------
// Finding MPS
@ -138,14 +137,29 @@ int maximal_planar_subgraph_finder::compute_removed_edge_size(const ogdf::Graph
start = std::chrono::high_resolution_clock::now();
#endif
sort_adj_list();
#ifdef TIME
end = std::chrono::high_resolution_clock::now();
std::cout << "sort_adj_list: " << std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() << std::endl;
#endif
#ifdef TIME
start = std::chrono::high_resolution_clock::now();
#endif
determine_edges();
#ifdef TIME
end = std::chrono::high_resolution_clock::now();
std::cout << "determine edges: " << std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() << std::endl;
#endif
#ifdef TIME
start = std::chrono::high_resolution_clock::now();
#endif
back_edge_traversal();
#ifdef TIME
end = std::chrono::high_resolution_clock::now();
std::cout << "remaining procedures: " << std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() << std::endl;
std::cout << "back edge traversal: " << std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() << std::endl;
#endif
return output_removed_edge_size();
}

View File

@ -5,7 +5,7 @@
#include "mps.h"
// #define DEBUG
#define DEBUG_MUTATION
// #define DEBUG_MUTATION
//-----------------------------------------------------------------------------------
// CONSTRUCTOR