Depth-first search algorithm implemented.