获取相邻顶点的Boost图 [英] Boost Graph Getting Adjacent Vertices
问题描述
我正在使用Boost库进行图形绘制。
该图的定义如下。
typedef boost :: adjacency_list< boost :: setS,boost :: setS ,boost :: undirectedS,uint32_t,float>邻接表;
使用适当的数据创建图形后,在一个单独的函数中,我想打印每个顶点的adjacent_vertices以及它们在开始时计算的适当的边缘权重。
创建部分工作正常,但是当我想提取相邻顶点时,我不会获取值。
typedef boost :: graph_traits< AdjacencyList> :: adjacency_iterator AdjacencyIterator;
AdjacencyList :: vertex_iterator i,end;
for(boost :: tie(i,end)= boost :: vertices(adjacency_list); i!= end; i ++){
AdjacencyIterator ai,a_end;
boost :: tie(ai,a_end)= boost :: adjacent_vertices(* i,adjacency_list);
for(; ai!= a_end; ai ++){
std :: cout<< * ai<< \t;
$ / code $ / pre
$ b $ p我得到的输出是十六进制数的内存地址。
如何得到顶点索引和边权重?解决方案您应该访问属性捆绑包,使用顶点/边描述符或使用属性映射:
使用<$ c>
运算符[]
$ c> operator []
#include< boost / graph / adjacency_list.hpp>
#include< iostream>
typedef boost :: adjacency_list< boost :: setS,boost :: setS,boost :: undirectedS,uint32_t,float>邻接表;
typedef boost :: graph_traits< AdjacencyList> :: adjacency_iterator AdjacencyIterator;
int main(){
AdjacencyList adjacency_list ;;
boost :: add_edge(
boost :: add_vertex(10,adjacency_list),
boost :: add_vertex(20,adjacency_list),
1.5f,
adjacency_list
);
boost :: add_edge(
boost :: add_vertex(30,adjacency_list),
boost :: add_vertex(40,adjacency_list),
2.5f,
adjacency_list
);
AdjacencyList :: vertex_iterator i,end; (boost :: tie(i,end)= boost :: vertices(adjacency_list); i!= end; i ++){
AdjacencyIterator ai,a_end;
boost :: tie(ai,a_end)= boost :: adjacent_vertices(* i,adjacency_list);
for(; ai!= a_end; ai ++){
std :: cout<< adjacency_list [* ai]<< \t;
}
}
}
输出:
10 20 30 40
使用属性映射:
boost :: property_map< AdjacencyList,boost :: vertex_bundle_t> :: type pmap = boost :: get(boost :: vertex_bundle,adjacency_list);
现在您可以使用
boost :: get(pmap,vertex_descriptor1) code>来访问顶点属性包
I am working with a graphing using Boost library. The graph is defined as follows.
typedef boost::adjacency_list<boost::setS,boost::setS,boost::undirectedS, uint32_t, float> AdjacencyList;
After creating the graph using appropriate data, in a separate function I want to print the adjacent_vertices of each vertex with their appropriate edge weight as computed in the beginning.
The creating part works well but when I want to extract adjacent vertices I dont get the values.
typedef boost::graph_traits<AdjacencyList>::adjacency_iterator AdjacencyIterator; AdjacencyList::vertex_iterator i, end; for (boost::tie(i, end) = boost::vertices(adjacency_list); i != end; i++) { AdjacencyIterator ai, a_end; boost::tie(ai, a_end) = boost::adjacent_vertices( *i, adjacency_list); for (; ai != a_end; ai++) { std::cout << *ai << "\t"; } }
The Output I get are memory address in Hexademial number. How can I get vertex indices and the edge weight?
解决方案You should access the property bundles, either using the graph's
operator[]
with the vertex/edge descriptor, or using the property map:Using the
operator[]
#include <boost/graph/adjacency_list.hpp> #include <iostream> typedef boost::adjacency_list<boost::setS,boost::setS,boost::undirectedS, uint32_t, float> AdjacencyList; typedef boost::graph_traits<AdjacencyList>::adjacency_iterator AdjacencyIterator; int main() { AdjacencyList adjacency_list;; boost::add_edge( boost::add_vertex(10, adjacency_list), boost::add_vertex(20, adjacency_list), 1.5f, adjacency_list ); boost::add_edge( boost::add_vertex(30, adjacency_list), boost::add_vertex(40, adjacency_list), 2.5f, adjacency_list ); AdjacencyList::vertex_iterator i, end; for (boost::tie(i, end) = boost::vertices(adjacency_list); i != end; i++) { AdjacencyIterator ai, a_end; boost::tie(ai, a_end) = boost::adjacent_vertices(*i, adjacency_list); for (; ai != a_end; ai++) { std::cout << adjacency_list[*ai] << "\t"; } } }
Output:
10 20 30 40
Using the property map:
boost::property_map<AdjacencyList, boost::vertex_bundle_t>::type pmap = boost::get(boost::vertex_bundle, adjacency_list);
Now you can use
boost::get(pmap, vertex_descriptor1)
to access the vertex property bundle这篇关于获取相邻顶点的Boost图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!