5

I am working with graphs with multiple edges and loops, and I want to eliminate all isomorphic graphs from a long list I've generated. The FindGraphIsomorphism function is very nice, but only works for simple graphs. I'm looking to find such a function or its equivalent for multigraphs. For example,

two multigraphs

Is there possibly a third-party package, or maybe even an external program that would find such graph isomorphisms?

Also, is it possible to compare graphics objects in Mathematica, to see whether they are similar? I have tried SameQ, but to no avail.

graphs with self-loops

I am assuming this is the result because Mathematica plots the graphs with floating-point values, so it is impossible to have two identical graphs like this. So is there a function that looks at the similarity between two graphics objects? I think something like that might do the trick for me in most cases.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Jānis Lazovskis
  • 402
  • 4
  • 12

1 Answers1

6

IGraph/M 0.1.3 or later supports multigraph isomorphism testing directly. Note: Version 0.3.110 fixes important bugs in multigraph (sub-)isomorphism. Upgrade to this release or later.

<< IGraphM`

g1 = Graph[{1 -> 3, 1 -> 4, 1 -> 4, 2 -> 3, 2 -> 3, 2 -> 4}]

g2 = Graph[{1 -> 2, 1 -> 2, 1 -> 3, 2 -> 4, 3 -> 4, 3 -> 4}]

As directed graphs they are not isomorphic:

IGIsomorphicQ[g1, g2]
(* False *)

As undirected ones they are:

g1 = Graph[{1 <-> 3, 1 <-> 4, 1 <-> 4, 2 <-> 3, 2 <-> 3, 2 <-> 4}]

g2 = Graph[{1 <-> 2, 1 <-> 2, 1 <-> 3, 2 <-> 4, 3 <-> 4, 3 <-> 4}]

IGIsomorphicQ[g1, g2]
(* True *)

We can get a specific mapping like so:

IGGetIsomorphism[g1, g2]
(* {<|1 -> 1, 3 -> 3, 4 -> 2, 2 -> 4|>} *)

The implementation is based on igraph's support for edge-colored graphs. Note that at the moment igraph itself (the library underlying IGraph/M) does not support multigraph isomorphism. It won't error on multigraphs, but it may not give correct results. It is important to be aware of this when using igraph from R/Python/C. IGraph/M, the Mathematica interface, does have checks for multigraphs, and can test for multigraph isomorphism by transforming them to edge-coloured simple graphs.

There's no builtin implementation for finding isomorphisms for multigraphs, but we can do the translation to edge-coloured graphs by hand:

asc1 = Counts[Sort /@ EdgeList[g1]]
(* <|1 <-> 3 -> 1, 1 <-> 4 -> 2, 2 <-> 3 -> 2, 2 <-> 4 -> 1|> *)

asc2 = Counts[Sort /@ EdgeList[g2]]
(* <|1 <-> 2 -> 2, 1 <-> 3 -> 1, 2 <-> 4 -> 1, 3 <-> 4 -> 2|> *)

IGVF2FindIsomorphisms[{Graph[VertexList[g1],Keys[asc1]], "EdgeColors" -> asc1}, {Graph[VertexList[g2],Keys[asc2]], "EdgeColors" -> asc2}]
(* {<|1 -> 1, 3 -> 3, 4 -> 2, 2 -> 4|>, <|1 -> 3, 3 -> 1, 4 -> 4, 2 -> 2|>, 
    <|1 -> 2, 3 -> 4, 4 -> 1, 2 -> 3|>, <|1 -> 4, 3 -> 2, 4 -> 3, 2 -> 1|>} *)
Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
  • Is there a way to test isomorphism of Edge-colored and Vertex-colored multigraphs with IGraphM ? – yarchik Dec 12 '17 at 12:56
  • The approach given bellow can deal with multi-graphs only non-simple graphs. Is there any way around this? – Jonathan Prieto-Cubides Aug 03 '20 at 18:19
  • @jonaprieto I don't understand what "can deal with multi-graphs only non-simple graphs" means. – Szabolcs Aug 03 '20 at 18:22
  • Try a graph with multiple edges and some loops. It won't work or am I missing something? trying to compute Aut(Bn), where Bn is the graph of n-loops and one vertex. – Jonathan Prieto-Cubides Aug 03 '20 at 18:24
  • @jonaprieto Please give a complete example and explain what you mean by "does not work". It's probably best to ask a new question. – Szabolcs Aug 03 '20 at 18:26
  • I use Bn=Graph[Table[1 -> 1, n]] as the graph instead of g1 and g2 in your example. Then, I got "VF2 does not support non-simple graphs". $Failed – Jonathan Prieto-Cubides Aug 03 '20 at 18:28
  • @jonaprieto Again, give a complete example. I did not use any VF2 functions on non-simple graph in the above post. I can't tell what you are doing. – Szabolcs Aug 03 '20 at 18:30
  • @jonaprieto It works here: https://i.stack.imgur.com/UtJOo.png You can also post on https://igraph.discourse.group/ if you feel that the question is not appropriate for M.SE. I do need a clear example to be able to help. – Szabolcs Aug 03 '20 at 18:38
  • That image is just testing if they are iso. We are interested here to know the set of isos. Anyways, I'm posting a new question for you. – Jonathan Prieto-Cubides Aug 03 '20 at 18:43
  • Check this out: https://mathematica.stackexchange.com/questions/227903/computing-the-set-of-automorphisms-of-a-non-simple-and-multi-graph – Jonathan Prieto-Cubides Aug 03 '20 at 18:51