Boost 如何在graphviz中写边的权重?

Boost how to write edge's weight in graphviz?

借助 boost,我正在尝试以 graphviz 格式编写一个非常大且密集的图,它是一个 adjacency_matrix。图表本身:boost::adjacency_matrix<boost::undirectedS, boost::no_property, boost::property<boost::edge_weight_t, float>, boost::no_property>.

我在 Whosebug 中搜索,Google,要么我不理解代码,要么是 LABEL writer 而不是 WEIGHT writer。

我的boost版本是1.72.0.

如果我的英语有误,我很抱歉。提前谢谢你。

标签作者 PropertyWriters 也一样。 PropertyWriters 用于写入权重(或任何其他 edge/vertex 属性)。

不过,我强烈建议使用 dynamic_properties 来简化流程。这是我在这个网站上的 20+ usage examples

这是我能想到的 ajacency_matrix 上最简单的应用程序:

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);
}

正在打印:

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