tags:

views:

505

answers:

2

This may be a rather novice or even wrong question so please be forgiving. Is there a way to compare 2 graphs created using the Boost Graph Library => with 1 graph created in memory and the 2nd loaded from an archive (i.e. 2nd was serialized out previously)?

I don't see an operator== provided in BGL's documentation, but not sure if that means that I have to write both traversal and comparison. Any pointers to tutorials, reference pages or samples would be most helpful

Thanks in advance Ganesh

+5  A: 

In the general case, graph equality is in NP, and thus is essentially impossible to solve. However if you're concerned about vertex labels being equal as well, then it suffices to iterate over all edges in both graph and make sure each is in the other graph as well.

Edit: If the vertex labels (values associated with each vertex) are the same on both graphs, and are unique and comparable, we can check isomorphism in O(V lg V + E lg E) easily, like so:

If |G1| != |G2|, the graphs are non-equal. Abort.

i = 0
For each vertex V in G1:
  G1_M[Label(V)] = V
  G1_I[V] = i
  i = i + 1

For each vertex V in G1:
  G1_E[V] = sort(map(λDestination -> G1_I[Destination]) Edges[V])

For each vertex V in G2:
  If G1_M[Label(V)] does not exist, the graphs are non-equal. Abort.
  G2_corresp[V] = G1_M[Label(V)]
  G2_I[V] = G1_I[G2_corresp[V]]

For each vertex V in G2:
  G1_E[V] = sort(map(λDestination -> G2_I[Destination]) Edges[V])
  Compare G1_E[G2_corresp[V]] and G2_E[V]. If non-equal, the graphs are non-equal. Abort.

If we get here, the graphs are equal.
bdonlan
Well, quite a lot of things are in NP and also in P. Last time I checked, graph isomorphism wasn't proved to be NP-Complete, but sadly it wasn't proved either not to be NP-Complete.
AProgrammer
Which means there's no _known_ polynomial time algorithm, so it might as well by NP-complete for all practical purposes :)
bdonlan
Thankd bdonlan. I didn't realize my terminology was wrong (perhaps thats why I was finding no answers to my searches). Also didn't realize that this was an NP problem. My definition of == was simply the equality of whats stored at a node and if a node has the same number of edges between itself and the other nodes (which also have to be same between the 2 graphs) and so on.
ossandcad
The interesting thing is that it is not proven to be NP-complete but no polynomial time solutions are known.
stribika
If the values stored at each node are unique, and you can build a map with them as keys, then you can simply create an isomorphism using those values and then verify that it applies to the graph. This can be done in log-linear time easily enough, or linear time if you can use your labels as hash keys.
bdonlan
Thanks again bdonlan, the values stored at each node are unique (as far as I can tell). And you mean I should create an std::map with these unique nodes as keys right? That sounds excellent. Definitely worth a try.
ossandcad
I added some pseudocode here of what I mean. I assume all the maps I define are std::maps, but in many cases you may be able to make them data members, etc, for better efficiency.
bdonlan
also, all of those maps can be made into unordered_maps if you can define an appropriate hash function on the keys
bdonlan
+6  A: 

Boost.Graph can do this but not with the == operator: http://www.boost.org/doc/libs/1_39_0/libs/graph/doc/isomorphism.html

It is a hard problem so it will take long for large graphs.

stribika
Thanks stribika. This seems to provide what I am looking for. My graphs are not large - very rarely more than 30 nodes and average around 10 nodes per graph with each child node connected to only one parent node and never to a sibling (kind of like a tree).
ossandcad
30 may be too many. The documentation says it is O(n!) and 30! is around 10^32.
stribika
I've used graph isomorphisms algorithms on graphs with thousands nodes. The run-time depends a lot on the way nodes are connected (we didn't use boost but our own implementation).
AProgrammer
Thanks stribika, In the words of wise fat man (Homer Simpson) - DoH!!! Didn't realize the O(|V|!) part of it. Tho the link you provided says that it worst case time complexity and I am *hoping* that the simplicity of my graph - a basic tree would not reach that complexity.
ossandcad
A tree is far simpler than a general graph to check for isomorphism, even if you don't have labels to help you to sort out the nodes as your other comment implies. (Using the unique label is probably the simplest way)
AProgrammer