Original Post
Hello,
I'm developing my pathfinding algorithm, using Dijstra's single-source shortest distance algorithm, for a simple Pac-Man like game. However, I'm seeing some terrible perfomance drops using a STL based priority queue, primarily the make_heap operation. Here is my code so far. I know its not pretty, but I would appreciate any feedback.
// the current Tile being visited by the algorithm
Tile *currentTile = NULL;
// initialize all of the Tile pointers for the algorithm and add them to V
for (int panel = 0; panel < NUMBER_OF_PANELS; panel++)
{
for (int column = 0; column < NUMBER_OF_COLUMNS; column++)
{
for (int row = 0; row < NUMBER_OF_ROWS; row++)
{
Tile *node = getTileAt(panel, column, row);
node->parent = NULL;
if (node->equals(inSourceTile))
{
node->distanceCost = 0;
}
else
{
node->distanceCost = INT_MAX;
}
V.push_back(node);
}
}
}
// make a heap out of V
make_heap(V.begin(), V.end(), cmp());
// run dijksta's algorithm until we have found the destination Tile
while (currentTile != inDestinationTile)
{
// get the smallest Tile pointer from the front of the heap
currentTile = V.front();
// Relax all of the current Tile's valid neighbors
Tile *tileUp, *tileDown, *tileLeft, *tileRight;
if (currentTile->getUp())
{
tileUp = getTileAt (currentTile->getUp());
relaxEdge (currentTile, tileUp);
}
if (currentTile->getDown())
{
tileDown = getTileAt (currentTile->getDown());
relaxEdge (currentTile, tileDown);
}
if (currentTile->getLeft())
{
tileLeft = getTileAt (currentTile->getLeft());
relaxEdge (currentTile, tileLeft);
}
if (currentTile->getRight())
{
tileRight = getTileAt (currentTile->getRight());
relaxEdge (currentTile, tileRight);
}
// remove the Tile pointer from the front of the head
vector<Tile *>::iterator startIterator;
startIterator = V.begin();
V.erase( startIterator );
// heapify V
make_heap(V.begin(), V.end(), cmp());
}
{Ed. Sorry for the crappy alignment.)
[edited by - ryansobol on May 28, 2004 9:45:42 PM]