-
Notifications
You must be signed in to change notification settings - Fork 182
While loop DFS implementation #1483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1091,13 +1091,19 @@ def directional_dfs_traverse( | |
| nodes = set() | ||
| user_nodes = set() | ||
|
|
||
| def dfs_traverse(node: node.Node): | ||
| nodes.add(node) | ||
| for n in next_nodes_fn(node): | ||
| if n not in nodes: | ||
| dfs_traverse(n) | ||
| if node.user_defined: | ||
| user_nodes.add(node) | ||
| def dfs_traverse_iterative(start_node: node.Node): | ||
| """Iterative DFS to avoid recursion depth limits with large DAGs.""" | ||
| stack = [start_node] | ||
| nodes.add(start_node) | ||
| while stack: | ||
| n = stack.pop() | ||
| if n.user_defined: | ||
| user_nodes.add(n) | ||
| # reversed() preserves the same traversal order as the recursive version | ||
| for next_n in reversed(next_nodes_fn(n)): | ||
| if next_n not in nodes: | ||
| nodes.add(next_n) | ||
| stack.append(next_n) | ||
|
Comment on lines
+1099
to
+1106
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can lead to duplicate nodes being on stack I think because you don't mark the nodes as seen until you pop from the stack instead of marking them when you push onto the stack.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep, fixed. |
||
|
|
||
| missing_vars = [] | ||
| for var in starting_nodes: | ||
|
|
@@ -1108,7 +1114,7 @@ def dfs_traverse(node: node.Node): | |
| # if it's not in the runtime inputs, it's a properly missing variable | ||
| missing_vars.append(var) | ||
| continue # collect all missing final variables | ||
| dfs_traverse(self.nodes[var]) | ||
| dfs_traverse_iterative(self.nodes[var]) | ||
| if missing_vars: | ||
| missing_vars_str = ",\n".join(missing_vars) | ||
| raise ValueError(f"Unknown nodes [{missing_vars_str}] requested. Check for typos?") | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI, this dfs transversal order should in principle be different from the
iterativeone.Since we are doing DAGs I think this should be fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
made it the same.