Advertisement
arxeiss

Dijkstra algorithm

May 22nd, 2015
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. /*
  2. Get shortest paths from sourceSuperPixel to all others
  3. */
  4. vector<double> GeodesicSaliencyPropagation::shortestPaths(SuperPixel* sourceSuperPixel)
  5. {
  6.     unsigned superPixelsCount = this->superPixels.size();
  7.     vector<double> lengthOfPaths(superPixelsCount);
  8.  
  9.     //set to all superpixels "infinite" length and non processed flag
  10.     for (unsigned i = 0; i < superPixelsCount; i++){
  11.         this->superPixels[i]->setProcessedFlag(false);
  12.         lengthOfPaths[i] = DBL_MAX;
  13.     }
  14.     //set length 0 to source superpixel
  15.     lengthOfPaths[sourceSuperPixel->getGraphIndex()] = 0.0;
  16.     for (unsigned i = 0; i < superPixelsCount - 1; i++){
  17.        
  18.         //select min length of path on non processed superpixels
  19.         double minLength = DBL_MAX;
  20.         int minIndex;
  21.         for (unsigned m = 0; m < superPixelsCount; m++){
  22.             if (this->superPixels[m]->isProcessed() == false && lengthOfPaths[m] < minLength){
  23.                 minLength = lengthOfPaths[m];
  24.                 minIndex = m;
  25.             }
  26.         }
  27.  
  28.         //Select closest superpixel, set as processed and select all paths to others superpixels
  29.         SuperPixel *current = this->superPixels[minIndex];
  30.         current->setProcessedFlag(true);
  31.         vector<SPPath*> paths = current->getPaths();
  32.  
  33.         //Travel through all paths to non processed superpixels
  34.         for (unsigned p = 0; p < paths.size(); p++){
  35.             SuperPixel *dest = paths[p]->nextSuperPixel(current);
  36.             if (dest->isProcessed() == false){
  37.                 int destIndex = dest->getGraphIndex();
  38.                 double deltaPk = paths[p]->getLength();
  39.  
  40.                 double lengthToDest = lengthOfPaths[minIndex] + deltaPk;
  41.                 //save closest path
  42.                 if (lengthToDest < lengthOfPaths[destIndex]){
  43.                     lengthOfPaths[destIndex] = lengthToDest;
  44.                 }
  45.             }
  46.         }
  47.     }
  48.     return lengthOfPaths;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement