[BGL] add_edge also adds a vertex
Hi all,
I am facing a problem with BGL. Here is a minimal piece of code which
reproduces the problem
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
Hi Olivier,
please post an example, which compiles. For example with all
necessary #include directives.
Olivier Tournaire
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
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!
I'll also add that adjacency_list with vecS as a vertex set will add vertices in add_edge if an invalid vertex descriptor is given -- of which vertices(g).second is an invalid itetator. It's equivalent to end(vertices(g)) if you're using Boost.Range. Andrew Sutton andrew.n.sutton@gmail.com
Thank you for your replies.
2010/2/7 Andrew Sutton
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!
I'll also add that adjacency_list with vecS as a vertex set will add vertices in add_edge if an invalid vertex descriptor is given -- of which vertices(g).second is an invalid itetator. It's equivalent to end(vertices(g)) if you're using Boost.Range.
I understand. Obviously, dereferencing a past the end iterator is a bad
idea.
I am now facing another problem using kolmogorov_max_flow algorithm.
Here is the code:
#include <iostream>
#include <vector>
using namespace std;
#include
Andrew Sutton andrew.n.sutton@gmail.com
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
The code below crashes (segfault) in the max_flow() function. Could you tell me what am I doing wrong ? It seems to work when I set a capacity capacity[p2.first] with a value smaller than 10 (capacity[p.first]). Am I missing something? Do I correctly use the reverse property_map?
Where precisely does it crash? Andrew Sutton andrew.n.sutton@gmail.com
Hi Andrew,
2010/2/8 Andrew Sutton
The code below crashes (segfault) in the max_flow() function. Could you
tell me what am I doing wrong ? It seems to work when I set a capacity capacity[p2.first] with a value smaller than 10 (capacity[p.first]). Am I missing something? Do I correctly use the reverse property_map?
Where precisely does it crash?
Here is the backtrace from gdb (sorry ...). Eric, no I did not forget to add
vertices in my graph. It is build in another function that I removed for
clarity. I have 333 vertices in my graph, and 4 edges. Do I correctly use
the edge_reverse property_map?
Regards,
Olivier
Program received signal SIGSEGV, Segmentation fault.
#0 0x08075839 in
boost::detail::kolmogorov
Andrew Sutton andrew.n.sutton@gmail.com
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Here is the backtrace from gdb (sorry ...). Eric, no I did not forget to add vertices in my graph. It is build in another function that I removed for clarity. I have 333 vertices in my graph, and 4 edges. Do I correctly use the edge_reverse property_map?
Can you pin down a line number? Andrew Sutton andrew.n.sutton@gmail.com
Andrew,
2010/2/8 Andrew Sutton
Here is the backtrace from gdb (sorry ...). Eric, no I did not forget to
add vertices in my graph. It is build in another function that I removed for clarity. I have 333 vertices in my graph, and 4 edges. Do I correctly use the edge_reverse property_map?
Can you pin down a line number?
Sorry for my previous message. Enabling debug flags, I had a better idea of the problem: /usr/include/boost/graph/kolmogorov_max_flow.hpp:115: [...] Assertion `m_rev_edge_map[m_rev_edge_map[*ei]] == *ei' failed. So, I add in my code: rev[p_reverse.first] = p.first; // ... rev[p2_reverse.first] = p2.first; It seems now to work correctly. Thanks for your help. Regards, Olivier
Andrew Sutton andrew.n.sutton@gmail.com
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Hi Olivier,
Olivier Tournaire
Here is the code:
#include <iostream> #include <vector> using namespace std;
#include
#include 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
::type capacity = get(edge_capacity, g); property_map ::type rev = get(edge_reverse, g);
add_vertex(g); add_vertex(g); add_vertex(g); or so ...
graph_traits<Graph>::vertex_iterator v_first, v_second, v_third; v_first = vertices(g).first; v_second = v_first; ++v_second; v_third = v_second; ++v_third;
Did you forget to add a few vertices before calling vertices(g) ? Eric
participants (4)
-
Andrew Sutton
-
Eric Wolf
-
eric@boese-wolf.eu
-
Olivier Tournaire