networkit.graphio

class networkit.graphio.BinaryEdgeListPartitionReader(firstNode=0, width=4)

Bases: object

Reads a partition file that contains a binary list of pairs (node, partition(node)). It is assumed that all integers are unsigned.

Parameters
  • firstNode (int, optional) – The id of the first node, this is subtracted from all read node ids. Default: 0

  • width (int, optional) – The width of the unsigned integer in bytes (4 or 8). Default: 4

read(paths)

Read the partition from one or multiple files

Parameters

paths (str or list(str)) – The input path(s)

Returns

The resulting partition.

Return type

networkit.Partition

class networkit.graphio.BinaryEdgeListPartitionWriter(firstNode=0, width=4)

Bases: object

Writes a partition file that contains a binary list of pairs (node, partition(node)).

Parameters
  • firstNode (int, optional) – The id of the first node, this is added to all writen node ids. Default: 0

  • width (int, optional) – The width of the unsigned integer in bytes (4 or 8). Default: 4

write(P, path)

Write the partition to the given file.

Parameters
class networkit.graphio.BinaryPartitionReader(width=4)

Bases: object

Reads a partition from a binary file that contains an unsigned integer of the given width for each node.

Parameters

width (int, optional) – The width of the unsigned integer in bytes (4 or 8). Default: 4

read(path)

Reads a partition from a binary file.

Parameters

path (str) – The input path.

Returns

The resulting partition.

Return type

networkit.Partition

class networkit.graphio.BinaryPartitionWriter(width=4)

Bases: object

Writes a partition to a file to contains a binary list of partition ids. Partition ids are unsigned integers.

Parameters

width (int, optional) – The width of the unsigned integer in bytes (4 or 8). Default: 4

write()

Write the partition to the given file.

class networkit.graphio.CoverReader(path, G)

Bases: object

Reads a cover from a file File format: each line contains the space-separated node ids of a community

Parameters
  • path (str) – Input file path.

  • G (networkit.Graph) – Graph corresponding to the community from path.

Returns

The resulting cover of a graph.

Return type

networkit.Cover

class networkit.graphio.CoverWriter(zeta, path)

Bases: object

Writes a partition to a file. File format: each line contains the space-separated node ids of a community

Parameters
class networkit.graphio.DotGraphWriter

Bases: networkit.graphio.GraphWriter

Writes graphs in the .dot/GraphViz format.

class networkit.graphio.EdgeListCoverReader(firstNode=1)

Bases: object

Reads a cover from an edge list type of file. File format: each line starts with a node id and continues with a list of the communities the node belongs to.

Parameters

firstNode (int, optional) – Id of first node. Default: 1

read(path, G)

Reads a cover from an edge list file.

Parameters
  • path (str) – The input path.

  • G (networkit.Graph) – Graph corresponding to the community from path.

Returns

Cover of graph.

Return type

networkit.Cover

class networkit.graphio.EdgeListPartitionReader(firstNode=1, sepChar='`\\`t')

Bases: object

Reads a partition from an edge list type of file.

Parameters
  • firstNode (int, optional) – Id of first node. Default: 1

  • sepChar (str) – Character which is used for data seperation. Default: ‘ ‘

read(path)

Reads a partition from ad edge list file.

Parameters

path (str) – The input path.

Returns

The resulting partition.

Return type

networkit.Partition

class networkit.graphio.EdgeListReader(self, separator, firstNode, commentPrefix='#', continuous=True, directed=False)

Bases: networkit.graphio.GraphReader

Reads a graph from various text-based edge list formats.

A line has to contain two or three entries separated with the separator symbol (one ASCII character). If at least one line contains three entries, the generated graph will be weighted and each line with only two fields will be interpreted as weight 1.0.

A file may contain the same edge multiple times; then, the weight of the first occurrence is used.

Undirected graphs need to include an edge only in one direction, i.e. edge {u, v} may be represented by (u, v) or (v, u) or both (again, only the first occurrence is used).

If the input file contains non-continuous node ids with large gaps or non-integer node labels, set the parameter continuous to False. Then, gaps are automatically removed and node ids are reassigned to [0, n) where n is the number of nodes in the graph. The mapping will be arbitrary and can be accessed using getNodeMap().

To shift continuous integer node labels which are not zero-indexed, set firstNode to the smallest id used in the file. firstNode will be ignored in the non-continuous case.

The file may also include line comments which start with the commentPrefix.

