2

Defining my boost::graph like the following, I get edge indices zero for all edges. Why? What am I doing wrong?

#include <iostream>
#include <boost/graph/adjacency_list.hpp>

int main() {
    typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, boost::no_property, boost::property<boost::edge_index_t, std::size_t> > Graph;
    typedef boost::graph_traits<Graph>::edge_descriptor Edge;

    Graph g(3);
    Edge e1 = boost::add_edge(0, 1, g).first;
    Edge e2 = boost::add_edge(1, 2, g).first;
    Edge e3 = boost::add_edge(2, 0, g).first;

    boost::property_map<Graph, boost::edge_index_t>::type eim = boost::get(boost::edge_index, g);
    size_t e1n = eim[e1],
           e2n = eim[e2],
           e3n = eim[e3];

    return 0;
}

As far as I can tell from documentation and examples, this should work.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
carlpett
  • 12,203
  • 5
  • 48
  • 82

1 Answers1

4

An adjacency_list doesn't have an edge index associated with it, only a vertex index. Which is quite logical once you think about how the graph is stored.

To have an edge index, you need to manually add it to the graph description, and then manually handle it.

Kornel Kisielewicz
  • 55,802
  • 15
  • 111
  • 149
  • Looking at [this example](http://www.boost.org/doc/libs/1_46_1/libs/graph/doc/using_property_maps.html#sec:exterior-properties), they are using an `adjacency_list` with edge indices. Right? Or am I missing something? – carlpett Sep 02 '11 at 12:16
  • @carlpett, notice that they have a `property >` which they manually assign in `add_edge(0, 1, 0, G);` – Kornel Kisielewicz Sep 02 '11 at 12:18
  • Then how to manually add it... It's too annoying that BGL library documentation is missing so much useful information. – Emerson Xu Apr 06 '16 at 02:35
  • @EmersonXu You pass it as a third argument when calling `add_edge(vertex1, vertex2, edge poperty, graph),` for example `add_edge(0, 1, 0, G)`. – Daniel Nov 19 '19 at 06:57