NetworkX
Python library for creating, manipulating, and analyzing complex networks with many graph algorithms built in.
Official Documentation ↗v3. Check the official docs for the latest API surface.Installation
pip install networkxMethods
No methods match this filter.
Overview
NetworkX is a Python library for creating, manipulating, and analyzing complex networks with a large set of graph algorithms. This reference lists the most frequently used APIs with accurate signatures and runnable snippets.
Core APIs
Graph.add_edge— Builds an undirected graph and adds edges with optional attributes such as weight.nx.shortest_path— Computes the shortest path between two nodes, optionally weighted.nx.pagerank— Ranks nodes by importance using the PageRank algorithm.
Example
import networkx as nx
g = nx.Graph()
g.add_edge('a', 'b', weight=2)
g.add_edge('b', 'c', weight=1)
print(g.number_of_nodes())
Notes
Graph class choice matters: directed or multigraph features change which algorithms apply. Performance is pure Python, so very large graphs need other tools.
Verification
Build a small weighted graph and confirm shortest_path returns the expected node sequence.
Errors
The official API reference declares the following error types:
NodeNotFound(shortest_path docstring Raises: NodeNotFound — Ifsourceis not inG.) — official text: “ Raises —— NodeNotFound Ifsourceis not inG.” (source: https://github.com/networkx/networkx/blob/main/networkx/algorithms/shortest_paths/generic.py, fetched 2026-11-08T17:41:29+08:00)NetworkXNoPath(shortest_path docstring Raises: NetworkXNoPath — source 与 target 指定但无路径时。) — official text: “ NetworkXNoPath Ifsourceandtargetare specified but no path exists between them.” (source: https://github.com/networkx/networkx/blob/main/networkx/algorithms/shortest_paths/generic.py, fetched 2026-11-08T17:41:29+08:00)ValueError(shortest_path docstring Raises: ValueError — method 不受支持时。) — official text: “ ValueError Ifmethodis not among the supported options.” (source: https://github.com/networkx/networkx/blob/main/networkx/algorithms/shortest_paths/generic.py, fetched 2026-11-08T17:41:29+08:00)PowerIterationFailedConvergence(pagerank docstring Raises: 在指定迭代次数内未收敛到给定容差时。) — official text: “ Raises —— PowerIterationFailedConvergence If the algorithm fails to converge to the specified tolerance within the specified number of iterations of the power iteration method.” (source: https://github.com/networkx/networkx/blob/main/networkx/algorithms/link_analysis/pagerank_alg.py, fetched 2026-11-08T17:41:29+08:00)
API reference evidence
Fetched from official sources on 2026-11-08T17:41:29+08:00 via WebFetch https://networkx.org/documentation/stable/reference/generated/networkx.Graph.add_edge.html 与 shortest_path 页均返回 GitHub Pages 404(2 次失败);改用 PowerShell Invoke-WebRequest 下载官方仓库 main 分支源码 docstring(raw.githubusercontent.com/networkx/networkx/main: networkx/classes/graph.py、networkx/algorithms/shortest_paths/generic.py、networkx/algorithms/link_analysis/pagerank_alg.py)并 Grep 提取.
Graph.add_edge→ signature from https://github.com/networkx/networkx/blob/main/networkx/classes/graph.py; official text: “def add_edge(self, u_of_edge, v_of_edge, **attr): “”“Add an edge between u and v.”nx.shortest_path→ signature from https://github.com/networkx/networkx/blob/main/networkx/algorithms/shortest_paths/generic.py; official text: “def shortest_path(G, source=None, target=None, weight=None, method=“dijkstra”): “”“Compute shortest paths in the graph.”nx.pagerank→ signature from https://github.com/networkx/networkx/blob/main/networkx/algorithms/link_analysis/pagerank_alg.py; official text: “def pagerank( G, alpha=0.85, personalization=None, max_iter=100, tol=1.0e-6, nstart=None, weight=“weight”, dangling=None, ): “”“Returns the PageRank of the nodes in the graph.”
FAQ
- Which graph classes should I use?
- Use Graph for undirected, DiGraph for directed, and MultiGraph when parallel edges matter; the choice changes algorithm behavior.
- Can I read a graph from a file?
- Yes. networkx provides readers such as read_edgelist and read_graphml plus writers to persist the same formats.
- How do I handle large graphs?
- Pure Python algorithms have limits; for very large graphs consider sparse matrix implementations via scipy or an external graph engine.
Report a problem with this API docOpens a pre-filled issue on GitHub.