Correct approach is depth first search, see dfs.py