java深度优先遍历

/*
  图的深度优先遍历

  已知用邻接矩阵A形式表示的无向图G,满足:
  如果图G中存在边(vi,vj),则A[i][j]=1,否则A[i][j]=0。
  顶点编号从1到n,从1号顶点开始以深度优先遍历方式输出图G中的每个顶点(有多个邻接点时按照序号从小到大顺序访问),对于非连通图按照顶点编号顺序选择下一个未遍历顶点继续深度优先遍历,直到图中所有顶点都被输出为止。
  Input
  按行排列的邻接矩阵A,矩阵每行元素占用一行,元素间用一个空格间隔,相邻矩阵间用一个空行间隔,处理到文件结束位置为止。
  Output
  从1号顶点开始按照深度优先遍历顺序输出图G中的每个顶点,顶点编号间用一个空格间隔,每个无向图的输出占用一行,最后一行输出后换行。
  Sample Input
  0 1 1 0 0
  1 0 0 0 1
  1 0 0 1 1
  0 0 1 0 1
  0 1 1 1 0

  Sample Output
  1 2 5 3 4
 */
public class DFSGraph {

    public static void dfs(int[][] matrix) {
        int[] visited = new int[matrix.length];
        ArrayStack<Integer> stack = new ArrayStack<>();
        int unvisit;
        while ((unvisit = getUnVisted(visited)) >= 0) {
            visited[unvisit] = 1;  //将当前的未被访问节点的状态改为已访问,并入栈
            System.out.print((unvisit + 1) + "\t");
            stack.push(unvisit);
            while (!stack.isEmpty()) {
                Integer index = stack.peek();  //查看栈顶的节点序号,从此节点开始寻找与该节点邻接的第一个未被访问节点
                boolean found = false;
                for (int i = 0; i < matrix[index].length; i++) {
                    if (index != i && visited[i] == 0 && matrix[index][i] == 1) {
                        //找到一个未被访问的邻接节点,将该节点标记为已访问,并入栈,从该节点开始继续深度搜索
                        visited[i] = 1;
                        System.out.print((i + 1) + "\t");
                        stack.push(i);
                        found = true;
                        break;
                    }
                }
                //found = false 找不到未访问的和当前节点邻接的所有节点了,此时将当前节点出栈,退到上个节点
                if (!found) {
                    stack.pop();
                }
            }
        }
    }

    public static int getUnVisted(int[] visited) {
        int index = -1;
        for (int i = 0; i < visited.length; i++) {
            if (visited[i] == 0) {
                index = i;
                break;
            }
        }
        return index;
    }

    public static void main(String[] args) {

        //图的邻接矩阵
        int[][] matrix = new int[][]{
                {0, 1, 1, 0, 0},
                {1, 0, 0, 0, 1},
                {1, 0, 0, 1, 1},
                {0, 0, 1, 0, 1},
                {0, 1, 1, 1, 0}};
        dfs(matrix);

    }


    private static class ArrayStack<T> {
        private int capacity = 8;//栈的默认容量
        private int size;//栈的大小

        private Object[] array;

        public ArrayStack() {
            this.array = new Object[this.capacity];
            this.size = 0;
        }

        public ArrayStack(int capacity) {
            this.capacity = capacity;
            this.array = new Object[this.capacity];
            this.size = 0;
        }

        public void clear() {
            this.size = 0;
            this.array = new Object[capacity];
        }

        public boolean isEmpty() {
            return size == 0;
        }

        public T peek() {
            if (isEmpty()) {
                return null;
            }
            return (T) this.array[this.size - 1];
        }

        public T pop() {
            if (isEmpty()) {
                return null;
            }
            T t = (T) this.array[size - 1];
            array[this.size - 1] = null;
            this.size--;
            return t;
        }

        public void push(T elment) {
            if (this.size >= this.capacity) {
                addStackCapacity();
            }
            this.array[size] = elment;
            this.size++;
        }

        public void addStackCapacity() {  //栈的容量不够则扩充栈的容量
            this.capacity = this.capacity * 2;
            Object[] newArray = new Object[this.capacity];
            System.arraycopy(this.array, 0, newArray, 0, this.array.length);
            this.array = newArray;
        }

        public int size() {
            return this.size;
        }
    }

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值