# From CLR Algorithms book
DFS(Graph: G):
# initialization
Forall vertices v in V:
set v.color to white
set v.parent to :none:
set time to 0
# main loop
Forall vertices v in V:
if v.color is white then DFS_Visit(v)
end # DFS
DFS_Visit(Vertex: v):
# discovery activities
set v.color to grey
increment time
set v.discover to time
# search neighbors
Forall vertices u neighbors of v: # i.e. v->u edge
if u.color is white:
# u is discovered
set u.parent to v # this is done here because we
# know v and because tree roots
# have no parents
DFS_Visit(u) # recursion!
# finishing activities
set v.color to black
increment time
set v.finish to time
end # DFS_Visit