refactor the dfs cyclic search from recursion to iterative approach (#91042)
Follow up on PR #86511
Python's 1000 limit on recursion depth is not practical for us to run cyclic check on larger graphs. This refactor avoids that issue.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/91042
Approved by: https://github.com/kit1980