Skip to content

While loop DFS implementation#1483

Merged
skrawcz merged 2 commits intomainfrom
stefan/while-dfs
Mar 7, 2026
Merged

While loop DFS implementation#1483
skrawcz merged 2 commits intomainfrom
stefan/while-dfs

Conversation

@skrawcz
Copy link
Copy Markdown
Contributor

@skrawcz skrawcz commented Feb 17, 2026

This should help someone create a really large and deep graph.

Changes

  • deep core change to DFS implementation

How I tested this

  • unit test
  • ran a small benchmark -- no performance regressions.

Notes

Even though this is a pretty core change I think this is isolated enough that we are confident in this change.
Alternatively we could enable an environment variable to switch, but I don't see much value in that given that we're swapping the function stack for a regular stack.

Checklist

  • PR has an informative and human-readable title (this will be pulled into the release notes)
  • Changes are limited to a single goal (no scope creep)
  • Code passed the pre-commit check & code is left cleaner/nicer than when first encountered.
  • Any change in functionality is tested
  • New functions are documented (with a description, list of inputs, and expected output)
  • Placeholder code is flagged / future TODOs are captured in comments
  • Project documentation has been updated if adding/changing functionality.

Comment on lines +1097 to +1105
n = stack.pop()
if n in nodes:
continue
nodes.add(n)
if n.user_defined:
user_nodes.add(n)
for next_n in next_nodes_fn(n):
if next_n not in nodes:
stack.append(next_n)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, fixed.

@@ -1091,13 +1090,19 @@ def directional_dfs_traverse(
nodes = set()
user_nodes = set()

Copy link
Copy Markdown
Contributor

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 iterative one.

Since we are doing DAGs I think this should be fine.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made it the same.

skrawcz added 2 commits March 6, 2026 23:51
This should help someone create a really large and deep graph.

This needs more testing/eyes to think about impact.
So this ensures we don't visit things twice. It also
ensures that we have the same order of traversal as before.
I don't want to change the order of behavior if we don't
have to.
@skrawcz skrawcz force-pushed the stefan/while-dfs branch from 9777085 to ca3bcc9 Compare March 7, 2026 07:52
@skrawcz skrawcz marked this pull request as ready for review March 7, 2026 07:53
@skrawcz skrawcz requested a review from jernejfrank March 7, 2026 07:53
Copy link
Copy Markdown
Contributor

@jernejfrank jernejfrank left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool, looks good!

@skrawcz skrawcz merged commit a47fc42 into main Mar 7, 2026
7 checks passed
@skrawcz skrawcz deleted the stefan/while-dfs branch March 7, 2026 17:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants