Search code examples
c++boost-graph

boost::graph : implementation independent way of getting edge from a pair of vertices


I had a look at the Boost graph concepts, but it does not provide any interface of getting any edge from a pair over vertices. I have tried:

boost::graph_traits<G>::edge_descriptor edge(u, v); // does not work

but it requires an additional pointer to property type. How can I get that?


Solution

  • The third parameter for boost::edge() is your graph.

    Note also that the function does not return the edge descriptor directly, but a pair containing the edge descriptor and a Boolean depending on the existence of the edge

    Something like this:

    G myGraph;   // construct the graph
    ....         // populate it
    ....         // select a pair of vertices u, v
    
    // get the edge between the vertices, if it exists
    typedef boost::graph_traits<G>::edge_descriptor edge_t;
    edge_t found_edge;
    std::pair < edge_t, bool > p = boost::edge( u, v, myGraph ); 
    if( ! p.second) {
       // edge does not exist
       ...
    } else {
       found_edge = p.first;
       ...
    }