【问题标题】:How to add colored edge in boost graph?如何在提升图中添加彩色边缘?
【发布时间】:2014-09-24 18:21:18
【问题描述】:
   int main()
   {
   using namespace std;
   using namespace boost;
   typedef adjacency_list< listS, vecS, directedS > digraph;

   // instantiate a digraph object with 8 vertices
   digraph g;


   // add some edges
   add_edge(0, 1, g);
   add_edge(1, 5, g);
   add_edge(5, 6, g);``
   add_edge(2, 3, g);
   add_edge(2, 4, g);

   // represent graph in DOT format and send to cout
   write_graphviz(cout, g);
   return 0;
   }       

请告诉我如何添加彩色边缘而不是彩色顶点。 例如顶点0和1之间的边缘我希望它给它一些颜色,例如红色 所以所有其他边应该是不同的颜色,顶点 0 和 1 之间的边应该是红色,我该如何设置该属性。

【问题讨论】:

    标签: c++ boost boost-graph


    【解决方案1】:

    您可以使用property writer 来完成此操作。

    这些方面的东西可以完成这项工作:

    #include <iostream>
    #include <boost/graph/graphviz.hpp>
    
    using namespace std;
    
      using namespace boost;
    
      typedef adjacency_list< listS, vecS, directedS > digraph;
    
        //  define a property writer to color the edges as required
      class color_writer {
      public:
    
          // constructor - needs reference to graph we are coloring
        color_writer( digraph& g ) : myGraph( g ) {}
    
          // functor that does the coloring
        template <class VertexOrEdge>
        void operator()(std::ostream& out, const VertexOrEdge& e) const {
    
            // check if this is the edge we want to color red
            if( source( e, myGraph ) == 0 &&
                target( e, myGraph ) == 1  )
                    out << "[color=red]";
        }
      private:
        digraph& myGraph;
      };
    
    int main()
    {
       using namespace std;
    
       // instantiate a digraph object with 8 vertices
       digraph g;
    
       // add some edges
       add_edge(0, 1, g);
       add_edge(1, 5, g);
       add_edge(5, 6, g);
       add_edge(2, 3, g);
       add_edge(2, 4, g);
    
    
       // represent graph in DOT format and send to cout 
       write_graphviz(cout, g,
            default_writer(),       // default ( do nothing ) vertex property writer
            color_writer( g ) );    // edge color property writer
       return 0;
    }
    

    运行这个会产生

    digraph G {
    0;
    1;
    2;
    3;
    4;
    5;
    6;
    0->1 [color=red];
    1->5 ;
    2->3 ;
    2->4 ;
    5->6 ;
    }
    

    当输入到点程序时给出:

    【讨论】:

      【解决方案2】:

      手册说这应该与PropertyWriter一起使用。

      【讨论】:

        【解决方案3】:

        我使用下面提到的代码使用 boost graph api 制作颜色边缘:

        #include <iostream>
        #include <string>
        #include <boost/graph/directed_graph.hpp>
        #include <boost/graph/labeled_graph.hpp>
        #include <boost/graph/graphviz.hpp>
        #include <boost/config.hpp>
        #include <boost/graph/adjacency_list.hpp>
        
        using namespace boost;
        using namespace std;
        
        
        template < typename Graph, typename VertexNameMap, typename EdgeNameMap > void
        print_dependencies(std::ostream & out, const Graph & g,
                           VertexNameMap name_map,EdgeNameMap edge_map)
        {
         //property_map<Graph, edge_name_t>::type name = get(edge_map, g);
          typename graph_traits < Graph >::edge_iterator ei, ei_end;
        cout<<"digraph G {"<<"\n";
         //for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
         //   out << get(name_map, source(*ei, g)) <<" ;" <<std::endl;
        
          for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
            out << get(name_map, source(*ei, g)) << "->"
              << get(name_map, target(*ei, g)) << " [color= "<<edge_map[*ei] <<"]"<<" ;" <<std::endl;
        
           cout<<"}"<<"\n";
        }
        
        
        
        int main() {
        
          using namespace boost;
          typedef boost::adjacency_list
          <
            //Store all edges as a std::vector
            boost::vecS,
            //Store all vertices in a std::vector
            boost::vecS,
            //Relations are both ways (in this example)
            //(note: but you can freely change it to boost::directedS)
            boost::directedS,
            //All vertices are person names of type std::string
            boost::property<boost::vertex_name_t,std::string>,
            //All edges are weights equal to the encounter frequencies
            // boost::property<boost::edge_weight_t,double>,
            boost::property<boost::edge_name_t,std::string>,
            //Graph itself has a std::string name
        //    boost::property< vertex_color_t, default_color_type >,
        
            boost::property<boost::graph_name_t,std::string>
          > Graph;
        
          Graph  g;
        
          typedef boost::graph_traits<Graph>::edge_descriptor ed;
          typedef std::pair<ed, bool> edgeName;
        
          typedef property<edge_name_t, string> EdgeProperty;
        
          string ab = "test";
          string cd = "hello";
        
          //All vertex names
          //Note: cannot use spaces
          std::vector<std::string> names;
          names.push_back("MrA");
          names.push_back("Mrs_B");
          names.push_back("Dr_C");
          names.push_back("Prof_D");
        
        
          // Graph::vertex_descriptor
        
          const Graph::vertex_descriptor v0 = boost::add_vertex(names[0],g);
          const Graph::vertex_descriptor v1 = boost::add_vertex(names[1],g);
          const Graph::vertex_descriptor v2 = boost::add_vertex(names[2],g);
          const Graph::vertex_descriptor v3 = boost::add_vertex(names[3],g);
          const Graph::vertex_descriptor v4 = boost::add_vertex(ab,g);
          boost::add_vertex(cd,g);
        
        
          boost::add_edge(v0, v1,EdgeProperty("red"), g);
          boost::add_edge(v1,v2,EdgeProperty("yellow"),g);
          boost::add_edge(v2,v3,EdgeProperty("red"),g);
          boost::add_edge(v3,v4,EdgeProperty("green"),g); 
          boost::add_edge(0,5,EdgeProperty("blue"),g);
        
          //write_graphviz(cout, g);
          print_dependencies(std::cout, g, get(vertex_name, g), get(edge_name, g) );
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-09-01
          • 1970-01-01
          • 1970-01-01
          • 2016-05-24
          • 2016-05-08
          • 2018-06-02
          相关资源
          最近更新 更多