Boost图列表或vec

Boost graph list or vec

本文关键字:vec 列表 Boost      更新时间:2023-10-16

我已经花了好几天的时间使用boost图形库。据我所知,在考虑VertexList和EdgeList存储时:

vec:

  • 拥有索引,因此可以使用它访问
  • 移除顶点时,迭代器失效

列表:

  • 没有指数
  • 不会使迭代器
  • 失效

它有点短,但这正是我的问题所在。我需要这些索引号,我希望以后能够轻松地删除顶点。

我有一个工作算法与这个图结构:

typedef boost::adjacency_list<
        boost::vecS, boost::vecS, boost::undirectedS, 
        topologicalmap::Intersection_Graph ,
        boost::edge_weight_t, 
        boost::no_property > Graph_boost;

我有一个自定义结构Intersection_Graph为我的顶点,我需要使用。这里我用vecS。

我想使用列表来代替能够删除顶点。同时,我也希望以后能在Dijkstra算法中使用它。

我有点明白,我需要有boost::vertex_index_t在我的列表,但我真的很困惑,如何做到这一点,并保持我的自定义结构在同一时间。

我试过这样做:

typedef boost::adjacency_list<
        boost::listS, boost::listS, boost::undirectedS, 
        boost::property<boost::vertex_index_t, topologicalmap::Intersection_Graph>,
        boost::edge_weight_t, 
        boost::no_property > Graph_boost;

但是我甚至不能再访问我的自定义结构了。另外,索引访问不工作。

我真的需要索引访问能力,因为算法我的图将依赖于返回父节点的索引。我觉得我可以使用顶点而不是索引,但这意味着要重新编写代码,我想知道我是否可以避免它。

所以我的问题是:有没有办法让列表在保持列表优势的同时以vecS的方式运行?

如果这听起来很愚蠢,请原谅我。我现在很困惑,所以我可能说了一些愚蠢的话。

内部属性配方为:

property<tag, type, next_property>
当然,除非你使Intersection_Graph 的行为像一个整型,否则你不能直接使用它作为vertex_index属性的类型。这也可能不是你想要的。

这个看起来更近:

boost::property<boost::vertex_index_t, int, topologicalmap::Intersection_Graph>

声明两个属性:

  1. 一个内部属性标记vertex_index_t(类型int)
  2. 一个绑定属性(类型为Intersection_Graph)。注意,绑定的属性可以通过vertex_bundle_t标签隐式访问。

现在,记住这一点,一切都应该是一帆风顺的:

Live On Coliru

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/random.hpp>
#include <boost/graph/graph_utility.hpp>
#include <boost/graph/iteration_macros.hpp>
#include <random>
#include <iostream>
using namespace boost;
namespace topologicalmap {
    struct Intersection_Graph {
        std::string bundled;
    };
}
typedef boost::adjacency_list<
        boost::listS, boost::listS, boost::undirectedS, 
        boost::property<boost::vertex_index_t, int, topologicalmap::Intersection_Graph>,
        boost::edge_weight_t, 
        boost::no_property > Graph_boost;
int main() {
    std::mt19937 prng { std::random_device {} () };
    Graph_boost g;
    generate_random_graph(g, 10, 20, prng);
    // assign indices
    int i = 0;
    BGL_FORALL_VERTICES(v, g, Graph_boost) { 
        get(vertex_index, g)[v] = i; 
        g[v].bundled = "id:" + std::to_string(i);
        i++;
    }
    // print the graph using the `bundled` property as a label:
    print_graph(g, get(&topologicalmap::Intersection_Graph::bundled, g));
    // do some index accesses:
    for (int i : {1,7})
        std::cout << "nVertex at index #" << i << " has a bundled property of '" << g[vertex(i,g)].bundled << "'";
}

打印例如(每次运行随机生成)

id:0 <--> id:8 id:8 id:7 id:6 id:1 
id:1 <--> id:3 id:4 id:4 id:3 id:0 id:2 
id:2 <--> id:7 id:1 
id:3 <--> id:1 id:7 id:1 id:9 id:4 
id:4 <--> id:1 id:1 id:5 id:6 id:3 
id:5 <--> id:4 id:9 
id:6 <--> id:0 id:9 id:4 id:8 
id:7 <--> id:3 id:0 id:2 id:9 
id:8 <--> id:0 id:0 id:6 
id:9 <--> id:7 id:6 id:3 id:5 
Vertex at index #1 has a bundled property of 'id:1'
Vertex at index #7 has a bundled property of 'id:7'

指出:

  • 图形现在"知道"vertex_index并不意味着它得到了维护;你必须自己填充:

    int i = 0;
    BGL_FORALL_VERTICES(v, g, Graph_boost) get(vertex_index, g)[v] = i++; 
    
  • 你实际上不需要有vertex_index与你的图形类型相关联,因为你可以把它作为一个命名参数传递给所有相关的算法。这包括构建依赖于vertex_index(例如make_iterator_property_map)的派生属性映射

  • 我相信也可以使用图形特征来关联顶点索引(但我过去没有这样做过)。如果你想将索引存储在Intersection_Graph结构体的成员中,这似乎是一个很好的方法。
  • 就像我在我的评论中说的,如果你存储vertex_descriptor s而不是整型索引,你可能不需要任何这一点。