Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Ford-Fulkerson算法中获取最大流的空指针异常_Java_Nullpointerexception_Max Flow - Fatal编程技术网

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

Java Ford-Fulkerson算法中获取最大流的空指针异常,java,nullpointerexception,max-flow,Java,Nullpointerexception,Max Flow,我已经编写了一个代码,用于从硬编码到main的图形中获取最大流。我按照指示去做,但我一直都会犯这个错误,我不知道为什么。我试过更换许多部件并进行调试,但我想不出来。这就是错误: 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.

我已经编写了一个代码,用于从硬编码到main的图形中获取最大流。我按照指示去做,但我一直都会犯这个错误,我不知道为什么。我试过更换许多部件并进行调试,但我想不出来。这就是错误:

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)
这是我的密码:

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

看起来您在FF中声明了“已访问”,但从未构建实际数组。

您将
已访问的
设置在哪里?如
visted=new boolean[somSize]
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: ");
    }
}