Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add colored edge in 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;
   }       

Please tell me how to add coloured edge not coloured vertex. for example edge between vertex 0 and 1 I want it to give some colour to it for example red so all other edges should be of different colour and edge between vertex 0 and 1 should be red colour, how can I set that property.

like image 308
humanshu Avatar asked Oct 01 '22 04:10

humanshu


2 Answers

You can do this with a property writer.

Something along these lines will do the job:

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

Running this produces

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

which when input to the dot program gives:

enter image description here

like image 104
ravenspoint Avatar answered Oct 27 '22 08:10

ravenspoint


The manual says this should work with a PropertyWriter.

like image 23
martin Avatar answered Oct 27 '22 09:10

martin