"""Utilities for constructing edge graphs from planar graphs.""" from typing import Any, cast from sage.all import Graph def get_edge_graph_pos(edge_graph: Graph, pos_src: dict[Any, Any]) -> dict[Any, tuple[Any, Any]]: """Return a position dict for edge graph vertices, using midpoints of the source positions.""" pos: dict[Any, tuple[Any, Any]] = {} for e in cast(list[Any], edge_graph.vertices()): # type: ignore pos[e] = ( (pos_src[e[0]][0] + pos_src[e[1]][0]) / 2, (pos_src[e[0]][1] + pos_src[e[1]][1]) / 2 ) return pos def get_edge_graph(graph: Graph, set_pos: bool = True) -> Graph: """Return the edge graph of the given graph, optionally setting vertex positions.""" pos_src: dict[Any, Any] | None = None pos: dict[Any, tuple[Any, Any]] = {} if set_pos: pos_src = cast(dict[Any, Any], graph.get_pos()) # type: ignore g = Graph() for e in cast(list[Any], graph.edges()): # type: ignore g.add_vertex(e) # type: ignore if pos_src: pos[e] = ( (pos_src[e[0]][0] + pos_src[e[1]][0]) / 2, (pos_src[e[0]][1] + pos_src[e[1]][1]) / 2 ) for v in cast(list[Any], graph.vertices()): # type: ignore incident_edges = cast(list[Any], graph.edges_incident(v)) # type: ignore if len(incident_edges) == 1: continue for i, e1 in enumerate(incident_edges): e2 = incident_edges[(i + 1) % len(incident_edges)] g.add_edge(e1, e2) # type: ignore if set_pos: g.set_pos(pos) # type: ignore return g