是否可以在邻接列表中删除提升图中的顶点?

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

本文关键字:顶点 删除 是否 列表      更新时间:2023-10-16

我创建了一个使用内部邻接列表的简单 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());检查顶点是否存在时,它返回 false,即顶点1仍然存在。

不,labeled_graph_adapter不知道如何更新或删除标签,这意味着当相应的描述符失效时(例如,当删除相应的图形元素或在某些图形模型中添加任何其他顶点/边时),任何标签都将失效

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

咆哮笔记:

  • labeled_graph适配器不是记录的库接口的一部分
  • 过去有很多问题,例如:
    • 干杯。请放心,我已经学会了艰难labeled_graph。坦率地说,我对它的维护状态感到有些震惊。编写一些更冗长的代码可能比处理泄漏的抽象更容易。当然,您可以自由地自己判断。如果你继续使用它,也许你可以改进它!

    • copy_graph - 具有捆绑属性的adjacency_list
    • https://github.com/boostorg/graph/pull/58