parse_adjlist

parse_adjlist(lines, comments='#', delimiter=None, create_using=None, nodetype=None)[source]

Parse lines of a graph adjacency list representation.

Parameters
lineslist or iterator of strings

Input data in adjlist format

create_usingNetworkX graph constructor, optional (default=nx.Graph)

Graph type to create. If graph instance, then cleared before populated.

nodetypePython type, optional

Convert nodes to this type.

commentsstring, optional

Marker for comment lines

delimiterstring, optional

Separator for node labels. The default is whitespace.

Returns
G: NetworkX graph

The graph corresponding to the lines in adjacency list format.

See also

read_adjlist

Examples

>>> lines = ["1 2 5", "2 3 4", "3 5", "4", "5"]
>>> G = nx.parse_adjlist(lines, nodetype=int)
>>> nodes = [1, 2, 3, 4, 5]
>>> all(node in G for node in nodes)
True
>>> edges = [(1, 2), (1, 5), (2, 3), (2, 4), (3, 5)]
>>> all((u, v) in G.edges() or (v, u) in G.edges() for (u, v) in edges)
True