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!

How can the code be executed for testing?
What does it print out now?

Edited 4 Years Ago by NormR1

This is only the bit of code actually doing the working, the rest is for file input. And it just prints out the starting point and the correct distance

It's part of a series of classes involved to execute, as I said this is my first time posting here so I'm unsure of the format! I apologise. Should I upload all the related files to it?

I can't test code that doesn't compile and execute. Maybe some one else will come along.

I'm done for tonight. Back tomorrow.

This article has been dead for over six months. Start a new discussion instead.