-
Notifications
You must be signed in to change notification settings - Fork 274
Add Kahn's algorithm to graph.h #1089
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 Kahn's algorithm to graph.h #1089
Conversation
src/util/graph.h
Outdated
@@ -19,6 +19,9 @@ Author: Daniel Kroening, [email protected] | |||
#include <ostream> | |||
#include <cassert> | |||
#include <algorithm> | |||
#include <queue> | |||
|
|||
#include <util/invariant.h> |
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.
Nit picking: in util
we just use #include "invariant.h"
src/util/graph.h
Outdated
} | ||
|
||
// if no edge is left in the graph, it is acyclic | ||
bool is_dag=none_of( |
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.
Doesn't this need a std::
?
src/util/graph.h
Outdated
@@ -226,6 +229,12 @@ class grapht | |||
// return value: number of SCCs | |||
std::size_t SCCs(std::vector<node_indext> &subgraph_nr); | |||
|
|||
bool is_dag() |
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.
Should be marked const
src/util/graph.h
Outdated
{ | ||
return size()==0 || !topsort().empty(); | ||
} | ||
std::list<node_indext> topsort(); |
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.
Should be marked const
(plus add a blank line in between)
src/util/graph.h
Outdated
@@ -226,6 +229,12 @@ class grapht | |||
// return value: number of SCCs | |||
std::size_t SCCs(std::vector<node_indext> &subgraph_nr); | |||
|
|||
bool is_dag() | |||
{ | |||
return size()==0 || !topsort().empty(); |
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.
I believe we've now got empty()
src/util/graph.h
Outdated
{ | ||
std::list<node_indext> nodelist; | ||
std::queue<node_indext> indeg0_nodes; | ||
std::map<node_indext, size_t> in_deg; |
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.
I would recommend using a zero-initialised std::vector
of size nodes.size()
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.
ok, changed this, but then we assume node_indext
to be an integral value
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.
I believe this is a reasonable (likely necessary) assumption.
src/util/graph.h
Outdated
for(const auto &edge : out(source)) | ||
{ | ||
const node_indext target=edge.first; | ||
INVARIANT(in_deg[target]!=0, "In-degree of node cannot be 0."); |
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.
Nit picking/question: with throw
we even have a lint check that the message does not start with an uppercase character; should the same apply to invariants?
src/util/graph.h
Outdated
bool is_dag=none_of( | ||
in_deg.begin(), | ||
in_deg.end(), | ||
[](std::pair<node_indext, size_t> p) { return p.second>0; }); |
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.
Wouldn't bool is_dag=nodelist.size()==nodes.size();
also do the trick?
719d054
to
bdaba10
Compare
@tautschnig thank you for the comments, I have adapted the PR |
src/util/graph.h
Outdated
bool is_dag=(nodelist.size()==nodes.size()); | ||
|
||
// return empty list in case of cyclic graph | ||
if(!is_dag) |
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.
Nit picking: you might now get rid of the is_dag
temporary and use if(nodelist.size()!=nodes.size())
bdaba10
to
de657ed
Compare
@tautschnig thanks, I have simplified the |
This adds topological sorting to
graph.h
.