Pog*_*ogo 5 c++ boost graph boost-graph
我正在为一些项目使用Boost Graph库,我想在图中找到边重复的次数.例如,
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, Node_Info, Edge_Info > Graph_t;
//node_info and Edge_info are external node and edge properties (structures)
Run Code Online (Sandbox Code Playgroud)
假设我有两个节点,node1和node2,它们之间有一条边(node1,node2).每个边的edge属性包含一个时间戳start,end ..并且图中可能有许多这样的边具有不同的时间戳.例如.
edge1 = (node1, node2) with start = 100, end = 200.
edge2 = (node1, node2) with start = 250, end = 400.
Run Code Online (Sandbox Code Playgroud)
我知道在增强图中,给定两个顶点,我们可以使用以下内容查找图中是否存在边.
std::pair < edge_t, bool > p = boost::edge( node1, node2, myGraph );
if(p.second == 1) cout << "edge exists!" << endl;
else cout << " does not exist " << endl;
Run Code Online (Sandbox Code Playgroud)
但这可能意味着即使多个边缘存在不同的边缘属性,它也只会返回任何一个边缘 - >问题
任何人都可以建议如何在两个给定节点之间获得这样的多边?谢谢!
有几种方法可以做到这一点.
1)只需检查所有转到所需目标的边缘:
boost::graph_traits<Graph_t>::out_edge_iterator ei, ei_end;
boost::tie(ei, ei_end) = out_edges( node1, myGraph );
int parallel_count = 0;
for( ; ei != ei_end; ++ei) {
if( target(*ei, myGraph) == node2 ) {
cout << "Found edge (node1, node2) with property: " << myGraph[*ei] << endl;
++parallel_count;
};
};
cout << "There are a total of " << parallel_count << " parallel edges." << endl;
Run Code Online (Sandbox Code Playgroud)
2)指定boost::multisetS
作为OutEdgeListS
模板参数adjacency_list
,这将启用一个额外的函数,该函数edge_range
返回所有出现u
和进入的"并行"边的迭代器范围v
,如文档页面所述:
Run Code Online (Sandbox Code Playgroud)std::pair<out_edge_iterator, out_edge_iterator> edge_range(vertex_descriptor u, vertex_descriptor v, const adjacency_list& g)
返回一对外边迭代器,它给出从u到v的所有并行边的范围.此函数仅在adjacency_list的OutEdgeList是根据目标顶点对外边缘进行排序的容器时才有效,并允许平行边.multisetS选择器选择这样的容器.
这个函数之所以可用,multisetS
是因为为了向平行边提供(容易)一系列迭代器,你需要将边组合在一起,这是multisetS
因为它们按顶点描述符排序,并且因此,所有平行的外边缘都组合在一起.这是唯一能给你这个的容器选择,否则,你必须使用选项(1)(注意:创建一个filter_iterator
(参见文档)可以派上用场,如果你真的经常使用它).