简体   繁体   English

无法为 bellman-ford 算法生成正确的图

[英]Can't generate right graph for bellman-ford algorithm

I have an implementation of the algorithm of Bellman - Ford.我有一个贝尔曼-福特算法的实现。 The input program supplied a list of edges.输入程序提供了一个边列表。 Without optimization it looks like this:没有优化它看起来像这样:

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;
                }
            }
        }

it has the complexity of O(V * E) But with optimization his works very fast.它具有 O(V * E) 的复杂性,但通过优化,他的工作速度非常快。 it looks like看起来像

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;
        }

In practice, if the number of vertices , for example ten thousand , in the outer loop had only 10-12 passes iterations instead of 10 thousand, and the algorithm completes its work .在实践中,如果顶点数,例如一万,在外循环中只有 10-12 次迭代而不是 1 万次,则算法完成其工作。

This is my generate code:这是我的生成代码:

//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
            }

But I need to generate a graph on which it will not be so fast to finish his work.但是我需要生成一个图表,在该图表上完成他的工作不会那么快。 That can be changed in the generator?那可以在发电机中改变吗?

The complexity of Bellman Ford algorithm like you said is O(|E|*|V|).像你说的 Bellman Ford 算法的复杂度是 O(|E|*|V|)。 In your generator, the probability of adding an edge is negligible (0.005) which is why according to me the code works fast.在您的生成器中,添加边缘的概率可以忽略不计(0.005),这就是为什么根据我的说法代码运行速度很快。

Increase the probability, there shall be more edges and consequently the Bellman Ford shall then take longer time.增加概率,将会有更多的边缘,因此贝尔曼福特将需要更长的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM