networkx 3.6.1
NetworkX is a Python library for creating, manipulating, and studying the structure, dynamics, and functions of complex networks. Version 3.6.1 supports Python 3.10+ and relies on NumPy/SciPy for matrix operations.
Overview
Four core graph classes cover most use cases:
| Class |
Edges |
Self-loops |
Parallel edges |
nx.Graph |
Undirected |
Yes |
No |
nx.DiGraph |
Directed |
Yes |
No |
nx.MultiGraph |
Undirected |
Yes |
Yes |
nx.MultiDiGraph |
Directed |
Yes |
Yes |
Nodes can be any hashable Python object (integers, strings, tuples). Edges carry optional attribute dictionaries. The library is organized into modules: generators, algorithms, linalg, drawing, readwrite, convert, and relabel.
Usage
import networkx as nx
# Create graphs
G = nx.Graph()
G.add_node(1, color="red")
G.add_edge(1, 2, weight=3.0)
G.add_edges_from([(1, 3), (2, 3)], weight=1.0)
# Or from generators
G = nx.path_graph(10)
G = nx.erdos_renyi_graph(100, 0.1, seed=42)
G = nx.barabasi_albert_graph(100, 3, seed=42)
# Basic properties
len(G) # number of nodes
G.number_of_edges() # number of edges
list(G.nodes(data=True)) # nodes with attributes
list(G.edges(data=True)) # edges with attributes
G.degree(1) # degree of node 1
# Subgraphs and views
H = G.subgraph([1, 2, 3]) # induced subgraph (view, not copy)
H = G.copy() # deep copy
# Converting types
DG = G.to_directed() # Graph -> DiGraph view
UG = DG.to_undirected() # DiGraph -> Graph view
Common Patterns
Weighted shortest paths:
path = nx.shortest_path(G, source=0, target=9, weight="weight")
length = nx.shortest_path_length(G, source=0, target=9, weight="weight")
# Dijkstra from single source
paths, lengths = nx.single_source_dijkstra(G, source=0, weight="weight")
Centrality measures:
nx.degree_centrality(G)
nx.betweenness_centrality(G)
nx.pagerank(G, alpha=0.85)
Community detection:
communities = nx.community.louvain_communities(G, seed=42)
partition = next(nx.community.girvan_newman(G))
Connected components:
components = list(nx.connected_components(G)) # undirected
scc = list(nx.strongly_connected_components(DG)) # directed
nx.is_connected(G) # single component?
Gotchas
weight parameter is a string key, not a boolean. Pass weight="weight" to use the "weight" edge attribute. Pass weight=None for unweighted (every edge costs 1). Never pass weight=True.
Graph views are live, not copies. G.subgraph(nodes) returns a view that reflects changes to G. Call .copy() if you need an independent graph. Views of views chain slowly — avoid nesting more than ~15 levels deep.
nx.shortest_path raises NetworkXNoPath when source and target are in different components. Use try/except or check nx.has_path(G, s, t) first.
DiGraph iteration follows edge direction. G.neighbors(node) on a DiGraph returns only successors (outgoing edges). Use G.predecessors(node) for incoming edges.
MultiGraph edges need keys. On multigraphs, G.edges(data=True) yields (u, v, data) but parallel edges share (u, v). Use G.edges(keys=True, data=True) for (u, v, key, data).
add_edges_from with attributes applies to all edges. G.add_edges_from([(a,b), (c,d)], weight=2) sets weight=2 on both edges. To set per-edge attributes, pass dicts: G.add_edges_from([(a, b, {"w": 1}), (c, d, {"w": 2})]).
Layout functions need NumPy. All nx.*_layout() functions require NumPy. Drawing with nx.draw() additionally needs Matplotlib.
create_using controls output type. Many generators and conversion functions accept create_using=nx.DiGraph() to force a specific graph class. Pass a class or an instance.
Node labels can be any hashable. Strings, tuples, even custom objects work as nodes — but not None. When converting between formats, label types may change (e.g., convert_node_labels_to_integers).
nx.pagerank on undirected graphs auto-converts to directed. Each undirected edge becomes two directed edges internally.
Community detection is stochastic. Functions like louvain_communities and girvan_newman may return different results across runs. Always pass seed for reproducibility.
References
- 01-graph-classes — Graph, DiGraph, MultiGraph, MultiDiGraph; node/edge APIs; views
- 02-generators — Classic graphs, random graphs, geometric, social networks
- 03-shortest-paths — BFS, DFS, Dijkstra, Bellman-Ford, A*, Floyd-Warshall, Johnson
- 04-centrality — Degree, betweenness, closeness, eigenvector, Katz, PageRank, HITs
- 05-communities — Louvain, Leiden, Girvan-Newman, label propagation, modularity
- 06-connectivity-components — Connected/strongly connected components, biconnected, articulation points, bridges
- 07-flow-matching — Max flow, min-cost flow, Gomory-Hu, matching algorithms
- 08-dag-algorithms — Topological sort, ancestors/descendants, transitive closure/reduction
- 09-conversion-io — NumPy/Pandas/SciPy conversion; GraphML, GEXF, edgelist, JSON
- 10-matrices-linalg — Adjacency/Laplacian/incidence matrices, spectra, algebraic connectivity
- 11-graph-operators — Union, intersection, difference, symmetric difference, products, complement
- 12-drawing-layout — Matplotlib drawing, layout algorithms, Graphviz/PyDot export
1---2name: networkx-3-6-1-23description: Python graph library (NetworkX 3.6.1) for creating, manipulating, and analyzing complex networks. Use this skill whenever the user works with graphs, networks, nodes, edges, shortest paths, centrality, community detection, spanning trees, flow networks, DAGs, topological sort, graph generators, adjacency matrices, Laplacian spectra, isomorphism, bipartite matching, or any network science task. Triggers on: graph algorithms, network analysis, node/edge operations, Dijkstra, BFS, DFS, PageRank, Louvain communities, connected components, minimum spanning tree, max flow, transitive closure, and anything involving NetworkX or the `nx` module.4---56# networkx 3.6.178NetworkX is a Python library for creating, manipulating, and studying the structure, dynamics, and functions of complex networks. Version 3.6.1 supports Python 3.10+ and relies on NumPy/SciPy for matrix operations.910## Overview1112Four core graph classes cover most use cases:1314| Class | Edges | Self-loops | Parallel edges |15|---|---|---|---|16| `nx.Graph` | Undirected | Yes | No |17| `nx.DiGraph` | Directed | Yes | No |18| `nx.MultiGraph` | Undirected | Yes | Yes |19| `nx.MultiDiGraph` | Directed | Yes | Yes |2021Nodes can be any hashable Python object (integers, strings, tuples). Edges carry optional attribute dictionaries. The library is organized into modules: `generators`, `algorithms`, `linalg`, `drawing`, `readwrite`, `convert`, and `relabel`.2223## Usage2425```python26import networkx as nx2728# Create graphs29G = nx.Graph()30G.add_node(1, color="red")31G.add_edge(1, 2, weight=3.0)32G.add_edges_from([(1, 3), (2, 3)], weight=1.0)3334# Or from generators35G = nx.path_graph(10)36G = nx.erdos_renyi_graph(100, 0.1, seed=42)37G = nx.barabasi_albert_graph(100, 3, seed=42)3839# Basic properties40len(G) # number of nodes41G.number_of_edges() # number of edges42list(G.nodes(data=True)) # nodes with attributes43list(G.edges(data=True)) # edges with attributes44G.degree(1) # degree of node 14546# Subgraphs and views47H = G.subgraph([1, 2, 3]) # induced subgraph (view, not copy)48H = G.copy() # deep copy4950# Converting types51DG = G.to_directed() # Graph -> DiGraph view52UG = DG.to_undirected() # DiGraph -> Graph view53```5455### Common Patterns5657**Weighted shortest paths:**58```python59path = nx.shortest_path(G, source=0, target=9, weight="weight")60length = nx.shortest_path_length(G, source=0, target=9, weight="weight")61# Dijkstra from single source62paths, lengths = nx.single_source_dijkstra(G, source=0, weight="weight")63```6465**Centrality measures:**66```python67nx.degree_centrality(G)68nx.betweenness_centrality(G)69nx.pagerank(G, alpha=0.85)70```7172**Community detection:**73```python74communities = nx.community.louvain_communities(G, seed=42)75partition = next(nx.community.girvan_newman(G))76```7778**Connected components:**79```python80components = list(nx.connected_components(G)) # undirected81scc = list(nx.strongly_connected_components(DG)) # directed82nx.is_connected(G) # single component?83```8485## Gotchas8687- **`weight` parameter is a string key, not a boolean.** Pass `weight="weight"` to use the `"weight"` edge attribute. Pass `weight=None` for unweighted (every edge costs 1). Never pass `weight=True`.8889- **Graph views are live, not copies.** `G.subgraph(nodes)` returns a view that reflects changes to `G`. Call `.copy()` if you need an independent graph. Views of views chain slowly — avoid nesting more than ~15 levels deep.9091- **`nx.shortest_path` raises `NetworkXNoPath`** when source and target are in different components. Use `try/except` or check `nx.has_path(G, s, t)` first.9293- **DiGraph iteration follows edge direction.** `G.neighbors(node)` on a DiGraph returns only *successors* (outgoing edges). Use `G.predecessors(node)` for incoming edges.9495- **MultiGraph edges need keys.** On multigraphs, `G.edges(data=True)` yields `(u, v, data)` but parallel edges share `(u, v)`. Use `G.edges(keys=True, data=True)` for `(u, v, key, data)`.9697- **`add_edges_from` with attributes applies to all edges.** `G.add_edges_from([(a,b), (c,d)], weight=2)` sets `weight=2` on both edges. To set per-edge attributes, pass dicts: `G.add_edges_from([(a, b, {"w": 1}), (c, d, {"w": 2})])`.9899- **Layout functions need NumPy.** All `nx.*_layout()` functions require NumPy. Drawing with `nx.draw()` additionally needs Matplotlib.100101- **`create_using` controls output type.** Many generators and conversion functions accept `create_using=nx.DiGraph()` to force a specific graph class. Pass a class or an instance.102103- **Node labels can be any hashable.** Strings, tuples, even custom objects work as nodes — but not `None`. When converting between formats, label types may change (e.g., `convert_node_labels_to_integers`).104105- **`nx.pagerank` on undirected graphs auto-converts to directed.** Each undirected edge becomes two directed edges internally.106107- **Community detection is stochastic.** Functions like `louvain_communities` and `girvan_newman` may return different results across runs. Always pass `seed` for reproducibility.108109## References110111- [01-graph-classes](references/01-graph-classes.md) — Graph, DiGraph, MultiGraph, MultiDiGraph; node/edge APIs; views112- [02-generators](references/02-generators.md) — Classic graphs, random graphs, geometric, social networks113- [03-shortest-paths](references/03-shortest-paths.md) — BFS, DFS, Dijkstra, Bellman-Ford, A*, Floyd-Warshall, Johnson114- [04-centrality](references/04-centrality.md) — Degree, betweenness, closeness, eigenvector, Katz, PageRank, HITs115- [05-communities](references/05-communities.md) — Louvain, Leiden, Girvan-Newman, label propagation, modularity116- [06-connectivity-components](references/06-connectivity-components.md) — Connected/strongly connected components, biconnected, articulation points, bridges117- [07-flow-matching](references/07-flow-matching.md) — Max flow, min-cost flow, Gomory-Hu, matching algorithms118- [08-dag-algorithms](references/08-dag-algorithms.md) — Topological sort, ancestors/descendants, transitive closure/reduction119- [09-conversion-io](references/09-conversion-io.md) — NumPy/Pandas/SciPy conversion; GraphML, GEXF, edgelist, JSON120- [10-matrices-linalg](references/10-matrices-linalg.md) — Adjacency/Laplacian/incidence matrices, spectra, algebraic connectivity121- [11-graph-operators](references/11-graph-operators.md) — Union, intersection, difference, symmetric difference, products, complement122- [12-drawing-layout](references/12-drawing-layout.md) — Matplotlib drawing, layout algorithms, Graphviz/PyDot export