添加一个edg时,使boost图仅包含两个顶点

2024-10-06 12:29:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用Boost库来处理C++中的图表。你知道吗

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

graph包含20001个顶点和一条边。然而,我只需要有两个顶点1000020000。你知道吗

一种可能的方法是将数据与节点相关联(例如,从this example);然而,可能有一种更简单的方法来实现这一点。如果顶点不是(0,1),我怎么能有一个有两个顶点和一条边的图呢?你知道吗

python networkx中,我将简单地使用graph.add_edge(10000, 20000),并获得所需的行为。有什么线索在C++中如何做到这一点?你知道吗


Tags: 数据方法add节点图表listgraph顶点
1条回答
网友
1楼 · 发布于 2024-10-06 12:29:22

应该为顶点集合使用基于节点的容器选择器。你知道吗

例如,使用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现在不是免费的(您必须为所有算法手动传递/维护它)。你知道吗

另一方面,迭代器和描述符是稳定的(除非删除)。你知道吗

相关问题 更多 >