C ++的boost ::图从向图得到父顶点 [英] c++ boost::graph get parent vertices from directed graph
问题描述
我有向图(通过从升压::图形库中的adjacency_graph实现),我试图找到某个顶点的父顶点。
在过去的(通过pygraph)我只是颠倒了有向图,然后做了邻居的搜索,但似乎有逆转的boost :: reverse_graph图令我有向图成图的双向,所以我不能使用该adjacent_vertices方法了。
有没有更好的方式来获取父顶点?
感谢。
下面是我当前的例子code:
的#include<升压/图/ adjacency_list.hpp>
#包括LT&;升压/图/ reverse_graph.hpp>
#包括LT&;&iostream的GT;TYPEDEF提振::的adjacency_list<提高::套,提振::血管内皮细胞,促进:: directedS>图形;
TYPEDEF提振:: reverse_graph<&图表GT; Rgraph;
图表的typedef :: vertex_descriptor的顶点;诠释的main()
{
图图;
顶点V0 =提振:: add_vertex(图)
顶点V1 =升压:: add_vertex(图形);
顶点V2 =提振:: add_vertex(图)
顶点V3 =提振:: add_vertex(图)
顶点V4 =提振:: add_vertex(图)
顶点V5 =提振:: add_vertex(图)
顶点V6 =提振:: add_vertex(图) 升压::的add_edge(V0,V1,图表);
提高::的add_edge(V1,V2,图表);
提高::的add_edge(V2,V3,图表);
提高::的add_edge(V2,V4,图表);
提高::的add_edge(V3,V5,图表);
提高::的add_edge(V4,V5,图表);
提高::的add_edge(V5,V6,图表); 图:: adjacency_iterator ibegin,IEND;
对于(升压::领带(ibegin,IEND)=提振:: adjacent_vertices(V2,图表)!= ibegin IEND ++ ibegin)
{
性病::法院LT&;< * ibegin<<的std :: ENDL;
} 性病::法院LT&;<的std :: ENDL<< ############# ############# RGRAPH<<的std :: ENDL<<的std :: ENDL; Rgraph rgraph(图形);
Rgraph :: adjacency_iterator rbegin,分割;
对于(升压::领带(rbegin,撕裂)=提振:: adjacent_vertices(V2,rgraph); rbegin =撕裂;!++ rbegin)
{
性病::法院LT&;< * rbegin<<的std :: ENDL;
}
性病::法院LT&;<的std :: ENDL; 返回0;
}
reverse_graph
要求调整图是模型<一个href=\"http://www.boost.org/libs/graph/doc/BidirectionalGraph.html\"><$c$c>BidirectionalGraph$c$c>.如果你改变你的图表的typedef的boost ::的adjacency_list&LT;提高::套,提振::血管内皮细胞,促进:: bidirectionalS&GT;图;
你的程序编译,并给出结果:
3
4############# ############# RGRAPH1
这是我相信的是你应该期望什么。
这不需要 reverse_graph
(但仍需要 bidirectionalS
)是用另一种方式:
走势:: out_edge_iterator out_begin,out_end;
对于(升压::领带(out_begin,out_end)= out_edges(V2,图形); out_begin = out_end;!++ out_begin)
{
性病::法院LT&;&LT;目标(* out_begin,图表)&LT;&LT;的std :: ENDL;
}
性病::法院LT&;&LT;的std :: ENDL;图:: in_edge_iterator in_begin,in_end;
对于(升压::领带(in_begin,in_end)= in_edges(V2,图形); in_begin = in_end;!++ in_begin)
{
性病::法院LT&;&LT;源(* in_begin,图表)&LT;&LT;的std :: ENDL;
}
性病::法院LT&;&LT;的std :: ENDL;
I have a directed graph (implemented via an adjacency_graph from the boost::graph library) and I'm trying to find the parent vertices of a certain vertex.
In the past (via pygraph) I have simply reversed the digraph, then done a neighbours search, but it appears that reversing the graph with boost::reverse_graph turns my digraph into a bidirectional graph, and therefore I can't use the adjacent_vertices method anymore.
Is there a better way to get the parent vertices?
Thanks.
Here's my current example code:
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/reverse_graph.hpp>
#include <iostream>
typedef boost::adjacency_list< boost::setS, boost::vecS, boost::directedS > Graph;
typedef boost::reverse_graph<Graph> Rgraph;
typedef Graph::vertex_descriptor Vertex;
int main()
{
Graph graph;
Vertex v0 = boost::add_vertex(graph);
Vertex v1 = boost::add_vertex(graph);
Vertex v2 = boost::add_vertex(graph);
Vertex v3 = boost::add_vertex(graph);
Vertex v4 = boost::add_vertex(graph);
Vertex v5 = boost::add_vertex(graph);
Vertex v6 = boost::add_vertex(graph);
boost::add_edge(v0,v1,graph);
boost::add_edge(v1,v2,graph);
boost::add_edge(v2,v3,graph);
boost::add_edge(v2,v4,graph);
boost::add_edge(v3,v5,graph);
boost::add_edge(v4,v5,graph);
boost::add_edge(v5,v6,graph);
Graph::adjacency_iterator ibegin, iend;
for (boost::tie(ibegin, iend) = boost::adjacent_vertices(v2, graph); ibegin != iend; ++ibegin)
{
std::cout << *ibegin << std::endl;
}
std::cout << std::endl << "############# RGRAPH #############" << std::endl << std::endl;
Rgraph rgraph(graph);
Rgraph::adjacency_iterator rbegin, rend;
for (boost::tie(rbegin, rend) = boost::adjacent_vertices(v2, rgraph); rbegin != rend; ++rbegin)
{
std::cout << *rbegin << std::endl;
}
std::cout << std::endl;
return 0;
}
reverse_graph
requires that the adapted graph be a model of BidirectionalGraph
. If you change your graph to typedef boost::adjacency_list< boost::setS, boost::vecS, boost::bidirectionalS > Graph;
your program compiles and gives the result:
3
4
############# RGRAPH #############
1
that I believe is what you should expect.
Another way that does not require the reverse_graph
(but still requires bidirectionalS
) is to use:
Graph::out_edge_iterator out_begin, out_end;
for (boost::tie(out_begin, out_end) = out_edges(v2,graph); out_begin != out_end; ++out_begin)
{
std::cout << target(*out_begin,graph) << std::endl;
}
std::cout << std::endl;
Graph::in_edge_iterator in_begin, in_end;
for (boost::tie(in_begin, in_end) = in_edges(v2,graph); in_begin != in_end; ++in_begin)
{
std::cout << source(*in_begin,graph) << std::endl;
}
std::cout << std::endl;
这篇关于C ++的boost ::图从向图得到父顶点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!