]> ruin.nu Git - germs.git/blobdiff - src/genealgorithms.cpp
calculate the proper distance, with hurdles
[germs.git] / src / genealgorithms.cpp
index f1e66aaa1cce5a2b9a1c3cf7b62c4c75351f4ee1..3fb2aaac0c7f180bd73edde5918731c7bcc8c8e0 100644 (file)
@@ -20,6 +20,7 @@
 
 #include "genealgorithms.h"
 #include "geneorder.h"
+#include "componenttree.h"
 
 #include <algorithm>
 #include <set>
@@ -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<Component> findComponents(const GeneOrder& go){
                        s = Sdir.top();
                }
                if (go[i] > 0 && dir[i] == dir[s] && static_cast<Gene>(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<Component> findComponents(const GeneOrder& go){
                        s = Srev.top();
                }
                if (go[i] < 0 && rev[i] == rev[s] && static_cast<Gene>(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)