Boost图列表或vec
Boost graph list or vec
我已经花了好几天的时间使用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>
声明两个属性:
- 一个内部属性标记
vertex_index_t
(类型int
) - 一个绑定属性(类型为
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而不是整型索引,你可能不需要任何这一点。
相关文章:
- Pybind11:将元组列表从Python传递到C++
- 从链接列表c++中删除一个项目
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- C++如何通过用户输入删除列表元素
- 读取文件的最后一行并输入到链接列表时出错
- 复制列表初始化的隐式转换的等级是多少
- LNK2038、MSVS2017 MAGMA的原因列表
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 没有为自己的结构调用列表推回方法
- 使用简单类型列表实现的指数编译时间.为什么
- 一对向量构造函数:初始值设定项列表与显式构造
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- 通过for循环使用用户输入填充列表
- C++:如何使函数只返回作为列表一部分的字符串
- 在for循环中使用auto vs decltype(vec.size())来处理字符串的向量
- 概念中的cv限定符需要表达式参数列表
- 下面是我为检测链接列表中的循环而制作的代码
- 建议在运行时将带有类实例的列表从c++导入qml
- 如何维护资源管理器项目视图中当前可见的项目列表
- Boost图列表或vec