首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >boost中顶点的问题::子图

问boost中顶点的问题::子图
EN

Stack Overflow用户
提问于 2014-05-14 09:34:55
回答 1查看 600关注 0票数 3

The issue

文章底部的代码打印出来:

代码语言:javascript
复制
Vertices in g  = [ 0 1 2 3 4 ]
Vertices in g' = [ 0 1 ]

我的预期产出是:

代码语言:javascript
复制
Vertices in g  = [ 0 1 2 3 4 ]
Vertices in g' = [ 3 4 ]

这是boost::子图中的一个bug,还是在我对库的理解中?

问题中的代码

代码语言:javascript
复制
#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;
}
EN

回答 1

Stack Overflow用户

发布于 2014-05-14 10:02:11

我太懒了,看不懂文件。boost:区分“本地”和“全局”描述符的子图。

add_vertex函数在向子图添加顶点时使用全局描述符。函数返回本地描述符。

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

产出:

代码语言:javascript
复制
Vertices in g  = [ 0 1 2 3 4 ]
Vertices (local) in g' = [ 0 1 ]
Vertices (global) in g' = [ 3 4 ]

来自于代码:

代码语言:javascript
复制
#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;
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23650821

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档