从boost :: labeled_graph获取节点标签 [英] Get node label from boost::labeled_graph
问题描述
我想在BGL的labelled_graph中检索带标签的节点的标签,但找不到找到此方法的方法.
I would like to retrieve the label of a labeled node in BGL's labeled_graph but cannot find a method to do this.
以下MWE演示了我在寻找什么:
The following MWE demonstrates what I am looking for:
//g++ -O3 question.cpp -o question.exe -I. --std=c++11 -lprotobuf-lite -lpthread -lz -losmpbf
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/labeled_graph.hpp>
#include <boost/graph/iteration_macros.hpp>
typedef long long node_id_t;
typedef boost::adjacency_list<
boost::listS, // Store out-edges of each vertex in a std::list
boost::listS, // Store vertex set in a std::list
boost::bidirectionalS, // The file dependency graph is directed
boost::no_property, // vertex properties
boost::no_property // edge properties
> AdjGraph;
typedef boost::labeled_graph<
AdjGraph,
node_id_t // Node ID
> LabeledGraph;
int main(){
LabeledGraph g;
add_vertex( 10, g );
add_vertex( 20, g );
add_vertex( 30, g );
add_vertex( 40, g );
add_vertex( 50, g );
boost::add_edge_by_label(10,30,g);
boost::add_edge_by_label(10,50,g);
boost::add_edge_by_label(30,40,g);
boost::add_edge_by_label(50,20,g);
BGL_FORALL_EDGES(e, g, LabeledGraph){
auto source_n = boost::source(e,g);
//How do I do the following?
//std::cerr<<boost::get_label_of_vertex(source_n)<<std::endl;
}
}
我的挖掘工作似乎不存在命令boost::get_label_of_vertex(source_n)
.
The command boost::get_label_of_vertex(source_n)
does not seem to exist in my diggings.
您知道吗,或者是否有另一种方式来获取此信息?
Do you know if it does, or if there is another way to get this information?
推荐答案
啊.我以错误的顺序找到了您的问题:)
Ah. I found your questions in the wrong order :)
我不认为labeled_graph<>
适配器具有此功能.相反,就像在我的其他答案中一样,我建议将标签存储为顶点属性(捆绑或vertex_index_t).
I don't think labeled_graph<>
adaptor has this feature. Instead, like in my other answer I suggest storing the label as a vertex property (bundled or vertex_index_t).
// g++ -O3 question.cpp -o question.exe -I. --std=c++11 -lprotobuf-lite -lpthread -lz -losmpbf
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/labeled_graph.hpp>
#include <boost/graph/iteration_macros.hpp>
struct MyVertex {
long long label;
MyVertex(long long label = 0) : label(label) {}
};
typedef boost::adjacency_list<boost::listS, // Store out-edges of each vertex in a std::list
boost::listS, // Store vertex set in a std::list
boost::bidirectionalS, // The file dependency graph is directed
MyVertex, // vertex properties
boost::no_property // edge properties
> AdjGraph;
int main() {
AdjGraph g;
std::map<size_t, AdjGraph::vertex_descriptor> vertex_map;
for (int i = 1; i<6; ++i)
vertex_map.emplace(10*i, add_vertex(10*i, g));
boost::add_edge(vertex_map[10], vertex_map[30], g);
boost::add_edge(vertex_map[10], vertex_map[50], g);
boost::add_edge(vertex_map[30], vertex_map[40], g);
boost::add_edge(vertex_map[50], vertex_map[20], g);
BGL_FORALL_EDGES(e, g, AdjGraph) {
auto source_n = boost::source(e, g);
std::cerr << g[source_n].label << "\n";
}
}
vertex_index_t
内部属性
vertex_index_t
interior property
Live On Coliru
// g++ -O3 question.cpp -o question.exe -I. --std=c++11 -lprotobuf-lite -lpthread -lz -losmpbf
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/labeled_graph.hpp>
#include <boost/graph/iteration_macros.hpp>
typedef boost::adjacency_list<boost::listS, // Store out-edges of each vertex in a std::list
boost::listS, // Store vertex set in a std::list
boost::bidirectionalS, // The file dependency graph is directed
boost::property<boost::vertex_index_t, size_t>, // vertex properties
boost::no_property // edge properties
> AdjGraph;
int main() {
AdjGraph g;
std::map<size_t, AdjGraph::vertex_descriptor> vertex_map;
for (int i = 1; i<6; ++i)
vertex_map.emplace(10*i, add_vertex(10*i, g));
boost::add_edge(vertex_map[10], vertex_map[30], g);
boost::add_edge(vertex_map[10], vertex_map[50], g);
boost::add_edge(vertex_map[30], vertex_map[40], g);
boost::add_edge(vertex_map[50], vertex_map[20], g);
auto index = boost::get(boost::vertex_index, g);
BGL_FORALL_EDGES(e, g, AdjGraph) {
auto source_n = boost::source(e, g);
std::cerr << boost::get(index, source_n) << "\n";
}
}
这篇关于从boost :: labeled_graph获取节点标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!