Search code examples
c++boostgraphboost-graph

Getting edge properties (including related vertices) from boost::adjacency_list


So, I must have gone through the Boost documentation for an hour today. I must be blind. I have, I hope, a simple question:

How do you get the corresponding vertices for an edge with boost::adjacency_list?

I have the following code which I'm trying to figure out:

typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS> Graph;
typedef boost::graph_traits<Graph>::edge_iterator EdgeIterator;
typedef std::pair<EdgeIterator, EdgeIterator> EdgePair;

EdgePair ep;
for (ep = edges(g); ep.first != ep.second; ++ep.first)
{
    // Get the two vertices that are joined by this edge...
}

Anyone know how to do this?

Thanks


Solution

  • You can find the functions you need in this page (in the section called "Non-Member Functions"). The ones you need are source and target.

    typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS> Graph;
    typedef boost::graph_traits<Graph>::edge_iterator EdgeIterator;
    typedef std::pair<EdgeIterator, EdgeIterator> EdgePair;
    typedef boost::graph_traits<Graph>::vertex_descriptor VertexDescriptor;
    
    EdgePair ep;
    VertexDescriptor u,v;
    for (ep = edges(g); ep.first != ep.second; ++ep.first)
    {
        // Get the two vertices that are joined by this edge...
        u=source(*ep.first,g);
        v=target(*ep.first,g);
    }