Isomorphism of colored multigraphs

One possible solution is to find an injective mapping from coloured multigraphs to coloured simple graphs. We then test the isomorphism of the transformed graphs with IGVF2IsomorphicQ.

My idea for such a mapping is the following:

  1. Subdivide every edge a <-> b to a <-> x <-> b. This eliminates multi-edges between distinct vertices.

  2. Subdivide self-loops a <-> a to a <-> x <-> y <-> a. This transforms self-loops to 3-cycles. A 3-cycle cannot be created with the type of subdivision in point 1. above, thus I believe that the injective property of the mapping stands.

The injective property, i.e. that non-isomorphic graphs map to non-isomoprhic ones, is critical. Therefore I encourage you to think through that the above approach is indeed valid.


To implement this, we start by introducing a new notation for coloured edges. col[a <-> b, c] represents an edge with colour c. Example:

edges = { col[1 <-> 2, 1], col[1 <-> 2, 2], 2 <-> 3 };

The following function brings edge representations to a canonical form. Uncoloured edges are assigned the colour 0.

normalizeEdges[edges_] :=
 Block[{TwoWayRule = UndirectedEdge},
  Replace[edges, e_UndirectedEdge -> col[e, 0], {1}]
 ]

This function creates a new symbol to be used as a node:

newNode[] := Unique[node]

Implement subdivision of normal edges and self-loops:

subdivide[col[UndirectedEdge[a_, a_], c_]] :=     
 With[{n1 = newNode[], n2 = newNode[]},
  Unevaluated@Sequence[
    col[UndirectedEdge[a, n1], c],
    col[UndirectedEdge[n1, n2], c],
    col[UndirectedEdge[n2, a], c]
    ]
  ]

subdivide[col[UndirectedEdge[a_, b_], c_]] :=
 With[{n1 = newNode[]},
  Unevaluated@Sequence[
    col[UndirectedEdge[a, n1], c],
    col[UndirectedEdge[n1, b], c]
    ]
  ]

Convert an edge list in the above format to a subdivided edge list in a format suitable for IGraph/M:

colouredSubdividedGraph[edges_] := 
 With[{newEdges = subdivide /@ normalizeEdges[edges]},
  {Graph[newEdges[[All, 1]]], "EdgeColors" -> newEdges[[All, 2]]}
 ]


colouredSubdividedGraph[edges]

Mathematica graphics

Finally, use IGVF2IsomorphicQ to compare these transformed coloured graphs.

I won't address the visualization question as that in itself deserves a separate question and has been asked before.

  • Color edges of a multigraph when there are parallel edges using Graph[] and EdgeStyle
  • Graph: Coloring Edges individually

Unfortunately, Mathematica cannot properly deal with edge attributes of multigraphs, which makes visualization very messy. If you feel limited by this problem, I encourage you to report it to Wolfram.


Preamble

A few weeks ago I needed to find isomorphisms of colored multigraphs for my research, so I quickly wrote some ugly code that only worked if (a) none of the original colors were the same as the multi-edge counts used to color multi-edges; and (b) multi-edges were not originally colored. This sloppy 'solution' didn't sit well with me, so I decided to write an answer.

Disclaimers:

  1. I haven't tested this terribly throroughly. Additional suggestions for test code is welcome -- I just don't currently have the willpower to invent exhaustive test cases (partialy because I wrote this code on an overnight flight). I did make some test cases, with an ad hoc series of assertions that I cooked up.
  2. I didn't read the links in Szabolcs' comments. They might do things better than I do.

Answer

Full code

The full code is on github.

If you're really bored, you can look up my horrible original implementation in the earliest version(s).

Warning -- It seems Mathematica 11.2 automatically replaces \[UndirectedEdge] with <-> in .m files, but only upon saving the file. I haven't investigated fully, but it seems that some edges are still \[UndirectedEdge]'s, while others are <->, and IGraphM distinguishes between them, so the tests break. As such, beware that this code will break if you open and save these .m files in MMA 11.2.

Overview

  • first combine original colors (I refer to these as optsColors, as they are specified in an option-like way to the isomorphism function) with multicolors (computed in the standard way) into a two-element list
    • write these combined 'colors' in an association (associated by edges), in a similar form to the standard color specification
  • map each color pair to an integer
    • this is done by adding each unique color pair as a key in a master association, with he nth unique color having value n.
    • this looks like, e.g., `<| {1,2} -> 1, {2,3} -> 2, ... |>

Note that the keys in this master association can be anything, not necessarily lists of integers

Colored (and multicolored) multi-edges:

(When I say 'multi-edge', I mean a graph which appears more than once in a multigraph. For now, I identify directed edges with their reverses. When I say a multi-edge is colored, I mean that different instances of that edge have different colors.)

If a multi-edge is given a color in the normal way, it is naturally assumed that every instance of that edge has that color. In order to give different instances of that edge distinct colors, one can specify, e.g., (1<->2) -> {2,3,4}, which gives one instance color 2, one color 3, and one color 4, with all others defaulting to zero.

It seems natural to identify different permutations of multi-edge colors -- e.g., (1<->2) -> {2,3,4} is identified with (1<->2) -> {3,2,4}. As such, all 'colors' which are lists will be assumed to refer to multi-edges, and will be sorted.

Further considerations

Things left to do

  • Deal with directed graphs
  • More exhaustive tests
  • Figure out a way around the \[UndirectedEdge] vs <-> issue
  • Consider type-checking color lists
  • Make sure multicolored multi-edges aren't colored with more colors than their multiplicity

Minor notes

  • I overloaded OP's decorateGraph function to work with IGVF2* color spec conventions. See the testing file.