Move Operation, op_to_transform_id, and operation_sequence_id to lib/operations

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-21 21:23:28 -04:00
parent b343a61149
commit 07ad553568
2 changed files with 25 additions and 23 deletions
+1 -23
View File
@@ -1,5 +1,4 @@
"""Example: colored pentagon reduction on a random 20-vertex triangulation.""" """Example: colored pentagon reduction on a random 20-vertex triangulation."""
import hashlib
import json import json
import base64 import base64
from collections import defaultdict from collections import defaultdict
@@ -9,30 +8,11 @@ from sage.all import graphs, Graph # type: ignore[attr-defined] # pylint: disa
from lib.tutte_embedding import tutte_embedding from lib.tutte_embedding import tutte_embedding
from lib.planar_embedding import outer_face, get_embedding_from_pos from lib.planar_embedding import outer_face, get_embedding_from_pos
from lib.colored_graphs import ColoredGraphId, VertexColoring, save_colored_graph from lib.colored_graphs import ColoredGraphId, VertexColoring, save_colored_graph
from lib.operations import Operation, operation_to_string, operation_sequence_id
DIR = Path(__file__).parent DIR = Path(__file__).parent
PALETTE = ['red', 'blue', 'green', 'yellow'] PALETTE = ['red', 'blue', 'green', 'yellow']
class Operation(TypedDict):
"""Information about a change made to a (colored) graph"""
name: Any
meta: Any
source_graph: Graph
source_canon_id: ColoredGraphId
result_graph: Graph
result_coloring: VertexColoring
result_canon_id: ColoredGraphId
def colored_graph_id_to_string(cid: ColoredGraphId) -> str:
return f"{cid['graph_id']} {cid['coloring_id']}"
def op_to_transform_id(op: Operation) -> str:
return f"{colored_graph_id_to_string(op['source_canon_id'])} -> {colored_graph_id_to_string(op['result_canon_id'])}"
def operation_sequence_id(ops: list[Operation]) -> str:
joined = "\n".join(op_to_transform_id(op) for op in ops)
return hashlib.sha256(joined.encode()).hexdigest()
def _neighbors_form_cycle(g: Graph, v: Any) -> bool: def _neighbors_form_cycle(g: Graph, v: Any) -> bool:
"""Return True if the neighbors of v induce a cycle in g.""" """Return True if the neighbors of v induce a cycle in g."""
return bool(cast(Graph, g.subgraph(g.neighbors(v))).is_cycle()) return bool(cast(Graph, g.subgraph(g.neighbors(v))).is_cycle())
@@ -188,6 +168,4 @@ def save_operation_sequence(op_sequence: list[ReduceOperation], g: Graph, colori
return op_seq_id return op_seq_id
op_sequence = reduce(G, starting_coloring, initial_canon_id) op_sequence = reduce(G, starting_coloring, initial_canon_id)
print("\nOp sequence:")
print(json.dumps(strip_graphs(op_sequence), indent=2))
save_operation_sequence(op_sequence, G, starting_coloring) save_operation_sequence(op_sequence, G, starting_coloring)
+24
View File
@@ -0,0 +1,24 @@
import hashlib
from typing import Any, TypedDict
from sage.all import Graph # type: ignore[attr-defined] # pylint: disable=no-name-in-module
from lib.colored_graphs import ColoredGraphId, VertexColoring
class Operation(TypedDict):
"""Information about a change made to a (colored) graph"""
name: Any
meta: Any
source_graph: Graph
source_canon_id: ColoredGraphId
result_graph: Graph
result_coloring: VertexColoring
result_canon_id: ColoredGraphId
def colored_graph_id_to_string(cid: ColoredGraphId) -> str:
return f"{cid['graph_id']} {cid['coloring_id']}"
def operation_to_string(op: Operation) -> str:
return f"{colored_graph_id_to_string(op['source_canon_id'])} -> {colored_graph_id_to_string(op['result_canon_id'])}"
def operation_sequence_id(ops: list[Operation]) -> str:
joined = "\n".join(operation_to_string(op) for op in ops)
return hashlib.sha256(joined.encode()).hexdigest()