可以在提升图库中即时计算边

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]: