简体   繁体   English

在BOOST图中找到给定2个顶点的多个边

[英]find multiple edges given 2 vertices in BOOST graph

I am using Boost Graph library for some project and i want to find number of times an edge is repeated in a graph. 我正在为一些项目使用Boost Graph库,我想在图中找到边重复的次数。 for example, 例如,

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)

suppose if i have two nodes, node1 and node2 and there is an edge between them (node1, node2). 假设我有两个节点,node1和node2,它们之间有一条边(node1,node2)。 edge property for each edge contains a timestamp start, end.. and there can be many such edges in the graph with different timestamps. 每个边的edge属性包含一个时间戳start,end ..并且图中可能有许多这样的边具有不同的时间戳。 for example. 例如。

edge1 = (node1, node2) with start = 100, end = 200.
edge2 = (node1, node2) with start = 250, end = 400.

I know that in a boost graph, given two vertices, we can find whether an edge exists in the graph or not using the following. 我知道在增强图中,给定两个顶点,我们可以使用以下内容查找图中是否存在边。

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;

But this could mean that it would only return any one edge even if multiple edges exist with different edge properties --> PROBLEM 但这可能意味着即使多个边缘存在不同的边缘属性,它也只会返回任何一个边缘 - >问题

can anyone suggest an idea how to get such multiple edges between two given nodes? 任何人都可以建议如何在两个给定节点之间获得这样的多边? Thanks! 谢谢!

There are a couple of ways to do this. 有几种方法可以做到这一点。

1) Just check the out-edges for all that go to the desired target: 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;

2) Specify boost::multisetS as the OutEdgeListS template argument to adjacency_list , this will enable an extra function called edge_range which returns a range of iterators for all the "parallel" edges coming out of u and going into v , as the documentation page states: 2)指定boost::multisetSOutEdgeListS模板参数adjacency_list ,这将使所谓的额外功能edge_range它返回一个迭代器范围内对所有的“水货”的边缘走出来的u并进入v ,作为文档页面状态:

 std::pair<out_edge_iterator, out_edge_iterator> edge_range(vertex_descriptor u, vertex_descriptor v, const adjacency_list& g) 

Returns a pair of out-edge iterators that give the range for all the parallel edges from u to v. This function only works when the OutEdgeList for the adjacency_list is a container that sorts the out edges according to target vertex, and allows for parallel edges. 返回一对外边迭代器,它给出从u到v的所有并行边的范围。此函数仅在adjacency_list的OutEdgeList是根据目标顶点对外边缘进行排序的容器时才有效,并允许平行边。 The multisetS selector chooses such a container. multisetS选择器选择这样的容器。

The reason why this function is only available for multisetS is because in order to provide (easily) a range of iterators to parallel edges, you need the edges to be grouped together, which is the case for multisetS because they are sorted by vertex-descriptor, and therefore, all parallel out-edges are grouped together. 此函数仅适用于multisetS的原因是,为了向并行边提供(容易)一系列迭代器,需要将边组合在一起,这就是multisetS的情况,因为它们按顶点描述符排序因此,所有平行的外边缘都被组合在一起。 That's the only container choice that will give you this, otherwise, you have to use option (1) (note: creating a filter_iterator (see docs ) could come in handy if you really use this a lot). 这是唯一能给你这个的容器选择,否则你必须使用选项(1)(注意:创建一个filter_iterator (参见docs )可以派上用场,如果你真的经常使用它)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM