Search code examples
boostgraphvertexbidirectional

How to add a vertex in bidirectional graph?


I'm using boost for an algorithm. However, its bidirectional graph seems to have no way to add a vertex. How could I initialise a MUTABLE bidirectional graph so that I can add vertices at any time?


Solution

  • I don't really see what the problem is: use the expressions listed in the docs

    Live On Coliru

    #include <boost/graph/adjacency_list.hpp>
    
    using namespace boost;
    using Graph = adjacency_list<vecS, vecS, bidirectionalS>;
    
    #include <boost/graph/graph_utility.hpp> // for display
    
    int main() {
        Graph g;
        auto a = add_vertex(g);
        auto b = add_vertex(g);
    
        add_edge(a,b,g);
    
        print_graph(g);
    }
    

    Prints

    0 --> 1 
    1 -->