optmized graph dfs