Parameters
  • separator (str) – The separator character. Must have length of exactly one.

  • firstNode (int) – The id of the first node, this value will be subtracted from all node ids.

  • commentPrefix (str, optional) – Lines starting with this prefix will be ignored. Default: #

  • continuous (bool, optional) – File uses continuous node ids. Default: True

  • directed (bool, optional) – Treat input file as a directed graph. Default: False

getNodeMap()

Returns mapping of non-continuous files.

The mapping is returned as dict(str, int) projecting the original labels (as Strings) to the reassigned node ids.

Returns

Mapping from labels to node ids.

Return type

dict(str,int)

class networkit.graphio.EdgeListWriter(separator, firstNode, bothDirections=False)

Bases: networkit.graphio.GraphWriter

Writes graphs in various edge list formats.

Parameters
  • separator (str) – The separator character.

  • firstNode (int) – The id of the first node, this value will be added to all node ids

  • bothDirections (bool, optional) – If undirected edges shall be written in both directions, i.e., as symmetric directed graph. Default: False

class networkit.graphio.Format(value)

Bases: networkit.graphio.__AutoNumber

Simple enumeration class to list supported file types. Possible values:

  • networkit.graphio.Format.DOT

  • networkit.graphio.Format.EdgeList

  • networkit.graphio.Format.EdgeListCommaOne

  • networkit.graphio.Format.EdgeListSpaceZero

  • networkit.graphio.Format.EdgeListSpaceOne

  • networkit.graphio.Format.EdgeListTabZero

  • networkit.graphio.Format.EdgeListTabOne

  • networkit.graphio.Format.GraphML

  • networkit.graphio.Format.GraphToolBinary

  • networkit.graphio.Format.GraphViz

  • networkit.graphio.Format.GEXF

  • networkit.graphio.Format.GML

  • networkit.graphio.Format.KONEC

  • networkit.graphio.Format.LFR

  • networkit.graphio.Format.METIS

  • networkit.graphio.Format.NetworkitBinary

  • networkit.graphio.Format.SNAP

class networkit.graphio.GEXFReader

Bases: object

This class provides a function to read a file in the GEXF (Graph Exchange XML Format) format. For more details see: http://gexf.net/

createEvent(eventTime, eventType, u, v, w)

Creates a NetworKit::GraphEvent from the supplied parameters and passes it to eventStream. :param eventTime: Timestep indicating when the event happen (creating an order of events). :type eventTime: int :param eventType: Abbreviation string representing a graph event. Should be one of the following:

e, an, dn, rn, ae, re, de, ce.

Parameters
  • u (int) – Id of node u involved in graph event.

  • v (int) – Id of node v involved in graph event.

  • w (float) – Edgeweight of edge between u and v.

getNodeMap()
Returns

Dictionary containing mapping from GEXF ID to node ID

Return type

dict(int : int)

mapDynamicNodes()

Node ID of a dynamic node must be determined before it’s mapped to its GEXF ID. This requires processing the sorted eventStream and figuring out the addition order of the nodes. After that, node addition/deletion/restoration operations of this node must be readded to eventStream with correct mapping. .. note:

New mapping of a node can be equal to old mapping of a node. In order to prevent collisions,
isMapped array must be maintained and controlled.
parseDynamics(element, elementType, controlList, u, v='0', w='0')

Determine the operations as follows: 1. Element has start and not deleted before: Create add event 2. Element has start and deleted before: Create restore event 3. Element has end:Create del event 4. If an element has end before start(or no start at all), add it to the initial graph 5. For dynamic edges, simply go over the attvalues and create weight update events A dynamic element must be defined either using only spells or inline attributes. These 2 shouldn’t be mixed. (For example, Gephi will treat them differently. It’ll ignore the inline declaration if the same element also contains spells) :param element: Element to add during reading a GEXF-file. :type element: str :param elementType: Element type (“n” for node or “e” for edge). :type elementType: str :param controlList: Dict with elements indicate element properties. Example {'elementAdded': False, 'elementDeleted': False} :type controlList: dict :param u: Node u involved in element parsing. :type u: str :param v: Node v involved in element parsing. Default: “0” :type v: str, optional :param w: Edgeweight w involved in element parsing. Default: “0” :type w: str, optional

read(fpath)

Reads and returns the graph object defined in fpath.

Parameters

fpath (str) – File path for GEXF-file.

class networkit.graphio.GEXFWriter

Bases: object

This class provides a function to write a NetworKit graph to a file in the GEXF format.

write(graph, fname, evenStream=[], mapping=[])

Writes a graph to the specified file fname.

