I am having trouble with implementing Dijkstra algorithm.
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 visited;
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. 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.).
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. 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.
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
.
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.
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. The first inner for loop looks each time for the shortest subpath to expland. Unfortuantely, you didn't reset smallest
, so that it starts with the smalest value of the previous iteration.
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++)
{
...
}
...
}
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.