build dfs function