Usage

nx-rustworkx plugs into NetworkX's backend dispatcher. Most users keep ordinary NetworkX graphs and enable automatic dispatch. Build a backend graph directly only when the same graph feeds many supported calls.

Install

pip install nx-rustworkx

Or, with uv:

uv add nx-rustworkx

Python 3.10+, NetworkX 3.4+, and rustworkx 0.18+ (as a published wheel) are required. The package does not compile custom Rust.

Automatic dispatch

Set the preferred backend before starting Python:

NETWORKX_BACKEND_PRIORITY=rustworkx python your_script.py

Or configure the running process:

import networkx as nx

nx.config.backend_priority = ["rustworkx"]

G = nx.gnp_random_graph(2_000, 0.01, seed=1)
scores = nx.betweenness_centrality(G)

The backend's should_run hook normally declines graphs with fewer than 200 nodes or 400 edges. It also declines functions whose measured conversion cost outweighs the rustworkx kernel.

nx.config.backends.rustworkx.min_nodes = 200
nx.config.backends.rustworkx.min_edges = 400
Dispatch behavior
CallWhat happens
Supported and large enough Convert, run rustworkx, remap the result.
Supported but not worth converting Stay on NetworkX.
Unsupported on an nx.Graph Stay on NetworkX.
backend="rustworkx" Try the backend regardless of its performance cutoff.

Select one call explicitly

lengths = nx.single_source_dijkstra_path_length(
    G,
    source="helsinki",
    backend="rustworkx",
)

Explicit selection bypasses performance policy, not compatibility. Arguments the implementation cannot honor still raise rather than silently returning a different answer.

Skip repeated conversion

NetworkX 3.6+ can construct the backend graph directly. Constructors such as empty_graph and from_edgelist work on every supported NetworkX version.

import networkx as nx

G = nx.Graph([(0, 1), (1, 2), (2, 0)], backend="rustworkx")
scores = nx.betweenness_centrality(G)

RustworkxGraph supports common construction calls, node and edge attributes, and the nodes, edges, adj, and degree views; nx.MultiGraph(..., backend="rustworkx") returns a RustworkxMultiGraph with NetworkX's edge keys (add_edge(u, v, key=...), edges(keys=True), G[u][v][key]). Neither is a complete replacement for the NetworkX classes: there is no drawing or I/O.

Generators

Graph generators can build rustworkx-backed graphs directly, so a pipeline never converts at all. Generator dispatch has its own priority list, separate from the algorithm one:

NETWORKX_BACKEND_PRIORITY_GENERATORS=rustworkx python your_script.py

Or in the running process:

nx.config.backend_priority.generators = ["rustworkx"]

G = nx.gnp_random_graph(20_000, 0.001)   # rustworkx-backed, sampled in Rust
H = nx.path_graph(100_000)               # rustworkx-backed, built in Rust

Deterministic generators (path_graph, complete_graph, grid_2d_graph, …) produce exactly the graph NetworkX would, verified against NetworkX's own test suite. Generators without a native kernel still return a backend graph whenever their NetworkX implementation builds on empty_graph; NetworkX's own code runs, mutating a rustworkx-backed graph.

Seeded random generators

rustworkx samples with its own RNG, so for the same seed it draws a different — equally valid — graph than NetworkX. By default nx-rustworkx protects seeded reproducibility: a call with an explicit seed falls back to NetworkX's sampler (still returning a rustworkx-backed graph under generator priority), while unseeded calls sample natively. Opt into native seeded sampling with:

nx.config.backends.rustworkx.native_seeded_generators = True

With the opt-in, the same seed reproduces the same graph on any platform for a pinned rustworkx version — but never NetworkX's graph for that seed, and rustworkx upgrades may change the stream. On this backend fast_gnp_random_graph shares gnp_random_graph's kernel, so both names give the same graph for the same seed.

Pair generator priority with fallback if the pipeline also calls functions the backend does not implement.

Fallback from a backend graph

An unsupported algorithm cannot run directly on a backend graph. Turn on fallback if NetworkX should convert it back to an nx.Graph:

nx.config.fallback_to_nx = True
triangles = nx.triangles(G)

Or set it for the whole process:

NETWORKX_FALLBACK_TO_NX=true python your_script.py

Without fallback, prefer an ordinary nx.Graph plus backend priority so unsupported functions naturally remain on NetworkX.