Java深度优先遍历和广度优先遍历邻接矩阵算法-数据结构和算法-07-图

无向图

在这里插入图片描述
使用邻接矩阵顺序存储此无向图
先构造存储,在遍历

深度优先遍历

深度优先遍历(Depth_First_Search),也有称为深度优先搜索,简称为DFS

深度优先搜索的过程类似于树的先序遍历,首先从例子中体会深度优先搜索。
根据上边的过程,可以得到图 1 通过深度优先搜索获得的顶点的遍历次序为:
V1 -> V2 -> V4 -> V8 -> V5 -> V3 -> V6 -> V7
所谓深度优先搜索,是从图中的一个顶点出发,每次遍历当前访问顶点的临界点,一直到访问的顶点没有未被访问过的临界点为止。然后采用依次回退的方式,查看来的路上每一个顶点是否有其它未被访问的临界点。访问完成后,判断图中的顶点是否已经全部遍历完成,如果没有,以未访问的顶点为起始点,重复上述过程。
深度优先搜索是一个不断回溯的过程。

广度优先搜索

广度优先搜索类似于树的层次遍历。从图中的某一顶点出发,遍历每一个顶点时,依次遍历其所有的邻接点,然后再从这些邻接点出发,同样依次访问它们的邻接点。按照此过程,直到图中所有被访问过的顶点的邻接点都被访问到。

最后还需要做的操作就是查看图中是否存在尚未被访问的顶点,若有,则以该顶点为起始点,重复上述遍历的过程。

还拿图 1 中的无向图为例,假设 V1 作为起始点,遍历其所有的邻接点 V2 和 V3 ,以 V2 为起始点,访问邻接点 V4 和 V5 ,以 V3 为起始点,访问邻接点 V6 、 V7 ,以 V4 为起始点访问 V8 ,以 V5 为起始点,由于 V5 所有的起始点已经全部被访问,所有直接略过, V6 和 V7 也是如此。
以 V1 为起始点的遍历过程结束后,判断图中是否还有未被访问的点,由于图 1 中没有了,所以整个图遍历结束。遍历顶点的顺序为:
V1 -> V2 -> v3 -> V4 -> V5 -> V6 -> V7 -> V8

邻接矩阵深度和广度优先遍历

直接上代码

package com.my.data.structure;

import java.util.*;

/**
 * 图邻接矩阵深度优先遍历和广度优先遍历
 */
public class GraphTest {
    public boolean[] isVisit;           //是否被访问过,深度优先
    public boolean[] bfsVisit;          //是否被访问过,广度优先

    public class Graph {
        public List<String> vertex;     //顶点集合
        public int[][] matrix;          //邻接矩阵集合
        public int edge;                //边数

        /**
         * 构造
         *
         * @param n 顶点数
         */
        public Graph(int n) {
            vertex = new ArrayList<String>(n);
            matrix = new int[n][n];
            isVisit = new boolean[n];
            bfsVisit = new boolean[n];
        }

        /**
         * 添加节点
         *
         * @param v 节点名称
         */
        public void addVertex(String v) {
            vertex.add(v);
        }

        /**
         * 构建邻接矩阵数组
         *
         * @param i
         * @param j
         * @param val
         */
        public void addMatrix(int i, int j, int val) {
            this.matrix[i][j] = val;
            this.matrix[j][i] = val;
        }

        public int[][] getMatrix() {
            return this.matrix;
        }
    }

    /**
     * 创建图
     *
     * @param n
     * @return
     */
    public Graph createGraph(int n) {
        return new Graph(n);
    }

    /**
     * 邻接矩阵顺序存储
     *
     * @param graph
     */
    public void createMatrix(Graph graph) {
        /**
         *    V1 V2 V3 V4 V5 V6 V7 V8
         * V1 0  1  1  0  0  0  0  0
         * V2 1  0  0  1  1  0  0  0
         * V3 1  0  0  0  0  1  1  0
         * V4 0  1  0  0  0  0  0  1
         * V5 0  1  0  0  0  0  0  1
         * V6 0  0  1  0  0  0  1  0
         * V7 0  0  1  0  0  1  0  0
         * V8 0  0  0  1  1  0  0  0
         */
        graph.addMatrix(0, 1, 1);
        graph.addMatrix(0, 2, 1);
        graph.addMatrix(1, 0, 1);
        graph.addMatrix(1, 3, 1);
        graph.addMatrix(1, 4, 1);
        graph.addMatrix(2, 0, 1);
        graph.addMatrix(2, 5, 1);
        graph.addMatrix(2, 6, 1);
        graph.addMatrix(3, 1, 1);
        graph.addMatrix(3, 7, 1);
        graph.addMatrix(4, 1, 1);
        graph.addMatrix(4, 7, 1);
        graph.addMatrix(5, 2, 1);
        graph.addMatrix(5, 6, 1);
        graph.addMatrix(6, 2, 1);
        graph.addMatrix(6, 5, 1);
        graph.addMatrix(7, 3, 1);
        graph.addMatrix(7, 4, 1);
    }

    /**
     * 邻接矩阵深度优先遍历
     *
     * @param graph
     * @param i
     */
    public void dfsSort(Graph graph, int i) {
        if (isVisit[i]) return;
        isVisit[i] = true;
        System.out.print(graph.vertex.get(i) + " ");
        int[][] tmp = graph.getMatrix();
        for (int j = 0; j < graph.vertex.size(); j++) {
            if (tmp[i][j] == 1 && !isVisit[j]) dfsSort(graph, j);
        }
    }

    /**
     * 邻接矩阵广度优先遍历
     *
     * @param graph
     */
    public void bfsSort(Graph graph, int i) {
        if (bfsVisit[i]) return;
        bfsVisit[i] = true;
        System.out.print(graph.vertex.get(i) + " ");
        Queue<Integer> queue = new LinkedList();
        queue.add(i);
        while (!queue.isEmpty()) {
            int idx = queue.poll();
            for (int j = 0; j < graph.vertex.size(); j++) {
                if (graph.getMatrix()[idx][j] == 1 && !bfsVisit[j]) {
                    bfsVisit[j] = true;
                    System.out.print(graph.vertex.get(j) + " ");
                    queue.add(j);
                }
            }
        }
    }

    public static void main(String[] args) {
        int n = 8;
        String[] vertexs = {"V1", "V2", "V3", "V4", "V5", "V6", "V7", "V8"};
        GraphTest graphTest = new GraphTest();
        Graph graph = graphTest.createGraph(n);
        for (int i = 0; i < vertexs.length; i++) {
            graph.addVertex(vertexs[i].toString());
        }
        graphTest.createMatrix(graph);
        /*int[][] data = graph.getMatrix();
        //System.out.println(Arrays.deepToString(data));
        //测试邻接矩阵二维数据
        for (int[] tmp : data) {
            for (int x : tmp) {
                System.out.print(x + " ");
            }
            System.out.println();
        }
        */
        System.out.println("邻接矩阵深度优先遍历");
        List<String> vertexList = graph.vertex;
        for (int i = 0; i < vertexList.size(); i++) {
            graphTest.dfsSort(graph, i);
        }
        System.out.println("");
        System.out.println("邻接矩阵广度优先遍历");
        for (int i = 0; i < vertexList.size(); i++) {
            graphTest.bfsSort(graph, i);
        }
    }
}

结果显示

邻接矩阵深度优先遍历
V1 V2 V4 V8 V5 V3 V6 V7 
邻接矩阵广度优先遍历
V1 V2 V3 V4 V5 V6 V7 V8 
Process finished with exit code 0
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南巷Dong

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值