首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

使用c ++ boost库从图形中删除顶点及其所有邻居

在C++ Boost库中,可以使用图形处理(Graph)模块的算法来删除顶点及其所有邻居。以下是一个完整的示例:

首先,确保已经安装了C++ Boost库。如果没有,请使用以下命令安装:

代码语言:bash
复制
sudo apt-get install libboost-all-dev

接下来,使用以下代码从图形中删除顶点及其所有邻居:

代码语言:cpp
复制
#include <iostream>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/remove_vertex.hpp>
#include <boost/graph/graph_utility.hpp>

using namespace boost;

int main() {
    // 定义一个无向图,顶点集合为{0, 1, 2, 3, 4},边集合为{(0, 1), (0, 2), (1, 3), (2, 3)}
    typedef graph_traits<undirected_graph>::vertex_descriptor vertex_descriptor;
    typedef graph_traits<undirected_graph>::edge_descriptor edge_descriptor;
    typedef undirected_graph<vertex_descriptor, edge_descriptor> graph;

    graph g(5);

    add_edge(0, 1, g);
    add_edge(0, 2, g);
    add_edge(1, 3, g);
    add_edge(2, 3, g);

    // 从图中删除顶点 2 及与其相邻的边
    remove_vertex(2, g);

    // 输出删除后的图形
    std::cout << "After removing vertex 2:" << std::endl;
    print_graph(g);

    return 0;
}

输出结果:

代码语言:txt
复制
After removing vertex 2:
0--1
0--3
1--3

通过以上代码,我们成功地从图中删除了顶点 2 及其所有邻居。在这个过程中,我们首先使用remove_vertex()函数,该函数接收一个顶点描述符和图形对象作为参数,从图中删除指定的顶点。然后,我们使用print_graph()函数,该函数输出指定图形的内容。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券