Java实现图的深度优先遍历和广度优先遍历

 private int vertexSize;//顶点数量

    public int getVertexSize() {
        return vertexSize;
    }



    public void setVertexSize(int vertexSize) {
        this.vertexSize = vertexSize;
    }

    private int [] vertexs;//顶点数组
    public int[][]  matrix;
    public int[][] getMatrix() {
        return matrix;
    }


    public void setMatrix(int[][] matrix) {
        this.matrix = matrix;
    }

    private static final int MAX_WEIGHT = 1000;
    private boolean [] isVisited;
    public Graph(int vertextSize){
        this.vertexSize = vertextSize;
        matrix = new int[vertextSize][vertextSize];
        vertexs = new int[vertextSize];
        for(int i = 0;i<vertextSize;i++){
            vertexs[i] = i;
        }
        isVisited = new boolean[vertextSize];
    }

    /**  * 创建图的过程  */  public void createGraph(){
        int [] a1 = new int[]{0,1,5,MAX_WEIGHT,MAX_WEIGHT,MAX_WEIGHT,MAX_WEIGHT,MAX_WEIGHT,MAX_WEIGHT};
        int [] a2 = new int[]{1,0,3,7,5,MAX_WEIGHT,MAX_WEIGHT,MAX_WEIGHT,MAX_WEIGHT};
        int [] a3 = new int[]{5,3,0,MAX_WEIGHT,1,7,MAX_WEIGHT,MAX_WEIGHT,MAX_WEIGHT};
        int [] a4 = new int[]{MAX_WEIGHT,7,MAX_WEIGHT,0,2,MAX_WEIGHT,3,MAX_WEIGHT,MAX_WEIGHT};
        int [] a5 = new int[]{MAX_WEIGHT,5,1,2,0,3,6,9,MAX_WEIGHT};
        int [] a6 = new int[]{MAX_WEIGHT,MAX_WEIGHT,7,MAX_WEIGHT,3,0,MAX_WEIGHT,5,MAX_WEIGHT};
        int [] a7 = new int[]{MAX_WEIGHT,MAX_WEIGHT,MAX_WEIGHT,3,6,MAX_WEIGHT,0,2,7};
        int [] a8 = new int[]{MAX_WEIGHT,MAX_WEIGHT,MAX_WEIGHT,MAX_WEIGHT,9,5,2,0,4};
        int [] a9 = new int[]{MAX_WEIGHT,MAX_WEIGHT,MAX_WEIGHT,MAX_WEIGHT,MAX_WEIGHT,MAX_WEIGHT,7,4,0};

        matrix[0] = a1;
        matrix[1] = a2;
        matrix[2] = a3;
        matrix[3] = a4;
        matrix[4] = a5;
        matrix[5] = a6;
        matrix[6] = a7;
        matrix[7] = a8;
        matrix[8] = a9;
    }

    /**  * 获取某个顶点的出度  * @return  */  public int getOutDegree(int index){
        int degree = 0;
        for(int j = 0;j<matrix[index].length;j++){
            int weight = matrix[index][j];
            if(weight!=0&&weight!=MAX_WEIGHT){
                degree++;
            }
        }
        return degree;
    }



    /**  * 入度  * @return  */   /**  * 获取某个顶点的第一个邻接点  */  public int getFirstNeighbor(int index){
        for(int j = 0;j<vertexSize;j++){
            if(matrix[index][j]>0&&matrix[index][j]<MAX_WEIGHT){
                return j;
            }
        }
        return -1;
    }
//
//    /**
//     * 根据前一个邻接点的下标来取得下一个邻接点
//     * @param v1表示要找的顶点
//     * @param v2 表示该顶点相对于哪个邻接点去获取下一个邻接点
//     */
    public int getNextNeighbor(int v,int index){
        for(int j = index+1;j<vertexSize;j++){
            if(matrix[v][j]>0&&matrix[v][j]<MAX_WEIGHT){
                return j;
            }
        }
        return -1;
    }

    /**  * 图的深度优先遍历算法  */  private void depthFirstSearch(int i){
        isVisited[i] = true;
        int w = getFirstNeighbor(i);//
        while(w!=-1){
            if(!isVisited[w]){
                //需要遍历该顶点
                System.out.println("访问到了:"+w+"顶点");
                depthFirstSearch(w);
            }
            w = getNextNeighbor(i, w);//第一个相对于w的邻接点
        }
    }

    /**  * 对外公开的深度优先遍历  */   public void depthFirstSearch(){
        isVisited = new boolean[vertexSize];
        for(int i = 0;i<vertexSize;i++){
            if(!isVisited[i]){
                System.out.println("访问到了:"+i+"顶点");
                depthFirstSearch(i);
            }
        }
        isVisited = new boolean[vertexSize];
    }

    public void broadFirstSearch(){
        isVisited = new boolean[vertexSize];
        for(int i =0;i<vertexSize;i++){
            if(!isVisited[i]){
                broadFirstSearch(i);
            }
        }
    }

    /**  * 实现广度优先遍历  * @param i  */  private void broadFirstSearch(int i) {
      int u,w;
        LinkedList<Integer> queue = new LinkedList<Integer>();
        System.out.println("访问到:"+i+"顶点");
        isVisited[i] = true;
        queue.add(i);//第一次把v0加到队列
        while(!queue.isEmpty()){
            u = (Integer)(queue.removeFirst()).intValue();
            w = getFirstNeighbor(u);
            while(w!=-1){
                if(!isVisited[w]){
                    System.out.println("访问到了:"+w+"顶点");
                    isVisited[w] = true;
                    queue.add(w);
                }
                w = getNextNeighbor(u, w);
            }
        }

    }


    /**  * 图的广度优先搜索算法  */   /**  * 获取两个顶点之间的权值  * @return  */  public int getWeight(int v1,int v2){
        int weight = matrix[v1][v2];
        return weight == 0?0:(weight == MAX_WEIGHT?-1:weight);
    }


    public int[] getVertexs() {
        return vertexs;
    }

    public void setVertexs(int[] vertexs) {
        this.vertexs = vertexs;
    }

    原文作者:数据结构之图
    原文地址: https://blog.csdn.net/u012845099/article/details/78385247
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