我是一个BGL新手,有一个(可能)简单的问题:我有一个有向图并使用边缘的捆绑属性,其中一个是int类型的索引 . 知道一个唯一索引,我想获得该边缘的相应edge_descriptor以便对其执行操作 . 以下示例总结了我的问题:
#include <boost/graph/adjacency_list.hpp>
struct EdgeProperties {
EdgeProperties(): distance(10), time_limit(5) {};
int index;
int distance;
int time_limit;
};
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, boost::no_property, EdgeProperties> Graph;
int main() {
Graph graph;
EdgeProperties edge_prop1, edge_prop2, edge_prop3, edge_prop4;
// Define edge properties
edge_prop1.index = 0;
edge_prop2.index = 1;
edge_prop3.index = 2;
edge_prop4.index = 3;
// Add edges to graph
boost::add_edge(0, 1, edge_prop1, graph);
boost::add_edge(0, 2, edge_prop2, graph);
boost::add_edge(1, 3, edge_prop3, graph);
boost::add_edge(2, 3, edge_prop4, graph);
// Get vertex_descriptor from an (int) index:
int vertex_index = 2;
boost::graph_traits<Graph>::vertex_descriptor v = boost::vertex(vertex_index, graph);
// I would like to get an edge_descriptor from an (int) index property:
// The following DOES NOT work:
boost::graph_traits<Graph>::edge_descriptor e = boost::edge(edge_prop1.index, graph);
}
我也读过有关属性 Map 的内容,但找不到解决方案我的问题 . 我更喜欢bundled properties而不是内部属性 . 有没有办法通过bundle属性将唯一的int类型索引分配给边缘并通过这些int类型值访问边缘?
1 回答
可悲的是,我不认为
boost::graph
在这里有直接的帮助 .首先,没有机制可以根据边缘属性的字段找到边缘(或者顶点) - BGL保留任何这样的映射,而你拥有的“索引”字段完全是出于你的目的 .
其次,有
boost::edges
函数返回图形所有边的迭代器范围 . 我虽然你可以将vecS作为边缘容器类型传递给adjacency_list模板,然后查看这个范围,但是根据http://www.boost.org/doc/libs/1_61_0/libs/graph/doc/EdgeListGraph.html,迭代器只需要是多遍输入迭代器,而实现就是这样 - 即使用vecS作为边缘类型,你不能做随机访问 .因此,似乎实现所需内容的唯一方法是将自己的
unodered_map
从索引保留为边缘描述符 .