The primitive graph operations that the algorithm uses are to enumerate the vertices of the graph, to store data per vertex, to enumerate the out-neighbours of a vertex, and to enumerate the in-neighbours of a vertex ; however the last can be done without, at the price of constructing a representation of the transpose graph during the forward traversal phase. The only additional data structure needed by the algorithm is an ordered listL of graph vertices, that will grow to contain each vertex once. If strong components are to be represented by appointing a separate root vertex for each component, and assigning to each vertex the root vertex of its component, then Kosaraju's algorithm can be stated as follows.
For each vertex u of the graph, mark u as unvisited. Let L be empty.
For each vertex u of the graph do Visit, where Visit is the recursive subroutine:
: If u is unvisited then:
:# Mark u as visited.
:# For each out-neighbour v of u, do Visit.
:# Prepend u to L.
: Otherwise do nothing.
For each element u of L in order, do Assign where Assign is the recursive subroutine:
: If u has not been assigned to a component then:
:# Assign u as belonging to the component whose root is root.
:# For each in-neighbour v of u, do Assign.
: Otherwise do nothing.
Trivial variations are to instead assign a component number to each vertex, or to construct per-component lists of the vertices that belong to it. The unvisited/visited indication may share storage location with the final assignment of root for a vertex. The key point of the algorithm is that during the first traversal of the graph edges, vertices are prepended to the list L in post-order relative to the search tree being explored. This means it does not matter whether a vertex v was first Visited because it appeared in the enumeration of all vertices or because it was the out-neighbour of another vertex u that got Visited; either way v will be prepended to L before u is, so if there is a forward path from u to v then u will appear before v on the final list L. As given above, the algorithm for simplicity employs depth-first search, but it could just as well use breadth-first search as long as the post-order property is preserved. The algorithm can be understood as identifying the strong component of a vertex u as the set of vertices which are reachable from u both by backwards and forwards traversal. Writing for the set of vertices reachable from by forward traversal, for the set of vertices reachable from by backwards traversal, and for the set of vertices which appear strictly before on the list L after phase 2 of the algorithm, the strong component containing a vertex appointed as root is Set intersection is computationally costly, but it is logically equivalent to a double set difference, and since it becomes sufficient to test whether a newly encountered element of has already been assigned to a component or not.