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

时间:2015-05-11 12:47:00

标签: c++ algorithm dijkstra adjacency-list adjacency-matrix

我的Dijkstra实现出现了奇怪的问题......我有2个算法,一个用于邻接矩阵,第二个用于邻接列表。它们几乎相同,唯一的区别在于来自这些结构的数字。

我在简单的二维矩阵(称为weightmat)中保留矩阵中的数字。 列表中的数字保存在名为nbhlist的列表数组中。 列表由名为ListNode的结构组成。

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

几个常规变量:顶点(顶点数),边(边数),vstart(起始顶点数)。

现在代码为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] << "->";
}

这是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] << "->";
    }

正如我所说,它们几乎相同。唯一不同的是:

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]));
            }
        }

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]));
                }
            }
        }

我的问题是 - 他们有时给我不同的输出,我不知道为什么。 你能帮我找到问题吗?

1 个答案:

答案 0 :(得分:2)

一个可能的错误是你的ListNode结构:

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

weightint,但根据您的代码的其余部分,您的权重为float,这可能会导致不必要的截断。