简体   繁体   English

Dijkstra算法无法正确输出

[英]Dijkstra Algorithm not outputting correctly

I am having trouble with implementing Dijkstra algorithm. 我在实现Dijkstra算法时遇到麻烦。

I have the following variables initialized : 我初始化了以下变量:

enum GraphLimit300 {MAX_NODES = 50};
typedef unsigned int Element300;
Element300 adjacencyMatrix[MAX_NODES][MAX_NODES];
Element300 distanceArray[MAX_NODES];
bool visitedSet[MAX_NODES];
int numberNodes;

Here is my current implementation of the algorithm: int startNode; 这是我当前对算法的实现:int startNode; int visited; 访问过int

unsigned int smallest = UINT_MAX;
do
{
    startNode = getStartNode300();
    setVisitedSet300();
    smallest = UINT_MAX;
    visitedSet[startNode] = true;
    for (int i = 0; i < numberNodes; i++)
    {
        distanceArray[i] = adjacencyMatrix[startNode][i];
    }
    for (int i = 0; i < numberNodes - 1; i++)   
    {
        for (int v = 0; v < numberNodes; v++)
        {
            if (visitedSet[v] == false)
            {
                if (distanceArray[v] < smallest)
                {
                    smallest = distanceArray[v];
                    visited = v;
                }
            }
        }
        visitedSet[visited] = true;
        for (int w = 0; w < numberNodes; w++)
        {
            if (visitedSet[w] == false)
            {
                distanceArray[w] = min(distanceArray[w], distanceArray[visited] + adjacencyMatrix[visited][w]);
            }
        }
    }

On this particular for loop where it should do the certain math to find the min between values at a certain index where the nodes are false and store the min in that index. 在这个特定的for循环上,应该执行一定的数学运算,以找到节点为假的某个索引处的值之间的最小值,并将最小值存储在该索引中。 What I found is that after it chooses the smallest value in distaceArray and set it to true(which is 3 if I start at 1 using the data file below, Which is correct.). 我发现是在它选择distaceArray中的distaceArray并将其设置为true之后(如果我使用下面的数据文件从1开始,则为3,这是正确的。)。

    for (int w = 0; w < numberNodes; w++)
        {
            if (visitedSet[w] == false)
            {
                distanceArray[w] = min(distanceArray[w], distanceArray[visited] + adjacencyMatrix[visited][w]);
            }
        }

It uses the nodes '0' and '2' as of this particular iteration is the nodes that are false. 截至此特定迭代,它使用的节点为“ 0”和“ 2”,即节点为假。 And does the math for times and stores them in the wrong array. 并进行数学运算并将其存储在错误的数组中。

I am using this data set that stores the numbers in adjacencyMatrix correctly. 我正在使用此数据集,该数据集将数字正确存储在adjacencyMatrix

0           5          10          4294967295
4294967295  0          4294967295  3
4294967295  7          0           4294967295
4294967295  4294967295 4           0

The correct output is: 正确的输出是:

Distance[0] =4294967295
Distance[1] =0 //Which is the node that I choose to start with
Distance[2] =7
Distance[3] =3

What I am getting is: 我得到的是:

Distance[0] =3
Distance[1] =0
Distance[2] =3
Distance[3] =3

I have done this process by hand to confirm that the correct output that I should be getting is true and it IS. 我已经手工完成了此过程,以确认我应该获取的正确输出是正确的并且是正确的。

Updated if: 在以下情况下更新:

if (visitedSet[w] == false && adjacencyMatrix[visited][w] != UINT_MAX)
                {
                    distanceArray[w] = min(distanceArray[w], distanceArray[visited] + adjacencyMatrix[visited][w]);
                }

The problem here is again, the fact that you shall only consider edges which exist, that is adjacencyMatrix[visited][w]!=UINT_MAX . 这里的问题还是,您只考虑存在的边,即adjacencyMatrix[visited][w]!=UINT_MAX

If you don't exclude these edges, distanceArray[visited] + adjacencyMatrix[visited][w] will overflow and the min() will not return the result that you expect. 如果不排除这些边缘, distanceArray[visited] + adjacencyMatrix[visited][w]将溢出,并且min()将不会返回您期望的结果。

You can solve this by changing this line: 您可以通过更改以下行来解决此问题:

if (visitedSet[w] == false && adjacencyMatrix[visited][w]!=UINT_MAX ) 

Edit: 编辑:

There is indeed another problem hidden in your nested for loops. 在嵌套的for循环中确实确实存在另一个问题。 The first inner for loop looks each time for the shortest subpath to expland. 第一个内部for循环每次都会寻找最短的exp子路径。 Unfortuantely, you didn't reset smallest , so that it starts with the smalest value of the previous iteration. 不幸的是,您没有重置smallest ,因此它以上一次迭代的最小值开始。

Update the looping as follows and you'll get your explected result: 如下更新循环,您将得到预期的结果:

for (int i = 0; i < numberNodes - 1; i++)   
{
    smallest = UINT_MAX; // !!!!!!!!!!!!!!!!!!!!!!!
    for (int v = 0; v < numberNodes; v++) 
    {
        ...
    }
    ...
} 

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

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