Java实现图的遍历(深搜与广搜)

本文以邻接矩阵作为存储结构,用Java实现图的遍历,话不多说,先给出的图的结构,如下:
Java实现图的遍历(深搜与广搜)_第1张图片

1、深度优先搜索遍历
思想:
沿着树的深度遍历树的节点,尽可能深的搜索树的分支。当节点v的所有边都己被探寻过,搜索将回溯到发现节点v的那条边的起始节点。这一过程一直进行到已发现从源节点可达的所有节点为止。如果还存在未被发现的节点,则选择其中一个作为源节点并重复以上过程,整个进程反复进行直到所有节点都被访问为止。(百度百科)

代码如下:

package com.ds.graph;

public class DFSTraverse {
    // 构造图的边
    private int[][] edges = { { 0, 1, 0, 0, 0, 1, 0, 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 }, { 0, 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 } };
    // 构造图的顶点
    private String[] vertexs = { "A", "B", "C", "D", "E", "F", "G", "H", "I" };
    // 记录被访问顶点
    private boolean[] verStatus;
    // 顶点个数
    private int vertexsNum = vertexs.length;

    public void DFSTra() {
        verStatus = new boolean[vertexsNum];
        for (int i = 0; i < vertexsNum; i++) {
            if (verStatus[i] == false) {
                DFS(i);
            }
        }
    }

    // 递归深搜
    private void DFS(int i) {
        System.out.print(vertexs[i] + " ");
        verStatus[i] = true;
        for (int j = firstAdjVex(i); j >= 0; j = nextAdjvex(i, j)) {
            if (!verStatus[j]) {
                DFS(j);
            }
        }
    }

    // 返回与i相连的第一个顶点
    private int firstAdjVex(int i) {
        for (int j = 0; j < vertexsNum; j++) {
            if (edges[i][j] > 0) {
                return j;
            }
        }
        return -1;
    }

    // 返回与i相连的下一个顶点
    private int nextAdjvex(int i, int k) {
        for (int j = (k + 1); j < vertexsNum; j++) {
            if (edges[i][j] == 1) {
                return j;
            }
        }
        return -1;
    }

    // 测试
    public static void main(String[] args) {
        new DFSTraverse().DFSTra();
    }

}

2、广度优先搜索遍历
思想:
从根节点开始,沿着树的宽度、按照层次依次遍历树的节点;

代码如下:

package com.ds.graph;

import java.util.LinkedList;
import java.util.Queue;

public class BFSTraverse_0100 {
    // 构造图的边
    private int[][] edges = { { 0, 1, 0, 0, 0, 1, 0, 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 }, { 0, 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 } };
    // 构造图的顶点
    private String[] vertexs = { "A", "B", "C", "D", "E", "F", "G", "H", "I" };
    // 记录被访问顶点
    private boolean[] verStatus;
    // 顶点个数
    private int vertexsNum = vertexs.length;

    // 广搜
    private void BFS() {
        verStatus = new boolean[vertexsNum];
        Queue temp = new LinkedList();
        for (int i = 0; i < vertexsNum; i++) {
            if (!verStatus[i]) {
                System.out.print(vertexs[i] + " ");
                verStatus[i] = true;
                temp.offer(i);
                while (!temp.isEmpty()) {
                    int j = temp.poll();
                    for (int k = firstAdjvex(j); k >= 0; k = nextAdjvex(j, k)) {
                        if (!verStatus[k]) {
                            System.out.print(vertexs[k] + " ");
                            verStatus[k] = true;
                            temp.offer(k);
                        }
                    }
                }
            }
        }
    }

    // 返回与i相连的第一个顶点
    private int firstAdjvex(int i) {
        for (int j = 0; j < vertexsNum; j++) {
            if (edges[i][j] > 0) {
                return j;
            }
        }
        return -1;
    }

    // 返回与i相连的下一个顶点
    private int nextAdjvex(int i, int k) {
        for (int j = (k + 1); j < vertexsNum; j++) {
            if (edges[i][j] > 0) {
                return j;
            }
        }
        return -1;
    }

    // 测试
    public static void main(String args[]) {
        new BFSTraverse_0100().BFS();
    }
}

你可能感兴趣的:(algorithms)