将boost图仅包含两个顶点,当添加一条边时。

3

我正在使用Boost库处理C++中的图形。

boost::adjacency_list <boost::vecS, boost::vecS, boost::bidirectionalS> DiGraph;
DiGraph graph;
boost::add_edge(10000, 20000, graph);

这个图形包含20001个顶点和一条边。然而,我只需要两个顶点1000020000

一种可能的方法是将数据与节点关联起来(例如从此示例中提取); 但是,也许有更简单的方法可以完成这个任务。如果顶点不是(0,1),如何创建一个具有2个顶点和一条边的图形?

Python Networkx 中,我只需使用 graph.add_edge(10000,20000) 就能获得所需的行为。有什么线索可以帮助我在C ++中实现这个呢?


默认行为确实是让索引识别顶点。因此,如果您只在顶点10和20之间创建边,则图将包含21个顶点。您需要的是具有自由标识符的顶点模型,因此,请研究链接的示例,它包含您所需的内容。好处是可以在顶点中放入任何您想要的内容。 - kebs
嗯,我确实更喜欢以某种方式避免这种情况。 - Sergey Ivanov
1个回答

1

您应该使用基于节点的容器选择器来选择顶点集合。

例如,使用 listS

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_utility.hpp>

int main() {
    typedef boost::adjacency_list <boost::vecS, boost::listS, boost::bidirectionalS, int> DiGraph;
    DiGraph graph;

    DiGraph::vertex_descriptor 
        v1 = add_vertex(100000, graph),
        v2 = add_vertex(200000, graph);

    boost::add_edge(v1, v2, graph);
    boost::print_graph(graph, boost::get(boost::vertex_bundle, graph));
}

请注意,现在不再免费提供vertex_index(您必须为所有算法手动传递/维护它)。
好处是,迭代器和描述符是稳定的(除非被删除)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接