Skip to content

Commit

Permalink
Fixed programm behavior for sets with cycles
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Zolotukhin authored and Mikhail Zolotukhin committed Dec 5, 2017
1 parent 2b00faa commit 629805f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
24 changes: 16 additions & 8 deletions src/Transversal.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "Graph.h"
#include <vector>
#include <utility>
#include <algorithm>
#include <stdexcept>
#include <iostream>

Expand All @@ -44,6 +45,7 @@ class Transversal {
Graph<T> graph;
Set<T> superset;
std::vector<std::pair<T,T>> last_route;
std::vector<std::pair<T,T>> tmp_route;
std::vector<std::pair<T,T>> bijection;

public:
Expand All @@ -53,24 +55,25 @@ class Transversal {
Transversal operator = (const Transversal&) = delete;
Transversal operator = (Transversal&&) = delete;

Transversal (const std::vector< Set<T> >& sets) : graph{}, superset{}, last_route{}, bijection{} {
Transversal (const std::vector< Set<T> >& sets) : graph{}, superset{}, last_route{}, tmp_route{}, bijection{} {
build_superset(sets);

if (sets.size() > superset.power()) {
throw wrong_input_error{"Transversal cannot be builded with current input"};
}

build_graph(sets);
//show_graph();
show_graph();

while (has_routes_left()) {
try {
build_some_route();
}
catch (no_way_in_graph_error) {
graph.remove_edge(std::pair<T,T>{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();
}
Expand All @@ -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:
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ int main()
vector< Set<string> > 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<string> > 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"
Expand Down

0 comments on commit 629805f

Please sign in to comment.