Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
346 views
in Technique[技术] by (71.8m points)

c++ - How to print a boost graph in graphviz with one of the properties displayed?

I see examples of this when using property maps, but not when using structs to handle the vertices and edges (I think this is called 'bundles').

I have vertices and edges defined as such, in an adjacency list graph.

struct Vertex
{
    string name;
    int some_int;
};

struct Edge
{
    double weight;
};

The graph is constructed as follows:

typedef boost::adjacency_list<boost::listS, boost::vecS, boost::directedS, Vertex, Edge> boost_graph;

I want to print my graph of these objects in Graphviz format, so I can view it as an image. However, I don't only want the nodes and edges. I also want the attribute name on vertices and weight on edges to appear in the image. How can I do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I just stumbled upon this question. Although it has an accepted answer, I thought I'll add my version too.

You are using bundled property in your graph. The correct way to get property map from your bundled properties is to use boost::get. So you can do something like:

boost::write_graphviz(std::cout, your_graph,
    boost::make_label_writer(boost::get(&Vertex::name, your_graph)),
    boost::make_label_writer(boost::get(&Edge::weight, your_graph)),
    );

Where your_graph is the graph object you have created.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...