【发布时间】:2018-05-16 07:31:20
【问题描述】:
for(int k=1; k<=Vertices-1; k++){
/*Every sublist has found the shortest to its adjacent vertices
thats why starting loop from k-1 then going into every edge of a vertex
and updating the shortest distance from it to the other.
*/
for(int i=k-1; i<Vertices; i++){
// Visiting every Vertex(V) and checking distance of its edges to some other vertex Vo.
for(int j=0; j<Edges[i].size(); j++){
int v = Edges[i].get(j).src;
int edge = Edges[i].get(j).dest;
int weight = Edges[i].get(j).weight;
// if exisiting distance is not infinity and from source to that vertex the weight is less then update it
if (dist[v]!=Integer.MAX_VALUE && dist[v]+weight<dist[edge]){
dist[edge]=dist[v]+weight;
//updating parent of destination to source.
parent[edge] = v;
}
}
}
}
我已经从(LinkedList)列表中实现了bellman ford,因为算法运行V-1(循环1)次并进入每个顶点(在Loop2中)它检查所有边缘(在loop3中)并更新距离目的地。我在这里很困惑,时间复杂度仍然是 O(VE) 或改变,我已经看到这项工作在 2 个循环中完成,这就是为什么,并且也会为每个经过的顶点找到最短路径,或者我必须从0开始?
【问题讨论】:
-
考虑在这篇文章中添加更多细节,这将提高您获得高质量答案的可能性。
标签: java performance data-structures shortest-path bellman-ford