vector<Node*> DijkstraSearch::Search(const Graph& graph, Node* _pStart, Node* _pFinish) { m_distances.insert(pair<Node*, int>(_pStart, 0)); vector<Node*> nodes = graph.GetAll(); while (!nodes.empty()) { Node* pNode = GetClosestNode(nodes); nodes.erase(remove(nodes.begin(), nodes.end(), pNode)); if (pNode == _pFinish) { break; } for (unsigned int uiIndex = 0; uiIndex < pNode->GetOutgoingEdges().size(); uiIndex++) { Edge* pOutgoingEdge = pNode->GetOutgoingEdges().at(uiIndex); Node* pDestination = pOutgoingEdge->GetDestination(); if (find(nodes.begin(), nodes.end(), pDestination) == nodes.end()) { continue; } int iDistance = m_distances.find(pNode)->second + pOutgoingEdge->GetWeight(); if (m_distances.find(pDestination) == m_distances.end()) { m_distances.insert(pair<Node*, int>(pDestination, iDistance)); m_previousNodes.insert(pair<Node*, Node*>(pDestination, pNode)); } else if (iDistance < m_distances.find(pDestination)->second) { m_distances.find(pDestination)->second = iDistance; m_previousNodes.find(pDestination)->second = pNode; } } } vector<Node*> shortestPath; shortestPath.push_back(_pFinish); Node* pNode = _pFinish; while (pNode != _pStart) { pNode = m_previousNodes.find(pNode)->second; shortestPath.push_back(pNode); } reverse(shortestPath.begin(), shortestPath.end()); return shortestPath; }
void AStar::Execute(const Graph &Graph, const string &VetexId) { const auto& Vertexes = Graph.GetVertexes(); Vertex* pVertexStart = Vertexes.find(VetexId)->second; vector< Vertex* > Q; // 初始化顶点 for (auto& it : Vertexes) { Vertex* pV = it.second; pV->PathfindingData.Cost = 0; pV->PathfindingData.pParent = nullptr; pV->PathfindingData.Heuristic = 0x0FFFFFFF; pV->PathfindingData.Flag = false; } // 初始化起始顶点 pVertexStart->PathfindingData.pParent = 0; pVertexStart->PathfindingData.Cost = 0; pVertexStart->PathfindingData.Heuristic = Estimate(pVertexStart, m_pVTarget); // 把起始顶点放入列表中 Q.push_back(pVertexStart); pVertexStart->PathfindingData.Flag = true; for (; Q.size() > 0;) { // 选出最小路径估计的顶点 auto v = ExtractMin(Q); v->PathfindingData.Flag = false; if (v == m_pVTarget) { return; } // 对所有的出边进行“松弛” const auto& EO = v->GetEdgesOut(); for (auto& it : EO) { Edge* pEdge = it.second; Vertex* pVEnd = pEdge->GetEndVertex(); bool bRet = Relax(v, pVEnd, pEdge->GetWeight()); // 如果松弛成功,加入列表中 if (bRet && pVEnd->PathfindingData.Flag == false) { Q.push_back(pVEnd); pVEnd->PathfindingData.Flag = true; } } } }
void Dijkstra::Execute(const Graph& graph, const string& vetexId) { m_Ret.PathTree.clear(); const auto &Vertexes = graph.GetVertexes(); Vertex* pVertexStart = Vertexes.find(vetexId)->second; vector< Vertex* > Q; //// 初始化 //for (auto& it : Vertexes) //{ // it.second->PathfindingData.Cost = 0x0FFFFFFF; // Q.push_back(it.second); // pVertexStart->PathfindingData.pParent = nullptr; //} ////m_Ret.PathTree[pVertexStart] = 0; // 起始顶点的前驱顶点为空 //pVertexStart->PathfindingData.Cost = 0; //for (; Q.size() > 0;) //{ // // 选出最小路径估计的顶点 // auto v = ExtractMin(Q); // // 对所有的出边进行“松弛” // const auto& EO = v->GetEdgesOut(); // for (auto& it : EO) // { // Edge* pEdge = it.second; // Relax(v, pEdge->GetEndVertex(), pEdge->GetWeight()); // } //} // 初始化 for (auto& it : Vertexes) { it.second->PathfindingData.Cost = 0x0FFFFFFF; pVertexStart->PathfindingData.pParent = nullptr; } // 初始化起始顶点 //m_Ret.PathTree[pVertexStart] = 0; // 起始顶点的前驱顶点为空 pVertexStart->PathfindingData.Cost = 0; pVertexStart->PathfindingData.pParent = nullptr; // 把起始顶点放入列表中 Q.push_back(pVertexStart); pVertexStart->PathfindingData.Flag = true; for (; Q.size() > 0;) { // 选出最小路径估计的顶点 auto v = ExtractMin(Q); v->PathfindingData.Flag = false; // 对所有的出边进行“松弛” const auto& EO = v->GetEdgesOut(); for (auto& it : EO) { Edge* pEdge = it.second; Vertex* pVEnd = pEdge->GetEndVertex(); bool bRel = Relax(v, pVEnd, pEdge->GetWeight()); if (bRel && pVEnd->PathfindingData.Flag == false) { Q.push_back(pVEnd); pVEnd->PathfindingData.Flag = true; } } } }