Parameters
  • graph (networkit.Graph) – The input graph.

  • fname (str) – The desired file path and name to be written to.

  • eventStream (list(networkit.dynamics.GraphEvent)) – Stream of events, each represented by networkit.dynamics.GraphEvent.

  • mapping (list(int)) – Random node mapping.

writeEvent(xmlElement, eventStream, graphElement)

Write a single event. This is a supporting function and should normally not be called independently. :param xmlElement: XML-encoded element, representing one GEXF-element. :type xmlElement: xml.etree.cElementTree :param eventStream: Stream of events, each represented by networkit.dynamics.GraphEvent. :type eventStream: list(networkit.dynamics.GraphEvent) :param graphElement: Tuple representing one graph element given by (node u, node v, edge weight w). :type graphElement: tuple(int, int, float)

class networkit.graphio.GMLGraphReader

Bases: networkit.graphio.GraphReader

Reader for the GML graph format, which is documented here: http://www.fim.uni-passau.de/fileadmin/files/lehrstuhl/brandenburg/projekte/gml/gml-technical-report.pdf

class networkit.graphio.GMLGraphWriter

Bases: networkit.graphio.GraphWriter

Writes a graph and its coordinates as a GML file: http://svn.bigcat.unimaas.nl/pvplugins/GML/trunk/docs/gml-technical-report.pdf

class networkit.graphio.GraphConverter(reader, writer)

Bases: object

Converts a format input to another or the same file format. The execute the conversion call convert.

Parameters
convert(inPath, outPath)

Execute the conversion.

Parameters
  • inPath (str) – The input path.

  • outPath (str) – The output path.

class networkit.graphio.GraphMLReader

Bases: object

This class serves as wrapper for the GraphMLSAX class which is able to parse a GraphML XML file and construct a graph.

read(fpath)

Parses a GraphML XML file and returns the constructed Graph

Parameters

fpath (str) – The path to the file as a String.

class networkit.graphio.GraphMLSAX

Bases: xml.sax.handler.ContentHandler

Parser for GraphML XML files, based on Pythons XML.SAX implementation.

characters(content)

Appends content string to the textbuffer.

Parameters

content (str) – String to be added.

endElement(name)

Finalizes parsing of the started Element and processes retrieved data.

Parameters

name (str) – Name of the element. Possible values: edge, data

getCharacterData()

Returns current textbuffer and clears it afterwards.

getGraph()

Return parsed graph.

startElement(name, attrs)

Parses all currently relevant XML tags and retrieves data.

Parameters
  • name (str) – Name of the element. Possible values: graph, node, edge, key, data

  • attr (dict()) – Attributes of element.

class networkit.graphio.GraphMLWriter

Bases: object

This class provides a function to write a NetworKit graph to a file in the GraphML format.

write(self, graph, fname, nodeAttributes={}, edgeAttributes={})

Writes a NetworKit graph to the specified file fname.

Parameters
  • graph (networkit.Graph) – The input graph.

  • fname (str) – The desired file path and name to be written to.

  • nodeAttributes (dict(), optional) – Dictionary of node attributes in the form attribute name => list of attribute values. Default: {}

  • edgeAttributes (dict(), optional) – Dictionary of edge attributes in the form attribute name => list of attribute values. Default: {}

class networkit.graphio.GraphReader

Bases: object

Abstract base class for graph readers

read(path)

Read graph given by path.

Parameters

path (str) – Path string.

Returns

The resulting graph.

Return type

networkit.Graph

class networkit.graphio.GraphToolBinaryReader

Bases: networkit.graphio.GraphReader

Reads the binary file format defined by graph-tool: http://graph-tool.skewed.de/static/doc/gt_format.html

class networkit.graphio.GraphToolBinaryWriter

Bases: networkit.graphio.GraphWriter

Reads the binary file format defined by graph-tool: http://graph-tool.skewed.de/static/doc/gt_format.html

class networkit.graphio.GraphWriter

Bases: object

Abstract base class for graph writers

write()

Write the graph to a file.

Parameters
  • G (networkit.Graph) – The graph to write.

  • paths (str) – The output path.

class networkit.graphio.KONECTGraphReader(remapNodes=False, handlingmethod=networkit.graphio.MultipleEdgesHandling.DISCARD_EDGES)

Bases: networkit.graphio.GraphReader

Reader for the KONECT graph format, which is described in detail on the KONECT website: http://konect.uni-koblenz.de/downloads/konect-handbook.pdf

