如何在增强图中添加彩色边缘?

时间:2014-08-01 14:03:40

标签: c++ boost boost-graph

   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之间的边缘应该是红色,我该如何设置该属性。

3 个答案:

答案 0 :(得分:2)

您可以使用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 ;
}

当输入点程序时给出:

enter image description here

答案 1 :(得分:1)

手册说这应该适用于PropertyWriter

答案 2 :(得分:1)

我使用下面提到的代码使用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) );

}
相关问题