簡體   English   中英

圖DFS算法NullPointerException

[英]Graph DFS Algorithm NullPointerException

我只是用Java上學,但一直都在顯示錯誤NullPointerException
我必須編寫一個程序,該程序可以從文件(頂點)中讀取數據,接下來我應該從該數據中計算圖形大小,然后我必須使用DFS算法來計算一致的組件。
我知道我的代碼不好,但是我仍在學習java(和英語:P)。

test.txt

0 1 0 4
1 2 1 4 1 5 1 8 1 11
2 6
3 1 3 7
4 8
5 2 5 8
6 5 6 7 6 9
8 4
9 5 9 7
10 8 10 11
11 8 11 9 11 12
12 3 12 6 12 9

例外

Exception in thread "main" java.lang.NullPointerException
    at Search.DFS(Search.java:17)
    at AppClient.main(AppClient.java:25)

AppClient.class

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOError;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class AppClient {

    private static final String filename = "C:\\test.txt";
    private static List<Integer> sizeGraphList = new ArrayList<Integer>();
    private static List<Integer> numberOfVertex = new ArrayList<Integer>();
    static Graph graph;
    static Search search;

    public static void main(String[] args) throws IOException {

        graph = new Graph(getGraphSize());
        addVertexToGraph();
        search = new Search();
        int result = search.DFS();
        System.out.println("Składowe: " + result);
    }

    private static int getGraphSize() throws IOException {
        String Path = filename;

        Path filePath = Paths.get(Path);
        Scanner in = new Scanner(filePath);

        while (in.hasNext()) {
            if (in.hasNextInt()) {
                sizeGraphList.add(in.nextInt());
            } else {
                in.next();
            }
        }

        for (Integer x : sizeGraphList) {
            System.out.println(x);
        }

        int sizeGraph = getSizeOfVertex(sizeGraphList);
        System.out.println("Size: " + sizeGraph);
        return sizeGraph;
    }

    private static int getSizeOfVertex(List<Integer> listOfVertex) {

        for (int i = 0; i < listOfVertex.size(); i++) {
            numberOfVertex.add(listOfVertex.get(i));
        }

        for (int i = 0; i < numberOfVertex.size(); i++) {
            for (int j = 1; j < numberOfVertex.size(); j++) {
                if (i != j && numberOfVertex.get(i) == numberOfVertex.get(j)) {
                    numberOfVertex.remove(j);
                }
            }
        }

        int size = numberOfVertex.size();
        return size;
    }

    private static void addVertexToGraph() {
        try (BufferedReader br = new BufferedReader(new FileReader(filename))) {

            String line = br.readLine();

            while (line != null) {
                // String[] edge = line.trim().split("[^\\d]+");
                String[] edge = line.trim().split("\\s+");

                for (int i = 0; i < edge.length - 1; i += 2) {
                    int v = Integer.parseInt(edge[i]);
                    int u = Integer.parseInt(edge[i + 1]);
                    //System.out.println(v + ", " + u);
                    graph.addEdge(v, u);
                }

                line = br.readLine();
            }

        } catch (IOException e) {
            System.out.println("File does not exist!!!");
        }
    }
}

圖類

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

class Graph {


    public List<LinkedList<Integer>> graphVertices;
    public int[] processingRange;



    Graph(int graphSize) {

        graphVertices = new ArrayList<>();
        processingRange = new int[graphSize+1];
        for (int i = 0; i < graphSize; i++) {
            graphVertices.add(i, new LinkedList<Integer>());
        }
    }

    void addEdge(int v, int u) {
        LinkedList<Integer> list = graphVertices.get(v);
        list.add(u);
        graphVertices.set(v, list);
    }
}

搜索類

import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

public class Search extends AppClient{

    private Stack<Integer> vertexesStack;
    private int range;
    private int scc;
    Graph graph;

    public Search() {

    }

    int DFS() {
        boolean visited[] = new boolean[graph.graphVertices.size()+1];
        for (int i = 0; i < graph.graphVertices.size(); i++)
            if (!visited[i]) {
                dfsSearch(i, visited);
            }
        return scc;
    }

    private void dfsSearch(int v, boolean visited[]) {
        graph.processingRange[v] = range++;
        visited[v] = true;
        vertexesStack.push(v);
        if (!checkDistance(v, visited)) {
            int top;
            do {
                top = vertexesStack.pop();
                graph.processingRange[top] = graph.graphVertices.size();
            } while (top != v);
            scc++;
        }
    }

    private boolean checkDistance(int v, boolean[] visited) {
        int minRange = graph.processingRange[v];

        for (int vertex : graph.graphVertices.get(v)) {

            if (!visited[vertex]){
                dfsSearch(vertex, visited);
            }

            if (graph.processingRange[vertex] < minRange) {
                minRange = graph.processingRange[vertex];
            }
        }
        if (minRange < graph.processingRange[v]) {
            graph.processingRange[v] = minRange;
            return true;
        }
        return false;
    }
}

您沒有在AppClient.java初始化search

addVertexToGraph();
search = new Search(); // add this
int result = search.DFS(); //possibly add graph as a parameter here

也初始化vertexesStackSearch.java並刪除局部變量graph ,因為你想引用一個在AppClient.java

private Stack<Integer> vertexesStack = new Stack<>(); // add this
private int range;
private int scc;
// Graph graph; // delete this

由於您的代碼沒有package xyz; 在頂部,您可能還需要更改int DFS() -> int DFS(Graph graph) ,然后在AppClient.java中將圖形傳遞給對DFS()調用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM