public boolean isConnected ( ) { this . visited = new HashMap < DensityGrid , Boolean > ( ) ; Iterator < DensityGrid > initIter = this . grids . keySet ( ) . iterator ( ) ; DensityGrid dg ; if ( initIter . hasNext ( ) ) { dg = initIter . next ( ) ; visited . put ( dg , this . grids . get ( dg ) ) ; boolean changesMade ; do { changesMade = false ; Iterator < Map . Entry < DensityGrid , Boolean > > visIter = this . visited . entrySet ( ) . iterator ( ) ; HashMap < DensityGrid , Boolean > toAdd = new HashMap < DensityGrid , Boolean > ( ) ; while ( visIter . hasNext ( ) && toAdd . isEmpty ( ) ) { Map . Entry < DensityGrid , Boolean > toVisit = visIter . next ( ) ; DensityGrid dg2V = toVisit . getKey ( ) ; Iterator < DensityGrid > dg2VNeighbourhood = dg2V . getNeighbours ( ) . iterator ( ) ; while ( dg2VNeighbourhood . hasNext ( ) ) { DensityGrid dg2VN = dg2VNeighbourhood . next ( ) ; if ( this . grids . containsKey ( dg2VN ) && ! this . visited . containsKey ( dg2VN ) ) toAdd . put ( dg2VN , this . grids . get ( dg2VN ) ) ; } } if ( ! toAdd . isEmpty ( ) ) { this . visited . putAll ( toAdd ) ; changesMade = true ; } } while ( changesMade ) ; } if ( this . visited . size ( ) == this . grids . size ( ) ) { //System.out.println("The cluster is still connected. "+this.visited.size()+" of "+this.grids.size()+" reached."); return true ; } else { //System.out.println("The cluster is no longer connected. "+this.visited.size()+" of "+this.grids.size()+" reached."); return false ; } }
Tests a grid cluster for connectedness according to Definition 3 . 4 Grid Group from Chen and Tu 2007 .