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.
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:
The manual says this should work with a PropertyWriter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With