ダイクストラ アルゴリズムの最短パスを正しく保つことに問題があります。最短の長さを正しく見つけて返します(実際の出力を使用して正しく使用されたアルゴリズムを使用して検証されます)が、作成される配列は正しくありません。この問題は、arraylist を更新しているときに見つかると思います。私のコードは次のとおりです。
public static Double Dijkstras(String startVertexName,
String endVertexName, ArrayList<String> shortestPath) {
HashSet<String> S = new HashSet<String>();
HashMap<String, String> temp = new HashMap<String, String>();
HashMap<String, Double> cost = new HashMap<String, Double>();
cost.put(startVertexName, 0.0);
for (String e : dataMap.keySet()) {
if (e.compareTo(startVertexName) != 0) {
cost.put(e, Double.MAX_VALUE);
}
}
while (!S.containsAll(dataMap.keySet())) {
ArrayList<String> notInS = new ArrayList<String>();
for (String x : dataMap.keySet()) {
if (!S.contains(x)) {
notInS.add(x);
}
}
Double smallest = Double.MAX_VALUE;
String foundK = "this is not right";
for (String k : notInS) {
if (cost.get(k) < smallest) {
smallest = cost.get(k);
foundK = k;
}
}
S.add(foundK);
notInS.remove(foundK);
HashSet<String> notInSAdjacent = new HashSet<String>();
for (String g : adjacencies.get(foundK).keySet()) {
if (notInS.contains(g)) {
notInSAdjacent.add(g);
}
}
String temp2 = "";
for (String j : notInSAdjacent) {
if ((cost.get(foundK) + adjacencies.get(foundK).get(j)) < cost
.get(j)) {
cost.put(j,
cost.get(foundK) + adjacencies.get(foundK).get(j));
temp2 = j;
}
}
if (!temp.containsKey(temp2)) {
shortestPath.add(foundK);
} else {
int index = shortestPath.indexOf(temp.get(temp2));
shortestPath.remove(index);
shortestPath.add(index, foundK);
}
temp.put(temp2, foundK);
if (foundK.compareTo(endVertexName) == 0) {
break;
}
}
return cost.get(endVertexName);
}
誰かが顕著なエラーを見ることができるかどうか教えてください。必要に応じて、サンプルの入力と出力を提供できます。ありがとう!