X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=src%2Fgenealgorithms.cpp;h=3fb2aaac0c7f180bd73edde5918731c7bcc8c8e0;hb=HEAD;hp=78b0f0572dc283b1e7e8f7278303954ff77cddac;hpb=9299a79715f5b11b0ab26bff80ab7a610030b126;p=germs.git diff --git a/src/genealgorithms.cpp b/src/genealgorithms.cpp index 78b0f05..3fb2aaa 100644 --- a/src/genealgorithms.cpp +++ b/src/genealgorithms.cpp @@ -20,6 +20,7 @@ #include "genealgorithms.h" #include "geneorder.h" +#include "componenttree.h" #include #include @@ -66,8 +67,8 @@ struct FindP{ }; -int countCycles(const GeneOrder& go){ - int cycles = 0; +size_t countCycles(const GeneOrder& go){ + size_t cycles = 0; set marked; vector intervals = findIntervals(go); vector points = findIntervalsAtPoints(intervals); @@ -92,6 +93,23 @@ int countCycles(const GeneOrder& go){ return cycles; } +size_t inversionDistance(const GeneOrder& go){ + size_t distance = go.size() - 1; + distance -= countCycles(go); + + ComponentTree t(findComponents(go)); + t.makeUnoriented(); + size_t leaves = t.countLeaves(); + distance += leaves; + if (leaves % 2 != 0){ + size_t sb = t.shortBranches(); + if (sb == 0) + distance += 1; + } + + return distance; +} + int sign(Gene g){ if (g > 0) return 1; @@ -140,7 +158,7 @@ std::vector findComponents(const GeneOrder& go){ s = Sdir.top(); } if (go[i] > 0 && dir[i] == dir[s] && static_cast(i - s) == p[i] - p[s]) - components.push_back(Component(p[s],p[i],(s+1 == i ? 0 : os[s]))); + components.push_back(Component(p[s],p[i],(s+1 == i ? 0 : os[s]),s,i)); //Reverse if (p[i-1] < p[i]) @@ -156,7 +174,7 @@ std::vector findComponents(const GeneOrder& go){ s = Srev.top(); } if (go[i] < 0 && rev[i] == rev[s] && static_cast(i - s) == p[s] - p[i]) - components.push_back(Component(-p[s],-p[i],(s+1 == i ? 0 : os[s]))); + components.push_back(Component(-p[s],-p[i],(s+1 == i ? 0 : os[s]),s,i)); //Update stacks if (go[i] > 0) @@ -167,18 +185,40 @@ std::vector findComponents(const GeneOrder& go){ return components; } +int sign2(Gene g){ + if (g < 0) + return -1; + return 1; +} /** * */ std::vector findIntervals(const GeneOrder& go){ - vector intervals(go.size()-1,Interval(go.size(),go.size())); + const size_t max = go.size(); + vector intervals(go.size()-1,Interval(max,max,false)); size_t n = 0; - for (GeneOrder::iterator g = go.begin(); g != go.end(); ++g, ++n){ + const GeneOrder::iterator end = go.end(); + for (GeneOrder::iterator g = go.begin(); g != end; ++g, ++n){ size_t i = abs(*g); - if (i < go.size() - 1) - intervals[i].first = n + (*g >= 0 ? 1 : 0); - if (i > 0) - intervals[i-1].second = n + (*g < 0 ? 1 : 0); + if (i < max - 1){ + Interval& curr = intervals[i]; + curr.first = n + (*g >= 0 ? 1 : 0); + + if (curr.second == max) + curr.oriented = *g < 0; + else + curr.oriented ^= *g < 0; + } + if (i > 0){ + Interval& prev = intervals[i-1]; + prev.second = n + (*g < 0 ? 1 : 0); + + if (prev.first == max) + prev.oriented = *g < 0; + else + prev.oriented ^= *g < 0; + } + } return intervals; } @@ -188,7 +228,7 @@ std::vector findIntervals(const GeneOrder& go){ */ std::vector findIntervalsAtPoints(const vector& intervals){ size_t max = intervals.size()+1; - vector points(max,Interval(max,max)); + vector points(max,Interval(max,max,false)); size_t n = 0; for (vector::const_iterator i = intervals.begin(); i != intervals.end(); ++i, ++n){ if (points[i->first].first == max){