首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取最大流的Ford-Fulkerson算法中的空指针异常

获取最大流的Ford-Fulkerson算法中的空指针异常
EN

Stack Overflow用户
提问于 2014-04-03 08:54:06
回答 1查看 158关注 0票数 0

我已经写了一个代码,用于从图中获得最大流量,该图被硬编码到main中。我按照说明操作,但我一直收到这个错误,而且我不确定为什么。我试着更改了很多部分并进行了调试,但我还是搞不清楚。这是错误:

代码语言:javascript
复制
Exception in thread "main" java.lang.NullPointerException
    at lib280.graph.FF.dft(FF.java:78)
    at lib280.graph.FF.isRes(FF.java:67)
    at lib280.graph.FF.findMax(FF.java:40)
    at lib280.graph.FF.main(FF.java:117)

下面是我的代码:

代码语言:javascript
复制
package lib280.graph;

public class FF {
    public boolean[] visited; 
    double parent[]; 
    public int maxflow; 

    public int findMax(double G[][], int s, int t){
        maxflow = 0;
        int a = G.length; //length of rows 
        int b = G[0].length; // length of columns
        int v = a*b; // vertices
        int i, j;
        double c;
        double[][] residual = new double[a][b]; //the initializion for the residuals 
        //Create residual of G 
        for (i=0;i<a;i++){
            for (j=0; j<b;j++){
                residual[i][j] = G[i][j];
            }
        }
        //while the path from s to t is residual, find max flow 
        while (isRes(residual, s, t, parent)){
            int path = Integer.MAX_VALUE; //(LINE 40)
            for (double h=t; h<s; h=parent[(int)h]){
                c = parent[(int)h];
                path = Math.min(path, (int)residual[(int)c][(int)h]);
            }

            for (double h=t; h<s; h=parent[(int)h]){
                c = parent[(int)h];
                residual[(int)c][(int)h] -= path; //residual for the edge
                residual[(int)h][(int)c] += path; //residual for the reverse edge
            }

            maxflow = maxflow+path; //update max
        }
        return maxflow;
    }

    public boolean isRes(double G[][], int s, int t, double parent[]){
        dft(G, s);
        return (visited[t]==true); //(Line 67)
        }

    public void dft(double G[][], int s){
        for (int i=0;i<G.length;i++){ // initializing every vertex not visited 
            visited[i] = false; // (Line 78)
        }
        dftHelper(G, s);
    }

    public void dftHelper(double G[][], int s){
        visited[s] = true;

        for (int i=0; i<G.length; i++){
            if (visited[i]==false && G[s][i]>0){ // for each node adjacent to s and if it is not reached
                dftHelper(G, i);
            }
        }
    }

这是main的精简版本:

代码语言:javascript
复制
public static void main(String args[]) throws Exception {
        FF a = new FF();
        double G[][] = hardcoded graph

        int x = a.findMax(G, 0, 5);
        System.out.println("Max flow" + x); //(Line 117)
        System.out.println("Final Residual Capacities: ");
    }
}
EN

回答 1

Stack Overflow用户

发布于 2014-04-03 08:57:55

看起来你在FF中声明了'visited‘,但是从来没有构造过一个实际的数组。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22825845

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档