diff --git a/colored_pentagon_reduction/example.py b/colored_pentagon_reduction/example.py index c494637..25f3e94 100644 --- a/colored_pentagon_reduction/example.py +++ b/colored_pentagon_reduction/example.py @@ -217,17 +217,17 @@ def squish(g: Graph, coloring: VertexColoring, v0: Any) -> tuple[Graph, VertexCo return g_prime, coloring_prime, v1, v2 -Step = SquishOperation | PluckOperation +ReduceOperation = SquishOperation | PluckOperation def reduce( g: Graph, coloring: VertexColoring, before_cid: ColoredGraphId, - steps: list[Step] | None = None, -) -> list[Step]: + op_sequence: list[ReduceOperation] | None = None, +) -> list[ReduceOperation]: """Repeatedly apply pluck/squish reductions until no candidates remain.""" - if steps is None: - steps = [] + if op_sequence is None: + op_sequence = [] print(f"Coloring: {coloring}") @@ -239,8 +239,8 @@ def reduce( 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) - steps.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, steps) + 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) 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): @@ -252,8 +252,8 @@ def reduce( 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) - steps.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, steps) + 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) if degree_5_candidates: v0 = degree_5_candidates[0] @@ -261,11 +261,11 @@ def reduce( 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) - steps.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, steps) + 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("DONE") - return steps + return op_sequence G = next(graphs.planar_graphs(20, minimum_degree=5)) @@ -296,20 +296,20 @@ def plot_to_data_uri(g: Graph, coloring: VertexColoring) -> str: png_bytes = Path(f.name).read_bytes() return f"data:image/png;base64,{base64.b64encode(png_bytes).decode()}" -def save_operation_sequence(steps: list[Step], g: Graph, coloring: VertexColoring) -> str: - """Save steps as JSON and Markdown under data/operations/. Returns the sequence id.""" - op_seq_id = operation_sequence_id(steps) +def save_operation_sequence(op_sequence: list[ReduceOperation], g: Graph, coloring: VertexColoring) -> str: + """Save op_sequence as JSON and Markdown under data/operations/. Returns the sequence id.""" + op_seq_id = operation_sequence_id(op_sequence) op_dir = DIR / "data" / "operations" / op_seq_id op_dir.mkdir(parents=True, exist_ok=True) - (op_dir / "colored_pentagon_contractions.json").write_text(json.dumps(strip_graphs(steps), indent=2)) + (op_dir / "colored_pentagon_contractions.json").write_text(json.dumps(strip_graphs(op_sequence), indent=2)) md_lines = [f"## start\n\n![start]({plot_to_data_uri(g, coloring)})"] - for step in steps: - meta_json = json.dumps(step['meta']) - md_lines.append(f"## {step['name']} {meta_json}\n\n![b]({plot_to_data_uri(step['g_prime'], step['coloring_prime'])})") + for op in op_sequence: + meta_json = json.dumps(op['meta']) + md_lines.append(f"## {op['name']} {meta_json}\n\n![b]({plot_to_data_uri(op['g_prime'], op['coloring_prime'])})") (op_dir / "colored_pentagon_contractions.md").write_text("\n".join(md_lines) + "\n") return op_seq_id -steps = reduce(G, starting_coloring, initial_cid) -print("\nSteps:") -print(json.dumps(strip_graphs(steps), indent=2)) -save_operation_sequence(steps, G, starting_coloring) +op_sequence = reduce(G, starting_coloring, initial_cid) +print("\nOp sequence:") +print(json.dumps(strip_graphs(op_sequence), indent=2)) +save_operation_sequence(op_sequence, G, starting_coloring)