Postaron
Postaron

Reputation: 99

Boost how to write edge's weight in graphviz?

With boost, I am trying to write in graphviz format a very large and dense graph which is an adjacency_matrix. The graph itself : boost::adjacency_matrix<boost::undirectedS, boost::no_property, boost::property<boost::edge_weight_t, float>, boost::no_property>.

I searched in StackOverflow, Google, and either I did not understand the code or it was a LABEL writer and not a WEIGHT writer.

My boost version is the 1.72.0.

Upvotes: 2

Views: 569

Answers (1)

sehe
sehe

Reputation: 392911

Label writers are PropertyWriters just as well. PropertyWriters are used to write weights (or any other edge/vertex attribute).

However, I highly recommend using dynamic_properties to simplify the process. Here's 20+ usage examples I have on this site.

Here's the simplest application on ajacency_matrix that I can think of:

Live On Coliru

#include <boost/graph/adjacency_matrix.hpp>
#include <boost/graph/graphviz.hpp>
#include <iostream>

using EP = boost::property<boost::edge_weight_t, float>;
using G  = boost::adjacency_matrix<boost::undirectedS, boost::no_property, EP>;

int main() {
    G g(5);

    add_edge(1, 2, 3.5f, g);
    add_edge(2, 3, 4.5f, g);

    boost::dynamic_properties dp;
    dp.property("node_id", get(boost::vertex_index, g));
    dp.property("weight",  get(boost::edge_weight,  g));
    boost::write_graphviz_dp(std::cout, g, dp);
}

Printing:

graph G {
0;
1;
2;
3;
4;
2--1  [weight=3.5];
3--2  [weight=4.5];
}

Upvotes: 3

Related Questions