4

I'm new to Boost graph library and I try to draw a graph using graphviz.

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>
#include <boost/utility.hpp>                // for boost::tie
#include <iostream>
#include <utility>                          // for std::pair

using namespace boost;
using namespace std;

class V {};
class C {};

void draw_test(){
    typedef boost::adjacency_list<boost::listS, boost::listS, boost::bidirectionalS, V, C > MyGraph;
        typedef boost::graph_traits<MyGraph>::vertex_descriptor vertex_descriptor;
    MyGraph g;
    vertex_descriptor a = add_vertex(V(), g);
    vertex_descriptor b = add_vertex(V(), g);
    add_edge(a, b, g);
    write_graphviz(std::cout, g);
}

int main() {
    draw_test();

    return 0;
}

But I got following error:

http://pastebin.com/KmTyyUHh

I'll be very thankful for any help

remdezx
  • 2,939
  • 28
  • 49
  • 2
    You can find the exact same problem in this questions ([1](http://stackoverflow.com/questions/7935417/how-provide-a-vertex-index-property-for-my-graph), [2](http://stackoverflow.com/questions/5781301/why-cant-i-use-boost-graph-write-graphviz-with-outedgelist-lists-and-vertexlist)). The problem is that `write_graphviz` requires a vertex index property map (as do many other Boost.Graph functions). An `adjacency_list` with `listS` as its VertexList doesn't have one by default. Unless you really need `listS` simply use `vecS` in your second template parameter. Or study the answers linked. –  Nov 15 '12 at 17:07
  • It worked! Thank you very much ;) Post your comment as an answer so I could accept it; – remdezx Nov 16 '12 at 09:27

1 Answers1

2

You can find the exact same problem in this questions (1, 2). The problem is that write_graphviz requires a vertex index property map (as many other Boost.Graph functions do). An adjacency_list with listS as its VertexList doesn't have one by default. Unless you really need listS simply use vecS in your second template parameter. Another alternative that you can find in the first linked answer is to create and initialize an external vertex index property map and then use an overload of write_graphviz that allows you to pass it explicitly. This is really useful for a multitude of algorithms when you need to use listS or setS in your graph.

Community
  • 1
  • 1