简体   繁体   English

Dijkstra的算法-邻接矩阵和列表

[英]Dijkstra's algorithm - adjacency matrix and list

I've got strange problem with my Dijkstra's implementation... I have 2 algorithms, one for adjacency matrix and the second one for adjacency list. 我的Dijkstra的实现遇到了一个奇怪的问题……我有2种算法,一种算法用于邻接矩阵,第二种算法用于邻接表。 They are almost identical and only difference is with passing numbers from these structures. 它们几乎是相同的,唯一的区别是这些结构传递的数字。

I keep numbers from matrix in simple two-dimensional matrix called weightmat. 我将矩阵中的数字保留在称为weightmat的简单二维矩阵中。 Numbers from list are kept in array of lists called nbhlist. 列表中的数字保存在称为nbhlist的列表数组中。 Lists are composed from structs called ListNode. 列表由称为ListNode的结构组成。

struct ListNode{    
        int number;             
        int weight;         
        ListNode* next;             
        ListNode(){                         
            number = weight = 0;
            next = 0;
        }
    };

And few general variables: vertex (number of vertices), edge (number of edges), vstart (number of start vertex). 很少有一般变量:顶点(顶点数),边(边数),vstart(起始顶点数)。

Now code of Dijkstra's algorithm for matrix: 现在,Dijkstra的矩阵算法代码:

typedef vector<vector<pair<int, float> > > Graph;
struct Compare{
    int operator() (const pair<int,float>& p1, const pair<int,float>& p2)
    {
        return p1.second > p2.second;
    }
};

vector<float> d(vertex);
vector<int> parent(vertex);

for (int i = 0; i < vertex; i++){
    d[i] = numeric_limits<float>::max();
    parent[i] = -1;
}

priority_queue<pair<int, float>, vector<pair<int, float> >, Compare> MatQueue;

d[vstart] = 0;
MatQueue.push(make_pair(vstart, d[vstart]));
while (!MatQueue.empty()){
    int u = MatQueue.top().first;
    if (u == vertex - 1) break;
    MatQueue.pop();
    for (int i = 0; i < vertex; i++){
        if (weightmat[u][i] != 0){
            int v = i;
            float w = weightmat[u][i];
            //cout << "U " << u << "Number " << i << " Weight " << weightmat[u][i] << endl;
            if (d[v]> d[u] + w){
                d[v] = d[u] + w;
                parent[v] = u;
                MatQueue.push(make_pair(v, d[v]));
            }
        }
    }
}

vector<int> path;
path.clear();
int p = vertex - 1;
path.push_back(vertex - 1);
while (p != vstart)
{
    p = parent[p];
    path.push_back(p);
}
for (int i = path.size()-1; i >=0; i--){
    cout << path[i] << "->";
}

And this is code of Dijkstra's algorithm for my lists: 这是Dijkstra的算法代码列表:

typedef vector<vector<pair<int, float> > > Graph;

    struct Compare{
        int operator() (const pair<int, float>& p1, const pair<int, float>& p2)
        {
            return p1.second > p2.second;
        }
    };

    vector<float> d(vertex);
    vector<int> parent(vertex);

    for (int i = 0; i < vertex; i++){
        d[i] = numeric_limits<float>::max();
        parent[i] = -1;
    }

    priority_queue<pair<int, float>, vector<pair<int, float> >, Compare> MatQueue;

    d[vstart] = 0;
    MatQueue.push(make_pair(vstart, d[vstart]));

    ListNode* hand = new ListNode;

    while (!MatQueue.empty()){
        int u = MatQueue.top().first;
        if (u == vertex - 1) break;
        MatQueue.pop();
        hand = NbhList[u];
        while (hand){
            int v = hand->number;
            float w = hand->weight;
            //cout << "U " << u << "Number " << v << " Weight " << w << endl;
            hand = hand->next;
            if (d[v] > d[u] + w){
                d[v] = d[u] + w;
                parent[v] = u;
                MatQueue.push(make_pair(v, d[v]));
            }
        }
    }


    vector<int> path;
    path.clear();
    int p = (vertex-1);
    path.push_back(p);
    while (p != vstart)
    {
        p = parent[p];
        path.push_back(p);
    }
    cout << endl << endl;
    for (int i = path.size() - 1; i >= 0; i--){
        cout << path[i] << "->";
    }

As I said, they are almost identical. 正如我所说,它们几乎是相同的。 Only difference: 唯一的区别:

MatQueue.pop();
        hand = NbhList[u];
        while (hand){
            int v = hand->number;
            float w = hand->weight;
            //cout << "U " << u << "Number " << v << " Weight " << w << endl;
            hand = hand->next;
            if (d[v] > d[u] + w){
                d[v] = d[u] + w;
                parent[v] = u;
                MatQueue.push(make_pair(v, d[v]));
            }
        }

And: 和:

MatQueue.pop();
        for (int i = 0; i < vertex; i++){
            if (weightmat[u][i] != 0){
                int v = i;
                float w = weightmat[u][i];
                //cout << "U " << u << "Number " << i << " Weight " << weightmat[u][i] << endl;
                if (d[v]> d[u] + w){
                    d[v] = d[u] + w;
                    parent[v] = u;
                    MatQueue.push(make_pair(v, d[v]));
                }
            }
        }

My problem is - they give me sometimes different outputs and I have no idea why. 我的问题是-它们有时给我不同的输出,我不知道为什么。 Could you help me find my problem? 你能帮我找到我的问题吗?

One possible bug is that in your ListNode struct: 一个可能的错误是在ListNode结构中:

struct ListNode{    
        int number;             
        int weight;         
        ListNode* next;             
        ListNode(){                         
            number = weight = 0;
            next = 0;
        }
    };

weight is an int , but your weights are float s according to the rest of your code, which might result in unwanted truncation. weight是一个int ,但根据代码的其余部分,权重为float ,这可能会导致不必要的截断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM