Dijkstra in Java implementation

I have written the following dijkstra implementation in java for a project. When I run the tests it works fine if the startNode is the 0 node but if the startNode is another one it just doesn't. Could anyone help me? I would be grateful...

Here is the code:

    double[][] nodesDistance = new double[graph.getNumOfNodes()][graph.getNumOfNodes()];
    double[] distance = new double[graph.getNumOfNodes()];
    int[] preD = new int [graph.getNumOfNodes()];
    int[] visited = new int [graph.getNumOfNodes()];

    double min;
    int nextNode = 0;

    for (int i = startNode; i < 0; i--) {

        visited[i] = 0; 
        preD[i] = 0;

        for (int j = startNode; j < 0; j--) {

            if (graph.existsEdge(i, j) == true) {

                nodesDistance[i][j] = graph.getEdgeDistance(i, j);
            }else {

                nodesDistance[i][j] = Double.POSITIVE_INFINITY;
            }
        }
    }

    for (int i = startNode; i < graph.getNumOfNodes(); i++) {

        visited[i] = 0; 
        preD[i] = 0;

        for (int j = startNode; j < graph.getNumOfNodes(); j++) {

            if (graph.existsEdge(i, j) == true) {

                nodesDistance[i][j] = graph.getEdgeDistance(i, j);
            }else {

                nodesDistance[i][j] = Double.POSITIVE_INFINITY;
            }
        }
    }
    distance = nodesDistance[startNode];
    distance[startNode] = 0;
    visited[startNode] = 1;

    for (int i = startNode; i < 0; i--) {

        min = 999.0;

        for (int j = startNode; j < 0; j--) {

            if ((min > distance[j]) && (visited[j] != 1)) {

                min = distance[j];
                nextNode = j;
            }
        }

        visited[nextNode] = 1;

        for (int c = startNode; c < 0; c--) {

            if (visited[c] != 1) {

                if (min + nodesDistance[nextNode][c] < distance[c]) {

                    distance[c] = min + nodesDistance[nextNode][c];
                    preD[c] = nextNode;
                }
            }
        }
    }

    for (int i = startNode; i < graph.getNumOfNodes(); i++) {

        min = 999.0;

        for (int j = startNode; j < graph.getNumOfNodes(); j++) {

            if ((min > distance[j]) && (visited[j] != 1)) {

                min = distance[j];
                nextNode = j;
            }
        }

        visited[nextNode] = 1;

        for (int c = startNode; c < graph.getNumOfNodes(); c++) {

            if (visited[c] != 1) {

                if (min + nodesDistance[nextNode][c] < distance[c]) {

                    distance[c] = min + nodesDistance[nextNode][c];
                    preD[c] = nextNode;
                }
            }
        }
    }

    for (int i = 0; i < graph.getNumOfNodes(); i++) {

        result.nodeDistance[i] = distance[i];
        result.nodeThrough[i] = preD[i];
    }

Upvotes: 0

Views: 671

Answers (1)

Glubus
Glubus

Reputation: 2855

Alright well since noone answered your question I guess I will try to. The reason I didn't do it sooner is because I wasn't sure about whether or not I could in fact answer your question since I can't right away see what the problem is arising from your code.

You say your problem is that your code can't deal with a starting node not being identified with id 0. The only way i think I can help you is by just explaining how I would implement Dijkstra's algorithm, as my approach seems to differ somewhat from yours.


Step 1. Initialization:

  • Define your node class with as a member a list of edges (with their cost/length) that represent the connections to the nodes that are adjacent to the node that is the owner of the instance. Also give each node an ID, a property that represents whether or not the node is visited, a property that determines its current distance travelled, and a property that represents its predecessor node in the optimal path.
  • Define your edge class with two nodes as members that define its endpoints. Also give it a property that holds its cost/length.
  • Create an array or dictionary of nodes with the nodes ID as keys and the nodes themselves as value. This array will function as a way of seeing which node has been visited or not.
  • Create a sorting datastructure (i.e. a binary heap or priorityqueue) of nodes that will function as the list of adjacent (or open) nodes that you can choose to visit. Make sure that your datastructure sorts the nodes by their distance travelled from low to high.
  • Make sure that all members of all nodes and edges have been set correctly (i.e. all neighbours have been configured). You should be able to do this with the input you get for the problem. Also set every distance travelled in each node to infinity (or something really high). However, give the starting node distance 0, and place it in the adjacent nodes datastructure.

Step 2. Searching:

  • Extract the node with the minimum distance travelled (that has not yet been visited) from the adjacent nodes datastructure and set its visisted property to true.
    • If the current visited node is the node you were looking for, stop the searching procedure.
  • For each of the nodes adjacent nodes, see if their distance travelled value is higher than the sum of the distance travelled by the current node, and the distance between the current node and the neighbour.

    • If this is the case, update the neighbours distance travelled value to the sum you just calculated, and add the node to the open nodes datastructure if it was not already in there (also make sure the datastructure updates its order). Also set the neighbours predecessor node to the current node.
    • If this is not the case, then the node has been already added to the datastructure, so you can leave it be.
  • Repeat.

Step 3. Backtracing

  • This step is pretty: just create an list of nodes and add the goal node to it. Then add the node that the goal has set as its predecessor in the previous procedure. Then do the same for that node, and keep doing this untill you find the starting node. Then return the list in the preferred format and pat yourself on the back for finding the shortest path.

Now I know this explanation contains a lot of information you already knew, though like I said at the start, I wasn't sure where the problem in your knowledge/code existed. Hopefully by reading this you can find what you need. If not, I'll try to find a better solution.

Upvotes: 1

Related Questions