public boolean isConnectedInDirection ( DiGraphNode < N , E > dNode1 , Predicate < E > edgeMatcher , DiGraphNode < N , E > dNode2 ) { // Verify the nodes. List < DiGraphEdge < N , E > > outEdges = dNode1 . getOutEdges ( ) ; int outEdgesLen = outEdges . size ( ) ; List < DiGraphEdge < N , E > > inEdges = dNode2 . getInEdges ( ) ; int inEdgesLen = inEdges . size ( ) ; // It is possible that there is a large assymmetry between the nodes, so pick the direction // to search based on the shorter list since the edge lists should be symmetric. if ( outEdgesLen < inEdgesLen ) { for ( int i = 0 ; i < outEdgesLen ; i ++ ) { DiGraphEdge < N , E > outEdge = outEdges . get ( i ) ; if ( outEdge . getDestination ( ) == dNode2 && edgeMatcher . apply ( outEdge . getValue ( ) ) ) { return true ; } } } else { for ( int i = 0 ; i < inEdgesLen ; i ++ ) { DiGraphEdge < N , E > inEdge = inEdges . get ( i ) ; if ( inEdge . getSource ( ) == dNode1 && edgeMatcher . apply ( inEdge . getValue ( ) ) ) { return true ; } } } return false ; }
DiGraphNode look ups can be expensive for a large graph operation prefer this method if you have the DiGraphNodes available .