X-Git-Url: https://ruin.nu/git/?p=germs.git;a=blobdiff_plain;f=src%2Fgenealgorithms.cpp;h=ec3a92ea1890f1d9e36554e10086ef4e695c06f3;hp=eecdf3fea92bd8048c6c47fd1ef466d5e4698c67;hb=65f449adad91a229757c0317c27ad9fb87a4d222;hpb=6be4116d6211ac4ba4c68dc2ab28f1bc5d77068e diff --git a/src/genealgorithms.cpp b/src/genealgorithms.cpp index eecdf3f..ec3a92e 100644 --- a/src/genealgorithms.cpp +++ b/src/genealgorithms.cpp @@ -56,12 +56,72 @@ 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; + for (size_t p = 1; p <= intervals.size(); ++p){ + cout << endl << "Point " << 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; + } + } + } + cout << f << ":" << s << endl; + points.push_back(Interval(f,s)); + } + return points; + +} + int countCycles(const GeneOrder& go){ int cycles = 0; set marked; - for (size_t p = 1; p < go.size() - 1; ++p){ + vector intervals = findIntervals(go); + vector points; + for (size_t p = 1; p < go.size(); ++p){ if (marked.find(p) != marked.end()) continue; + Interval i = intervals[points[p-1].first]; + while (marked.find(p) != marked.end()){ + marked.insert(p); + if (i == intervals[points[p-1].first]) + i = intervals[points[p-1].second]; + else + i = intervals[points[p-1].first]; + + if (p == i.first) + p = i.second; + else + p = i.first; + } + ++cycles; } return cycles; } @@ -70,7 +130,10 @@ std::vector findComponents(const GeneOrder& go){ return vector(); } -//TODO: Think of a better than O(n^2) implementation +/** + * 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){ @@ -100,3 +163,4 @@ std::vector findIntervals(const GeneOrder& go){ } return intervals; } +