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目的地。我在这里感到困惑的是,时间复杂性仍然是o(ve)或改变的,我已经看到这项工作在2个循环中完成了,这就是为什么,也会发现每个顶点经过的最短路径,否则我必须从0开始?
在您检查v时间的所有边缘时,复杂性仍然为 O(VE)
。有关更多信息,请阅读此信息。