0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

グラフ描画(Boost Graph Library + Graphviz 利用)

Last updated at Posted at 2019-05-31

こんにちは。
Boost Graph LibraryGraphviz を使い、グラフ描画してみました1 2
graphviz.jpg

$ g++ -std=c++11 -I/usr/local/include graphviz.cpp
$ ./a.out; dot -Tpdf graphviz.dot -o graphviz.pdf
graphviz.cpp
# include <boost/graph/graphviz.hpp>
# include <boost/range/adaptor/indexed.hpp>

typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, int64_t, int64_t> graph_t;

int main()
{
    graph_t g;
    std::vector<std::array<int, 2>> edges = {{0, 1}, {0, 2}, {0, 3}, {1, 2}, {2, 1}, {3, 4}, {3, 5}};
    for(auto&& it : edges | boost::adaptors::indexed()){
        auto eid = it.index();
        auto u = it.value()[0];
        auto v = it.value()[1];
        auto e = add_edge(u, v, eid, g);
        ename[e.first] = std::to_string(eid);
    }
    std::string fname = "graphviz.dot";
    std::cout << "writing " << fname << '\n';
    std::ofstream file(fname);
    boost::write_graphviz(file, g);
    boost::write_graphviz(file, g, boost::default_writer(), make_label_writer(boost::make_assoc_property_map(ename)));
}
  1. なおもしも、 このコードで、add_edge へ与える vertex 番号を連続させずスキップさせると、スキップされた孤立 vertex が生じます(例えばコード内の 56 へ置き換えると、孤立した 5 が描かれます)。

  2. "How to use boost make_label_writer to write edge properties?" (Stack Overflow) も参考になります。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?