Skip to content

NetworkX

Data & AnalyticsData SciencePython

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 networkx

Getting started

The smallest useful thing you can do with it, and what each part means.

Creating a simple graph
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())
Creates a simple undirected graph with three nodes and edges connecting them. Prints the list of nodes and edges.
Visualizing a graph
import matplotlib.pyplot as plt
nx.draw(G, with_labels=True, node_color='lightblue', edge_color='gray')
plt.show()
Visualizes the graph using Matplotlib with labeled nodes and custom colors.

Advanced usage

Where the library earns its place over a simpler alternative.

Computing shortest path
path = nx.shortest_path(G, source=1, target=3)
print(path)
Finds the shortest path between two nodes in the graph.
Degree centrality
centrality = nx.degree_centrality(G)
print(centrality)
Calculates the degree centrality for each node, indicating the relative importance of nodes.
Directed graph example
DG = nx.DiGraph()
DG.add_edge('A', 'B')
DG.add_edge('B', 'C')
nx.draw(DG, with_labels=True, node_color='lightgreen')
plt.show()
Creates a directed graph and visualizes it.
Weighted graph example
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)
Adds weighted edges and calculates the shortest path length using Dijkstra’s algorithm.

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.