提升如何在图形可视化中写入边缘的权重?

Boost how to write edge's weight in graphviz?

本文关键字:边缘 权重 可视化 图形      更新时间:2023-10-16

使用 boost,我正在尝试以图形可视化格式编写一个非常大且密集的图,这是一个adjacency_matrix。图表本身:boost::adjacency_matrix<boost::undirectedS, boost::no_property, boost::property<boost::edge_weight_t, float>, boost::no_property>

我在StackOverflow,Google上搜索,要么我不理解代码,要么它是一个LABEL编写器而不是WEIGHT编写器。

我的提升版本是 1.72.0。

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

标签编写器也同样PropertyWriterPropertyWriters用于写入权重(或任何其他边/顶点属性(。

但是,我强烈建议使用dynamic_properties来简化过程。这是我在这个网站上的20+使用示例。

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

住在科里鲁

#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];
}