Bellman Ford检测最短长度的负循环

时间:2014-10-18 11:21:00

标签: java algorithm graph dynamic-programming bellman-ford

解决UVA http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=40的这个仲裁问题但是我发现了最短长度的负循环(这里的长度是顶点数)。这是我的代码成功检测到负循环

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class _104 {

public static void main(String[] args) throws NumberFormatException,
        IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(
            System.in));
    String input;
    while ((input = reader.readLine()) != null) {
        int n = Integer.parseInt(input);
        double[][] cost = new double[n + 1][n + 1];
        double[] spEstimate = new double[n + 1];
        int parent[] = new int[n + 1];
        for (int i = 0; i < n + 1; i++) {
            spEstimate[i] = Double.MAX_VALUE;
            cost[0][i] = 0;
            cost[i][0] = Double.MAX_VALUE;
            parent[i] = Integer.MAX_VALUE;
        }
        spEstimate[0] = 0.0;
        parent[0] = 0;
        for (int i = 1; i < n + 1; i++) {
            String[] line = reader.readLine().split("\\s+");
            for (int j = 1; j < n + 1; j++) {
                if (i == j) {
                    cost[i][j] = 0;
                } else if (i < j) {
                    cost[i][j] = -(Math
                            .log(Double.parseDouble(line[j - 2])) / Math
                            .log(2));
                } else {
                    cost[i][j] = -(Math
                            .log(Double.parseDouble(line[j - 1])) / Math
                            .log(2));
                }
            }
        }
        int save = 1, s = 1;
        boolean flag = BellmanFord(n, cost, spEstimate, parent);
         display(cost);
        // Relax all edges once more
        boolean brk = true;
        for (int i = 0; i < cost.length && brk; i++) {
            for (int j = 0; j < cost.length && brk; j++) {
                //relax(i, j, spEstimate, cost[i][j], parent);
            }
        }

        ArrayList<Integer> path = new ArrayList<Integer>();
        while (parent[save] != s) {
            path.add(save);
            save = parent[save];
        }
        if (flag) {
            System.out.println("no arbitrage sequence exists");
        } else {
            path.add(0, path.get(path.size() - 1));
            for (int i = path.size() - 1; i >= 0; --i) {
                System.out.println(path.get(i));
            }
        }
    }
    reader.close();
}

public static boolean BellmanFord(int n, double[][] cost, double[] sp,
        int[] parent) {
    for (int k = 0; k < n - 1; k++) {
        for (int i = 0; i < cost.length; i++) {
            for (int j = 0; j < cost.length; j++) {
                relax(i, j, sp, cost[i][j], parent);
            }
        }
    }
    // Relax all edges once more to detect cycle
    for (int i = 0; i < cost.length; i++) {
        for (int j = 0; j < cost.length; j++) {
            if (sp[j] > (sp[i] + cost[i][j])) {
                return false;
            }
        }
    }
    return true;
}

static void relax(int i, int j, double[] sp, double cij, int[] parent) {
    if (sp[j] > (sp[i] + cij)) {
        sp[j] = sp[i] + cij;
        System.out.println("relaxed " + i + " " + j + " " + cij + " "
         + sp[i] + " " + sp[j]);
        parent[j] = i;
    }
}

static void display(double[][] cost) {
    System.out.println("Display Cost");
    for (int i = 0; i < cost.length; i++) {
        for (int j = 0; j < cost.length; j++) {
            System.out.print(cost[i][j] + "\t");
        }
        System.out.println();
    }
}

static void display(double[] sp) {
    for (int i = 0; i < sp.length; i++) {
        System.out.println(sp[i]);
    }
}
}

2 个答案:

答案 0 :(得分:2)

你可以这样做:

  1. 修复周期的起始顶点(让我们称之为v)。

  2. 假设dist[i] = 0 if i = v and INF otherwise

  3. 运行Ford-Bellman算法
  4. 如果存在包含v的负循环,则在Ford-Bellman算法k中外循环的dist[v]次迭代后将变为负数。因此,您只需在每次迭代后检查k是否仍然是非负数,就可以轻松找到这样的最小dist[v]

  5. 所有k中最小的v就是答案。

答案 1 :(得分:0)

通过考虑增加长度的循环而不是如kraskevich所描述的找到负循环,可以解决该问题。两种方法的最坏情况复杂度是O(n ^ 4)。这种方法类似于Floyd-Warshall,您可以考虑增加长度而不是中间顶点。

您可以找到包含图表和代码here的详细说明。