Rustworkx: A High-Performance Graph Library for Python
Rustworkx is a general-purpose, high-performance graph library for Python, implemented in Rust. It provides efficient data structures and algorithms for working with graphs and complex networks, offering a fast alternative to other Python graph libraries, especially for performance-critical applications. The current version is 0.17.1, with a consistent release cadence that includes multiple minor/patch releases per year.
Warnings
- breaking The library was renamed from `retworkx` to `rustworkx`. While `retworkx` may still work for older versions, the legacy name will no longer be supported starting from the `1.0.0` release.
- breaking The minimum supported Rust version required for building `rustworkx` from source has been raised to `1.79` for versions `0.17.1` and later. Users installing pre-compiled binaries are not affected.
- gotcha Unlike NetworkX, `rustworkx` uses stable integer indices for all nodes and edges. While arbitrary Python objects can be stored as node/edge payloads, all graph operations (adding/removing/accessing) must use these integer indices, not the payload objects directly.
- gotcha Many `rustworkx` algorithm functions are explicitly typed (e.g., `rustworkx.graph_something` for `PyGraph` and `rustworkx.digraph_something` for `PyDiGraph`). Passing the wrong graph type will result in an error.
- gotcha When developing `rustworkx` from source, `pip install -e` (editable install) does not fully work as expected. Local changes to Rust code will not be reflected live in the Python environment without a full reinstallation.
Install
-
pip install rustworkx -
pip install 'rustworkx[all]'
Imports
- rustworkx
import rustworkx
- rx
import rustworkx as rx
Quickstart
import rustworkx as rx
# Create an undirected graph
graph = rx.PyGraph()
# Add nodes with data payloads (e.g., strings)
a = graph.add_node("A")
b = graph.add_node("B")
c = graph.add_node("C")
# Add edges with weights
graph.add_edges_from([(a, b, 1.5), (a, c, 5.0), (b, c, 2.5)])
# Find the shortest path using Dijkstra's algorithm
# Nodes are referenced by their integer indices
path = rx.dijkstra_shortest_paths(graph, a, c, weight_fn=float)
print(f"Shortest path from A to C: {path}")
# Access node data using its index
print(f"Node at index {a}: {graph[a]}")