Depth first search algorithm on Python