0

测试用例是:有3个顶点1,2,3这样

边缘:源-目的地

1-2
1-3
2-3

随着 dfs 从 1 到 2 再到 3 。以下是发现和低谷时期:

1 discT:0,lowT:0;

2 discT:1,lowT:1;

3 discT:2,lowT:2;

由于 2 的光盘时间小于 3 的低时间。由于不应该是的定理,2成为关节点。

难道我做错了什么 。请解释。下面是我的 dfs 函数->

public void dfs(){
             ArrayDeque<vertex> st=new ArrayDeque<>();
             st.push(vertexList.get(0));
             int pt=1;
            vertexList.get(0).discTime=0;
            vertexList.get(0).lowTime=0;
            vertexList.get(0).visited=true;
            int numberOfverticesCovered=0;
             while(!st.isEmpty()){
                 vertex v=st.peek();
                 System.out.println("considering "+v.label);
                 vertex p=getAdjacent(v);
                 if(p==null)
                 {
                     System.out.println("left with no unvisited adjacent vertices "+v.label);
                     if(v!=vertexList.get(0)){
                         LinkedList<edge> le=adjList.get(v.label-1);
                         for (edge e : le)
                         {
                                 if(v.discTime<=e.destination.lowTime)
                                 {
                                    artPoints.add(v);
                                     System.out.println("new articulation point found "+v.label+" for edge "+e.source.label+" and "+e.destination.label);
                                     System.out.println("disc time of "+v.label+"  is "+v.discTime+" and low time is "+v.lowTime);
                                     System.out.println("disc time of adj "+e.destination.label+"  is "+e.destination.discTime+" and low time is "+e.destination.lowTime);
                                    break;
                                 }
                                    v.lowTime=Math.min(v.lowTime, e.destination.lowTime);
                                    System.out.println("new low time of "+v.label+"  is "+v.lowTime);
                         }
                     }
                     numberOfverticesCovered+=1;
                     st.pop();
                 }
                 else
                 {
                     v.children+=1;
    //                 System.out.println("adding child "+p.label+" to parent "+v.label);
                     p.discTime=pt;
                     p.lowTime=pt;
                     p.parent=v;
                     st.push(p);
                     pt+=1;
                 }
                 if(st.isEmpty()&& numberOfverticesCovered!=vertexList.size()){
                     for (vertex object : vertexList) {
                         if(!object.visited)
                         {
                             object.discTime=pt;
                             object.lowTime=pt;
                             object.visited=true;
                             st.push(object);
                             break;
                         }
                     }
                 }
             }

             if(vertexList.get(0).children>1 ) //  put in check for back edge for the other children so that they are not connected to each other.
             {             
                 artPoints.add(vertexList.get(0));
                 System.out.println("added root as an articulation point and it has "+vertexList.get(0).children);
             }

         } 

    }
4

0 回答 0