C++ Boost库,如何获取相邻节点?

C++ Boost库,如何获取相邻节点?,c++,boost,C++,Boost,生成一个包含n个节点的图,并随机添加边之后,我将如何获得特定节点的所有邻居。是否有类似NetworkX的G.neights(i)的功能 这就是我到目前为止所得到的,创建邻接列表 #include <iostream> #include <boost/graph/adjacency_list.hpp> #include <boost/graph/connected_components.hpp> using namespace boost; using nam

生成一个包含
n
个节点的图,并随机添加边之后,我将如何获得特定节点的所有邻居。是否有类似NetworkX的
G.neights(i)
的功能

这就是我到目前为止所得到的,创建邻接列表

#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/connected_components.hpp>

using namespace boost;
using namespace std;

int main() {

    int N = 10000;

    struct status_t{
        typedef vertex_property_tag kind;
    };

    typedef
    property <status_t, string> status;

    typedef
    adjacency_list<vecS, vecS, undirectedS, status> MyGraph;

    MyGraph g (N);

    // add some random edges
    add_edge(0, 1, g);
    add_edge(100, 153, g);
    add_edge(634, 12, g);
    add_edge(94, 3, g);

    property_map<MyGraph, status_t>::type status_map = get(status_t(), g);

    for (int i = 0; i < 10; i++){
        status_map[i] = "S";
    }

    return 0;
}
#包括
#包括
#包括
使用名称空间boost;
使用名称空间std;
int main(){
int N=10000;
结构状态{
typedef vertex_property_标记种类;
};
类型定义
财产状况;
类型定义
邻接列表图;
myg(N);
//添加一些随机边
添加_边(0,1,g);
添加_边缘(100153,g);
添加_边(634,12,g);
添加_边(94,3,g);
属性映射::类型status\u map=get(status\u t(),g);
对于(int i=0;i<10;i++){
状态图[i]=“S”;
}
返回0;
}
像打印一样打印它们

for (auto vd : make_iterator_range(neighbours))
    std::cout << "94 has adjacent vertex " << vd << "\n";
如果只需要输出边,则假定为
定向
双向
,在这种情况下,还可以执行以下操作:

    for (auto ed : make_iterator_range(boost::out_edges(94, g)))
        std::cout << "outgoing: " << ed << "\n";
    for (auto ed : make_iterator_range(boost::in_edges(94, g)))
        std::cout << "incident: " << ed << "\n";

漂亮干净的代码。但是请注意,使用自定义属性并不复杂,现在可以使用属性包:
94 has adjacent vertex 93
94 has adjacent vertex 3
    for (auto ed : make_iterator_range(boost::out_edges(94, g)))
        std::cout << "outgoing: " << ed << "\n";
    for (auto ed : make_iterator_range(boost::in_edges(94, g)))
        std::cout << "incident: " << ed << "\n";
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/connected_components.hpp>
#include <iostream>

using namespace boost;
using namespace std;

int main() {
    int N = 10000;

    struct status_t { typedef vertex_property_tag kind; };
    typedef property<status_t, string> status;
    typedef adjacency_list<vecS, vecS, bidirectionalS, status> MyGraph;

    MyGraph g(N);

    // add some random edges
    add_edge(0, 1, g);
    add_edge(100, 153, g);
    add_edge(634, 12, g);

    add_edge(93, 94, g);
    add_edge(94, 3, g);

    property_map<MyGraph, status_t>::type status_map = get(status_t(), g);

    for (int i = 0; i < 10; i++) {
        status_map[i] = "S";
    }

    {
        auto neighbours = boost::adjacent_vertices(94, g);
        for (auto vd : make_iterator_range(neighbours))
            std::cout << "94 has adjacent vertex " << vd << "\n";

        // prints
        // for undirected:
        //  94 has adjacent vertex 93
        //  94 has adjacent vertex 3
        // for directed/bidirectionalS:
        //  94 has adjacent vertex 3
    }

    {   // for bidirectionalS:
        for (auto ed : make_iterator_range(boost::out_edges(94, g)))
            std::cout << "outgoing: " << ed << "\n";
        for (auto ed : make_iterator_range(boost::in_edges(94, g)))
            std::cout << "incident: " << ed << "\n";
    }

}
94 has adjacent vertex 3
outgoing: (94,3)
incident: (93,94)