如何将邻接表中的顶点标记为颜色?

时间:2019-06-26 17:37:15

标签: java breadth-first-search adjacency-list undirected-graph

我用Integers实现了邻接表,添加和删除诸如边的顶点对我来说很好用。 但是现在我想做一个BreadthFirstSearch为了得到一个BFSTree并且我需要标记顶点例如白色,灰色或黑色。

我知道它不能像我在代码中写下来那样真正地工作,只是想弄清楚我要做什么。但是,否则我将如何实施呢?我真的被困在这里。

public class UndirectedGraph {    
    private int vertexCount;    
    private LinkedList<Integer>[] adjacencyListArray;    
    private static UndirectedGraph graph;    
    private String color;    

    public UndirectedGraph(int n) {    
        this.vertexCount = n;    
        this.adjacencyListArray = new LinkedList[vertexCount];    
        for (int i = 0; i < vertexCount; i++) {    
            adjacencyListArray[i] = new LinkedList<>();    
        }    
    }    

    public void addEdge(int i, int j) {    
        graph.adjacencyListArray[i].add(j + 1);    
        graph.adjacencyListArray[j].add(i + 1);    
    }    

    public Graph getBFSTree(int s) {    
        for (int i = 0; i < vertexCount; i++) {    
            for (int j = 0; j < adjacencyListArray[i].size(); j++) {    
                adjacencyListArray[i].get(j).color = "white";    
            }    
        }    
    }    
}

0 个答案:

没有答案