C++ boost::子图中的顶点问题
问题 文章底部的代码打印出来:C++ boost::子图中的顶点问题,c++,boost,boost-graph,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/gr
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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
使用名称空间boost;
//底层图的表示与实现
类型定义邻接列表特征;
//图形表示
typedef子图<邻接列表>图;
//在顶点和边上迭代
typedef图形特征::顶点迭代器顶点迭代器;
typedef图形特征::边迭代器边迭代器;
内部主(空)
{
图g;
添加_边(0,1,g);
添加_边(1,2,g);
添加_边(3,4,g);
Graph sub=g.创建_子图();
添加_顶点(3,子顶点);
添加_顶点(4,子顶点);
结对贵宾;
我懒得看文档。
区分“局部”和“全局”描述符的子图
向子图添加顶点时,“添加顶点”函数使用全局描述符。
函数的作用是:返回局部描述符
我需要做的是,在子图上使用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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
使用名称空间boost;
//底层图的表示与实现
类型定义邻接列表特征;
//图形表示
typedef子图<邻接列表>图;
//在顶点和边上迭代
typedef图形特征::顶点迭代器顶点迭代器;
typedef图形特征::边迭代器边迭代器;
内部主(空)
{
图g;
添加_边(0,1,g);
添加_边(1,2,g);
添加_边(3,4,g);
Graph sub=g.创建_子图();
添加_顶点(3,子顶点);
添加_顶点(4,子顶点);
结对贵宾;
是的,它们是子图中的第一个和第二个顶点,所以它们使用局部索引0和1。