繁体   English   中英

是否可以在邻接列表中的Boost Graph中删除顶点?

[英]Is it possible to delete a vertex in my Boost Graph in an adjacency list?

我创建了一个简单的带有内部邻接表的Boost标记图。 当我调用remove_vertex(v1, graph.graph()); ,我可以看到顶点数量已减少到1 ,但是当我检查顶点是否仍然存在时,它仍然返回true。

我已经试过graph.remove_vertex("1"); 以及remove_vertex(v1, graph.graph()); ,两者似乎都没有删除顶点。

#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/labeled_graph.hpp>
#include <cmath> // infinity

using namespace std;

struct EdgeProperties {
    double weight = INFINITY;
    EdgeProperties() = default;
    EdgeProperties(const double d) : weight(d) {}
};

struct Node
{
    string id;
    Node() = default;
    Node(const string i) : id(i) {} 
};

typedef boost::labeled_graph<boost::adjacency_list<boost::hash_setS, boost::hash_setS, boost::directedS, Node, EdgeProperties>, std::string> BoostGraph;
typedef BoostGraph::vertex_descriptor Vertex;
typedef BoostGraph::edge_descriptor Edge;

int main(){

    BoostGraph graph;

    const Vertex& v1 = add_vertex("1", Node("1"), graph);
    const Vertex& v2 = add_vertex("2", Node("2"), graph);

    const pair<Edge, bool>& edge = add_edge(v1, v2, EdgeProperties(INFINITY), graph);

    assert(2 == boost::num_vertices(graph));
    assert(1 == boost::num_edges(graph));
    assert(boost::edge(v1, v2, graph).second); // edge from v1->v2 exists

    // delete v1
    clear_vertex(v1, graph);
    graph.remove_vertex("1");

    assert(graph.vertex("1") == graph.null_vertex());

    assert(1 == boost::num_vertices(graph));
    assert(0 == boost::num_edges(graph));
    assert(not boost::edge(v1, v2, graph).second); // edge from v1->v2 shouldn't exist anymore

    cout << "All tests passed" << endl;
    return 0;
}

我可以看到assert(1 == boost::num_vertices(graph)); 正在工作,但是当我使用assert(graph.vertex("1") == graph.null_vertex());检查顶点是否存在时assert(graph.vertex("1") == graph.null_vertex()); ,则返回false,即顶点1仍然存在。

否,labeled_graph_adapter不知道如何更新或删除标签,这意味着当相应的描述符被无效任何标签将被无效 (例如,当相应的图形元素被删除,或在一些图模型时的任何其它顶点/边是添加)。

根据您使用的确切模型,可以通过简单地重新标记现有顶点来完成更新标签,但是不支持删除操作(只需扫描代码库以查看对_map执行的所有操作)。

出租注意事项:

暂无
暂无

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

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