X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=src%2Fgenealgorithms.cpp;h=24e7b43575ac30a04c46723283a3ee81390a8592;hb=500fc81f4ccf04414ebbd495d167eb2d6b2ac82d;hp=154df354bc346592f53a1373b49bbc1c72a0b79d;hpb=74e3465418ae60633025efb581e399308ffdf4b1;p=germs.git diff --git a/src/genealgorithms.cpp b/src/genealgorithms.cpp index 154df35..24e7b43 100644 --- a/src/genealgorithms.cpp +++ b/src/genealgorithms.cpp @@ -22,31 +22,16 @@ #include "geneorder.h" #include +#include #include +#include using namespace std; std::pair longestSequences(const GeneOrder& go){ + vector > v = robinsonSchensted(go); + return pair(v[0].size(),v.size()); } - -/* -robSche2 :: [Int] -> [[Int]] -> [[Int]] -robSche2 [] ys = ys -robSche2 (x:xs) ys = robSche2 xs $ robSche4 x ys - -robSche3 :: Int -> [Int] -> (Maybe Int,[Int]) -robSche3 x ys = let yless = [y | y <- ys, y < x] in - let ymore = [y | y <- ys, y > x] in - case ymore of - [] -> (Nothing, yless++[x]) - (y:ys) -> (Just y, yless++(x:ys)) - -robSche4 :: Int -> [[Int]] -> [[Int]] -robSche4 x [] = [[x]] -robSche4 x (y:ys) = case robSche3 x y of - (Nothing, y) -> y:ys - (Just x, y) -> y:robSche4 x ys -*/ std::vector > robinsonSchensted(const GeneOrder& go){ vector > v; for (GeneOrder::iterator i = go.begin(); i != go.end(); ++i){ @@ -70,3 +55,114 @@ 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); + } +}; + + +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; +} + +/** + * TODO: Think of a better than O(n^2) implementation. + * Possibly move to cache result + */ +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; + +}