1

I am the using LEMON Graph library and was wondering if there is an efficient way to receive a ListDigraph::Node x via the value of its corresponding ListDigraph::NodeMap?

I was thinking about something like:

lemon::ListDigraph lg;
lemon::ListDigraph::NodeMap<std::string> nodeColor(lg);

lemon::ListDigraph::Node n = lg.addNode();
nodeColor[n] = "red";

lemon::ListDigraph::Node m = lg.addNode();
nodeColor[m] = "green";

# now I'd like to have something like:
lemon::ListDigraph::Node x = nodeColor.getNodeFromColor("red");

Does something like this already exist in LEMON? If there is no other way than writing my own map, how do I return the key (Node)? Can I iterate over the underlying values of the map?

Artem Zankovich
  • 2,319
  • 20
  • 36
TKr
  • 13
  • 3

2 Answers2

0

Unfortunately, it is not possible to obtain the keyset (or the respective nodes) from Lemon's NodeMap, see the NodeMap reference.

There is also no way to iterate over the map. The best way to overcome this is to write your own map, as you write yourself, or use additional helper maps (or similar containers).

0

As far as I know, that's not possible in Lemon out of the box. In particular, because the NodeMap is not necessarily one-to-one. There is nothing stopping you from giving all nodes the value "red", and so the backwards query would not have a unique correct result.

You also cannot directly iterate over the contents of a NodeMap. You can, however, iterate over all nodes in the graph, and ask for the corresponding values:

for (lemon::ListDigraph::NodeIt n(lg); n != lemon::INVALID; ++n) {
    if (nodeColor[n] == "red) {
        // do something
    }
}

Or, as you mentioned, you can define your own map:

#include <unordered_map>

lemon::ListDigraph lg;
lemon::ListDigraph::NodeMap<std::string> nodeColor(lg);
std::unordered_map<std::string, lemon::ListDigraph::Node> color2node;

lemon::ListDigraph::Node n = lg.addNode();
nodeColor[n] = "red";
color2node.emplace("red", n);

lemon::ListDigraph::Node m = lg.addNode();
nodeColor[m] = "green";
color2node.emplace("green", m);

lemon::ListDigraph::Node x = node2color.at("red");
mwhite
  • 47
  • 4