From 65f449adad91a229757c0317c27ad9fb87a4d222 Mon Sep 17 00:00:00 2001 From: Michael Andreen Date: Tue, 19 Jun 2007 10:30:11 +0000 Subject: [PATCH] findIntervalAtPoints implemnted and passes test --- src/genealgorithms.cpp | 68 ++++++++++++++++++++++++++++++++- src/genealgorithms.h | 5 +++ src/test/genealgorithmstest.cpp | 30 ++++++++++++--- 3 files changed, 96 insertions(+), 7 deletions(-) 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; } + diff --git a/src/genealgorithms.h b/src/genealgorithms.h index 4c938e3..b0e75b9 100644 --- a/src/genealgorithms.h +++ b/src/genealgorithms.h @@ -58,5 +58,10 @@ std::vector findComponents(const GeneOrder& go); */ std::vector findIntervals(const GeneOrder& go); +/** + * Creates a list with the intervals at each point. + */ +std::vector findIntervalsAtPoints(const std::vector& intervals); + #endif diff --git a/src/test/genealgorithmstest.cpp b/src/test/genealgorithmstest.cpp index 2b9ae79..0fca4d2 100644 --- a/src/test/genealgorithmstest.cpp +++ b/src/test/genealgorithmstest.cpp @@ -23,7 +23,8 @@ class TESTNAME : public CPPUNIT_NS::TestFixture CPPUNIT_TEST( testRobinsonSchensted ); CPPUNIT_TEST( testLongestSequences ); CPPUNIT_TEST( testFindIntervals ); - CPPUNIT_TEST( testCountCycles ); + CPPUNIT_TEST( testFindIntervalsAtPoints ); + //CPPUNIT_TEST( testCountCycles ); CPPUNIT_TEST_SUITE_END(); protected: @@ -88,11 +89,30 @@ protected: CPPUNIT_ASSERT(go10 == v[0]); CPPUNIT_ASSERT(go12 == v[2]); - GeneOrder go2(_validPerm2.begin(),_validPerm2.end()); + GeneOrder go2(_validPerm3.begin(),_validPerm3.end()); + v = findIntervals(go2); + CPPUNIT_ASSERT_EQUAL(16ul,v.size()); + Interval go20(1,2); + Interval go22(4,2); + CPPUNIT_ASSERT(go20 == v[0]); + CPPUNIT_ASSERT(go22 == v[2]); + } + void testFindIntervalsAtPoints (){ + GeneOrder go(_validPerm.begin(),_validPerm.end()); + vector v = findIntervals(go); + v = findIntervalsAtPoints(v); + CPPUNIT_ASSERT_EQUAL(4ul,v.size()); + Interval go10(0,0); + Interval go12(2,2); + CPPUNIT_ASSERT(go10 == v[0]); + CPPUNIT_ASSERT(go12 == v[2]); + + GeneOrder go2(_validPerm3.begin(),_validPerm3.end()); v = findIntervals(go2); - CPPUNIT_ASSERT_EQUAL(9ul,v.size()); - Interval go20(1,3); - Interval go22(1,4); + v = findIntervalsAtPoints(v); + CPPUNIT_ASSERT_EQUAL(16ul,v.size()); + Interval go20(0,3); + Interval go22(1,1); CPPUNIT_ASSERT(go20 == v[0]); CPPUNIT_ASSERT(go22 == v[2]); } -- 2.39.2