如何打印图形与显示多个属性的Graphviz [英] How to print a graph in graphviz with multiple properties displayed

查看:245
本文介绍了如何打印图形与显示多个属性的Graphviz的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是基于关闭的:<一href=\"http://stackoverflow.com/questions/9181183/how-to-print-a-boost-graph-in-graphviz-with-one-of-the-properties-displayed\">How打印图表,单个属性显示

My question is based off of: How to print a graph with a single property displayed

我使用捆绑的属性:

 typedef struct vert{
   std::string name;
 };

 typedef struct edge{
   int capacity;
   int weight;
 }; 

typedef adjacency_list<listS, vecS, undirectedS, vert, edge> Graph;
Graph g;
vector<int,int> ele;

我有以下的称为一个循环,的创建边缘:

  edge prop;
  prop.weight = 5;
  prop.capacity = 4;
  add_edge(ele.first,ele.second, prop, g);

本段是打印图形以圆点格式。

This segment is what prints the graph to dot format.

ofstream dot("graph.dot");
write_graphviz(dot, g, 
  boost::make_label_writer(boost::get(&vert::name, g)),
  boost::make_label_writer(boost::get(&edge::weight, g)),
  boost::make_label_writer(boost::get(&edge::capacity, g)));

该错误是:

/usr/include/boost/graph/graphviz.hpp: In function ‘void boost::write_graphviz(std::ostream&, const Graph&, VertexPropertiesWriter, EdgePropertiesWriter, GraphPropertiesWriter, VertexID) [with Graph = boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, vert, edge, boost::no_property, boost::listS>, VertexPropertiesWriter = boost::label_writer<boost::bundle_property_map<boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, vert, edge, boost::no_property, boost::listS>, long unsigned int, vert, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, EdgePropertiesWriter = boost::label_writer<boost::bundle_property_map<boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, vert, edge, boost::no_property, boost::listS>, boost::detail::edge_desc_impl<boost::undirected_tag, long unsigned int>, edge, int> >, GraphPropertiesWriter = boost::label_writer<boost::bundle_property_map<boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, vert, edge, boost::no_property, boost::listS>, boost::detail::edge_desc_impl<boost::undirected_tag, long unsigned int>, edge, int> >, VertexID = boost::vec_adj_list_vertex_id_map<boost::property<boost::vertex_bundle_t, vert, boost::no_property>, long unsigned int>]’:
/usr/include/boost/graph/graphviz.hpp:260:   instantiated from ‘void boost::write_graphviz(std::ostream&, const Graph&, VertexPropertiesWriter, EdgePropertiesWriter, GraphPropertiesWriter) [with Graph = Graph, VertexPropertiesWriter = boost::label_writer<boost::bundle_property_map<boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, vert, edge, boost::no_property, boost::listS>, long unsigned int, vert, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, EdgePropertiesWriter = boost::label_writer<boost::bundle_property_map<boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, vert, edge, boost::no_property, boost::listS>, boost::detail::edge_desc_impl<boost::undirected_tag, long unsigned int>, edge, int> >, GraphPropertiesWriter = boost::label_writer<boost::bundle_property_map<boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, vert, edge, boost::no_property, boost::listS>, boost::detail::edge_desc_impl<boost::undirected_tag, long unsigned int>, edge, int> >]’
file_format.cc:194:   instantiated from here
/usr/include/boost/graph/graphviz.hpp:236: error: no match for call to ‘(boost::label_writer<boost::bundle_property_map<boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, vert, edge, boost::no_property, boost::listS>, boost::detail::edge_desc_impl<boost::undirected_tag, long unsigned int>, edge, int> >) (std::basic_ostream<char, std::char_traits<char> >&)’

奇怪的是我,因为这个作品:

It is strange to me because this works:

write_graphviz(dot, g,
  boost::make_label_writer(boost::get(&vert_info::name, g)));

和输出如下:

graph G {
0[label="0"];
1[label="1"];
2[label="2"];
3[label="3"];
4[label="4"];
5[label="5"];
6[label="6"];
7[label="7"];
8[label="8"];
9[label=""];  // this is another problem that I will have to fix but beside the point
0--9 ;
0--5 ;
0--2 ;
0--1 ;
...
...
}

我的目标是让每个节点的标识,每条边标有它的容量和重量。

My goal is to have each node labeled, and each edge labelled with its capacity and weight.

推荐答案

您可以找到 的所有重载为 write_graphviz 。之所以你的第一个错误是您尝试使用过载预计在其第五个参数的图形性能作家。

You can find here the list of all the overloads for write_graphviz. The reason for your first error is that the overload you tried to use expects a graph property writer in its fifth argument.

辅助函数 make_label_writer 简单地创建一个属性的作家,从一个顶点或命名您的图形到一个graphviz的顶点或边属性的边缘<$分配一个单独的属性C $ C>标签

The helper function make_label_writer simply creates a property writer that assigns a single property from either a vertex or an edge of your graph to a graphviz vertex or edge attribute named label.

为了达到你想要什么,你需要创建一个自定义的<一个href=\"http://www.boost.org/libs/graph/doc/write-graphviz.html#concept%3aPropertyWriter\"><$c$c>property作家 在其中指定每个边的属性到 graphviz的属性你需要。我会亲自使用重量 - > 标签容量 - > taillabel headlabel

In order to achieve what you want you need to create a custom property writer in which you assign each of your edge properties to the graphviz attributes you need. I would personally use weight->label and capacity->taillabel or headlabel.

template <class WeightMap,class CapacityMap>
class edge_writer {
public:
  edge_writer(WeightMap w, CapacityMap c) : wm(w),cm(c) {}
  template <class Edge>
  void operator()(ostream &out, const Edge& e) const {
    out << "[label=\"" << wm[e] << "\", taillabel=\"" << cm[e] << "\"]";
  }
private:
  WeightMap wm;
  CapacityMap cm;
};

template <class WeightMap, class CapacityMap>
inline edge_writer<WeightMap,CapacityMap> 
make_edge_writer(WeightMap w,CapacityMap c) {
  return edge_writer<WeightMap,CapacityMap>(w,c);
}

最后你的 write_graphviz 调用,简直是:

ofstream dot("graph.dot");
write_graphviz(dot, g, 
  boost::make_label_writer(boost::get(&vert::name, g)),
  make_edge_writer(boost::get(&edge::weight,g),boost::get(&edge::capacity,g)));

这篇关于如何打印图形与显示多个属性的Graphviz的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