From f8d714f26e2caf4bab2443d2afe6862e6ea2caf9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Radek=20Hu=C5=A1ek?= <husek@iuuk.mff.cuni.cz>
Date: Thu, 8 Jul 2021 21:01:18 +0200
Subject: [PATCH] Extend UnderlyingGraph parameter to free edges

---
 graph_tools/parameters.py | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/graph_tools/parameters.py b/graph_tools/parameters.py
index 5c781d6..c1843f2 100644
--- a/graph_tools/parameters.py
+++ b/graph_tools/parameters.py
@@ -109,6 +109,10 @@ def _init_():
       BV((0, 0, 0), Graph([[0], []], multiedges=True, loops=True, immutable=True))
     ]
 
+    FREE_EDGE = [
+      BV((0, 0), Graph([[0], []], multiedges=True, loops=True, immutable=True))
+    ]
+
     def join_boundaries(self, x, _):
       offsets = [0]
       for b in x:
@@ -137,17 +141,25 @@ def _init_():
     def merge_values(self, a, b): assert False
 
     def finalize(self, b):
+      from sage.all import Graph
       assert len(b) == 1
       assert b[0].boundary == tuple()
-      return b[0].value
+      G = Graph(b[0].value, immutable=False)
+
+      for v in G.vertices():
+        if G.degree(v) != 2: continue
+        u, w = G.neighbors(v)
+        G.add_edge(u, w)
+        G.delete_vertex(v)
+
+      return Graph(G, immutable=True)
 
 
   @Singleton
   class VertexCount(GraphParameterBase):
     """Count vertices of gadget.
 
-    Same as `gadget.eval(UnderlyingGraph).num_verts()` but
-    also works for gadgets containing FREE_EDGE.
+    Same as `gadget.eval(UnderlyingGraph).num_verts()`.
     """
     CUBIC_VERTEX = [ BV(None, 1) ]
     FREE_EDGE = [ BV(None, 0) ]
-- 
GitLab