从图中移除顶点
c++ remove vertex from a graph
本文关键字:顶点 更新时间:2023-10-16
以下代码使用boost.1.46.1进行编译
#include <boost/graph/adjacency_list.hpp>
struct Node {
int id;
};
struct Edge {
int source;
int target;
int weight;
};
int main() {
/* an adjacency_list like we need it */
typedef boost::adjacency_list<
boost::setS, // edge container
boost::listS, // vertex container
boost::bidirectionalS, // directed graph
Node, Edge> Graph;
typedef boost::graph_traits<Graph>::vertex_descriptor Vertex;
Graph gp1;
std::cout << "Number of vertices 1: " << boost::num_vertices(gp1) << std::endl;
Vertex v1 = boost::add_vertex(gp1);
Vertex v2 = boost::add_vertex(gp1);
std::cout << "Number of vertices 2: " << boost::num_vertices(gp1) << std::endl;
gp1[v1].id = 3;
gp1[v2].id = 4;
Graph gp2(gp1);
std::cout << "Number of vertices 3: " << boost::num_vertices(gp2) << std::endl;
boost::remove_vertex(v2, gp2);
std::cout << "Number of vertices 4: " << boost::num_vertices(gp1) << std::endl;
std::cout << "Number of vertices 5: " << boost::num_vertices(gp2) << std::endl;
boost::graph_traits<Graph>::vertex_iterator it, end;
for (boost::tie( it, end ) = vertices(gp2); it != end; ++it) {
if ( gp2[*it].id == 3 ) {
boost::remove_vertex(*it, gp2);
}
}
std::cout << "Number of vertices 6: " << boost::num_vertices(gp1) << std::endl;
std::cout << "Number of vertices 7: " << boost::num_vertices(gp2) << std::endl;
return 0;
}
gp2是如何知道v2在:"boost::remove_vertex(v2, gp2)"为什么gp1的顶点数减少了1?
为什么在:"boost::remove_vertex(*it, gp2)"会出现分割错误?我该怎么补救呢?
请注意,sehe的解决方案仅适用于具有VertexList=listS的图形,尤其不适用于具有VertexList=vecS的图形。还要注意的是,通常你不能存储顶点描述符或迭代器,之后再删除它们,因为从Boost Graph Library网站:
void remove_vertex(vertex_descriptor u, adjacency_list&g)
…如果adjacency_list的VertexList模板参数为vecS,然后为all图的顶点描述符、边描述符和迭代器是此操作无效。的内置vertex_index_t属性每个顶点被重新编号,以便在操作后顶点索引仍然形成一个连续的范围[0,num_vertices(g)). ...
在迭代顶点集合时正在修改它。
首先收集要移除的顶点,然后移除它们。或者使用以下模式:
// Remove all the vertices. This is OK.
graph_traits<Graph>::vertex_iterator vi, vi_end, next;
tie(vi, vi_end) = vertices(G);
for (next = vi; vi != vi_end; vi = next) {
++next;
remove_vertex(*vi, G);
}
样本取自本页:http://www.boost.org/doc/libs/1_47_0/libs/graph/doc/adjacency_list.html(这是谷歌返回当你寻找remove vertices boost graph
)
编辑
快速翻译到您的示例中:
boost::graph_traits<Graph>::vertex_iterator vi, vi_end, next;
boost::tie(vi, vi_end) = vertices(gp2);
for (next = vi; vi != vi_end; vi = next) {
++next;
if (gp2[*vi].id == 3)
remove_vertex(*vi, gp2);
}
输出:Number of vertices 1: 0
Number of vertices 2: 2
Number of vertices 3: 2
Number of vertices 4: 1
Number of vertices 5: 2
Number of vertices 6: 1
Number of vertices 7: 1
不再崩溃:)
相关文章:
- 如何循环打印顶点结构
- D3D11-将混合权重和索引传递到顶点着色器
- 从返回的顶点缓冲区查询顶点结构
- Vulkan 中的动态顶点缓冲区格式设置
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- 如何将一半传递给顶点着色器?
- 在 DirectX 11 中从 GPU 读回顶点缓冲区(并获取顶点)
- QT QOpenGLWidget:如何在不使用数据块复制的情况下修改VBO中的单个顶点值?
- 使用 OpenGL 4.5 更改所选顶点的颜色
- Direct3D 11 - HLSL - 获取顶点索引 ID
- 创建异构顶点数据数组的可移植方法
- 使用 glvertex4i 传递网格面索引时的顶点着色器错误
- 无法从 cso 文件创建顶点着色器(从 fx 文件创建)
- OpenGL 相机移动程序顶点着色器问题
- 使用 std::list 存储顶点并使用 SFML 绘制它们
- 为什么顶点数组对象会导致错误?
- 从 CGAL 3D 网格生成中获取顶点坐标的 -6.27744e+66:mesh_implicit_sphere示例
- 将顶点位置从顶点传递到片段着色器 - 仅在使用 Nsight 进行调试时有效
- FBX SDK 在预期 3 个顶点时返回 4 个顶点
- 我对 MyGraph 属性顶点名称和边权重有问题