Skip to content
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

Add dfs-search #456

Merged
merged 22 commits into from
Jan 10, 2022
Merged

Add dfs-search #456

merged 22 commits into from
Jan 10, 2022

Conversation

georgios-ts
Copy link
Collaborator

Widely used graph libraries like Boost Graph Library and graph-tool
provide the ability to insert callback functions at specified event
points for many common graph search algorithms. petgraph has a similar
concept in petgraph::visit::depth_first_search function. This commit
implements an iterative version of depth_first_search, that will be
used in a follow-up in the pending PRs #444, #445. At the same time it
exposes this new functionality in Python by letting users subclassing
retworkx.visit.DFSVisitor and provide their own implementation for
the appropriate callback functions.

The benefit is less memory consumption since we avoid storing the results
but rather let the user take the desired action at specified points. For
example, if a user wants to process the nodes in dfs-order, we don't need
to create a new list with all the graph nodes in dfs-order but rather the user
can process a node on the fly. We can (probably) leverage this approach
in other algorithms as an alternative for our inability to provide "real" python
iterators.

Widely used graph libraries like Boost Graph Library and graph-tool
provide the ability to insert callback functions at specified event
points for many common graph search algorithms. petgraph has a similar
concept in `petgraph::visit::depth_first_search` function. This commit
implements an iterative version of `depth_first_search`, that will be
used in a follow-up in the pending PRs Qiskit#444, Qiskit#445. At the same time it
exposes this new functionality in Python by letting users subclassing
`retworkx.visit.DFSVisitor` and provide their own implementation for
the appropriate callback functions.

The benefit is less memory consumption since we avoid storing the results
but rather let the user take the desired action at specified points. For
example, if a user wants to process the nodes in dfs-order, we don't need
to create a new list with all the graph nodes in dfs-order but rather the user
can process a node on the fly. We can (probably) leverage this approach
in other algorithms as an alternative for our inability to provide "real" python
iterators.
@coveralls
Copy link

coveralls commented Sep 27, 2021

Pull Request Test Coverage Report for Build 1678805145

  • 132 of 138 (95.65%) changed or added relevant lines in 4 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.03%) to 98.479%

Changes Missing Coverage Covered Lines Changed/Added Lines %
retworkx-core/src/traversal/dfs_visit.rs 73 79 92.41%
Totals Coverage Status
Change from base Build 1678440305: -0.03%
Covered Lines: 11399
Relevant Lines: 11575

💛 - Coveralls

Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

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

I really like this idea. I was thinking about doing something similar for iterating over a dag in topological order (which terra does internally for a bunch of operations). But having this as a starting pattern lets us expose it for that and BFS which will give a lot of flexibility. I haven't reviewed the code in detail yet but the only other thing missing is some test coverage of the new functionality.

@georgios-ts
Copy link
Collaborator Author

Yeah, test coverage is certainly missing. I was thinking that depth_first_search will be covered for the most part after i use it in #444, #445, but in any case i will go ahead and add some tests.

@mtreinish mtreinish added this to the 0.11.0 milestone Oct 2, 2021
georgios-ts added a commit to georgios-ts/retworkx that referenced this pull request Oct 20, 2021
This commit implements a `breadth_first_search` that provides
the ability to insert callback functions at specified event points.
Python users should subclass `retworkx.visit.BFSVisitor` and overwrite
the appropriate callback functions.

Similar to Qiskit#456
This was referenced Oct 20, 2021
mtreinish added a commit that referenced this pull request Nov 17, 2021
* Add bfs-search
This commit implements a `breadth_first_search` that provides
the ability to insert callback functions at specified event points.
Python users should subclass `retworkx.visit.BFSVisitor` and overwrite
the appropriate callback functions.

Similar to #456

* update docs from code review

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

* docs

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

* docs + a comment in the code

* pseudo-code is not a doc test

* fix typo in docs

* do not raise if StopSearch exception

* more tests

* black

* add missing example from docs

* black again

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
e-eight pushed a commit to e-eight/retworkx that referenced this pull request Dec 5, 2021
* Add bfs-search
This commit implements a `breadth_first_search` that provides
the ability to insert callback functions at specified event points.
Python users should subclass `retworkx.visit.BFSVisitor` and overwrite
the appropriate callback functions.

Similar to Qiskit#456

* update docs from code review

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

* docs

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

* docs + a comment in the code

* pseudo-code is not a doc test

* fix typo in docs

* do not raise if StopSearch exception

* more tests

* black

* add missing example from docs

* black again

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
@mtreinish
Copy link
Member

I think this needs a rebase since #469 has merged and there is some overlap with that. I'd like to get this and #489 in for 0.11.0 which I'm hoping we can release in the first half of this month

Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

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

Overall this LGTM, thanks for updating it and I'm sorry about the slow review. I think there are 2 minor things inline one around a duplicated macro definition and the other about a missing link to the upstream dfsevent struct in petgraph to show where things came from on it (since it'll have separate documentation). Other than that I think this is ready to go.

retworkx-core/src/traversal/dfs_visit.rs Outdated Show resolved Hide resolved
retworkx-core/src/traversal/dfs_visit.rs Show resolved Hide resolved
@mtreinish mtreinish merged commit 89277f0 into Qiskit:main Jan 10, 2022
@IvanIsCoding IvanIsCoding mentioned this pull request Jan 11, 2022
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.

3 participants