【问题标题】:find multiple edges given 2 vertices in BOOST graph在 BOOST 图中找到给定 2 个顶点的多条边
【发布时间】:2014-02-08 10:44:32
【问题描述】:

我正在为某个项目使用 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)

假设我有两个节点,node1 和 node2,并且它们之间有一条边(node1,node2)。每个边缘的边缘属性包含一个时间戳开始,结束......并且图中可以有许多具有不同时间戳的此类边缘。例如。

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

我知道在 boost 图中,给定两个顶点,我们可以使用以下方法找出图中是否存在边。

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;

但这可能意味着即使存在具有不同边缘属性的多条边,它也只会返回任何一条边 --> 问题

谁能提出一个想法,如何在两个给定节点之间获得如此多的边?谢谢!

【问题讨论】:

    标签: c++ boost graph boost-graph


    【解决方案1】:

    有几种方法可以做到这一点。

    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) 将boost::multisetS 指定为adjacency_listOutEdgeListS 模板参数,这将启用一个名为edge_range 的额外函数,该函数为来自u 的所有“平行”边返回一系列迭代器并进入v,正如documentation page 所述:

    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(请参阅 docs)可能会派上用场)。

    【讨论】:

    • 嗨 Mikael,我刚刚在下面发布了一个答案,我有点卡住了。你能推荐一下吗?
    猜你喜欢
    • 1970-01-01
    • 2019-10-23
    • 2021-12-08
    • 1970-01-01
    • 2011-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-25
    相关资源
    最近更新 更多