DFS,BFS 深度優先遍歷,廣度優先遍歷

java實現DFS,BFS算法

源代碼
圖的結構java實現

public class DFSAndBFS {
    /**
     * BFS 只能遍歷連通圖
     *
     * @param graph 圖
     * @param i     起始訪問下標
     * @param flag  訪問標記數組
     * @param queue 訪問隊列
     */
    private static void BFS(GraphMatrix graph, int i, boolean[] flag, Queue<Integer> queue) {
        if (!flag[i]) {
            flag[i] = true;
            System.out.print(graph.vertexList.get(i) + " ");
            queue.offer(i);
            while (!queue.isEmpty()) {
                //取出隊列中結點
                int k = queue.poll();
                for (int j = 0; j < graph.getVertexNum(); j++) {
                    //訪問與k結點相連的所有結點,如果沒有被訪問,標記結點
                    if (graph.edges[k][j] == 1 && !flag[j]) {
                        flag[j] = true;
                        System.out.print(graph.vertexList.get(j) + " ");
                        //結點插入隊列
                        queue.offer(j);
                    }
                }
            }
        }
    }

    /**
     * BFS 可以遍歷非連通圖
     *
     * @param graph 圖
     */
    static void BFSTraverse(GraphMatrix graph) {
        boolean[] flag = new boolean[graph.getVertexNum()];
        Queue<Integer> queue = new LinkedList<>();
        for (int i = 0; i < graph.getVertexNum(); i++) {
            BFS(graph, i, flag, queue);
        }
    }

    /**
     * DFS 只能遍歷連通圖
     *
     * @param graph 圖
     * @param i     起始訪問下標
     * @param flag  訪問標記數組
     */
    private static void DFS(GraphMatrix graph, int i, boolean[] flag) {
        flag[i] = true;// 第i個頂點被訪問
        System.out.print(graph.vertexList.get(i) + " ");
        for (int j = 0; j < graph.getVertexNum(); j++) {
            if (!flag[j] && graph.edges[i][j] == 1) {
                DFS(graph, j, flag);
            }
        }
    }

    static void DFSTraverse(GraphMatrix graph) {
        boolean[] flag = new boolean[graph.getVertexNum()];
        for (int i = 0; i < graph.getVertexNum(); i++) {
            if (!flag[i]) {// 當前頂點沒有被訪問
                DFS(graph, i, flag);
            }
        }
    }

    /**
     * DFS非遞歸
     *
     * @param graph
     */
    static void DFS_Map(GraphMatrix graph) {
        boolean[] flag = new boolean[graph.getVertexNum()];
        LinkedList<Integer> stack = new LinkedList<>();
        for (int i = 0; i < graph.getVertexNum(); i++) {
            if (!flag[i]) {
                flag[i] = true;
                System.out.print(graph.vertexList.get(i) + " ");
                stack.push(i);
            }
            while (!stack.isEmpty()) {
                int k = stack.pop();
                for (int j = 0; j < graph.getVertexNum(); j++) {
                    if (graph.edges[k][j] == 1 && !flag[j]) {
                        flag[j] = true;
                        System.out.print(graph.vertexList.get(j) + " ");
                        stack.push(j);
                        break;
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        GraphMatrix<String> graph = new GraphMatrix<>("ABCDEFGHI");
        graph.addEdge("A", "B");
        graph.addEdge("A", "F");
        graph.addEdge("A", "G");
        graph.addEdge("B", "C");
        graph.addEdge("B", "G");
        graph.addEdge("B", "I");
        graph.addEdge("C", "D");
        graph.addEdge("C", "I");
        graph.addEdge("D", "E");
        graph.addEdge("D", "G");
        graph.addEdge("D", "H");
        graph.addEdge("D", "I");
        graph.addEdge("E", "F");
        graph.addEdge("E", "H");
        graph.addEdge("F", "G");
        graph.addEdge("G", "H");
        graph.information();
        System.out.println("DFS遞歸:");
        DFSTraverse(graph);
        System.out.println("\nDFS非遞歸:");
        DFS_Map(graph);
        System.out.println("\nBFS非遞歸:");
        BFSTraverse(graph);
    }
}

log

邊的權重表:
0 1 0 0 0 1 1 0 0 
1 0 1 0 0 0 1 0 1 
0 1 0 1 0 0 0 0 1 
0 0 1 0 1 0 1 1 1 
0 0 0 1 0 1 0 1 0 
1 0 0 0 1 0 1 0 0 
1 1 0 1 0 1 0 1 0 
0 0 0 1 1 0 1 0 0 
0 1 1 1 0 0 0 0 0 
DFS遞歸:
A B C D E F G H I 
DFS非遞歸:
A B C D E F G H I 
BFS非遞歸:
A B F G C I E D H 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章