Skip to content

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

Merged
merged 1 commit into from
Jul 15, 2017

Conversation

mgudemann
Copy link
Contributor

This adds topological sorting to graph.h.

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>
Copy link
Collaborator

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(
Copy link
Collaborator

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()
Copy link
Collaborator

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();
Copy link
Collaborator

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();
Copy link
Collaborator

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;
Copy link
Collaborator

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()

Copy link
Contributor Author

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

Copy link
Collaborator

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.");
Copy link
Collaborator

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; });
Copy link
Collaborator

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?

@mgudemann mgudemann force-pushed the feature/graph_topological_sort branch from 719d054 to bdaba10 Compare July 5, 2017 10:53
@mgudemann
Copy link
Contributor Author

@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)
Copy link
Collaborator

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())

@mgudemann mgudemann force-pushed the feature/graph_topological_sort branch from bdaba10 to de657ed Compare July 5, 2017 12:24
@mgudemann
Copy link
Contributor Author

@tautschnig thanks, I have simplified the bool is_dag away

@kroening kroening merged commit 1b2a765 into diffblue:master Jul 15, 2017
@mgudemann mgudemann deleted the feature/graph_topological_sort branch August 21, 2017 13:18
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