我对有向图的实现工作正常。这是一个“惰性”版本,因为它使用简单的优先级队列而不是索引队列。我更改了代码以获得无向图的解决方案,但它不起作用。dijkstra(int s)
是一个类的方法Graph
。的实现Graph
基于邻接列表。整个代码基于 Sedgewick 书中的解释。
public void dijkstra(int s) {
marked = new boolean[V];
distTo = new int[V];
edgeTo = new Edge[V];
Comparator<Edge> comparator = new Comparator<Edge>() {
public int compare(Edge e1, Edge e2) {
int dist1 = distTo[e1.either()] + e1.weight();
int dist2 = distTo[e2.either()] + e2.weight();
if (dist1 < dist2) {
return -1;
} else if (dist1 > dist2) {
return 1;
} else {
return 0;
}
}
};
pq = new PriorityQueue<Edge>(comparator);
for (int v = 0; v < V; ++v) {
distTo[v] = Integer.MAX_VALUE;
}
distTo[s] = 0;
relax(s);
while (!pq.isEmpty()) {
Edge e = pq.poll();
int v = e.either();
int w = e.other(v);
if (!marked[w]) {
relax(w);
}
}
}
private void relax(int v) {
marked[v] = true;
for (Edge e : adj[v]) {
int w = e.other(v);
if (distTo[w] > distTo[v] + e.weight()) {
distTo[w] = distTo[v] + e.weight();
edgeTo[w] = e;
pq.add(e);
}
}
}