Reputation: 1
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
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:
Step 2. Searching:
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.
Repeat.
Step 3. Backtracing
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