1

Is there a way to quickly display all the perfect matchings of the vertices of a square lattice?

The following code only finds one:

HighlightGraph[
 GridGraph[{4, 4}, VertexStyle -> White, 
  EdgeStyle -> Gray], {Style[
   FindIndependentEdgeSet[GridGraph[{4, 4}]], Blue,Thick]}]

enter image description here

apg
  • 2,075
  • 10
  • 13

1 Answers1

3

Ok, so the trick I have used is to construct the LineGraph of the lattice, and then find all its largest independent vertex sets.

On e.g. the 4 x 4 square lattice, the following code outputs all 36 perfect matchings of the vertices:

    perfectsets[g_] := Module[{},
    allsets = FindIndependentVertexSet[LineGraph[g], Infinity, All];
    maximalsets = {};
    For[i = 1, i <= Length[allsets], i++,
    If[Length[allsets[[i]]] == 2 n,
    maximalsets = Append[maximalsets, allsets[[i]]];
    ];]; maximalsets];

    n = 4;
    g = GridGraph[{n, n}, VertexStyle -> White, 
       EdgeStyle -> {Gray, Thick}];

    matchings = EdgeList[g][[#]] & /@ perfectsets[g];
    ListofPerfectMatchings = 
     Table[HighlightGraph[
       GridGraph[{n, n}, VertexStyle -> White, 
        EdgeStyle -> Gray], {Style[matchings[[i]], Blue, Thick]}], {i, 1, 
       Length[matchings]}]

i.e.

All perfect matchings of the vertices of the 4x4 square lattice.

See also yode's answer in this post, which is where I got the idea.

apg
  • 2,075
  • 10
  • 13
  • Note this will find all matchings, not necessarily perfect, so a modification according to yode's comment to the OP will speed things up, but it requires an external package to be installed. – apg May 23 '17 at 08:31
  • Also note $n$ must be even for a perfect matching to exist. – apg May 23 '17 at 08:35