可以在提升图库中即时计算边
On-the-fly calculation of edges in Boost Graph Library possible?
本文关键字:计算 更新时间:2023-10-16
在我的应用程序中,我有某种图形,其中每个节点/顶点可以相互连接,但实际连接已确定在运行时。
当然,通过迭代来实现这一点是微不足道的覆盖所有现有顶点并将它们连接到最后一个顶点我已经添加到图表中并在运行时使用过滤的图表决定连接仍然存在。
目的是使用BFS或DFS或BGL提供的其他算法。
有没有其他方法可以更有效地完成这项任务?通过示例:在初始化时添加所有(!(顶点并具有一些一种在运行时检查边缘的回调?
这就是我试图解决它的方式,但那个不起作用:
#include <boost/graph/adjacency_matrix.hpp>
#include <boost/graph/graph_utility.hpp>
struct VD { };
struct ED { };
struct Graph : boost::adjacency_matrix<boost::directedS, VD, ED>
{
Graph() : boost::adjacency_matrix<boost::directedS, VD, ED>(4) { }
//=================================================================
// Functions required by the AdjacencyMatrix concept
template <typename D, typename VP, typename EP, typename GP, typename A>
std::pair<typename adjacency_matrix<D,VP,EP,GP,A>::edge_descriptor, bool>
edge(typename adjacency_matrix<D,VP,EP,GP,A>::vertex_descriptor u,
typename adjacency_matrix<D,VP,EP,GP,A>::vertex_descriptor v,
const adjacency_matrix<D,VP,EP,GP,A>& g)
{
// Connect vertex 1 and 2
bool exists = (u == 1 && v == 2);
typename boost::adjacency_matrix<D,VP,EP,GP,A>::edge_descriptor
e(exists, u, v, boost::detail::get_edge_property(g.get_edge(u,v)));
return std::make_pair(e, exists);
}
};
int main() {
Graph g;
print_graph(g);
std::vector<int> component(num_vertices(g));
int num = boost::connected_components(g, &component[0]);
}
任何指示将不胜感激!
来自 BGL 概念:
Boost 图库 (BGL( 的核心是接口或概念(用泛型编程的说法(,它定义了如何以数据结构中立的方式检查和操作图。事实上,BGL接口甚至不需要使用数据结构来实现,因为对于某些问题,基于某些函数隐式定义图更容易或更有效。
没有标准的模型实现,但文档包含第 19 章:图形适配器中的示例
它显示了grid_graph
适配器,它可能与您的用例非常匹配。如果没有,它应该会给你关于如何根据概念要求创建隐式图模型的好主意。
[2]:
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 递归函数计算序列中的平方和(并输出过程)
- (C++)分析树以计算返回错误值的简单算术表达式
- 我的字符计数代码计算错误.为什么
- 在计算中使用二的幂有多有利可图
- 如何计算文件中的"columns"数?
- 计算排序向量的向量中唯一值的计数
- 如何使用 std::累积在 C++ 中计算总和立方体
- 使用Qt C++计算类似Git的SHA1哈希
- OpenCV C++.快速计算混淆矩阵
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- C++如何计算用户输入的数字中的偶数位数
- 如何计算数据类型的范围,例如int
- 类似枚举的计算常量
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 计算缩放多边形的比例,得到给定的多边形面积
- 在C++中如何在没有pow的情况下进行基础计算
- 计算平均值,不包括上次得分
- 如何计算多映射中重复对的数量