Question: Task:CompletetheBFSmethodintheprovidedcodeincompletecodeGraph.java fpublic void removeEdge (E eltfrom, E eltto) { if ( nodeMap . containskey (eltfrom) & & nodeMap . containskey (eltto) ) { GraphNode nfrom

Task:CompletetheBFSmethodintheprovidedcodeincompletecodeGraph.java

Task:CompletetheBFSmethodintheprovidedcodeincompletecodeGraph.java \fpublic void removeEdge (E eltfrom, E eltto) { if ( nodeMap. containskey (eltfrom) & & nodeMap . containskey (eltto) ) { GraphNodenfrom = getNode (eltfrom) ; GraphNode nto = getNode (eltto) ; if( nfrom. hasNeighbor (nto) ) { nfrom . removeEdge (nto ) ;w w public void makeUndirected( ) { Iterator> edgeIter = edges () ; while ( edgeIter . hasNext( ) ) { GraphEdge e= edgeIter . next( ) ; GraphNode from = e. from( ), to = e. to( ) ; if ( ! to. hasNeighbor

\fpublic void removeEdge (E eltfrom, E eltto) { if ( nodeMap . containskey (eltfrom) & & nodeMap . containskey (eltto) ) { GraphNode nfrom = getNode (eltfrom) ; GraphNode nto = getNode (eltto) ; if ( nfrom. hasNeighbor (nto) ) { nfrom . removeEdge (nto ) ; w w public void makeUndirected( ) { Iterator> edgeIter = edges ( ) ; while ( edgeIter . hasNext( ) ) { GraphEdge e = edgeIter . next( ) ; GraphNode from = e. from( ) , to = e. to( ) ; if ( ! to. hasNeighbor (from) ) to . addEdge ( from) ; w public boolean isUndirected( ) { Iterator> edgeIter = edges ( ) ; while ( edgeIter . hasNext ( ) ) { GraphEdge e = edgeIter . next( ) ; GraphNode from = e. from( ), to = e. to( ) ; if ( ! to. hasNeighbor (from) ) return false; return true;\f\fpublic Collection> nodes ( ) { return nodeMap . values ( ) ; } public Iterator> edges( ) { Iterator> it = new Iterator> ( ) { private Iterator> fromIter = null; private Iterator> toIter = null; private GraphNode from = null, to = null; private int state = 0; // start state, end state is 4 private final int open = 1, closed = 2; private int lock = open; private void step( ) { / / sets from and to fields boolean breakLoop = false; while ( ! breakLoop ) { switch ( state ) { case 0: fromIter = nodeMap . values ( ) . iterator ( ) ; if ( fromIter . hasNext( ) ) { from = fromIter . next ( ) ; toIter = from. neighbors ( ) ; state = 1; } else { state = 4; breakLoop = true; break; case 1: if ( tolter . hasNext( ) ) { to = toIter . next( ) ; state = 2; breakLoop = true; } else { state = 3;case 2: if ( tolter . hasNext( ) ) { to = toIter . next ( ) ; state = 2; breakLoop = true; } else { state = 3; } break; case 3: if ( fromIter . hasNext( ) ) { from = fromIter . next( ) ; toIter = from. neighbors ( ) ; state = 1; } else { state = 4; breakLoop = true; break; default: // include case 4: (== end state) breakLoop = true; break; w w w\fpublic static void main (String[ ] args) { Graph gl = null; String fname = "resources \\\\GraphTest- Integer . txt"; / /String fname = "resources/GraphTest- Integer. txt"; // Linux/Mac users may need to use this path for fname instead Scanner sonr = null; try { sonr = new Scanner (new File (fname ) ) ; catch (Exception e) { System. out . println( "Cannot open file "+fname ) ; System. exit(1) ; ScanGraph sg = new ScanGraph (new ScanInteger (sonr) ) ; sg. skipComment ( ) ; if ( sg. hasNext( ) ) g1 = sg. next ( ) ; System. out . println( ) ; System . out . println( "Is gl undirected? " + gl . isUndirected( ) ) ; System. out . printIn(gl) ; System. out . println ( ) ; System . out . printIn( "Is g undirected? " + g1 . isUndirected ( ) ) ; System . out . printin( ) ; g1 . makeUndirected( ) ; System . out . println(gl ) ; System. out . printIn ( ) ; System. out . println( "Is g undirected now? " + gl . isUndirected ( ) ) ; System . out . printin ( ) ; System. out . println( "BFS list = "+ gl.bfs(2) ) ; System. out . println( ) ; System. out . println ( "BFS previous map = "+ g1. bfsPrev (2) ) ; System . out . printin( ) ; System. out . println ( "==== = = = System. out . printIn( ) ; w w

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!