gpt4 book ai didi

c++ - boost 如何在graphviz中写入边的权重?

转载 作者:行者123 更新时间:2023-12-03 07:01:04 24 4
gpt4 key购买 nike

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

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

我的 boost 版本是 1.72.0。

如果我犯了错误,我为我的英语感到抱歉。先感谢您。

最佳答案

标签作家是 PropertyWriter s也一样。 PropertyWriters用于写入权重(或任何其他边/顶点属性)。

但是,我强烈建议使用 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];
}

关于c++ - boost 如何在graphviz中写入边的权重?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60102920/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com