c++ - Dijkstra algorithm with Adjacency list Undirected Graph -


i'm trying write method find shortest path length 1 vertex another, using dijkstra algorithm. doesn't give correct output. give advice problem is?

unsigned distdijkstra(unsigned vert1, unsigned vert2)     {         vector<vertex3*> pq;         vertex3* v;         unsigned distance = uint_max;         (unsigned = 0; < vertices.size(); i++)         {             if (vertices[i]->value == vert1)                 v = new vertex3(vertices[i], 0);             else                  v = new vertex3(vertices[i], distance);             pq.push_back(v);         }          make_heap(pq.begin(), pq.end(), lowerprior);         unsigned newdist;         while (!pq.empty())         {             vertex3* currv = pq.front();              // if destination reached             if (currv->vert->value == vert2)                 return currv->dist;              pop_heap(pq.begin(), pq.end(), lowerprior);             pq.pop_back();              // checking if route through currv shorter             (unsigned = 0; < currv->vert->adjlist.size(); i++)             {                 vertex3* nextv = nullptr;                 (unsigned j = 0; j < pq.size(); j++)                     if (pq[j]->vert == currv->vert->adjlist[i]) {                         nextv = pq[j];                         break;                     }                 if (nextv != nullptr)                 {                     newdist = currv->dist + getweight(currv->vert, nextv->vert);                     if (newdist < nextv->dist)                         nextv->dist = newdist;                 }                 else                     continue;             }             sort_heap(pq.begin(), pq.end(), lowerprior);             }         return uint_max;     } 

after updating distances, vector won't heap anymore. further more, calling sort_heap in drop heap property, instead of fixing it. (see http://en.cppreference.com/w/cpp/algorithm/sort_heap) call make_heap instead.

also note memory leak: never deallocate memory allocated vertex3 objects.


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -