How to print a graph in graphviz with multiple properties displayed

后端 未结 1 2001
别那么骄傲
别那么骄傲 2020-12-17 01:23

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

I am using bundled properties:

 typedef struct vert{
   std::stri         


        
相关标签:
1条回答
  • 2020-12-17 01:32

    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.

    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.

    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);
    }
    

    And finally your write_graphviz invocation would simply be:

    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)));
    
    0 讨论(0)
提交回复
热议问题