X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=src%2Fgenealgorithms.cpp;h=3fb2aaac0c7f180bd73edde5918731c7bcc8c8e0;hb=c997c8f4931cde3ede1b51905c10c4d9c442e630;hp=f1e66aaa1cce5a2b9a1c3cf7b62c4c75351f4ee1;hpb=4b63e18f4e100d3506ed170d4798539b08d19bc0;p=germs.git diff --git a/src/genealgorithms.cpp b/src/genealgorithms.cpp index f1e66aa..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 @@ -93,9 +94,20 @@ size_t countCycles(const GeneOrder& go){ } size_t inversionDistance(const GeneOrder& go){ - size_t cycles = countCycles(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 go.size() - 1 - cycles; + return distance; } int sign(Gene g){ @@ -146,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]) @@ -162,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)