DFS algorithm works for custom graphs