Rename Operation fields for clarity and grouping
source_graph/source_canon_id for before state, result_graph/result_coloring/result_canon_id for after state. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -23,11 +23,11 @@ class Operation(TypedDict):
|
||||
"""Information about a change made to a (colored) graph"""
|
||||
name: Any
|
||||
meta: Any
|
||||
before: ColoredGraphId
|
||||
after: ColoredGraphId
|
||||
g: Graph
|
||||
g_prime: Graph
|
||||
coloring_prime: VertexColoring
|
||||
source_graph: Graph
|
||||
source_canon_id: ColoredGraphId
|
||||
result_graph: Graph
|
||||
result_coloring: VertexColoring
|
||||
result_canon_id: ColoredGraphId
|
||||
|
||||
class CanonicalColoredGraph(TypedDict):
|
||||
"""Canonical representation of a colored graph"""
|
||||
@@ -39,7 +39,7 @@ 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['before'])} -> {colored_graph_id_to_string(op['after'])}"
|
||||
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)
|
||||
@@ -152,7 +152,7 @@ ReduceOperation = SquishOperation | PluckOperation
|
||||
def reduce(
|
||||
g: Graph,
|
||||
coloring: VertexColoring,
|
||||
before_cid: ColoredGraphId,
|
||||
source_canon_id: ColoredGraphId,
|
||||
op_sequence: list[ReduceOperation] | None = None,
|
||||
) -> list[ReduceOperation]:
|
||||
"""Repeatedly apply pluck/squish reductions until no candidates remain."""
|
||||
@@ -166,11 +166,11 @@ def reduce(
|
||||
|
||||
for v in g.vertices():
|
||||
if g.degree(v) == 3 and _neighbors_form_cycle(g, v):
|
||||
g_prime, coloring_prime = pluck(g, coloring, v)
|
||||
print(f"\nG' (after pluck v0={v}): {g_prime.order()} vertices, {g_prime.size()} edges")
|
||||
_, _, after_cid = save_colored_graph(g_prime, coloring_prime)
|
||||
op_sequence.append(PluckOperation(name='pluck', meta=PluckMeta(v0=v), g=g, g_prime=g_prime, coloring_prime=coloring_prime, before=before_cid, after=after_cid))
|
||||
return reduce(g_prime, coloring_prime, after_cid, op_sequence)
|
||||
result_graph, result_coloring = pluck(g, coloring, v)
|
||||
print(f"\nG' (after pluck v0={v}): {result_graph.order()} vertices, {result_graph.size()} edges")
|
||||
_, _, result_canon_id = save_colored_graph(result_graph, result_coloring)
|
||||
op_sequence.append(PluckOperation(name='pluck', meta=PluckMeta(v0=v), source_graph=g, source_canon_id=source_canon_id, result_graph=result_graph, result_coloring=result_coloring, result_canon_id=result_canon_id))
|
||||
return reduce(result_graph, result_coloring, result_canon_id, op_sequence)
|
||||
if g.degree(v) == 4 and _neighbors_form_cycle(g, v):
|
||||
degree_4_candidates.append(v)
|
||||
elif g.degree(v) == 5 and _neighbors_form_cycle(g, v):
|
||||
@@ -178,21 +178,21 @@ def reduce(
|
||||
|
||||
if degree_4_candidates:
|
||||
v0 = degree_4_candidates[0]
|
||||
g_prime, coloring_prime, v1, v2 = squish(g, coloring, v0)
|
||||
result_graph, result_coloring, v1, v2 = squish(g, coloring, v0)
|
||||
print(f"Shared-color neighbors: v1={v1}, v2={v2} (color {coloring[v1]})")
|
||||
print(f"\nG' (after squish v0={v0}): {g_prime.order()} vertices, {g_prime.size()} edges")
|
||||
_, _, after_cid = save_colored_graph(g_prime, coloring_prime)
|
||||
op_sequence.append(SquishOperation(name='squish', meta=SquishMeta(v0=v0, v_merged=[v1, v2]), g=g, g_prime=g_prime, coloring_prime=coloring_prime, before=before_cid, after=after_cid))
|
||||
return reduce(g_prime, coloring_prime, after_cid, op_sequence)
|
||||
print(f"\nG' (after squish v0={v0}): {result_graph.order()} vertices, {result_graph.size()} edges")
|
||||
_, _, result_canon_id = save_colored_graph(result_graph, result_coloring)
|
||||
op_sequence.append(SquishOperation(name='squish', meta=SquishMeta(v0=v0, v_merged=[v1, v2]), source_graph=g, source_canon_id=source_canon_id, result_graph=result_graph, result_coloring=result_coloring, result_canon_id=result_canon_id))
|
||||
return reduce(result_graph, result_coloring, result_canon_id, op_sequence)
|
||||
|
||||
if degree_5_candidates:
|
||||
v0 = degree_5_candidates[0]
|
||||
g_prime, coloring_prime, v1, v2 = squish(g, coloring, v0)
|
||||
result_graph, result_coloring, v1, v2 = squish(g, coloring, v0)
|
||||
print(f"Shared-color neighbors: v1={v1}, v2={v2} (color {coloring[v1]})")
|
||||
print(f"\nG' (after squish v0={v0}): {g_prime.order()} vertices, {g_prime.size()} edges")
|
||||
_, _, after_cid = save_colored_graph(g_prime, coloring_prime)
|
||||
op_sequence.append(SquishOperation(name='squish', meta=SquishMeta(v0=v0, v_merged=[v1, v2]), g=g, g_prime=g_prime, coloring_prime=coloring_prime, before=before_cid, after=after_cid))
|
||||
return reduce(g_prime, coloring_prime, after_cid, op_sequence)
|
||||
print(f"\nG' (after squish v0={v0}): {result_graph.order()} vertices, {result_graph.size()} edges")
|
||||
_, _, result_canon_id = save_colored_graph(result_graph, result_coloring)
|
||||
op_sequence.append(SquishOperation(name='squish', meta=SquishMeta(v0=v0, v_merged=[v1, v2]), source_graph=g, source_canon_id=source_canon_id, result_graph=result_graph, result_coloring=result_coloring, result_canon_id=result_canon_id))
|
||||
return reduce(result_graph, result_coloring, result_canon_id, op_sequence)
|
||||
|
||||
print("DONE")
|
||||
return op_sequence
|
||||
@@ -203,7 +203,7 @@ print(f"G: {G.order()} vertices, {G.size()} edges")
|
||||
print(f"Degree sequence: {sorted(G.degree_sequence(), reverse=True)}")
|
||||
starting_coloring_classes = G.coloring()
|
||||
starting_coloring = {v: i for i, cls in enumerate(starting_coloring_classes) for v in cls}
|
||||
_, _, initial_cid = save_colored_graph(G, starting_coloring)
|
||||
_, _, initial_canon_id = save_colored_graph(G, starting_coloring)
|
||||
G.is_planar(set_embedding=True, set_pos=True)
|
||||
|
||||
def strip_graphs(obj: Any) -> Any:
|
||||
@@ -235,11 +235,11 @@ def save_operation_sequence(op_sequence: list[ReduceOperation], g: Graph, colori
|
||||
md_lines = [f"## start\n\n})"]
|
||||
for op in op_sequence:
|
||||
meta_json = json.dumps(op['meta'])
|
||||
md_lines.append(f"## {op['name']} {meta_json}\n\n})")
|
||||
md_lines.append(f"## {op['name']} {meta_json}\n\n})")
|
||||
(op_dir / "colored_pentagon_contractions.md").write_text("\n".join(md_lines) + "\n")
|
||||
return op_seq_id
|
||||
|
||||
op_sequence = reduce(G, starting_coloring, initial_cid)
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user