Parameter handlingmethod can be one of the following:

  • networkit.graphio.MultipleEdgesHandling.DISCARD_EDGES

  • networkit.graphio.MultipleEdgesHandling.SUM_WEIGHTS_UP

  • networkit.graphio.MultipleEdgesHandling.KEEP_MINIMUM_WEIGHT

Parameters
  • remapNodes (bool, optional) – Indicates whether nodes are remapped. Default: False

  • handlingmethod (networkit.graphio.MultipleEdgesHandling, optional) – Sets method of handling multiple edges. Default: networkit.graphio.MultipleEdgesHandling.DISCARD_EDGES

class networkit.graphio.LineFileReader

Bases: object

Reads a file and puts each line in a list of strings.

read(path)

Reads a file and returns list of strings.

Parameters

path (str) – The input path.

Returns

List of strings, each string representing one line of an input file.

Return type

list(str)

class networkit.graphio.METISGraphReader

Bases: networkit.graphio.GraphReader

Reads the METIS adjacency file format [1]. If the Fast reader fails, use readGraph(path, graphio.formats.metis) as an alternative. [1]: http://people.sc.fsu.edu/~jburkardt/data/metis_graph/metis_graph.html

class networkit.graphio.METISGraphWriter

Bases: networkit.graphio.GraphWriter

Writes graphs in the METIS format.

class networkit.graphio.MatReader(key='G')

Bases: object

Matlab file reader. File format: Adjacency matrix in matlab file format.

Parameters

key (str, optional) – Key to identify graph. Default: ‘G’

read(path)

Reads a graph from a matlab file.

Parameters

path (str) – The input path.

Returns

The resulting graph.

Return type

networkit.Graph

class networkit.graphio.MatWriter(key='G')

Bases: object

Matlab file writer.

Parameters

key (str, optional) – Key to identify graph. Default: ‘G’

write(G, path, key='G')

Writes a graph to a file.

Parameters
  • G (networkit.Graph) – The input graph.

  • path (str) – The output path.

  • key (str, optional) – Key to identify graph. Default: ‘G’

class networkit.graphio.NetworkitBinaryReader

Bases: networkit.graphio.GraphReader

Reads a graph written in the custom Networkit format. Further information can be found here: https://github.com/networkit/networkit/blob/master/networkit/cpp/io/NetworkitBinaryGraph.md

readFromBuffer(state)

Read graph based on input buffer.

Parameters

buffer (list(int)) – Input data buffer.

class networkit.graphio.NetworkitBinaryWriter

Bases: networkit.graphio.GraphWriter

Writes a graph written in the custom Networkit format. Further information can be found here: https://github.com/networkit/networkit/blob/master/networkit/cpp/io/NetworkitBinaryGraph.md

writeToBuffer(state)

Write graph to data buffer.

Parameters

G (networkit.Graph) – The input graph.

class networkit.graphio.PartitionReader

Bases: object

Reads a partition from a file. File format: line i contains subset id of element i.

read(path)

Reads a partition from a file.

Parameters

path (str) – The input path.

Returns

The resulting partition.

Return type

networkit.Partition

class networkit.graphio.PartitionWriter

Bases: object

Writes a partition to a file. File format: line i contains subset id of element i.

write(zeta, path)

Writes a partition to a file. File format: line i contains subset id of element i.

Parameters
class networkit.graphio.SNAPEdgeListPartitionReader(path, nodeMap, G)

Bases: object

Reads a partition from a SNAP ‘community with ground truth’ file

class networkit.graphio.SNAPGraphReader(directed=False, remapNodes=True, nodeCount=0)

Bases: networkit.graphio.GraphReader

Reads a graph from the SNAP graph data collection: http://snap.stanford.edu/data/index.html

Parameters
  • directed (bool, optional) – Indicates whether input represents a directed graph. Default: False

  • remapNodes (bool, optional) – Indicates whether nodes should be remapped. Default: True

  • nodeCount (int, optional) – Indicate the first node id. Default: 0

class networkit.graphio.SNAPGraphWriter

Bases: networkit.graphio.GraphWriter

Writes graphs in a format suitable for the Georgia Tech SNAP software: http://snap-graph.sourceforge.net/

class networkit.graphio.ThrillGraphBinaryReader(n=0)

Bases: networkit.graphio.GraphReader

Reads a graph format consisting of a serialized DIA of vector<uint32_t> from thrill. When the number of nodes is given, reading the graph is more efficient. Otherwise nodes are added to the graph as they are encountered. Edges must be present only in one direction.

