gpt4 book ai didi

c++ - Dijkstra 算法 - 邻接矩阵和列表

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:57:59 35 4
gpt4 key购买 nike

我的 Dijkstra 实现有一个奇怪的问题...我有 2 种算法,一种用于邻接矩阵,另一种用于邻接列表。它们几乎相同,唯一的区别在于这些结构中传递的数字。

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

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

还有几个通用变量:vertex(顶点数)、edge(边数)、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]));
}
}
}

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

最佳答案

一个可能的错误是在您的 ListNode 结构中:

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

weight 是一个 int,但根据您的其余代码,您的权重是 float,这可能会导致不需要的截断。

关于c++ - Dijkstra 算法 - 邻接矩阵和列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30168063/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com