私の次のコードは、有向グラフに対して完全に正常に機能しており、無向グラフが与えられた場合、最短パスを返しません。
public void Djikstra(int s){
boolean[] marked = new boolean[V];
dist = new double[V];
for(int i = 0; i<V; i++){ # initializing array
dist[i] = Double.POSITIVE_INFINITY;
}
dist[s] = 0.0;
Queue<Integer> pqs = new PriorityQueue<Integer>();
pqs.add(s);
while(!pqs.isEmpty()){
int v = pqs.poll();
if(marked[v]) continue;
marked[v] = true;
for(Edge e : get_list(v)){ # get_list(v) will return an iterable from the adjacency list at index v
v = e.getV()
int w = e.getW();
if(dist[w] > dist[v] + e.getWeight()){
dist[w] = dist[v] + e.getWeight();
distances[w] = e #all the distances will be stored in this array
pqs.add(w);
}
}
}
}
ここで私の間違いは何ですか?単純なエラーだと確信しています。いくつかのヒントでうまくいくでしょう。
ありがとう。
編集:
public void addEdge(Edge e){
adj[e.getV()].add(e);
adj[e.getW()].add(e);
}