Parameters

n (int, optional) – The number of nodes. Default: 0

read(paths)

Read the graph from one or multiple files.

Parameters

paths (str or list(str)) – The input path(s).

Returns

The resulting graph.

Return type

networkit.Graph

class networkit.graphio.ThrillGraphBinaryWriter

Bases: networkit.graphio.GraphWriter

Writes a graph format consisting of a serialized DIA of vector<uint32_t> from Thrill. Edges are written only in one direction.

networkit.graphio.convertGraph(fromFormat, toFormat, fromPath, toPath=None)

Converts a graph given by a set of file formats and path.

Parameters
networkit.graphio.getConverter(fromFormat, toFormat)

Returns a converter for a given set of file formats.

Parameters
Returns

Corresponding GraphConverter object.

Return type

networkit.graphio.GraphConverter

networkit.graphio.getReader(fileformat, *kargs, **kwargs)

Returns reader based on input fileformat.

Parameters
  • fileformat (networkit.graphio.Format) – A supported file format.

  • *kargs (tuple()) – Additional input parameter (depending on the file format).

  • **kwargs (dict()) – Additional input parameter (depending on the file format).

networkit.graphio.getWriter(fileformat, *kargs, **kwargs)

Returns reader based on input fileformat.

Parameters
  • fileformat (networkit.graphio.Format) – A supported file format.

  • *kargs (tuple()) – Additional input parameter (depending on the file format).

  • **kwargs (dict()) – Additional input parameter (depending on the file format).

networkit.graphio.readGraph(path, fileformat, *kargs, **kwargs)

Read graph file in various formats and return a graph.

Parameters
  • fileformat (networkit.graphio.Format) – A supported file format.

  • *kargs (tuple()) – Additional input parameter (depending on the file format).

  • **kwargs (dict()) – Additional input parameter (depending on the file format). In case of a custom edge list, pass the generic Fromat.EdgeList accompanied by the defining paramaters as follows: separator, firstNode, commentPrefix, continuous, directed. commentPrefix, continuous=True and directed are optional because of their default values. firstNode is not needed when continuous=True.

networkit.graphio.readGraphs(dirPath, pattern, fileformat, some=None, exclude=None, **kwargs)

Read all graph files contained in a directory whose filename contains the pattern, return a dictionary of name to Graph object.

Parameters
  • dirPath (str) – Path, which contains input graphs.

  • pattern (str) – Unix-style string pattern for file selection.

  • fileformat (networkit.graphio.Format) – A supported file format.

  • some (int, optional) – Restrict number of graphs to be read. Default: None

  • exclude (str, optional) – Unix-style string pattern for file exclusion. Default: None

  • **kwargs (dict()) – Additional input parameter (depending on the file format). In case of a custom edge list, pass the generic Fromat.EdgeList accompanied by the defining paramaters as follows: separator, firstNode, commentPrefix, continuous, directed. commentPrefix, continuous=True and directed are optional because of their default values. firstNode is not needed when continuous=True.

networkit.graphio.readMat(key='G')

Reads a Graph from a matlab object file containing an adjacency matrix and returns a graph.

Parameters

key (str, optional) – Key to identify graph. Default: ‘G’

networkit.graphio.readStream(path, mapped=True, baseIndex=0)

Read a graph event stream from a file.

Parameters
  • path (str) – The input path.

  • mapped (bool, optional) – Indicates whether the ids should be mapped. Default: True

  • baseIndex (int, optional) – Sets base index of nodes. Default: 0

networkit.graphio.writeGraph(G, path, fileformat, *kargs, **kwargs)

Write graph to various output formats.

Parameters
  • G (networkit.Graph) – The input graph.

  • path (str) – Output file path.

  • fileformat (networkit.graphio.Format) – A supported file format.

  • *kargs (tuple()) – Additional input parameter (depending on the file format).

  • **kwargs (dict()) – Additional input parameter (depending on the file format). In case of a custom edge list, pass the generic Fromat.EdgeList accompanied by the defining paramaters as follows: separator, firstNode, commentPrefix, continuous, directed. commentPrefix, continuous=True and directed are optional because of their default values. firstNode is not needed when continuous=True.

networkit.graphio.writeMat(G, path, key='G')

Writes a graph to a file.

Parameters
  • G (networkit.Graph) – The input graph.

  • path (str) – The output path.

  • key (str, optional) – Key to identify graph. Default: ‘G’

networkit.graphio.writeStream(stream, path)

Write a graph event stream to a file.

Parameters