### Dijkstra Algorithm won't terminate

Hi,

I've resumed a dijkstra school work from a few months ago and found that in special cases the main dijkstra while-loop won't terminate.I use a 2-dimensional int matrix for distances and when I've arrived at the end point I use the previous-array to get the backwardspath from end -> start.
Here is the code:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475`` ``````void Graph::dijkstra(int start, int end) { int currentNode = start, smallestCost = INFINITY; int smallestCostIx = -1, infinity = INFINITY; bool allSearched = true; bool *searched = new bool[this->nrOfAirports]; int *distance = new int[this->nrOfAirports]; int *previous = new int[this->nrOfAirports]; for(int ix = 0 ; ix < this->nrOfAirports ; ix++) { searched[ix] = false; distance[ix] = INFINITY; previous[ix] = -1; } distance[start] = 0; //A -> A = 0 searched[start] = true; for(int ix = 0 ; ix < this->nrOfAirports; ix++) { if(!searched[ix]) { allSearched = false; } } while(!searched[end] && !allSearched) { smallestCost = INFINITY; for(int ix = 0 ; ix < this->nrOfAirports; ix++) { if(this->matrix[currentNode][ix] > 0 && !searched[ix]) { if(distance[ix] == infinity) { distance[ix] = distance[currentNode] + this->matrix[currentNode][ix]; } if(distance[ix] < smallestCost) { smallestCost = distance[ix]; smallestCostIx = ix; } } } for(int ix = 0 ; ix < this->nrOfAirports; ix++) { if(this->matrix[smallestCostIx][ix] > 0) { if(distance[ix] > distance[smallestCostIx] + this->matrix[smallestCostIx][ix]) { distance[ix] = distance[smallestCostIx] + this->matrix[smallestCostIx][ix]; previous[ix] = smallestCostIx; } } } searched[smallestCostIx] = true; currentNode = smallestCostIx; allSearched = true; for(int ix = 0 ; ix < this->nrOfAirports; ix++) { if(!searched[ix]) { allSearched = false; } } } delete[]searched; delete[]distance; delete[]previous; }``````

Why will it sometimes not terminate?
Hi henk13

I suppose the thing to do is use the debugger to track variables.

Hope this helps