简体   繁体   中英

Dijkstra's Algorithm c++

I am supposed to implement a Dijkstra's algorithm with an adjacency matrix and a distance array. These are implemented through the class Graph300 . The program inputs a file with a weighted matrix and it is getting into the matrix fine.

The distance array is outputted for the user. But my Dijkstras is not working correctly. I keep getting UINT_MAX for every vertex except the starting vertex (user specified) :

Input matrix (4x4):
0           5           10           4294967295
4294967295  0           4294967295   3
4294967295  7           0            4294967295
4294967295  4294967295  4            0

My distance array should output:
distance[0]=4294967295 -nopath
distance[1]=0  -startnode
distance[2]=7
distance[3]=3

My output now (that is wrong):
distance[0]=4294967295 -nopath
distance[1]=0  -startnode
distance[2]=4294967295
distance[3]=4294967295

Here is my algorithm; numberNodes is the the dimension of the vertex. startNode is user specified. The matrix array and distance array are of type unsigned int :

void Graph300::dijkstra300()
{
    int startNode=getStartNode300();

    for (int i = 0; i < numberNodes; i++)
        distanceArray[i] = UINT_MAX;

    distanceArray[startNode] = 0;

    for (int count = 0; count < numberNodes-1; count++)
    {
         unsigned int min = INT_MAX, min_index;

         for (int v = 0; v < numberNodes; v++)
             if (visitedSet[v] == false && distanceArray[v] <= min)
                 min = distanceArray[v], min_index = v;

         int u = min_index;
         visitedSet[u] = true;

         for (int v = 0; v < numberNodes; v++)
             if (!visitedSet[v] && adjacencyMatrix[u][v] && distanceArray[u] != UINT_MAX
                && distanceArray[u]+adjacencyMatrix[u][v] < distanceArray[v])
                  distanceArray[v] = distanceArray[u] + adjacencyMatrix[u][v];
    }
    view300();  // display result
}

First look:

It's difficult to be 100% sure to solve the issue, because you didn't show us the class member types nor a typical example of failure. Nevertheless a confustion pops to the eye: is the type of distanceArray[] unsigned int or int ?

Here you assume it's unsigned:

distanceArray[i] = UINT_MAX;   

But later you compare this unsigned with a signed integer:

int min = INT_MAX, min_index;   // min is signed integer !

for (int v = 0; v < numberNodes; v++)
   if (... && distanceArray[v] <= min)  // when comparing signed and unsigned you might not get the expected result !!

Mismatches between signed and unsigned might lead to unexpected results in your comparison (see online demo ). To avoid this kind of issues, either you work only with unsigned values, or, change all your UINT_MAX to INT_MAX .

Solution

I could find out the problem with the additional data you provided, and switching all distances to unsigned int. You have to correct your if :

        if (!visitedSet[v] && adjacencyMatrix[u][v] && adjacencyMatrix[u][v] != UINT_MAX  
            && distanceArray[u] + adjacencyMatrix[u][v] < distanceArray[v])

because you have to consider only existing edges ( adjacencyMatrix[u][v]!= UINT_MAX ). In your code you made the comparison with distanceArray .

I could run your code (outside of a class) and with your input obtain the expected output. Here the online demo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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