Reputation: 139
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.
Upvotes: 5
Views: 1226
Reputation: 20457
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:
Upvotes: 2
Reputation: 139
I use below mentioned code to make colour edges using 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) );
}
Upvotes: 1