Add a utility for finding minimal topological sorts#6884
Merged
Conversation
Reuse the code implementing Kahn's topological sort algorithm with a new configuration that uses a min-heap to always choose the best available element. Also add wrapper utilities that can find topological sorts of graphs with arbitrary element types, not just indices.
kripken
approved these changes
Aug 29, 2024
src/support/topological_orders.h
Outdated
| // the indices of its children, which will appear after it in the order. | ||
| TopologicalOrders(const std::vector<std::vector<size_t>>& graph); | ||
| TopologicalOrders(const std::vector<std::vector<size_t>>& graph) | ||
| : TopologicalOrders(graph, false) {} |
| const std::vector<size_t>& operator*() const { | ||
| return TopologicalOrders::operator*(); | ||
| } | ||
| }; |
Member
There was a problem hiding this comment.
This constructor is identical to the parent, and this operator doesn't seem to modify anything either - I am sure I'm missing something?
Member
Author
There was a problem hiding this comment.
Nope, this is providing a small subset of the functionality of the superclass, packaged under a different name that is more appropriate for the common case of needing just one topological sort.
Member
There was a problem hiding this comment.
Ah, thanks... I was missing the word "private" on line 108. Makes sense now.
| Graph graph(3); | ||
| graph[2].push_back(1); | ||
| std::vector<size_t> expected{0, 2, 1}; | ||
| EXPECT_EQ(*MinTopologicalSort(graph), expected); |
Member
There was a problem hiding this comment.
Maybe add comments to these, e.g. for this one IIUC the point is to show that 2 is before 1, and the unconstrained 0 is smaller so it appears before them both.
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reuse the code implementing Kahn's topological sort algorithm with a new
configuration that uses a min-heap to always choose the best available
element.
Also add wrapper utilities that can find topological sorts of graphs
with arbitrary element types, not just indices.