グラフを作りたかったので、graphvixを使ってみました。
【参考】Boost.Graph Graphviz形式で重みを出力 #include <fstream> #include <vector> #include <string> #include <boost/graph/adjacency_list.hpp> #include <boost/graph/graphviz.hpp> struct EdgeWithInfo { int nodeS; int nodeE; int weight; std::string name; }; typedef boost::adjacency_list<boost::listS, boost::vecS, boost::directedS, boost::no_property, boost::property<boost::edge_all_t, EdgeWithInfo>> Graph; typedef std::pair<int, int> Edge; enum { A, B, C, D, E, N }; const std::string name[] = {"A", "B", "C", "D", "E"}; int main() { const std::vector<EdgeWithInfo> edgeWithInfo = { {A, B, 3, "→B"}, {A, C, 1, "→C"}, {A, D, 4, "→D"}, {B, E, 2, "→E"}, {C, E, 5, "→E"}, {D, E, 6, "→E"}}; std::vector<Edge> edges; std::transform(edgeWithInfo.begin(), edgeWithInfo.end(), std::back_inserter(edges), [](auto ei) { return std::make_pair(ei.nodeS, ei.nodeE); }); const Graph g(edges.begin(), edges.end(), edgeWithInfo.begin(), N); std::ofstream file("test.dot"); boost::write_graphviz(file, g, boost::make_label_writer(name), [](auto &graph_) { return [&graph_](std::ostream &out, auto edge) { const EdgeWithInfo &ewi = *reinterpret_cast<const EdgeWithInfo *>(&boost::get(boost::edge_all, graph_, edge)); out << "["; out << "weight=" << ewi.weight << ","; out << "label=" << ewi.name; out << "]"; }; }(g)); } digraph G { 0[label=A]; 1[label=B]; 2[label=C]; 3[label=D]; 4[label=E]; 0->1 [weight=3,label=→B]; 0->2 [weight=1,label=→C]; 0->3 [weight=4,label=→D]; 1->4 [weight=2,label=→E]; 2->4 [weight=5,label=→E]; 3->4 [weight=6,label=→E]; }正攻法ではない気がするけど、まあ良しとしよう。 PR |
|
忍者ブログ [PR] |