2

When I build a graph g with this code:

ListDigraph g;

for (int i = 0; i < 7; ++i)
  g.addNode();

its nodes will have indexes {0..6}, which I tested by calling g.id() on them. How can I get a node by using its index? For example, I would like to add an arc to g by calling:

g.addArc(<node n>, <node m>);
Paul Jurczak
  • 7,008
  • 3
  • 47
  • 72
  • 1
    Assuming lemon doesn't have such function and assuming the id doesn't change, maybe you can maintain your own vector to which you can push new nodes? – eerorika Sep 05 '14 at 12:30
  • @user2079303 Yes, this is an option, but I would prefer using Lemon API. – Paul Jurczak Sep 05 '14 at 12:35
  • In that case you can use the node iterator and check the nodes linearly until you find the correct id. – eerorika Sep 05 '14 at 12:38
  • @user2079303 _check the nodes linearly until you find the correct id_ - that would be hugely inefficient. – Paul Jurczak Sep 05 '14 at 12:56
  • That's the fastest way to search a linked list which is what `ListDigraph` uses internally (a vector of lists according to the documentation). – eerorika Sep 05 '14 at 12:59

1 Answers1

3

It can be done with nodeFromId member function, see Graph Class Reference:

g.addArc(g.nodeFromId(n), g.nodeFromId(m));
Paul Jurczak
  • 7,008
  • 3
  • 47
  • 72