先日のグラフをGraphvizの*.dot形式で出力してみよう。
#include <fstream> #include <vector> #include <string> #include <boost/graph/adjacency_list.hpp> #include <boost/graph/graphviz.hpp> typedef boost::adjacency_list<boost::listS, boost::vecS, boost::directedS, boost::no_property, boost::property<boost::edge_weight_t, int>> Graph; typedef std::pair<int, int> Edge; enum { A, B, C, D, E, N }; const std::string name = "ABCDE"; int main() { const std::vector<Edge> edges = { {A, B}, {A, C}, {A, D}, {B, E}, {C, E}, {D, E} }; const std::vector<int> weights = { 3, 1, 4, 5, 2, 6 }; const Graph g(edges.begin(), edges.end(), weights.begin(), N); // graphvizの形式(*.dot)で出力 std::ofstream file("test.dot"); boost::write_graphviz(file, g, boost::make_label_writer(name.c_str())); }
出力されたtest.dot
digraph G { 0[label="A"]; 1[label="B"]; 2[label="C"]; 3[label="D"]; 4[label="E"]; 0->1 ; 0->2 ; 0->3 ; 1->4 ; 2->4 ; 3->4 ; }
test.dotをpngに変換。
dot -Tpng test.dot -o test.png
こんな感じになりました。
weightはどうやって出力するんだろう。あとで調べる。