eulerize#
- graph_tool.structure.eulerize(g, weight=None)[git][source]#
Return the edges to duplicate to make a connected undirected graph Eulerian.
- Parameters:
- g
Graph Connected undirected graph.
- weight
EdgePropertyMap(optional, default:None) Edge lengths. If given, the total length of the duplicated edges is minimized; otherwise, their number.
- g
- Returns:
- edges
numpy.ndarray Array of shape
(k, 2)with the endpoints of the edges to add (copies of existing edges), after which every vertex has even degree.
- edges
See also
eulerian_pathEulerian path or circuit
Notes
A connected graph has an Eulerian circuit if and only if every vertex has even degree (see
eulerian_path()). The edges returned are copies of existing edges, chosen so that after their addition every vertex has even degree, and so that their total length \(\sum_e w_e\) (their number, if no lengths are given) is minimal. The vertices of odd degree are paired by a minimum-weight perfect matching of their shortest-path distances, and the edges of the shortest paths between the paired vertices are duplicated [edmonds-matching-1973], which solves the route inspection (Chinese postman) problem. The time is dominated by \(O(V_{\text{odd}})\) shortest-path searches, which run in parallel, and the matching. An Eulerian circuit of the result is given byeulerian_path().References
[edmonds-matching-1973]Jack Edmonds and Ellis L. Johnson, “Matching, Euler tours and the Chinese postman”, Mathematical Programming 5, 88-124 (1973), DOI: 10.1007/BF01580113 [sci-hub, @tor]
Examples
>>> g = gt.lattice([3, 3]) >>> es = gt.eulerize(g) >>> print(len(es)) 4 >>> g.add_edge_list(es) >>> print(gt.eulerian_path(g, circuit=True) is not None) True
The duplicated edges can be shown in a drawing of the result:
>>> pos = g.new_vp("vector<double>", ... vals=[(i % 3, i // 3) for i in range(9)]) >>> ecolor = g.new_ep("string", vals=["#999999"] * 12 + ["#e41a1c"] * 4) >>> gt.graph_draw(g, pos, vertex_fill_color="#cccccc", edge_color=ecolor, ... vertex_size=40, edge_pen_width=2, ... output="eulerize-lattice.pdf") <...>
A \(3\times 3\) lattice made Eulerian, with the duplicated edges shown in red.#