What it is
NetworkX is a Python library for the creation, manipulation, and study of complex networks of nodes and edges. It provides tools to analyze the structure and dynamics of graphs and networks.
NetworkX allows you to create different types of graphs (undirected, directed, multigraphs), add nodes and edges, compute network metrics, visualize graphs, and perform network algorithms such as shortest paths, clustering, and centrality measures.
Installation
pip install networkxGetting started
The smallest useful thing you can do with it, and what each part means.
import networkx as nx
G = nx.Graph()
G.add_node(1)
G.add_nodes_from([2, 3])
G.add_edge(1, 2)
G.add_edges_from([(2, 3), (3, 1)])
print(G.nodes())
print(G.edges())import matplotlib.pyplot as plt
nx.draw(G, with_labels=True, node_color='lightblue', edge_color='gray')
plt.show()Advanced usage
Where the library earns its place over a simpler alternative.
path = nx.shortest_path(G, source=1, target=3)
print(path)centrality = nx.degree_centrality(G)
print(centrality)DG = nx.DiGraph()
DG.add_edge('A', 'B')
DG.add_edge('B', 'C')
nx.draw(DG, with_labels=True, node_color='lightgreen')
plt.show()G.add_edge(1, 2, weight=4)
G.add_edge(2, 3, weight=7)
length = nx.dijkstra_path_length(G, source=1, target=3)
print(length)Errors and fixes
The failures you are most likely to hit, and what actually resolves them.
- NetworkXError: The node {} is not in the graph
- Ensure the node exists in the graph before performing operations on it.
- NetworkXNoPath
- Check connectivity between source and target nodes when computing shortest paths.
Best practices
- Choose the appropriate graph type: Graph, DiGraph, MultiGraph, or MultiDiGraph.
- Use built-in NetworkX algorithms for analysis rather than manually iterating over nodes and edges.
- Combine with Matplotlib, Plotly, or Graphviz for enhanced visualizations.
- Label nodes meaningfully for readability in graphs.
- Leverage built-in centrality, clustering, and connectivity metrics for network analysis.
Background
Why it exists, and what it was reacting to.
NetworkX was created by Aric Hagberg, Dan Schult, and Pieter Swart in 2004 to provide a flexible framework for analyzing complex networks in Python. It is widely used in research, social network analysis, biology, computer science, and many other fields that involve graph theory.
