0
点赞
收藏
分享

微信扫一扫

日撸java 三百行 (day 23 )图的深度优先遍历

安七月读书 2022-04-27 阅读 84

深度优先遍历可以借用一个辅助栈:

今天的测试用例(非强连通图):

首先选取顶点D为初始顶点:

接着根据邻接矩阵的信息把E压栈:

由于D已经被访问过,并且E没有与其余节点相连接,将E出栈,D同理:

 等D出栈后,队列为空,但是标记数组显示还有顶点未被访问,则程序接着运行:

 

 代码及运行效果:

    /**
     *********************
     * Depth first traversal.
     *
     * @param paraStartIndex The start index.
     * @param flag the isvisited flag
     * @return The sequence of the visit.
     *********************
     */
    public String depthFirstTraversal(int paraStartIndex,boolean flag[]){
        ObjectStack tempStack=new ObjectStack();
        String resultString="";
        int tempNumNodes=connectivityMatrix.getRows();
        flag[paraStartIndex]=true;
        resultString+=paraStartIndex+" ";
        tempStack.push(paraStartIndex);
        int tempIndex=paraStartIndex;
        int tempNext;
        Integer tempInteger;
        while (true){
           tempNext=-1;
           for(int i=0;i<tempNumNodes;++i){
               if(flag[i]){
                   continue;
               }//Of if

               if(connectivityMatrix.getData()[tempIndex][i]==0){
                   continue;
               }//Of if

               tempStack.push(i);
               flag[i]=true;
               tempNext=i;
               resultString+=i+" ";
               break;
           }//of for i
            if(tempNext==-1){
                tempInteger=(Integer) tempStack.pop();
                if(tempStack.isempty()){
                   break;
                }
                else {
                    tempInteger=(Integer) tempStack.pop();
                    tempIndex=tempInteger.intValue();
                    tempStack.push(tempInteger);
                }//Of if

            }
            else {tempIndex=tempNext;}
        }//Of if

        return resultString;
    }//Of depthFirstTraversal

    /**
     *********************
     * Unit test for depthFirstTraversal.
     *********************
     */
    public static void depthFirstTraversalTest(){
        int[][] tempMatrix = { { 0, 1, 1, 0, 0 }, { 1, 0, 1, 0 ,0 }, { 1, 1, 0, 0 ,0}, { 0, 0, 0, 0 ,1},{0, 0, 0, 1, 0} };
        Graph tempGraph = new Graph(tempMatrix);
        System.out.println(tempGraph);
        boolean flag[]=new boolean[tempGraph.connectivityMatrix.getData().length];
        String tempSequence = "";
        try {
            tempSequence = tempGraph.depthFirstTraversal(3,flag);
        } catch (Exception ee) {
            System.out.println(ee);
        } // Of try.
        for(int i=0;i<tempGraph.connectivityMatrix.getData().length;++i){
            if(!flag[i]){
                tempSequence+=tempGraph.depthFirstTraversal(i,flag);
            }//of if
        }//of for i
        System.out.println("The depth first order of visit: " + tempSequence);
    }// of depthFirstTraversalTest

    /**
     * ********************
     * The entrance of the program.
     *
     * @param args Not used now.
     * ********************
     */
    public static void main(String args[]) {
        depthFirstTraversalTest();
    }// Of main

 

 

 

 总结:广度优先用队列,深度优先使用栈,需要注意的是一旦找到与当前顶点连通的顶点后就得break当前循环。

举报

相关推荐

0 条评论