X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=src%2Fgenealgorithms.cpp;h=04875daca3031c0803ed527b9a36742d8965289a;hb=172455c7649ea714eb13f9ac1fd96652f0617ca0;hp=b2d7fa6132b0463ee9a1b1cdf8f240bddf1204f9;hpb=3db2f8d8f21614408dfd072cc45e618c7905461f;p=germs.git diff --git a/src/genealgorithms.cpp b/src/genealgorithms.cpp index b2d7fa6..04875da 100644 --- a/src/genealgorithms.cpp +++ b/src/genealgorithms.cpp @@ -22,7 +22,9 @@ #include "geneorder.h" #include +#include #include +#include using namespace std; std::pair longestSequences(const GeneOrder& go){ @@ -53,3 +55,111 @@ std::vector > robinsonSchensted(const GeneOrder& go){ } return v; } + +struct FindP{ + size_t p; + FindP(size_t p) : p(p) {} + bool operator()(Interval i){ + return (i.first == p || i.second == p); + } +}; + + +std::vector findIntervalsAtPoints(const vector& intervals){ + vector points; + points.push_back(Interval(intervals.size(),intervals.size())); //Dummy interval to match point and index + for (size_t p = 1; p <= intervals.size(); ++p){ + size_t f = 0; + size_t s = 0; + bool found = false; + size_t n = 0; + for (vector::const_iterator i = intervals.begin(); i != intervals.end(); ++i, ++n){ + if (i->first == p){ + if (!found){ + f = n; + found = true; + }else{ + s = n; + break; + } + } + if (i->second == p){ + if (!found){ + f = n; + found = true; + }else{ + s = n; + break; + } + } + } + points.push_back(Interval(f,s)); + } + return points; + +} + +int countCycles(const GeneOrder& go){ + int cycles = 0; + set marked; + vector intervals = findIntervals(go); + vector points = findIntervalsAtPoints(intervals); + for (size_t p = 1; p < go.size(); ++p){ + if (marked.find(p) != marked.end()) + continue; + Interval i = intervals[points[p].first]; + while (marked.find(p) == marked.end()){ + marked.insert(p); + if (i == intervals[points[p].first]) + i = intervals[points[p].second]; + else + i = intervals[points[p].first]; + + if (p == i.first) + p = i.second; + else + p = i.first; + } + ++cycles; + } + return cycles; +} + +std::vector findComponents(const GeneOrder& go){ + return vector(); +} + +/** + * TODO: Think of a better than O(n^2) implementation + * Possibly move it to GeneOrder too + */ +std::vector findIntervals(const GeneOrder& go){ + vector intervals; + for (size_t i = 0; i < go.size() - 1; ++i){ + size_t f = 0; + size_t s = 0; + bool found = false; + size_t n = 0; + for (GeneOrder::iterator g = go.begin(); g != go.end(); ++g, ++n){ + if (static_cast(abs(*g)) == i){ + f = n; + if (*g >= 0) + ++f; + if (found) + break; + found = true; + } + if(static_cast(abs(*g)) == i+1){ + s = n; + if (*g < 0) + ++s; + if (found) + break; + found = true; + } + } + intervals.push_back(Interval(f,s)); + } + return intervals; +} +