检查根节点剪切JAVA

时间:2014-04-14 00:55:44

标签: java algorithm graph graph-algorithm

我正在尝试更改此代码以检查根节点是否为剪切节点。如果根目录是DFS树中有多个子节点,则它是一个剪切节点。如果我们检测到根节点是剪切节点,则打印出来。我知道我需要更多的装饰,我只是在寻找一些意见。

CODE SO FAR:

public static void dfsDriver( AdjacencyListGraph g ) {

        startTimeClock = 1;

        v = some arbitrary vertex in the graph (such as vertex “A”)

       dfs( v );

    }



 private static void dfs(Vertex v) {

        v.setLabel(VISITED );

        v.put( START_TIME, startTimeClock++ );


        for (Edge e: graph.incidentEdges(v)) {

                Vertex w = graph.opposite(v, e);

                if ( w.get(DFS_STATUS) != VISITED ) {

                     w.put( PARENT, v );   

                     dfs(w); 

                }
                else {
                         ;   // edge e=(v,w) is either a dotted non-tree edge, or w is parent of v
                }
        }
}    

1 个答案:

答案 0 :(得分:1)

您可能只需要进行此测试:

boolean isCutRoot(Vertex root) {
    final Edge[] rootEdges = graph.incidentEdges(root);
    return rootEdges != null && rootEdges.length >= 2;
}

但是,如果从根的多个边可能指向同一个顶点,则需要修改上述方法以检查rootEdges中的边是否引用至少2个不同的顶点。

相关问题