Skip to content

Commit 686685b

Browse files
committed
this preserving direction dijkstra passes the test
1 parent bfd348b commit 686685b

File tree

1 file changed

+44
-27
lines changed

1 file changed

+44
-27
lines changed

src/subgraph.cpp

+44-27
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ std::pair<bool,ParallelMap32color> DBG::dijkstra(std::pair<uint64_t,DBGkmer32col
462462
std::vector<uint64_t> destinations;
463463
FibonacciHeap<std::pair<const uint64_t, DBGkmer32>*> Q; // node priority queue Q
464464
phmap::parallel_flat_hash_map<uint64_t,uint8_t> dist;
465-
phmap::parallel_flat_hash_map<uint64_t,uint64_t> prev; // distance table
465+
phmap::parallel_flat_hash_map<uint64_t,std::pair<uint64_t,bool>> prev; // distance table
466466
ParallelMap32color discoveredNodes;
467467

468468
dist[source.first] = 1;
@@ -471,16 +471,20 @@ std::pair<bool,ParallelMap32color> DBG::dijkstra(std::pair<uint64_t,DBGkmer32col
471471

472472
uint64_t key;
473473
int16_t depth = 0;
474+
bool direction = true, isFw;
474475

475476
while (Q.size() > 0 && depth < userInput.kmerDepth + 1) { // The main loop
476477
explored = false; // if there are still node in the queue we cannot be done
477478
ParallelMap *map;
478479
// ParallelMap32 *map32;
479480

480-
bool isFw = false;
481481
std::pair<const uint64_t, DBGkmer32>* u = Q.extractMin(); // Remove and return best vertex
482-
483-
auto checkNext = [&,this] (uint64_t key) {
482+
auto got = prev.find(u->first); // check direction
483+
if (got != prev.end()) {
484+
direction = got->second.second;
485+
}
486+
487+
auto checkNext = [&,this] (uint64_t key, bool direction) {
484488
auto startNode = DBGsubgraph->find(key);
485489
if (startNode == DBGsubgraph->end()) { // if we connect to the original graph we are done
486490
auto nextKmer = graphCache->find(key); // check if the node is in the cache (already visited)
@@ -504,38 +508,51 @@ std::pair<bool,ParallelMap32color> DBG::dijkstra(std::pair<uint64_t,DBGkmer32col
504508
Q.insert(&*nextKmer, 0);
505509
}
506510
if (alt < dist[nextKmer->first]) {
507-
prev[nextKmer->first] = u->first;
511+
prev[nextKmer->first] = std::make_pair(u->first,direction);
508512
dist[nextKmer->first] = alt;
509513
Q.decreaseKey(&*nextKmer, alt);
510514
}
511515
}
512516
return true;
513517
};
514518
uint8_t edgeCount = 0, exploredCount = 0;
515-
for (uint8_t i = 0; i<4; ++i) { // forward edges
516-
if (u->second.fw[i] > userInput.covCutOff) {
517-
uint8_t nextKmer[k];
518-
buildNextKmer(nextKmer, u->first, i, true); // compute next node
519-
key = hash(nextKmer, &isFw);
520-
bool found = checkNext(key);
521-
if (found) {
522-
++exploredCount;
523-
if (key != source.first && DBGsubgraph->find(key) != DBGsubgraph->end())
524-
destinations.push_back(u->first);
519+
for (uint8_t i = 0; i<4; ++i) {
520+
521+
if (direction || depth == 0) { // forward edges
522+
523+
if (depth == 0)
524+
direction = true;
525+
526+
if (u->second.fw[i] > userInput.covCutOff) {
527+
uint8_t nextKmer[k];
528+
buildNextKmer(nextKmer, u->first, i, true); // compute next node
529+
key = hash(nextKmer, &isFw);
530+
bool found = checkNext(key, isFw ? direction : !direction);
531+
if (found) {
532+
++exploredCount;
533+
if (key != source.first && DBGsubgraph->find(key) != DBGsubgraph->end())
534+
destinations.push_back(u->first);
535+
}
536+
++edgeCount;
525537
}
526-
++edgeCount;
527538
}
528-
if (u->second.bw[i] > userInput.covCutOff) { // backward edges
529-
uint8_t nextKmer[k];
530-
buildNextKmer(nextKmer, u->first, i, false); // compute next node
531-
key = hash(nextKmer, &isFw);
532-
bool found = checkNext(key);
533-
if (found) {
534-
++exploredCount;
535-
if (key != source.first && DBGsubgraph->find(key) != DBGsubgraph->end())
536-
destinations.push_back(u->first);
539+
if (!direction || depth == 0) {
540+
541+
if (depth == 0)
542+
direction = false;
543+
544+
if (u->second.bw[i] > userInput.covCutOff) { // backward edges
545+
uint8_t nextKmer[k];
546+
buildNextKmer(nextKmer, u->first, i, false); // compute next node
547+
key = hash(nextKmer, &isFw);
548+
bool found = checkNext(key, isFw ? direction : !direction);
549+
if (found) {
550+
++exploredCount;
551+
if (key != source.first && DBGsubgraph->find(key) != DBGsubgraph->end())
552+
destinations.push_back(u->first);
553+
}
554+
++edgeCount;
537555
}
538-
++edgeCount;
539556
}
540557
}
541558
depth += 1;
@@ -547,7 +564,7 @@ std::pair<bool,ParallelMap32color> DBG::dijkstra(std::pair<uint64_t,DBGkmer32col
547564
while (destination != source.first) { // construct the shortest path with a stack S
548565
discoveredNodes.insert(*graphCache->find(destination)); // push the vertex onto the stack
549566
dist.erase(destination);
550-
destination = prev[destination];
567+
destination = prev[destination].first;
551568
}
552569
}
553570
}else{

0 commit comments

Comments
 (0)