【问题标题】:BGL: Using bundled properties to store vertex descriptor of another vertexBGL:使用捆绑属性存储另一个顶点的顶点描述符
【发布时间】:2014-09-23 02:26:52
【问题描述】:

我正在尝试使用boost::adjacency list 和捆绑属性创建一个树形图来存储每个顶点的父级,我想以一种在删除顶点时它们不会失效的方式存储顶点描述符,所以我使用@ 987654322@,代码应该是这样的

// custom vertex for tree graphs
struct tree_vertex_info{
    // descriptor of parent vertex
    boost::graph_traits<Tree>::vertex_descriptor parent_in_tree;
};
// the tree graph type
typedef boost::adjacency_list<boost::listS, boost::listS, boost::directedS
        tree_vertex_info, boost::no_property, graph_info> Tree;

但这不起作用,因为 Tree 必须在结构定义之后定义。还有其他方法可以使用捆绑属性吗?我以为我可以使用 int 变量而不是 vertex_descriptor 类型来存储 vertex_descriptor 但由于我使用boost::listS 来存储它们,我不确定是否可以。

【问题讨论】:

    标签: c++ vertex boost-graph


    【解决方案1】:

    理论上,你可以有这样的东西:

    struct tree_vertex_info;  // forward-declaration
    
    typedef boost::adjacency_list<
      boost::listS, boost::listS, boost::directedS, 
      tree_vertex_info, boost::no_property, graph_info> Tree;
    
    struct tree_vertex_info {
      boost::graph_traits<Tree>::vertex_descriptor parent_in_tree;
    };
    

    但是,这需要boost::adjacency_list 类模板支持不完整的类型(这就是tree_vertex_info 在只有前向声明的情况下,直到编译器到达完整的声明它)。据我所知,boost::adjacency_list 类不支持不完整的类型(而且我很了解它的实现,我认为它不会起作用),当然不能保证支持它们。

    我实际上正在开发一个新版本的boost::adjacency_list,我称之为boost::adjacency_list_BC,因为它基于Boost.Container 容器,并且支持不完整的类型。但是,它仍处于 beta 阶段(关注 herehere),最新版本的 Boost.Container 似乎已经破坏了一些我仍然需要弄清楚的东西。顺便说一句,我还有许多 BGL 树数据结构以及树的新 BGL 概念和特征(因为您似乎正在实现一种树)。

    另外,如果你这样做的动机确实是你所拥有的(“树中的父级”),那么你应该在你的adjacency_list 中使用boost::bidirectionalS 以便能够从一个子顶点到它的父级(这就是boost::bidirectionalS 的意思,你得到一个BidirectionalGraph)。

    最后,要真正解决您所处的这种情况,您必须使用类型擦除技术。一种简单的现成方法是使用boost::any 擦除 vertex_descriptor 的类型,如下所示:

    struct tree_vertex_info{
      // descriptor of parent vertex
      boost::any parent_in_tree;
    };
    
    // the tree graph type
    typedef boost::adjacency_list<boost::listS, boost::listS, boost::directedS
            tree_vertex_info, boost::no_property, graph_info> Tree;
    

    只需查看Boost.Any 以获取使用说明。

    我认为我可以使用 int 变量而不是 vertex_descriptor 类型来存储 vertex_descriptor,但由于我使用 listS 来存储它们,我不确定是否可以。

    不,你不能。您不能特别依赖 vertex_descriptor 类型(例如,您不能假设它是整数类型,更不用说“int”)。我碰巧知道 vertex_descriptor 通常是迭代器类型(例如std::list&lt;T&gt;::iterator)或大小类型(例如std::size_tstd::vector&lt;T&gt;::size_type),但这是您不应该也不能依赖的实现细节。

    【讨论】:

    • boost::any 似乎已经解决了问题!我的第一个想法是使用双向图,但由于我试图实现一个动态算法来维护最小长度路径,所以这不太好用,因为当我需要边的父节点时,我必须检查所有相邻边然后检查其中哪些导致最低深度顶点,在这种情况下,我必须存储深度。非常感谢您的帮助!除了解决方案之外,您还给了我一些我不确定的线索!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多