From 629805f368aa00dd9dc1c034b1c48d4aefa8131a Mon Sep 17 00:00:00 2001 From: Mikhail Zolotukhin Date: Tue, 5 Dec 2017 21:15:15 +0300 Subject: [PATCH] Fixed programm behavior for sets with cycles --- src/Transversal.h | 24 ++++++++++++++++-------- src/main.cpp | 4 ++-- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/Transversal.h b/src/Transversal.h index 8949561..4075869 100644 --- a/src/Transversal.h +++ b/src/Transversal.h @@ -24,6 +24,7 @@ #include "Graph.h" #include #include +#include #include #include @@ -44,6 +45,7 @@ class Transversal { Graph graph; Set superset; std::vector> last_route; + std::vector> tmp_route; std::vector> bijection; public: @@ -53,7 +55,7 @@ class Transversal { Transversal operator = (const Transversal&) = delete; Transversal operator = (Transversal&&) = delete; - Transversal (const std::vector< Set >& sets) : graph{}, superset{}, last_route{}, bijection{} { + Transversal (const std::vector< Set >& sets) : graph{}, superset{}, last_route{}, tmp_route{}, bijection{} { build_superset(sets); if (sets.size() > superset.power()) { @@ -61,7 +63,7 @@ class Transversal { } build_graph(sets); - //show_graph(); + show_graph(); while (has_routes_left()) { try { @@ -69,8 +71,9 @@ class Transversal { } catch (no_way_in_graph_error) { graph.remove_edge(std::pair{T{"BEGIN"}, T{last_route.back().second}}); + //std::cout << "Defective element" << std::endl; } - //show_last_route(); + show_last_route(); change_direction_of_edges(); remove_start_and_end(); } @@ -96,7 +99,7 @@ class Transversal { for (auto view : bijection) { std::cout << view.first << " -> " << view.second << ", "; } - std::cout << " }" << std::endl; + std::cout << "}" << std::endl; }; private: @@ -137,6 +140,7 @@ class Transversal { void build_some_route() { last_route.clear(); + tmp_route.clear(); auto start_edge = graph.get_edge_starting_with(T{"BEGIN"}); build_route_from(start_edge); @@ -153,10 +157,14 @@ class Transversal { auto possible_ways = graph.get_edges_starting_with(edge.second); for (auto way : possible_ways) { - auto has_reached_the_end = build_route_from(way); - if (has_reached_the_end ) { - last_route.push_back(way); - return true; + if (std::find(tmp_route.begin(), tmp_route.end(), way) == tmp_route.end()) { + tmp_route.push_back(way); + auto has_reached_the_end = build_route_from(way); + if (has_reached_the_end ) { + last_route.push_back(way); + return true; + } + tmp_route.pop_back(); } } return false; diff --git a/src/main.cpp b/src/main.cpp index 9709ee8..19f47dd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -57,10 +57,10 @@ int main() vector< Set > sets_generation() { srand(time(NULL)); - enum {MAX_NUMBER_OF_SETS = 10}; + enum {MAX_NUMBER_OF_SETS = 5, MIN_NUMBER_OF_SETS = 3}; std::vector< Set > sets {}; - int number_of_sets = rand() % MAX_NUMBER_OF_SETS + 1; + int number_of_sets = MIN_NUMBER_OF_SETS + rand() % (MAX_NUMBER_OF_SETS-MIN_NUMBER_OF_SETS + 1); static const string mask { "0123456789"