Search code examples
boostboost-graph

set the number of vertices in boost:graph


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.


Solution

  • 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.