0
while(!adjacents.isEmpty())
{
   if(! adjacents.front()->visited ) //visited is a bool to see if it's been visited or not
   {
      if( (tentdist + abs(adjacents.front()->val - val)) < adjacents.front()->tentdist)
      {
         adjacents.front()->tentdist = tentdist + abs(adjacents.front()->val - val);
         adjacents.front()->prev = this; //prev is a pointer to another vertex
      }
   }
}

Hi guys, I was wondering whether if my section for neighbor checking is correct. I think that it might be off somewhere, because when I reach to certain desired vertices, the prev pointer is incorrect when I loop through it.

adjacents is a queue full of pointers to neighboring vertices. All vertices have:

int val - value containing node value. Weight between vertices A and B, for example, would be the absolute value of B.val minus A.val

bool visited - a boolean to see whether if the neighboring node has been visited or not

Vertex * prev - a pointer to another vertices, adjusted when a shorter tentative distance has been discovered

int tentdist - tentative distance. The starting vertex is initialized to zero, while all the others in the graph to INT_MAX.

I was thinking that the comparison might be a little off? For one iteration of this, (this section of the algorithm is looped by every current vertex) my tentdist suddenly got set to INT_MAX. At another instance, it stopped the prev linked from the start node to the finish node, so when I looped through the prev starting from the desired node, it suddenly stopped (as it was set to NULL). Is there anything wrong with this?

WhiZTiM
  • 21,207
  • 4
  • 43
  • 68

0 Answers0