This implementation runs depth-first search from each vertex. The constructor takes time proportional to
V(V + E) (in the worst case) and uses space proportional to V2, where
V is the number of vertices and E is the number of edges.
For large digraphs, you may want to consider a more sophisticated algorithm.
Nuutila proposes two algorithm for the problem (based on strong
components and an interval representation) that runs in E + V time on typical digraphs.
This implementation runs depth-first search from each vertex. The constructor takes time proportional to V(V + E) (in the worst case) and uses space proportional to V2, where V is the number of vertices and E is the number of edges.
For large digraphs, you may want to consider a more sophisticated algorithm. Nuutila proposes two algorithm for the problem (based on strong components and an interval representation) that runs in E + V time on typical digraphs.