简体   繁体   中英

Boost Graph Library: Are Vertex Descriptors Necessarily Unique?

I realize this might be pedantic, but are BGL vertex descriptors always unique? For background, I have the following graph definition:

typedef boost::adjacency_list<boost::setS, boost::listS, boost::undirectedS, VProp, EProp> Graph;

Where I'm using listS as the node data structure. I know that this makes my descriptors, which appear to be of type void* , "stable": when a node is removed from the graph, other descriptors for other nodes can still be used.

I've looked through the BGL documentation for forever, though, and I can't find a guarantee that these descriptors are unique . Essentially, I'm wondering if two descriptors are == if and only if the underlying node data they point to is the same. In other words, is comparing descriptors directly a valid way to check node equality?

Clearly this is true when the node data structure is vecS (since the descriptor is an index in that case), but I haven't found any information in the case of listS in the BGL docs.

Thanks in advance

To the title:

Are Vertex Descriptors Necessarily Unique?

Yes.

In other words, is comparing descriptors directly a valid way to check node equality?

Yes.

Clearly this is true when the node data structure is vecS

Exactly. I was just going to give this exact example to give a partial proof. This means that your understanding of the library is already quite deep.

I happen to have looked into the code a little deeper and know that the descriptor type in case of node-based vertex container selector (listS, setS etc). will amount to a type-erased version of a direct reference to elements in that (implementation defined) container.

Note that the actual type of the elements will vary depending on many of the template arguments, and this is why the descriptor is cast to the opaque void* so

  1. user code doesn't have to know about the specific internals
  2. user code cannot (accidentally) abuse those internals via the descriptor

Given this nature, we can at once explain both the stability (it matches the reference stability for the underlying container implementation) and the unique-ness guarantee (descriptor identity coincides with object identity of elements in the (vertex) storage container).

Note This answer only reassures you about the case of adjacency_list<> instantiations. There are other Graph models. I happen to know off-hand most have very similar guarantees for which the proof follows largely the same structure.


I have not (yet) scrutinized the documentation to verify your claim that it is undocumented, and I'm not convinced that adding the explicit guarantee helps, but I commend you for asking the question. It's a good habit for (C++) programmers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM