-
Notifications
You must be signed in to change notification settings - Fork 4
/
nxeuler.py
69 lines (56 loc) · 1.64 KB
/
nxeuler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import networkx as nx
import toolz as tz
@tz.curry
def add_edges(g_, edges_iter):
for e in edges_iter:
g_.add_edge(*e)
return g_
def edge_from_kmer(kmer):
return (kmer[:-1], kmer[1:])
def eulerian_path(g, algorithm='Fleury'):
"""Return a Eulerian path for the semi-eulerian graph ``g``.
Parameters
----------
g : nx.DiGraph, nx.MultiDiGraph
The input graph. The graph must be semi-Eulerian.
algorithm : {'Fleury', 'Hierholzer'}, optional
Which algorithm to use to find the path. Hierholzer is faster
but has the distinct disadvantage that I haven't implemented
it yet. =P
Returns
-------
edges_iter : iterator of edges
An Eulerian path of ``g``.
Notes
-----
An Eulerian path is one that visits every edge in a graph exactly
once.
"""
source, sink = _source_and_sink(g)
g.add_edge(sink, source)
edges_iter = nx.eulerian_circuit(g, source=source)
return edges_iter
def _source_and_sink(g):
"""Find the source and sink of a semi-Eulerian graph
Parameters
----------
g : nx.DiGraph
The input graph.
Returns
-------
source, sink : nodes
A node for which in-degree is 1 less than out-degree, and
vice-versa.
"""
source, sink = None, None
for node in g.nodes_iter():
indeg, outdeg = g.in_degree(node), g.out_degree(node)
if indeg == outdeg - 1:
source = node
continue
if outdeg == indeg - 1:
sink = node
continue
if source is not None and sink is not None:
break
return source, sink