[英]Printing the names of vertices in topological order with the Boost Graph Library
I am trying to use the Boost Graph Library. 我正在尝试使用Boost Graph Library。 I want to print out a topological sort for my graph.
我想为我的图形打印一种拓扑排序。 However the output I want on my graph is the actual names of the vertex, not the number positions.
但是我想在图形上输出的是顶点的实际名称,而不是数字位置。 For example, on the following example:
例如,在以下示例中:
typedef boost::adjacency_list<vecS, vecS, directedS,
property<vertex_name_t, std::string>,
property<edge_weight_t, int> > Graph;
typedef boost::graph_traits<Graph>::vertex_descriptor Vertex;
typedef std::vector< Vertex > container;
Graph g;
BOOST_CHECK(read_graphviz(in, g, dp, "id"));
container c;
topological_sort(g, std::back_inserter(c));
std::cout << "A topological ordering: ";
for ( container::reverse_iterator ii=c.rbegin(); ii!=c.rend(); ++ii)
std::cout <<*ii<<" ";
std:: cout <<std::endl;
I get the following output: 我得到以下输出:
A topological ordering: 45 40 41 34 35 33 43 30 31 36 32 26 27 25 23 24 19 46 47 18 48 17 20 21 49 50 16 51 15 44 14 22 42 13 37 38 9 11 28 29 12 7 39 6 8 5 10 3 4 0 2 1
Those values are the positions of the ordered vertices, but I would rather have the name of each vertex. 这些值是有序顶点的位置,但是我宁愿使用每个顶点的名称。 Does anyone know how to do this?
有谁知道如何做到这一点?
I've always felt it easier to work with custom Node/Edge classes when using BGL: 使用BGL时,我一直觉得使用自定义Node / Edge类更容易:
struct Node {
int x;
int y;
std::string name;
};
struct Edge {
int weight;
};
//boost:: qualifiers removed for brevity
typedef adjacency_list<vecS, vecS, directedS, Node, Edge> Graph;
...
{
Graph g;
Graph::vertex_descriptor v = ...;
g[v].x = 1;
std::cout << g[v].name << std::endl;
}
But yes, as *ii
is a vertex descriptor, you should be able to use it in the way @Tom Sirgedas mentioned. 但是可以,因为
*ii
是一个顶点描述符,所以您应该能够使用@Tom Sirgedas提到的方式使用它。 (Who posted his answer while I was writing mine) (谁在我写我的文章时发表了答案)
Hmm, this generic programming confuses me :) Try this code. 嗯,这种通用编程使我感到困惑:)尝试这段代码。 It should compile, at least!
至少应该编译!
std::cout << get(vertex_name, g, *ii) << " ";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.