in src/main/java/org/apache/commons/graph/scc/KosarajuSharirAlgorithm.java [176:220]
private void searchRecursive( final DirectedGraph<V, E> g, final V source,
final Collection<V> coll, final Set<V> visited,
final boolean forward )
{
final LinkedList<V> stack = new LinkedList<V>();
stack.addLast( source );
while ( !stack.isEmpty() )
{
final V v = stack.removeLast();
// if the vertex has already been visited it can be put into the
// collection, as we are now finished expanding this vertex
// the if takes both cases into account:
// * step1: forward && visited.contains(v)
// * step2: !forward && !visited.contains(v)
if ( ! ( forward ^ visited.contains( v ) ) )
{
coll.add( v );
continue;
}
// add the current vertex to the stack, so it is visited again
// when all connected vertices have been visited
stack.addLast( v );
if ( forward )
{
visited.add( v );
}
else
{
visited.remove( v );
}
// add all not yet visited vertices that can be reached from this
// vertex to the stack
for ( V w : g.getOutbound( v ) )
{
if ( ! ( forward ^ ! visited.contains( w ) ) )
{
stack.addLast( w );
}
}
}
}