filtering graph using dfs