我的Dijkstra算法没有选择短路径

时间:2017-05-05 19:33:49

标签: c++ algorithm queue graph-algorithm dijkstra

我尝试用自定义图形的两个自定义顶点结构及其边缘编码Dijkstra算法,算法编译但是选择最佳距离时做出错误的选择。代码如下。

class Graph {
struct Edge;
struct Vertex {
    std::string _data;  //name of vertex    
    std::list<Edge*> _ins;
    std::list<Edge*> _outs;
    bool visited;
    int BestDist;//best distance from source to sink

};
struct Edge {
    int _weight;
    Vertex *_from;
    Vertex *_to;
    bool travelled; //if the edge has been travalled
};
//Graph of String as key to Values as vertexes
std::unordered_map<std::string, Vertex *> _mainData; 

//Dijkstra Algo
int BestDistance(std::string source, std::string sink) {

    //to save vertexes names
    std::queue<std::string> q;

    //for each vertex set dist and path to infinity
    for (auto startItr : _mainData)
    {
        startItr.second->BestDist = Max;
        startItr.second->visited = false;
        for (auto kachal : startItr.second->_outs)
        {
            kachal->travelled = false;
        }
    }

    //set source distacne to 0 sicen it is visiting itself
    _mainData[source]->BestDist = 0;
    q.push(source);

    //while there is unknown distance vertex and we havent reach sink yet
    while (!q.empty() )
    {
        //smallest unknow distance vertex
        std::string currentVer = q.front(); q.pop();
        //set that vertex to visited
        _mainData[currentVer]->visited = true;

        //for each vertex adj to current vertex
        for (auto adjVer : _mainData[currentVer]->_outs) {
            //if that vertex is not visted
            if (!adjVer->travelled) {
                int cvw = adjVer->_weight; //cost of edge from cuurent vertex to adj vertex

                //if current vert.distance +cvw < adj vertex distance
                if (_mainData[currentVer]->BestDist + cvw < _mainData[adjVer->_to->_data]->BestDist) {
                    //update adj vertex
                    q.push(adjVer->_to->_data);
                    //deacrease adj vertex distacne  to current distacne + cvw
                    _mainData[adjVer->_to->_data]->BestDist = _mainData[currentVer]->BestDist + cvw;
                    //marked the travlled edge true
                    adjVer->travelled = true;
                }
            }
        }
    }
    return _mainData[sink]->BestDist;
}

这是我的主要内容:

 #include "stdafx.h"
 #include "Graph.h"
 #include <iostream>

int main()
 {
 Graph myGraph;
 myGraph.Add("A");
 myGraph.Add("B");
 myGraph.Add("C");
 myGraph.Add("D");
 myGraph.Add("E");
 myGraph.Add("F");
 myGraph.Add("G");

 myGraph.Connect("A", "B",20);
 myGraph.Connect("A", "C",30);
 myGraph.Connect("B", "D",200);
 myGraph.Connect("C", "F",100);
 myGraph.Connect("C", "G",200);
 myGraph.Connect("D", "E",50);
 myGraph.Connect("E", "F",1);
 myGraph.Connect("F", "G",30);
 std::cout << "best distacne example : " << myGraph.BestDistance("A", "G");

因此,当我运行代码时,从A到G的最佳距离将返回为160(A-> C-> F-> G),但代码返回280,即(A-> C) - &GT; G)。我可以提供我的添加和连接功能,但我确信它们正常工作。

1 个答案:

答案 0 :(得分:0)

所以在分析了上述算法之后,我意识到我的错误是将每个边缘标记为是否行进,并根据这个做出决定。我应该做的是标记每个顶点是否被访问。其余的都没问题。所以这是我的Dijkstra算法实现的正确版本,在正加权图中找到最短路径。希望它有所帮助

//Dijkstra Algo
int BestDistance(std::string source, std::string sink) {

    //to save vertexes names
    std::queue<std::string> q;

    //for each vertex set dist and path to infinity
    for (auto startItr : _mainData)
    {
        startItr.second->BestDist = Max;
        startItr.second->visited = false;
    }

    //set source distacne to 0 sicen it is visiting itself
    _mainData[source]->BestDist = 0;

    q.push(source);

    //while there is unknown distance vertex and we havent reach sink yet
    while (!q.empty() )
    {
        //smallest unknow distance vertex
        std::string currentVer = q.front(); q.pop();

        //set that vertex to visited
        _mainData[currentVer]->visited = true;

        //for each vertex adj to current vertex
        for (auto adjVer : _mainData[currentVer]->_outs) {
            //if that vertex is not visted
            if (!adjVer->_to->visited) {
                int cvw = adjVer->_weight; //cost of edge from cuurent 
                                           //vertex to adj vertex

                //if current vert.distance +cvw < adj vertex distance
                if (_mainData[currentVer]->BestDist + cvw < 
                   _mainData[adjVer->_to->_data]->BestDist) {

                    q.push(adjVer->_to->_data);

                    //deacrease adj vertex distacne  to current distacne + cvw
                    _mainData[adjVer->_to->_data]->BestDist = _mainData[currentVer]->BestDist + cvw;
                    //setting the path of adj vertext to his previous one
                    _mainData[adjVer->_to->_data]->path = _mainData[currentVer]; 
                }
            }
        }
    }
    return _mainData[sink]->BestDist;
}