-
Notifications
You must be signed in to change notification settings - Fork 745
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 a utility for finding minimal topological sorts #6884
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.
src/support/topological_orders.h
Outdated
@@ -36,7 +38,8 @@ struct TopologicalOrders { | |||
|
|||
// Takes an adjacency list, where the list for each vertex is a sorted list of | |||
// 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) {} |
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.
Maybe an enum for this?
const std::vector<size_t>& operator*() const { | ||
return TopologicalOrders::operator*(); | ||
} | ||
}; |
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.
This constructor is identical to the parent, and this operator doesn't seem to modify anything either - I am sure I'm missing something?
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.
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.
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.
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); |
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.
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.
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.