在 Boost.Graph 中向图形添加边
Adding edges to a graph in Boost.Graph
我正在尝试使用Boost.Graph Library来运行Goldberg的Max-Flow Algorithm。Boost.Graph称之为push_relabel_max_flow。
但是,我很难理解库及其类型系统。我在上面链接的文档给出了一个示例代码。但在该示例中,图形是从文件中读取的。我想在运行时生成图形。这是我到目前为止的代码(大部分是从示例中复制的):
typedef boost::adjacency_list_traits<boost::vecS, boost::vecS, boost::directedS> Traits;
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS,
boost::property<boost::vertex_name_t, std::string>,
boost::property<boost::edge_capacity_t, long,
boost::property<boost::edge_residual_capacity_t, long,
boost::property<boost::edge_reverse_t, Traits::edge_descriptor>>>> DirectedGraph;
DirectedGraph g;
Traits::vertex_descriptor s, t;
s = boost::add_vertex(g);
t = boost::add_vertex(g);
boost::add_vertex(g);
boost::add_vertex(g);
在图形中添加 4 个顶点后,我应该"连接"它们,即制作具有容量、剩余容量和反向值的边。此任务的函数boost::add_edge()
但我不知道如何传递我的参数。示例代码没有显示它,因为正如我所说,数据是从文件中读取的,然后直接解析为图形。也许有Boost.Graph库经验的人可以告诉我怎么做。
您可以在
顶点s
和t
之间添加边,如下所示:
boost::add_edge(s, t, {33, 44}, g);
此处将edge_capacity
设置为 33,将edge_residual_capacity
设置为 44。
要实际访问边缘属性,据我所知,您必须执行以下操作:
std::cout << boost::get(boost::edge_capacity, g, boost::edge(s,t,g).first) << 'n';
这很烦人。如果改用捆绑属性会更容易,如下所示:
typedef boost::adjacency_list_traits<boost::vecS, boost::vecS, boost::directedS> Traits;
struct VertexProps {
std::string name;
};
struct EdgeProps {
long capacity;
long residual_capacity;
Traits::edge_descriptor reverse;
};
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS,
VertexProps, EdgeProps > DirectedGraph;
然后,您可以以相同的方式添加顶点和边,但更容易访问边属性,例如
auto e = boost::edge(s,t,g).first; // get the edge descriptor for an edge from s to t, if any
std::cout << g[e].capacity << 'n';
要在添加的匿名第三和第四个顶点之间添加边,您可以摆脱
boost::add_edge(2, 3, {17, 26}, g);
由于底层存储是vector
的,所以vertex_descriptor
只是向量索引(又名size_t
,又名unsigned long
在这里)。但要更严格地说,你应该这样做
boost:add_edge(boost::vertex(2, g), boost::vertex(3, g), {17, 26}, g);
为了获得第 3 和第 4 顶点的vertex_descriptor
。
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 如何在C++中从两个单独的for循环中添加两个数组
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 如何仅为一个函数添加延迟
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 使用std::transform将一个范围的元素添加到另一个范围中
- 如何将更多文件夹添加到c++include路径
- 如何将元素添加到数组的线程安全函数?
- 通过相邻顶点列表添加和减去图形中的边
- 制作完成后是否可以将节点添加到图形中
- CMAKE:在多图形/二进制C 项目中添加通用标头的IDE可见性
- 巨大的内存占用,同时向图形添加边缘
- 在 Boost.Graph 中向图形添加边
- 在使用 BOOST 图形库生成的图形中添加随机边
- 添加可绘制对象层以将逻辑与图形分离
- 如何向C++添加图形
- 使用邻接列表将顶点添加到图形
- 动态添加到图形数据结构中
- 图形在borland c++中添加
- 添加纹理到QT OpenGL场景图形