Java Can';bellman-ford算法的t生成右图

Java Can';bellman-ford算法的t生成右图,java,algorithm,graph,bellman-ford,Java,Algorithm,Graph,Bellman Ford,我有一个贝尔曼-福特算法的实现。 输入程序提供了一个边列表。 如果没有优化,它看起来是这样的: int i, j; for (i = 0; i < number_of_vertices; i++) { distances[i] = MAX; } distances[source] = 0; for (i = 1; i < number_of_vertices - 1; ++i) {

我有一个贝尔曼-福特算法的实现。 输入程序提供了一个边列表。 如果没有优化,它看起来是这样的:

int i, j;
        for (i = 0; i < number_of_vertices; i++) {
            distances[i] = MAX;
        }
        distances[source] = 0;
        for (i = 1; i < number_of_vertices - 1; ++i) {

            for (j = 0; j < e; ++j) { //here i am calculating the shortest path
                if (distances[edges.get(j).source] + edges.get(j).weight < distances[edges.get(j).destination]) {
                    distances[edges.get(j).destination] = distances[edges.get(j).source] + edges.get(j).weight;
                }
            }
        }
inti,j;
对于(i=0;i<顶点数;i++){
距离[i]=最大值;
}
距离[源]=0;
对于(i=1;i<顶点的个数-1;++i){
对于(j=0;j
它具有O(V*E)的复杂性 但是他的优化工作非常快。看起来像

while (true) {

            boolean any = false;
            for (j = 0; j < e; ++j) { //here i am calculating the shortest path
                if (distances[edges.get(j).source] + edges.get(j).weight < distances[edges.get(j).destination]) {
                    distances[edges.get(j).destination] = distances[edges.get(j).source] + edges.get(j).weight;
                    any = true;
                }
            }
            if (!any) break;
        }
while(true){
布尔值any=false;
对于(j=0;j
在实践中,如果外循环中的顶点数(例如一万个)只有10-12次迭代,而不是一万次,则该算法完成其工作

这是我的生成代码:

//q - vertices

for (int q = 100; q <= 20000; q += 100) {
          List<Edge> edges = new ArrayList();
                for (int i = 0; i < q; i++) {
                    for (int j = 0; j < q; j++) {
                        if (i == j) {
                            continue;
                        }

                        double random = Math.random();
                        if (random < 0.005) {
                            int x = ThreadLocalRandom.current().nextInt(1, 100000);
                           edges.add(new Edge(i, j, x));
                            edges++;
                        }
                    }

                }
              //write edges to file edges
            }
//q-顶点

对于(int q=100;q像您所说的Bellman-Ford算法的复杂性是O(| E |*| V |)。在您的生成器中,添加边的概率可以忽略不计(0.005),这就是我认为代码运行速度快的原因


增加概率,将有更多的边,因此Bellman-Ford将花费更长的时间。

因此,您的问题是:Bellman-Ford(或一般最短路径算法)的硬示例是什么?@gilleain是的,使用我的生成器,外循环的迭代次数太少,需要随机生成“硬”/“简单”这个算法的图形,他不能用它来这么快完成任务。@gilleain我需要它,因为我有Dijkstra算法的实现,它使用二叉树,算法复杂度为O(E*log(V))还有工作时间,正如Bellman Ford with Optimization的这个实现所理解的那样。然后它看起来更像是一个数学问题-哪类图有大量的短路径?类似的。在我的生成器中,例如,如果顶点=10000->Edge≈500000.但外部循环的迭代次数不受影响。如果我公开生成边的百分比为30%,例如我有3000个顶点,我在while(true)循环上有10次迭代,而没有优化必须是3000。在这个例子中,Dijkstra运行3523毫秒,Bellman Ford 3024,尽管Dijkstra的复杂性(E*log(V))@Birthright具有10000个顶点和500000条边的图不是稠密的。在我看来,只有在更大和更密集的图(更多的顶点和边)上,差异才会变得明显.i在稀疏图中测试算法。但好的。如果生成概率为80%,我得到的结果就是。顶点=3000,边=7196374,同时的迭代次数(true)loop=9。Bellman-Ford-4851毫秒,Dijkstra使用二叉树-5311毫秒如果while循环的迭代次数仅为9,则Bellman-Ford将取O(9*E),Dijkstra将取O(log(3000)*E).所以我需要增加循环中的迭代次数,如果以不同的方式生成图形,这是可能的。也许我在某个地方错了,如果我生成另一个图形,那么Dijkstra的工作时间也会更长,但目前我不喜欢Dijkstra比Bellman Ford慢