I'm trying to Implement Dijkstras pathfinding algorithm using a priorty_queue. However, I am running into a strange error when attempting pop a specific node from the queue. The error received is:
Debugg Assertion Failed!
program: ...algorithm
Expression: invalid heap
Here is my code, There's probably more than you need. The program crashes when I try to pop my endNode 'e':
Main (Initializing Graph, Nodes and Edges)
DijkstraGraph::Node* a = new DijkstraGraph::Node('a');
DijkstraGraph::Node* b = new DijkstraGraph::Node('b');
DijkstraGraph::Node* c = new DijkstraGraph::Node('c');
DijkstraGraph::Node* d = new DijkstraGraph::Node('d');
DijkstraGraph::Node* e = new DijkstraGraph::Node('e');
DijkstraGraph graph;
graph.AddNode(a);
graph.AddNode(b);
graph.AddNode(c);
graph.AddNode(d);
graph.AddNode(e);
// Third parameter specifies the weight of the edge
graph.AddEdge(a, b, 3);
graph.AddEdge(a, d, 2);
graph.AddEdge(a, c, 5);
graph.AddEdge(b, e, 5);
graph.AddEdge(d, e, 3);
graph.AddEdge(d, c, 5);
graph.AddEdge(c, e, 3);
Node struct
struct Node
{
Vector2 pos;
bool inQueue = false;
char nodeChar;
float gScore;
Node* parent;
std::vector<Edge> connections;
Node() : gScore(INFINITY), parent(nullptr) {}
Node(Vector2 a_pos, Node* a_parent) :
pos(a_pos), gScore(INFINITY), parent(a_parent), nodeChar('') {}
Node(Vector2 a_pos) :
pos(a_pos), gScore(INFINITY), parent(nullptr), nodeChar('') {}
Node(char nodeChar) :
pos(Vector2(0,0)), gScore(INFINITY), parent(nullptr), nodeChar(nodeChar){}
};
Dijkstras PathFinding Algorithm Function
void DijkstraGraph::FindPath(Node * startNode, std::list<Node*> &endNodes, std::list<Node*>& outPath)
{
int currentStep = 0;
std::priority_queue < Node*, std::vector<Node*>, cmp> queue ;
std::list<Node*> traversedList;
startNode->gScore = 0;
startNode->parent = nullptr;
Node* endNode = nullptr;
Node* currentNode = nullptr;
queue.push(startNode);
while (queue.empty() == false)
{
currentNode = queue.top();
queue.pop();
for each (Edge edge in currentNode->connections)
{
bool isTraversed = std::find(traversedList.begin(), traversedList.end(), edge.connection) != traversedList.end();
float gScore = 0;
if (!isTraversed)
{
gScore += currentNode->gScore + edge.weight;
edge.connection->gScore = gScore;
edge.connection->parent = currentNode;
if (!edge.connection->inQueue)
{
queue.push(edge.connection);
edge.connection->inQueue = true;
}
traversedList.push_back(currentNode);
}
else
{
if (gScore < edge.connection->gScore)
{
edge.connection->gScore = gScore;
edge.connection->parent = currentNode;
}
}
}
}
currentNode = endNodes.front();
while (currentNode != nullptr)
{
outPath.push_back(currentNode);
currentNode = currentNode->parent;
}
Aucun commentaire:
Enregistrer un commentaire