Hi there, I've been trying for hours to get my Dijkstra algorithm to print the correct path. It collects the correct distance properly, but I just can't get it to print out the path of nodes that it uses! Any help would be great appreciated. 'route' is my string variable to hold the route. I've tried various methods but none of them worked, so I deleted them out of the code.Here's my code:
g.getVertex(currentPoint).setTentativeMin(0);
String route = Integer.toString(startPoint);
while (g.getVertex(endPoint).getVisited() == false){
int smIndex = -1;; // Initialising our index
int minimum = Integer.MAX_VALUE;
g.getVertex(currentPoint).setVisited(true);
for (int i = 0 ; i < g.size(); i++){
if (g.getVertex(i).getVisited() == false){
for (AdjListNode N: g.getVertex(currentPoint).getAdjList()){
if ((g.getVertex(currentPoint).getTentativeMin() +
N.getVertexWeight()) < g.getVertex(i).getTentativeMin() && i == N.getVertexNumber()){
g.getVertex(i).setTentativeMin((g.getVertex(currentPoint).getTentativeMin() + N.getVertexWeight()));
}
}
}
}
for (int i = 0; i < g.size(); i++){
if(g.getVertex(i).getTentativeMin() < minimum && g.getVertex(i).getVisited() == false) {
smIndex = i;
minimum = g.getVertex(i).getTentativeMin();
}
}
currentPoint=smIndex;
}
int printSmall = g.getVertex(endPoint).getTentativeMin();
System.out.println("Shortest distance from vertex " + startPoint + " to vertex " + endPoint + " is " + printSmall + "\n");
System.out.println("Shortest Path: " +route);
// end timer and print total time
long end = System.currentTimeMillis();
System.out.println("\nElapsed time: " + (end - start) + " milliseconds");
}
}
I'm a first time poster here. Any help would be greatly appreciated!