[英]Dijkstra in Java implementation
I have written the following dijkstra implementation in java for a project. 我已经在Java中为项目编写了以下dijkstra实现。 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. 当我运行测试时,如果startNode是0节点,则工作正常,但是如果startNode是另一个节点,则它不是。 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];
}
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. 您说您的问题是您的代码无法处理未标识为ID 0的起始节点。我认为我能为您提供帮助的唯一方法是仅说明我将如何实现Dijkstra的算法,因为我的方法似乎有所不同从你的。
Step 1. Initialization: 步骤1.初始化:
Step 2. Searching: 第2步:搜索:
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 步骤3.回溯
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. 如果没有,我将尝试找到更好的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.