[Lemon-user] Code snippet to iterate over a map ?

Kovács Péter kpeter at inf.elte.hu
Thu Jun 24 21:39:58 CEST 2010


Hi,

If you would like to iterate over all nodes, then you can simply use NodeIt:

   for (GraphType::NodeIt u(g); u != INVALID; ++u)
     cout << g.id(u) << " pred arc: " << g.id(bfs.predArc(u)) << "\n";

   for (GraphType::NodeIt u(g); u != INVALID; ++u)
     cout << g.id(u) << " pred node: " << g.id(bfs.predNode(u)) << "\n";

If your PredMap is a standard NodeMap<Arc>, then you can also use its 
MapIt or ConstMapIt iterator type (though it is not documented yet as 
far as I know):

   typedef GraphType::NodeMap<GraphType::Arc> PredMap;
   PredMap pred = bfs.predMap();
   for (PredMap::MapIt it(pred); it != INVALID; ++it)
     cout << g.id(it) << " pred arc: " << g.id(*it) << "\n";

On the other hand, if you would like to iterate over the visited nodes 
only (for efficiency reasons), then you could either
   1. run the algorithm step-by-step and do the required operations 
meantime (consider to use init(), addSource(), emptyQueue(), nextNode(), 
processNextNode() functions) or
   2. run the algorithm at once using run(), but use a special 
ProcessedMap to keep track the visited nodes (consider to use e.g. 
LoggerBoolMap for this purpose):
http://lemon.cs.elte.hu/pub/doc/1.2/a00515.html#g21ca379ec2c92eccd71b76df0a9eee8c
Once you stored the nodes in a container, you can easily iterate over 
them and you can obtain pred values using predNode(), predArc() or 
predMap().

I hope that I could answer your questions.

Best regards,
Peter


> Hi,
>
> I can't get the map iterator from predMap to work after a bfs (don't
> know the syntaxis for that). How can I iterate over all results
> including nodes (keys) and values? I couldn't find an example either. A
> small code snippet will be more than helpful.
>
>
> Thank You,
> Diego




More information about the Lemon-user mailing list