贝尔曼福特表演

时间:2017-12-02 08:54:53

标签: java performance data-structures shortest-path bellman-ford

   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开始吗?

1 个答案:

答案 0 :(得分:0)

就V时间检查所有边缘而言,复杂度仍为O(VE)。有关详细信息,请阅读this