set the number of vertices in boost:graph

家住魔仙堡 提交于 2019-12-11 09:08:46

问题


Given an empty boost::graph g, I want to set the number of vertices in this graph and add some edges then. But from the documentation, I cannot find related functions. All examples I found defines the size of vertices in initialization (like Graph g(10) defines a graph with 10 vertices). But I don't know the size when I define the graph. I want to first define a Graph g, and set the size later.


回答1:


The simplest approach is to call the boost::add_vertex( graph ) method for each vertex you want.

Here is a nice place to start Using C++ Boost's Graph Library

Note that you not HAVE to add the vertices one by one. If all you care about are the edges, then add_edge() will add missing vertices for you.




回答2:


You can probably try a little dirty trick like:

add_edge(0,4999,g);
remove_edge(0,4999,g);

It exploits the side-effect of add_edge for adjacency_list, namely, the fact that BGL extends the vector of vertices if necessary.



来源:https://stackoverflow.com/questions/17597655/set-the-number-of-vertices-in-boostgraph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!