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 之间的边应该是红色,我该如何设置该属性。
最佳答案
您可以使用 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 ;
}
当输入点程序时给出:
关于c++ - 如何在 boost 图中添加彩色边?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25082053/