Skip to main content
Ctrl+K
Logo image Logo image

Site Navigation

  • Install
  • Tutorial
  • Reference
  • Gallery
  • Developer
  • Releases
  • Guides
    • devel (latest)
    • current (stable)

Site Navigation

  • Install
  • Tutorial
  • Reference
  • Gallery
  • Developer
  • Releases
  • Guides
    • devel (latest)
    • current (stable)

Section Navigation

  • Introduction
  • Graph types
  • Algorithms
  • Functions
  • Graph generators
  • Linear algebra
  • Converting to and from other data formats
  • Relabeling nodes
  • Reading and writing graphs
    • Adjacency List
    • Multiline Adjacency List
    • Edge List
    • GEXF
    • GML
    • GraphML
    • JSON
    • LEDA
    • SparseGraph6
    • Pajek
    • Matrix Market
  • Drawing
  • Randomness
  • Exceptions
  • Utilities
  • Glossary

node_link_graph#

node_link_graph(data, directed=False, multigraph=True, attrs=None, *, source='source', target='target', name='id', key='key', link='links')[source]#

Returns graph from node-link data format. Useful for de-serialization from JSON.

Parameters:
datadict

node-link formatted graph data

directedbool

If True, and direction not specified in data, return a directed graph.

multigraphbool

If True, and multigraph not specified in data, return a multigraph.

attrsdict

A dictionary that contains five keys ‘source’, ‘target’, ‘name’, ‘key’ and ‘link’. The corresponding values provide the attribute names for storing NetworkX-internal graph data. Default value:

dict(source=’source’, target=’target’, name=’id’,

key=’key’, link=’links’)

Deprecated since version 2.8.6: The attrs keyword argument will be replaced with the individual keywords: source, target, name, key and link. in networkx 3.2.

If the attrs keyword and the new keywords are both used in a single function call (not recommended) the attrs keyword argument will take precedence.

The values of the keywords must be unique.

sourcestring

A string that provides the ‘source’ attribute name for storing NetworkX-internal graph data.

targetstring

A string that provides the ‘target’ attribute name for storing NetworkX-internal graph data.

namestring

A string that provides the ‘name’ attribute name for storing NetworkX-internal graph data.

keystring

A string that provides the ‘key’ attribute name for storing NetworkX-internal graph data.

linkstring

A string that provides the ‘link’ attribute name for storing NetworkX-internal graph data.

Returns:
GNetworkX graph

A NetworkX graph object

See also

node_link_data, adjacency_data, tree_data

Notes

Attribute ‘key’ is only used for multigraphs.

To use node_link_data in conjunction with node_link_graph, the keyword names for the attributes must match.

Examples

Create data in node-link format by converting a graph.

>>> G = nx.Graph([('A', 'B')])
>>> data = nx.node_link_data(G)
>>> data
{'directed': False, 'multigraph': False, 'graph': {}, 'nodes': [{'id': 'A'}, {'id': 'B'}], 'links': [{'source': 'A', 'target': 'B'}]}

Revert data in node-link format to a graph.

>>> H = nx.node_link_graph(data)
>>> print(H.edges)
[('A', 'B')]

To serialize and deserialize a graph with JSON,

>>> import json
>>> d = json.dumps(node_link_data(G))
>>> H = node_link_graph(json.loads(d))
>>> print(G.edges, H.edges)
[('A', 'B')] [('A', 'B')]
On this page
  • node_link_graph()

© Copyright 2004-2023, NetworkX Developers.

Built with the PyData Sphinx Theme 0.12.0.

Created using Sphinx 5.2.3.