[英]Djikstra's Algorithm using Adjacency List and Priority queue in java
I am having trouble understand why my dijkstraShortestPath(int startVertex) function is not working properly. 我无法理解为什么我的dijkstraShortestPath(int startVertex)函数无法正常工作。 I am following pseudocode for my project, but I do not understand what I am doing wrong.
我正在为我的项目使用伪代码,但是我不明白我在做什么错。
Only the walk for my start vertex is showing up for my algorithm. 对于我的算法,只有我的起始顶点显示。
I also have a DFS, but I am not sure if I should be using it in my dijkstraShortestPath method, and if I do, how do I implement it? 我也有一个DFS,但是我不确定是否应该在dijkstraShortestPath方法中使用它,如果可以,如何实现它?
I think my issue is either in the "while loop" or the way I am initializing my priority queue named "pq". 我认为我的问题是在“ while循环”中还是在初始化名为“ pq”的优先级队列的方式中。
Link to FULL code: https://www.dropbox.com/s/b848b9ts5lrfn01/Graph%20copy.java?dl=0 链接到完整代码: https : //www.dropbox.com/s/b848b9ts5lrfn01/Graph%20copy.java? dl =0
Link to pseudocode: https://www.dropbox.com/s/tyia0sr3t9r8snf/Dijkstra%27s%20Algorithm%20%281%29.docx?dl=0 链接到伪代码: https : //www.dropbox.com/s/tyia0sr3t9r8snf/Dijkstra%27s%20Algorithm%20%281%29.docx? dl =0
Link to requirements: https://www.dropbox.com/s/rq8km8rp4jvyxvp/Project%202%20Description-1%20%282%29.docx?dl=0 链接到要求: https : //www.dropbox.com/s/rq8km8rp4jvyxvp/Project%202%20Description-1%20%282%29.docx?dl=0
Below is the code for my Dijkstra Algorithm. 以下是我的Dijkstra算法的代码。
public void dijkstraShortestPaths(int startVertex) {
// Initialize VARS and Arrays
int count = 0, start = startVertex;
int[] d;
int[] parent;
d = new int[nVertices];
parent = new int[nVertices];
DistNode u;
// 10000 is MAX/Infinity
for (int i = 0; i < nVertices; i++) {
parent[i] = -1;
d[i] = 10000;
}
// Initialize Start vertex distance to 0
d[startVertex] = 0;
// Setup Priotiry Queue
PriorityQueue<DistNode> pq = new PriorityQueue<DistNode>();
for(int i = 0; i < adjList[start].size(); i++){
pq.add(new DistNode(adjList[start].get(i).destVertex, adjList[start].get(i).weight));
}
System.out.print(pq);
//
while (count < nVertices && !pq.isEmpty()) {
// remove DistNode with d[u] value
u = pq.remove();
count++;
System.out.println("\n\nu.vertex: " + u.vertex);
// for each v in adjList[u] (adjacency list for vertex u)
for(int i = 0; i < adjList[u.vertex].size();i++){
// v
int v = adjList[u.vertex].get(i).destVertex;
System.out.println("v = " + v);
// w(u,v)
int vWt = adjList[u.vertex].get(i).weight;
System.out.println("vWt = " + vWt + "\n");
if((d[u.vertex] + vWt) < d[v]){
d[v] = d[u.vertex] + vWt;
parent[v] = u.vertex;
pq.add(new DistNode(v,d[v]));
}
}
}
printShortestPaths(start, d, parent);
}
The problem with your using a PriorityQueue
is that the content of the the priority queue is unrelated to the content of the array d
; 您使用
PriorityQueue
的问题在于,优先级队列的内容与数组d
的内容无关; and the statement: 并声明:
pq.add(new DistNode(v,d[v]));
Should replace any DistNode
in pq
with vertex v
, otherwise, you may visit the same vertex multiple times. 应该用顶点
v
替换pq
任何DistNode
,否则,您可能会多次访问相同的顶点。
I'm not sure that the PriorityQueue
is the right tool for the job. 我不确定
PriorityQueue
是否适合该工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.