是否可以在邻接列表中删除提升图中的顶点?
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());
检查顶点是否存在时,它返回 false,即顶点1
仍然存在。
不,labeled_graph_adapter不知道如何更新或删除标签,这意味着当相应的描述符失效时(例如,当删除相应的图形元素或在某些图形模型中添加任何其他顶点/边时),任何标签都将失效。
根据您使用的确切模型,可以通过简单地重新标记现有顶点来完成更新标签,但删除不是受支持的操作(只需扫描代码库以查找在_map
上执行的所有操作)。
咆哮笔记:
- labeled_graph适配器不是记录的库接口的一部分
- 过去有很多问题,例如:
干杯。请放心,我已经学会了艰难
labeled_graph
。坦率地说,我对它的维护状态感到有些震惊。编写一些更冗长的代码可能比处理泄漏的抽象更容易。当然,您可以自由地自己判断。如果你继续使用它,也许你可以改进它!- copy_graph - 具有捆绑属性的adjacency_list
- https://github.com/boostorg/graph/pull/58
相关文章:
- 如何循环打印顶点结构
- 将数组的地址分配给变量并删除
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- C/C++编译器通常会删除重复的库吗
- 从链接列表c++中删除一个项目
- C++如何通过用户输入删除列表元素
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 是否需要删除包含对象的"pair"?
- 如何在自删除后将对象设置为nullptr
- 迭代时从向量和内存中删除对象
- 使用函数"remove"删除重复元素
- 如何从多映射中删除特定的重复项
- 如何从网格中删除面及其顶点列表
- 是否可以在邻接列表中删除提升图中的顶点?
- 从提升::adjacency_list图中删除多个顶点
- 如何从 CGAL::Linear_cell_complex_for_combinatorial_map 中删除顶点、边或
- 从Boost Graph中删除多个顶点
- 为什么Boost图库在删除顶点时会使所有迭代器无效
- 顶点缓冲区对象(删除进程)opengl
- 使用提升图形库删除顶点后无法删除边