eulerian_path

Contents

eulerian_path#

graph_tool.structure.eulerian_path(g, source=None, circuit=False, edges=False)[git][source]#

Return an Eulerian path or circuit, i.e. one that traverses every edge exactly once.

Parameters:
gGraph

Graph to be used.

sourceVertex or int (optional, default: None)

If given, the path will start at this vertex.

circuitbool (optional, default: False)

If True, only a closed path (an Eulerian circuit) is returned.

edgesbool (optional, default: False)

If True, the path is returned as a sequence of edge indices instead of vertices.

Returns:
pathnumpy.ndarray or None

The vertices (\(E + 1\) of them) or the edge indices of the path, or None if there is no Eulerian path (or circuit) starting at source.

See also

eulerize

Edges to duplicate to make a graph Eulerian

tsp_tour

Tour that visits every vertex

tournament_hamiltonian_path

Path that visits every vertex of a tournament

Notes

An undirected graph has an Eulerian path if the vertices with edges are connected and at most two of them have an odd degree, in which case the path starts and ends at them; for a circuit, all degrees must be even. A directed graph has one if the vertices with edges are weakly connected, and the out-degree of every vertex equals its in-degree, except possibly for a start vertex with one more out-edge and an end vertex with one more in-edge. The path is found with the algorithm of Hierholzer [hierholzer-moglichkeit-1873] in \(O(V + E)\) time.

References

[hierholzer-moglichkeit-1873]

Carl Hierholzer and Chr. Wiener, “Ueber die Möglichkeit, einen Linienzug ohne Wiederholung und ohne Unterbrechung zu umfahren”, Mathematische Annalen 6, 30-32 (1873), DOI: 10.1007/BF01442866 [sci-hub, @tor]

Examples

>>> g = gt.Graph([(0, 1), (1, 2), (2, 0), (0, 3), (3, 4), (4, 0)],
...              directed=False)
>>> print(gt.eulerian_path(g, circuit=True))
[0 1 2 0 3 4 0]

The order in which the edges are traversed can be shown in a drawing:

>>> es = gt.eulerian_path(g, circuit=True, edges=True)
>>> step = g.new_ep("int")
>>> step.a[es] = np.arange(1, len(es) + 1)
>>> pos = g.new_vp("vector<double>", vals=[(0, 0), (-1, -1), (-1, 1),
...                                        (1, 1), (1, -1)])
>>> gt.graph_draw(g, pos, vertex_text=g.vertex_index,
...               vertex_fill_color="#cccccc", edge_color="#999999",
...               edge_text=step, vertex_size=40, vertex_font_size=18,
...               edge_font_size=18,
...               output="eulerian_path-bowtie.pdf")
<...>
../_images/eulerian_path-bowtie.png

Eulerian circuit of a graph of two triangles that share a vertex, with the edges labeled by their position in the circuit.#