implementation of DFS