c++ - boost::subgraph 中的顶点问题

标签 c++ boost boost-graph

问题

帖子底部的代码打印出来:

Vertices in g  = [ 0 1 2 3 4 ]
Vertices in g' = [ 0 1 ]

我期望的输出是:

Vertices in g  = [ 0 1 2 3 4 ]
Vertices in g' = [ 3 4 ]

这是 boost::subgraph 中的错误还是我对库的理解错误?

有问题的代码

#include <sstream>
#include <iostream>

#include <boost/graph/subgraph.hpp>
#include <boost/graph/adjacency_list.hpp>

using namespace std;
using namespace boost;

// Underlying graph representation and implementation
typedef adjacency_list_traits<vecS, vecS, directedS> Traits;

// Graph representation
typedef subgraph< adjacency_list<vecS, vecS, directedS,
    property<vertex_color_t, int>, property<edge_index_t, int> > > Graph;

// Iterating over vertices and edges
typedef graph_traits<Graph>::vertex_iterator vertex_iter;
typedef graph_traits<Graph>::edge_iterator edge_iter;

int main(void)
{
    Graph g;
    add_edge(0,1, g);
    add_edge(1,2, g);
    add_edge(3,4, g);

    Graph sub = g.create_subgraph();
    add_vertex(3, sub);
    add_vertex(4, sub);

    pair<vertex_iter, vertex_iter> vip;

    cout << "Vertices in g  = [ ";
    vip = vertices(g);
    for(vertex_iter vi = vip.first; vi != vip.second; ++vi) {
        cout << *vi << " ";
    }
    cout << "]" << endl;

    cout << "Vertices in g' = [ ";
    vip = vertices(sub);
    for(vertex_iter vi = vip.first; vi != vip.second; ++vi) {
        cout << *vi << " ";
    }
    cout << "]" << endl;

    return 0;
}

最佳答案

我懒得看文档了。 boost::subgraph 区分“本地”和“全局”描述符。

add_vertex 函数在向子图添加顶点时使用全局描述符。 vertices() 函数返回局部描述符。

我需要做的是在子图上使用方法 local_to_global() 将局部描述符“解析”为我期望的全局描述符。

输出:

Vertices in g  = [ 0 1 2 3 4 ]
Vertices (local) in g' = [ 0 1 ]
Vertices (global) in g' = [ 3 4 ]

来自代码:

#include <sstream>
#include <iostream>

#include <boost/graph/subgraph.hpp>
#include <boost/graph/adjacency_list.hpp>

using namespace std;
using namespace boost;

// Underlying graph representation and implementation
typedef adjacency_list_traits<vecS, vecS, directedS> Traits;

// Graph representation
typedef subgraph< adjacency_list<vecS, vecS, directedS,
    property<vertex_color_t, int>, property<edge_index_t, int> > > Graph;

// Iterating over vertices and edges
typedef graph_traits<Graph>::vertex_iterator vertex_iter;
typedef graph_traits<Graph>::edge_iterator edge_iter;

int main(void)
{
    Graph g;
    add_edge(0,1, g);
    add_edge(1,2, g);
    add_edge(3,4, g);

    Graph sub = g.create_subgraph();
    add_vertex(3, sub);
    add_vertex(4, sub);

    pair<vertex_iter, vertex_iter> vip;

    cout << "Vertices in g  = [ ";
    vip = vertices(g);
    for(vertex_iter vi = vip.first; vi != vip.second; ++vi) {
        cout << *vi << " ";
    }
    cout << "]" << endl;

    cout << "Vertices (local) in g' = [ ";
    vip = vertices(sub);
    for(vertex_iter vi = vip.first; vi != vip.second; ++vi) {
        cout << *vi << " ";
    }
    cout << "]" << endl;

    cout << "Vertices (global) in g' = [ ";
    vip = vertices(sub);
    for(vertex_iter vi = vip.first; vi != vip.second; ++vi) {
        cout << sub.local_to_global(*vi) << " ";
    }
    cout << "]" << endl;

    return 0;
}

关于c++ - boost::subgraph 中的顶点问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23650821/

相关文章:

c++ - boost 路径指向的目录中文件的路径

c++ - 使用 boost spirit qi 解析器迭代填充 BGL 图

c++ - Boost Graph 通过 vertex_descriptor 访问属性

c++ - 如何复制 boost::property_map?

c++ - 在 Java EE Web 应用程序中打开 TCP 套接字

c# - P/Invoke 是否执行 DLL 然后将其关闭?

c++ - 分配兼容 vector ?

c++ - Cmake 将选项设置回默认值

C++ 编译陷入 boost interprocess lib 'error: `::ftruncate' 尚未声明'

c++ - “make_error_code”未在此范围内声明,并且在实例化时通过参数相关查找未找到任何声明