【问题标题】:BGL: Custom Property Writer for Bundled Properties and listS as VertexList/EdgeListBGL:捆绑属性和列表的自定义属性编写器作为 VertexList/EdgeList
【发布时间】:2019-03-27 13:46:37
【问题描述】:

我从this 帖子中获取以下代码,并将 VertexList 和 EdgeList 更改为 listS 而不是 vecS。

我发现由于缺少索引make_label_writer(get(&VertexProps::name, g)) 不起作用。有人可以告诉我应该如何更改代码以使其正常工作。我更喜欢带有自定义属性编写器的解决方案。

非常感谢。

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>

struct VertexProps { std::string name; };
struct EdgeProps   { std::string name; };
typedef boost::adjacency_list<boost::listS, boost::listS, boost::directedS, VertexProps, EdgeProps> Graph;

int main() {
    Graph g(3);
    g[0].name = "one";
    g[1].name = "two";
    g[2].name = "three";
    add_edge(1, 0, {"e1"}, g);
    add_edge(2, 1, {"e2"}, g);
    add_edge(1, 2, {"e3"}, g);
    add_edge(2, 0, {"e4"}, g);

write_graphviz(std::cout, g,
        make_label_writer(get(&VertexProps::name, g)),
        make_label_writer(get(&EdgeProps::name, g)));

}


为了完整起见,这里是sehe 使用自定义属性编写器对解决方案进行的修改。请不要忘记支持原始答案。

Wandbox

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>

struct VertexProps { std::string name; };
struct EdgeProps   { std::string name; };
typedef boost::adjacency_list<boost::listS, boost::listS, boost::directedS, VertexProps, EdgeProps> Graph;

template <class Name>
class my_label_writer {
public:
    my_label_writer(Name _name) : name(_name) {}
    template <class VertexOrEdge>
    void operator()(std::ostream& out, const VertexOrEdge& v) const {
        out << "[label=\"" << name[v].name << "\"]";
    }
private:
    Name name;
};


int main() {
    Graph g;
    Graph::vertex_descriptor v0 = add_vertex({"one"}, g);
    Graph::vertex_descriptor v1 = add_vertex({"two"}, g);
    Graph::vertex_descriptor v2 = add_vertex({"three"}, g);
    add_edge(v1, v0, {"e1"}, g);
    add_edge(v2, v1, {"e2"}, g);
    add_edge(v1, v2, {"e3"}, g);
    add_edge(v2, v0, {"e4"}, g);

    std::map<Graph::vertex_descriptor, int> vertex_index;
    for (auto vd : boost::make_iterator_range(vertices(g)))
        vertex_index[vd] = vertex_index.size();

    my_label_writer<Graph> w(g);

    write_graphviz(std::cout, g,
        w,
        make_label_writer(get(&EdgeProps::name, g)),
        boost::default_writer{}, // graph_property_writer
        boost::make_assoc_property_map(vertex_index));
}

【问题讨论】:

标签: boost boost-graph


【解决方案1】:

listS 的顶点描述符类型不是整数,因此不适合作为顶点索引。

您现在需要使用实际的描述符:

Graph g;
Graph::vertex_descriptor v0 = add_vertex({"one"}, g);
Graph::vertex_descriptor v1 = add_vertex({"two"}, g);
Graph::vertex_descriptor v2 = add_vertex({"three"}, g);
add_edge(v1, v0, {"e1"}, g);
add_edge(v2, v1, {"e2"}, g);
add_edge(v1, v2, {"e3"}, g);
add_edge(v2, v0, {"e4"}, g);

此外,在编写时,您必须提供 vertex_index 属性映射。顺便说一句,这需要您通过graph_property_writer 备考:

std::map<Graph::vertex_descriptor, int> vertex_index;
for (auto vd : boost::make_iterator_range(vertices(g)))
    vertex_index[vd] = vertex_index.size();

write_graphviz(std::cout, g,
    make_label_writer(get(&VertexProps::name, g)),
    make_label_writer(get(&EdgeProps::name, g)),
    boost::default_writer{}, // graph_property_writer
    boost::make_assoc_property_map(vertex_index));

演示

Live On Wandbox

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>

struct VertexProps { std::string name; };
struct EdgeProps   { std::string name; };
typedef boost::adjacency_list<boost::listS, boost::listS, boost::directedS, VertexProps, EdgeProps> Graph;

int main() {
    Graph g;
    Graph::vertex_descriptor v0 = add_vertex({"one"}, g);
    Graph::vertex_descriptor v1 = add_vertex({"two"}, g);
    Graph::vertex_descriptor v2 = add_vertex({"three"}, g);
    add_edge(v1, v0, {"e1"}, g);
    add_edge(v2, v1, {"e2"}, g);
    add_edge(v1, v2, {"e3"}, g);
    add_edge(v2, v0, {"e4"}, g);

    std::map<Graph::vertex_descriptor, int> vertex_index;
    for (auto vd : boost::make_iterator_range(vertices(g)))
        vertex_index[vd] = vertex_index.size();

    write_graphviz(std::cout, g,
        make_label_writer(get(&VertexProps::name, g)),
        make_label_writer(get(&EdgeProps::name, g)),
        boost::default_writer{}, // graph_property_writer
        boost::make_assoc_property_map(vertex_index));
}

打印

digraph G {
0[label=one];
1[label=two];
2[label=three];
1->0 [label=e1];
1->2 [label=e3];
2->1 [label=e2];
2->0 [label=e4];
}

【讨论】:

  • 太棒了!非常感谢。顶点索引属性映射正是我需要的提示。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-18
  • 2016-01-25
  • 2015-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多