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

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

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

首先选取顶点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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值