A networkx-like wrapper for the NetworKit library
nxnk
aims at providing a drop-in replacement for networkx Graph and DiGraph classes, whenever node and edge attributes are not necessary except for edge weight.
NetworKit (https://github.com/kit-parco/networkit) is a python library focusing on high performance graph analysis by means of a finely tuned C++ backend, whereas NetworkX (https://github.com/networkx/networkx) is a widely used python-only library for constructing, manipulating and analyzing graphs.
With its focus on performance, NetworKit offers an ideal library for the analysis of large graphs.
This library, nxnk, is an attempt at easing the creation and editing of graphs like NetworkX while maintaining the performance-enhanced algorithms of NetworKit.
In creating and editing graphs, the performance of nxnk is comparable (slightly better: 10-50%) to that of NetworkX.
You could think of nxnk
as providing a mapping between your graph nodes (any hashable object) and NetworKit's internal node representation (an integer), by using a networkx-like interface.
This is an alpha release, subject to change and quite incomplete. At present, only Graph
and DiGraph
are supported, with only some bridge functions to access NetworKit algorithm implementations. To access the latter, use the self.nkG
, which is an instance of a NetworKit graph.Graph
, and translate between user-defined nodes and NetworKit-defined nodes (the "knodes") using the dictionaries nodes
and knodes
in either Graph
or DiGraph
, or use the convenience methods to_user_nodes
and to_networkit_nodes
. Otherwise, all presently existing methods operate exclusively in user-defined node IDs, taking as argument and returning only user-defined node IDs (with the exception of to_networkit_nodes
).
nxnk
targets python 3, and recommends python 3.6+.
-
NetworkX Graph and DiGraph classes offer a number of methods suffixed with "_iter" (e.g.
networkx.Graph.edges_iter
), which are implemented as generators (with theyield
keyword; see: https://networkx.github.io/documentation/development/_modules/networkx/classes/graph.html#Graph), with the "_iter"-free method (e.g.networkx.Graph.edges
) callinglist
on the return value of the twin "_iter" method. Innxnk
, these additional "_iter" methods are merely provided by compatibility with networkx and marked as deprecated. By design, the proper methods are implemented directly as generators. In this aspect, nxnk follows a similar shift from python 2 to python 3, in that e.g. therange
function went from returning a list to merely promising to generate one on the fly, and removed from python 3 all the "_iter"-suffixed methods that had been added to python 2 for performance reasons. -
nxnk
does not store a dictionary of properties for every node and for every edge, unlike NetworkX. Allnxnk
provides is the means to store a floating-point value as edge weight, mirroring the capabilities of thenetworkit.Graph
. Note that you will have to initializenxnk.Graph
ornxnk.DiGraph
withweighted=True
(the default) for graph edges to be able to hold weights other than 1.0. Deriving from this fundamental feature,nxnk
does not provide any means to store edge or node property maps (networkx stores a dictionary for every node and edge, even an empty one when not providing any properties), and therefore, all methods that in networkx take attribute maps do not do so innxnk
. Fortunately, these are always provided as optional arguments in networkx, and therefore, if you code does not use them,nxnk
will work as a drop-in replacement. -
Networkx graph classes offer the method
degree
which works for both a single node, a list of nodes, or all nodes, returning different data types depending on the argument. Innxnk
, functions always return the same data type (or None), and thereforedegree
is limited to returning the degree of a single node. Usedegrees
for an iterable of the degree of all nodes, and mapdegree
to an iterable of a subset of nodes when wanting the degree of a subset of nodes.
Given that nxnk
Graph and DiGraph classes offer the same function interface as their homonimous classes in NetworkX, most of the NetworkX library can operate correctly with nxnk
versions of Graph and DiGraph.
For cases where NetworkX library functions expect lists instead of generators, call the method nx_adapter()
in either Graph or DiGraph which will produce an editable view (NXGraph
or NXDiGraph
) with methods that behave like those of NetworkX (e.g. returning a list instead of a generator).