Boost logo

Boost Users :

Subject: Re: [Boost-users] [BGL] add_edge also adds a vertex
From: Eric Böse-Wolf (eric_at_[hidden])
Date: 2010-02-07 04:37:56


Hi Olivier,

please post an example, which compiles. For example with all
necessary #include directives.

Olivier Tournaire <olitour_at_[hidden]> writes:

>     std::pair< graph_traits<Graph>::edge_descriptor, bool > p = add_edge
> (*vertices(g).first, *vertices(g).second, g);

The function vertices returns an iterator range. In my understanding an
iterator range is a pair of iterators [first, second) so that second
can be reached from first with increment operations, _but_ is not part
of the iterator range itself. It could be an iterator like one returned
by the end-function of a stl-vector. It shall not be dereferenced!

Therefore If I alter your code to read the following, everything is ok.

#include <iostream>
#include <boost/property_map/property_map.hpp>
#include <boost/graph/graph_concepts.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/kolmogorov_max_flow.hpp>

using namespace boost;
typedef adjacency_list_traits < vecS, vecS, directedS > Traits;
typedef adjacency_list < vecS, vecS, directedS,
property < vertex_index_t, long,
property < vertex_color_t, boost::default_color_type,
property < vertex_distance_t, long,
property < vertex_potential_t, long, // stores the unary energy associated to
           //each vertex
property < vertex_predecessor_t, Traits::edge_descriptor > > > > >,
property < edge_capacity_t, long, // stores the binary energy associated to
           //each edge
property < edge_residual_capacity_t, long,
property < edge_reverse_t, Traits::edge_descriptor > > > > Graph;

int main(int argc, char** argv)
{
    Graph g;

//property_map<Graph, edge_capacity_t>::type capacity = get(edge_capacity,
//g);
//property_map<Graph, edge_reverse_t>::type rev = get(edge_reverse, g);

 add_vertex(g);
 add_vertex(g);

 graph_traits<Graph>::vertex_iterator v_first, v_second;
 v_first = vertices(g).first;
 v_second = v_first;
 ++v_second;
 
 std::pair< graph_traits<Graph>::edge_descriptor, bool > p = add_edge
(*v_first, *v_second, g);
 std::pair< graph_traits<Graph>::edge_descriptor, bool > p_reverse =
add_edge(*v_second, *v_first, g);

 std::cout << "num_vertices = " << num_vertices(g) << std::endl; // output:
//3!!!!
 std::cout << "num_edges = " << num_edges(g) << std::endl; // output: 2

 return 0;
}

Yours sincerely,

Eric


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net