The following code is not compiled.
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/topological_sort.hpp>
#include <boost/function_output_iterator.hpp>
typedef boost::adjacency_list<
boost::setS, // outedge list
boost::setS, // vertex list
boost::directedS, // undirected
boost::no_property, // vertex prop
boost::no_property, // edge prop
boost::no_property, // graph prop
boost::setS // edgelist
> Graph;
bool hasCycle(const Graph& aG)
{
try {
boost::topological_sort(
aG,
boost::make_function_output_iterator([](int){}));
} catch(boost::not_a_dag const&)
{
return true;
}
return false;
}
It works after changing vertice lists to be vecS
http://coliru.stacked-crooked.com/a/abeb9e3f96e92af0
Is this limitation because DFS needs a deterministic visits?
Thank you,
The "limitation" is documented as the need for a vertex index. You could add one (note that you also should replace int by Graph::vertex_descriptor), or adjust the graph type to include one:
Adding an external index property map
Live On Coliru
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/topological_sort.hpp>
#include <boost/function_output_iterator.hpp>
typedef boost::adjacency_list<
boost::setS, // outedge list
boost::setS, // vertex list
boost::directedS, // undirected
boost::no_property, // vertex prop
boost::no_property, // edge prop
boost::no_property, // graph prop
boost::setS // edgelist
> Graph;
bool hasCycle(const Graph& aG)
{
try {
std::map<Graph::vertex_descriptor, size_t> index;
auto pmap = boost::make_assoc_property_map(index);
for (auto vd : boost::make_iterator_range(boost::vertices(aG)))
index[vd] = index.size();
boost::topological_sort(
aG,
boost::make_function_output_iterator([](Graph::vertex_descriptor){}),
boost::vertex_index_map(pmap));
}
catch (boost::not_a_dag const&)
{
return true;
}
return false;
}
int main() {
}
Adding an internal index property map
This amounts to shifting the burden to the caller. This might make (a lot of) sense depending on the performance requirements for you application.
Live On Coliru
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/topological_sort.hpp>
#include <boost/function_output_iterator.hpp>
typedef boost::adjacency_list<
boost::setS, // outedge list
boost::setS, // vertex list
boost::directedS, // undirected
boost::property<boost::vertex_index_t, int>, // vertex prop
boost::no_property, // edge prop
boost::no_property, // graph prop
boost::setS // edgelist
> Graph;
bool hasCycle(const Graph& aG)
{
try {
boost::topological_sort(
aG,
boost::make_function_output_iterator([](Graph::vertex_descriptor){})
);
}
catch (boost::not_a_dag const&)
{
return true;
}
return false;
}
#include <boost/graph/random.hpp>
#include <random>
int main() {
std::mt19937 prng{ std::random_device{}() };
Graph g;
boost::generate_random_graph(g, 100, 200, prng);
auto index = boost::get(boost::vertex_index, g);
int gen_id = 0;
for (auto vd : boost::make_iterator_range(boost::vertices(g))) {
boost::put(index, vd, gen_id++);
std::cout << "Vertex " << boost::get(index, vd) << "\n";
}
bool check = hasCycle(g);
}
Related
I tried to create a graph disallowing parallel edges using boost::setS, but it gives me an compilation error...
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>
#include <boost/graph/properties.hpp>
#include <boost/property_map/property_map.hpp>
#include <boost/graph/named_function_params.hpp>
#include <boost/graph/iteration_macros.hpp>
#include <random>
struct VertexData
{
long account_id;
};
struct EdgeData
{
// std::list<long> transaction_ids;
long edge_id;
};
typedef boost::adjacency_list<boost::vecS, boost::setS,
boost::directedS,
VertexData,
boost::property<boost::edge_weight_t, double, EdgeData>
> MyGraphType;
void test_insert_data(size_t vertex_size, size_t edge_size)
{
std::random_device rd; // Only used once to initialise (seed) engine
std::mt19937 rng(rd()); // Random-number engine used (Mersenne-Twister in this case)
std::uniform_int_distribution<int> uni(0, vertex_size - 1); // Guaranteed unbiased
auto random_integer = uni(rng);
MyGraphType g;
for (size_t i = 0; i < vertex_size; i++)
add_vertex(g);
for (size_t j = 0; j < edge_size; j++)
{
auto from = uni(rng);
auto to = uni(rng);
add_edge(from, to, g);
}
}
I got an error below:
error: no matching function for call to "add_edge(int&, int&, MyGraphType&)"
But if I switch the boost::setS to boost::vectorS, it works all right.
Please some expert point out where the program goes wrong... Thank you....
sorry I messed up the order of the definition of graph, it should be:
typedef boost::adjacency_list<boost::setS, boost::vecS,
boost::directedS,
VertexData,
boost::property<boost::edge_weight_t, double, EdgeData>
> MyGraphType;
I need to run A* on a graph with some of the edges removed. To do so I construct a filtered graph with blacklisted edges and I want to run A* on the filtered graph. The blacklisted edges are embedded in the class BlackListEdgeConstraint, that I initialize by passing to its constructor the list of the forbidden edges. This BlackListEdgeConstraint is correctly constructed, and I construct the filtered graph with these constrain. The problem is that when I run A* on the filtered graph, another BlackListEdgeConstraint object is mysteriously constructed with an empty constructor, and no edge is in effect blacklisted. Why is it happening?
This is my complete code:
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/filtered_graph.hpp>
#include <boost/graph/astar_search.hpp>
using namespace std;
typedef std::pair<int, int> Edge;
typedef boost::adjacency_list<boost::listS, boost::vecS, boost::directedS, boost::no_property, boost::property<boost::edge_weight_t, int> > graph_t;
typedef boost::graph_traits<graph_t>::vertex_descriptor vertex_descriptor;
struct BlackListEdgeConstraint
{
private:
std::set<Edge> blackList;
graph_t* g;
public:
BlackListEdgeConstraint():blackList(std::set<Edge>() ),g(NULL){throw std::runtime_error("This should not be called");};
BlackListEdgeConstraint(std::set<Edge>& list, graph_t* g_) : blackList(list), g(g_)
{
Edge edge = *blackList.begin();
std::cout<<"The black list contains "<< edge.first<<"-"<<edge.second<<std::endl;
}
/**
* This is the "predicate function" used by boost::filtered_graph (
* see http://www.boost.org/doc/libs/1_64_0/libs/graph/doc/filtered_graph.html )
* It it returns true, the edge is included in the filtered graph, otherwise it is excluded.
*/
bool operator()(const boost::graph_traits<graph_t>::edge_descriptor& e) const
{
Edge edge(source(e,*g), target(e,*g) );
std::cout<<"Should we include edge "<<source(e,*g)<<" ->"<< target(e,*g)<<" represented by a descriptor "<<e<<"? ";
//Include the edge if it's not in the blacklist.
bool include = (blackList.find( edge ) == blackList.end() );
std::cout<<include<<std::endl;
return include;
}
};
template<class GraphType>
class MyHeuristic : public boost::astar_heuristic<GraphType, double>
{
private:
const GraphType* g;
public:
MyHeuristic(const GraphType* g_): g(g_) {};
double operator()(vertex_descriptor v)
{
return 0;
}
};
//Used to terminate our search
struct GoalsReached{};
class MyVisitor : public boost::default_astar_visitor
{
private:
vertex_descriptor goal;
public:
MyVisitor(vertex_descriptor goal_): goal(goal_){};
template<class GraphType>
void examine_vertex(vertex_descriptor u, const GraphType& g)
{ if (u==goal) throw GoalsReached(); }
};
int main()
{
Edge edge_array[] = {Edge(0,1), Edge(1,2), Edge(2,3), Edge(3,0), Edge(1,3) };
int weights[] = {1,1,1,1,1};
int num_arcs = sizeof(edge_array) / sizeof(Edge);
int num_nodes = 4;
// Graph created from the list of edges
graph_t g(edge_array, edge_array + num_arcs, weights, num_nodes);
// Create descriptor for the source node
vertex_descriptor s = vertex(0, g);
vertex_descriptor goal = vertex(3,g) ;
std::set<Edge> blacklist; blacklist.insert( Edge(1,3) );
BlackListEdgeConstraint filter(blacklist, &g);
boost::filtered_graph<graph_t, BlackListEdgeConstraint> filtered(g, filter);
cout<<"filtered constructed"<<endl;
// Create vectors to store the predecessors (p) and the distances from the root (d)
std::vector<vertex_descriptor> p(num_vertices(filtered));
std::vector<int> d(num_vertices(filtered));
try{
cout<<"Launching astar_search"<<endl;
boost::astar_search(filtered, s,
MyHeuristic<boost::filtered_graph<graph_t, BlackListEdgeConstraint>>(&filtered),
boost::predecessor_map(&p[0]).
distance_map(&d[0]).
visitor(MyVisitor(goal) )
);
cout<<"astar_search launched"<<endl;
}catch(const GoalsReached& )
{ // Print the path
std::vector<boost::graph_traits<graph_t>::vertex_descriptor > path;
boost::graph_traits<graph_t>::vertex_descriptor current = goal;
while(current!=s)
{
path.push_back(current);
current = p[current];
}
path.push_back(s);
// Prints the path obtained in reverse
std::vector<boost::graph_traits<graph_t>::vertex_descriptor >::reverse_iterator it;
for (it = path.rbegin(); it != path.rend(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
}
return EXIT_SUCCESS;
}
And this is the output:
The black list contains 1-3
filtered constructed
Launching astar_search
terminate called after throwing an instance of 'std::runtime_error'
what(): This should not be called
My boost version is 1.54
The problem is that, when boost::astar_search(..) is invoked, the empty constructor BlackListEdgeConstraint() is called.
I don't know how you reach the conclusion. I cannot reproduce that:
Live On Coliru
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/filtered_graph.hpp>
#include <boost/graph/astar_search.hpp>
struct VertexProperties {
};
struct EdgeProperties {
double weight = 1;
};
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, VertexProperties, EdgeProperties> MyGraph;
struct StreetDirectory {
using Graph = MyGraph;
using Edge = Graph::edge_descriptor;
using Vertex = Graph::vertex_descriptor;
};
struct BlackListEdgeConstraint : StreetDirectory
{
private:
std::set<StreetDirectory::Edge> blackList;
public:
BlackListEdgeConstraint(const std::set<Edge>& list) : blackList(list) {};
BlackListEdgeConstraint()
{
throw std::runtime_error("This should not be called");
};
bool operator()(const Edge& e) const
{
//Include the edge if it's not in the blacklist.
return blackList.find(e) == blackList.end();
}
};
int main() {
MyGraph graph;
const std::set<StreetDirectory::Edge> blacklistEdges {
add_edge(1,2,graph).first,
add_edge(1,3,graph).first,
add_edge(2,4,graph).first,
};
add_edge(4,2,graph);
BlackListEdgeConstraint filter(blacklistEdges);
boost::filtered_graph<MyGraph, BlackListEdgeConstraint> filtered(graph, filter);
std::vector<StreetDirectory::Vertex> p(boost::num_vertices(filtered)); //Output variable
std::vector<double> d(boost::num_vertices(filtered)); //Output variable
boost::default_astar_visitor vis;
boost::astar_search(
filtered,
1,
[](auto /*vd*/) { return 1; },
boost::predecessor_map(&p[0]).
weight_map(boost::get(&EdgeProperties::weight, filtered)).
distance_map(&d[0]).
visitor(vis)
);
}
Notes
in general functors are passed by value in (standard) library algorithms. So you'd use std::reference_wrapper<BlackListEdgeConstraint> if you wanted to use the same instance. But like I said, I don't see it happening, so it's not a problem AFAICT
in your sample there seemed to be no edge-weight map indicated. I don't see how that should have compiled
I am trying to run the fruchterman_reingold_force_directed_layout algorithm on a boost::adjacency_list<boost::listS, boost::listS, boost::bidirectionalS, Vertex, Edge> graph.
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/fruchterman_reingold.hpp>
#include <boost/graph/random_layout.hpp>
typedef boost::rectangle_topology<> RectTopology;
typedef RectTopology::point_type point;
struct Vertex {
Vertex(int i) : index(i) {};
int index;
point position;
};
struct Edge {
double weight;
};
typedef boost::adjacency_list<boost::listS, boost::listS, boost::bidirectionalS, Vertex, Edge> Graph;
int main() {
Graph graph;
auto vertexIdPropertyMap = boost::get(&Vertex::index, graph);
Vertex n1(0);
boost::add_vertex(std::move(n1), graph);
Vertex n2(1);
boost::add_vertex(std::move(n2), graph);
Vertex n3(2);
boost::add_vertex(std::move(n3), graph);
boost::add_edge(boost::vertex(0, graph), boost::vertex(1, graph), Edge(), graph);
boost::add_edge(boost::vertex(0, graph), boost::vertex(2, graph), Edge(), graph);
boost::minstd_rand gen;
RectTopology rectangleTopology(gen, -25, -25, 25, 25);
boost::random_graph_layout(graph, boost::get(&Vertex::position, graph), rectangleTopology);
boost::fruchterman_reingold_force_directed_layout(graph, boost::get(&Vertex::position, graph), rectangleTopology);
return 0;
}
When compiling, the first error is the following:
error: forming reference to void
typedef value_type& reference;
I have done some research and apparently this is due to the fact that it is a listS vertex list - with vecS it works.
However, I still want to use a listS and apparently it is possible:
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/fruchterman_reingold.hpp>
#include <boost/graph/random_layout.hpp>
enum vertex_position_t { vertex_position };
namespace boost { BOOST_INSTALL_PROPERTY(vertex, position); }
typedef boost::rectangle_topology<> RectTopology;
typedef RectTopology::point_type point;
struct simple_edge {
int first, second;
};
int main() {
enum { A, B, C };
simple_edge triangular_edges[2] = {
{A, B}, {A, C}
};
typedef boost::adjacency_list<boost::listS, boost::listS, boost::bidirectionalS,
// Vertex properties
boost::property<boost::vertex_index_t, int,
boost::property<vertex_position_t, point> >,
// Edge properties
boost::property<boost::edge_weight_t, double>> Graph;
Graph g(&triangular_edges[0], &triangular_edges[1], 3);
boost::minstd_rand gen;
RectTopology rect_top(gen, -25, -25, 25, 25);
boost::random_graph_layout(g, boost::get(vertex_position, g), rect_top);
boost::fruchterman_reingold_force_directed_layout(g, boost::get(vertex_position, g), rect_top);
return 0;
}
The problem here is that I don't want to use the "old-style" properties but the bundled properties instead. Here someone was attempting something similar, but the fruchterman_reingold_force_directed_layout does not offer this index map if I am not mistaking.
It is straight forward to add edge weights to a graph as internal properties:
void InternalProperties()
{
std::cout << "InternalProperties()" << std::endl;
// Graph with internal edge weights
using EdgeWeightProperty = boost::property<boost::edge_weight_t, double>; // <tag, type>
using GraphWithInternalEdgeWeightsType = boost::adjacency_list<boost::setS, // out edge container
boost::vecS, // vertex container
boost::undirectedS, // directed or undirected
boost::no_property, // vertex properites
EdgeWeightProperty> // edge properties
;
// Create a graph object
GraphWithInternalEdgeWeightsType g(3);
// add two edges with edge weights
EdgeWeightProperty e1 = 5;
add_edge(0, 1, e1, g);
EdgeWeightProperty e2 = 3;
add_edge(1, 2, e2, g);
boost::property_map<GraphWithInternalEdgeWeightsType, boost::edge_weight_t>::type edgeWeightMap = get(boost::edge_weight_t(), g);
using edge_iter = boost::graph_traits<GraphWithInternalEdgeWeightsType>::edge_iterator;
std::pair<edge_iter, edge_iter> edgePair;
for(edgePair = edges(g); edgePair.first != edgePair.second; ++edgePair.first) {
std::cout << edgeWeightMap[*edgePair.first] << " ";
}
}
Now if I want to do the same thing and demonstrate using "external properties", I came up with this, but there is really no link at all back to the original graph:
void ExternalProperties()
{
std::cout << std::endl << "ExternalProperties()" << std::endl;
// Graph with external edge weights
using GraphWithExternalEdgeWeightsType = boost::adjacency_list<boost::setS, // out edge container
boost::vecS, // vertex container
boost::undirectedS> // directed or undirected
;
// Create a graph object
GraphWithExternalEdgeWeightsType g(3);
// add edges (without edge weights)
add_edge(0, 1, g);
add_edge(1, 2, g);
// create a map from edge_descriptors to weights and populate it
std::map<GraphWithExternalEdgeWeightsType::edge_descriptor, double> edgeWeightMap;
edgeWeightMap[boost::edge(0,1,g).first] = 5;
edgeWeightMap[boost::edge(1,2,g).first] = 3;
using edge_iter = boost::graph_traits<GraphWithExternalEdgeWeightsType>::edge_iterator;
std::pair<edge_iter, edge_iter> edgePair;
for(edgePair = edges(g); edgePair.first != edgePair.second; ++edgePair.first) {
std::cout << edgeWeightMap[*edgePair.first] << " ";
}
}
Is there any way to make something like get(boost::edge_weight_t(), g); (from the internal example) return this map? Like to say g.setPropertyMap(boost::edge_weight_t, edgeWeightMap) in this external example?
I'm not sure what the gain is, but perhaps this helps for inspiration:
#include <boost/graph/adjacency_list.hpp>
#include <boost/property_map/property_map.hpp>
#include <map>
#include <iostream>
namespace MyLib {
struct MyGraph : boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS> {
using base_type = boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS>;
using base_type::adjacency_list;
std::map<edge_descriptor, double> m_weights;
};
auto get(boost::edge_weight_t, MyGraph& g) { return boost::make_assoc_property_map(g.m_weights); }
auto get(boost::edge_weight_t, MyGraph const& g) { return boost::make_assoc_property_map(g.m_weights); }
}
namespace boost {
template <> struct graph_traits<MyLib::MyGraph> : graph_traits<adjacency_list<setS, vecS, undirectedS> > {};
template <> struct property_map<MyLib::MyGraph, edge_weight_t, void> {
using Traits = graph_traits<MyLib::MyGraph>;
using Edge = Traits::edge_descriptor;
using type = boost::associative_property_map<std::map<Edge, double> >;
using const_type = boost::associative_property_map<std::map<Edge, double> > const;
};
}
void ExternalProperties() {
std::cout << "ExternalProperties()" << std::endl;
// Graph with external edge weights
// Create a graph object
using Graph = MyLib::MyGraph;
Graph g(3);
// add edges (without edge weights)
add_edge(0, 1, g);
add_edge(1, 2, g);
// create a map from edge_descriptors to weights and populate it
auto edgeWeightMap = MyLib::get(boost::edge_weight, g);
edgeWeightMap[boost::edge(0, 1, g).first] = 5;
edgeWeightMap[boost::edge(1, 2, g).first] = 3;
using edge_iter = boost::graph_traits<Graph>::edge_iterator;
std::pair<edge_iter, edge_iter> edgePair;
for (edgePair = edges(g); edgePair.first != edgePair.second; ++edgePair.first) {
std::cout << edgeWeightMap[*edgePair.first] << " ";
}
}
int main() {
ExternalProperties();
}
I've not been able to avoid ambiguity with boost::get in such a way that you can trust ADL to pick the "best" overload without namespace qualification.
Live On Coliur
The following code has errors:
cannot bind ‘std::basic_ostream’ lvalue to ‘std::basic_ostream&&
#include <boost/graph/graphviz.hpp>
void foo(int,char*[])
{
using namespace boost;
typedef boost::adjacency_list<
boost::setS, // outedge list
boost::setS, // vertex list
boost::directedS, // undirected
boost::no_property, // vertex prop
boost::no_property, // edge prop
boost::no_property, // graph prop
boost::setS // edgelistc
> Graph;
Graph g;
std::ostringstream dotname;
dotname << "a.dot";
std::ofstream dot(dotname.str());
write_graphviz(dot, g);
}
It works when
boost::vecS, // vertex list
Is it expected?
Change the vertex container selector to setS changes the vertex descriptor into a type that is not streamable.
You should, as with many many other algorithms in BGL, pass a separate vertex index:
IN: VertexAndEdgeListGraph& g
A directed or undirected graph. The graph's type must be a model of
VertexAndEdgeListGraph. In most cases, the graph must have an internal
vertex_index property map.
External Vertex Id mapping
Live On Coliru
#include <boost/graph/graphviz.hpp>
#include <boost/graph/random.hpp>
#include <random>
int main(int,char*[])
{
using namespace boost;
typedef boost::adjacency_list<setS, setS, directedS> Graph;
Graph g;
std::mt19937 prng{std::random_device{}()};
generate_random_graph(g, 3, 5, prng);
std::map<Graph::vertex_descriptor, size_t> ids;
for (auto u : make_iterator_range(vertices(g)))
ids[u] = ids.size();
default_writer w;
write_graphviz(std::cout, g, w, w, w, make_assoc_property_map(ids));
}
Prints e.g.
digraph G {
1;
2;
3;
1->2 ;
2->1 ;
2->3 ;
3->1 ;
3->2 ;
}
Internal property map:
You can put the property internally without much changing:
Live On Coliru
#include <boost/graph/graphviz.hpp>
#include <boost/graph/random.hpp>
#include <random>
int main(int,char*[])
{
using namespace boost;
typedef boost::adjacency_list<setS, setS, directedS, property<vertex_index_t, size_t> > Graph;
Graph g;
std::mt19937 prng{std::random_device{}()};
generate_random_graph(g, 3, 5, prng);
auto ids = get(vertex_index, g);
size_t num = 0;
for (auto u : make_iterator_range(vertices(g)))
put(ids, u, num++);
write_graphviz(std::cout, g);
}