简体   繁体   中英

Check root node cut JAVA

I am trying to alter this code to check whether the root node was a cut node. A root is a cut node if-and-only-if it has more than one child in the DFS tree. And print out if we detect that the root node is a cut node. I know I will need some more decorations, I was just looking for some input.

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
                }
        }
}    

You may just need this test:

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

However, if it is possible for multiple edges from the root to point to the same vertex, you need to modify the above method to check that the edges in rootEdges reference at least 2 different vertices.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM