Returns True if and only if `G` is Eulerian. A graph is *Eulerian* if it has an Eulerian circuit. An *Eulerian circuit* is a closed walk that includes each edge of a graph exactly once. Graphs with isolated vertices (i.e. vertices with zero degree) are not considered to have Eu
(G)
| 20 | |
| 21 | @nx._dispatchable |
| 22 | def is_eulerian(G): |
| 23 | """Returns True if and only if `G` is Eulerian. |
| 24 | |
| 25 | A graph is *Eulerian* if it has an Eulerian circuit. An *Eulerian |
| 26 | circuit* is a closed walk that includes each edge of a graph exactly |
| 27 | once. |
| 28 | |
| 29 | Graphs with isolated vertices (i.e. vertices with zero degree) are not |
| 30 | considered to have Eulerian circuits. Therefore, if the graph is not |
| 31 | connected (or not strongly connected, for directed graphs), this function |
| 32 | returns False. |
| 33 | |
| 34 | Parameters |
| 35 | ---------- |
| 36 | G : NetworkX graph |
| 37 | A graph, either directed or undirected. |
| 38 | |
| 39 | Examples |
| 40 | -------- |
| 41 | >>> nx.is_eulerian(nx.DiGraph({0: [3], 1: [2], 2: [3], 3: [0, 1]})) |
| 42 | True |
| 43 | >>> nx.is_eulerian(nx.complete_graph(5)) |
| 44 | True |
| 45 | >>> nx.is_eulerian(nx.petersen_graph()) |
| 46 | False |
| 47 | |
| 48 | If you prefer to allow graphs with isolated vertices to have Eulerian circuits, |
| 49 | you can first remove such vertices and then call `is_eulerian` as below example shows. |
| 50 | |
| 51 | >>> G = nx.Graph([(0, 1), (1, 2), (0, 2)]) |
| 52 | >>> G.add_node(3) |
| 53 | >>> nx.is_eulerian(G) |
| 54 | False |
| 55 | |
| 56 | >>> G.remove_nodes_from(list(nx.isolates(G))) |
| 57 | >>> nx.is_eulerian(G) |
| 58 | True |
| 59 | |
| 60 | |
| 61 | """ |
| 62 | if G.is_directed(): |
| 63 | # Every node must have equal in degree and out degree and the |
| 64 | # graph must be strongly connected |
| 65 | return all( |
| 66 | G.in_degree(n) == G.out_degree(n) for n in G |
| 67 | ) and nx.is_strongly_connected(G) |
| 68 | # An undirected Eulerian graph has no vertices of odd degree and |
| 69 | # must be connected. |
| 70 | return all(d % 2 == 0 for v, d in G.degree()) and nx.is_connected(G) |
| 71 | |
| 72 | |
| 73 | @nx._dispatchable |
no test coverage detected
searching dependent graphs…