using namespace lemon;
directive is added to the code at the beginning.
The nodes and the arcs of a graph are identified by two datatypes called ListDigraph::Node and ListDigraph::Arc. You can add new components the graph by the addNode() and the addArc() member functions, like this:
ListDigraph g; ListDigraph::Node a = g.addNode(); ListDigraph::Node b = g.addNode(); ListDigraph::Node c = g.addNode(); ListDigraph::Node d = g.addNode(); g.addArc(a,b); g.addArc(b,c); g.addArc(c,d); g.addArc(d,a);
Of course, addArc() also returns the created arc:
ListDigraph::Arc diag = g.addArc(a,c);
int cnt = 0; for(ListDigraph::NodeIt n(g); n!=INVALID; ++n) cnt++; std::cout << "Number of nodes: " << cnt << std::endl;
Here ListDigraph::NodeIt is an iterator class that lists the nodes. You must give the graph to the constructor and it will be set to the first node. The next node is obtained by the prefix ++ operator. If there is no more nodes in the graph, the iterator will be set to INVALID.
for(ListDigraph::NodeIt u(g); u!=INVALID; ++u) for(ListDigraph::NodeIt v(g); v!=INVALID; ++v) if(u!=v) g.addArc(u,v);
The items are also ordered by the 'less than' operator. For example this code will add only one of the opposite arcs.
for(ListDigraph::NodeIt u(g); u!=INVALID; ++u) for(ListDigraph::NodeIt v(g); v!=INVALID; ++v) if(u<v) g.addArc(u,v);
int cnt = 0; for(ListDigraph::ArcIt a(g); a!=INVALID; ++a) cnt++; std::cout << "Number of arcs: " << cnt << std::endl;
Finally, you can also list the arcs starting from or arriving at a certain node with ListDigraph::OutArcIt and ListDigraph::InArcIt. Their usage are the same, but you must also give the node to the constructor.
int cnt = 0; for(ListDigraph::OutArcIt a(g,start); a!=INVALID; ++a) cnt++; std::cout << "Number of arcs leaving the node 'start': " << cnt << std::endl;
So, if you want to assign int values to each node, you have to allocate a concepts::Digraph::Node Map "NodeMap<int>".
ListDigraph::NodeMap<int> map(g);
As you see, the graph you want to assign a map is given to the constructor. Then you can access its element as if it were a vector.
map[a]=2; map[b]=3; map[c]=map[a]+map[b];
As a more complex example, let's create a map that assigns a unique integer number to each node.
ListDigraph::NodeMap<int> id(g); int cnt=0; for(ListDigraph::NodeIt n(g); n!=INVALID; ++n, ++cnt) id[n]=cnt;
You can also give an initial value of the elements as a second parameter.
For example the following code puts the number of outgoing edges in a map.
ListDigraph::NodeMap<int> out_deg(g,0); for(ListDigraph::ArcIt a(g); a!=INVALID; ++a) out_deg[g.source(a)]++;
1.5.8