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.
pip install rustworkxVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates creating a `PyGraph` (undirected graph), adding nodes and edges with associated data, and calculating the shortest path between two nodes using Dijkstra's algorithm. Node and edge data can be any Python object, and nodes are referenced by stable integer indices.
Update all import statements from `import retworkx` to `import rustworkx` and update package names in `requirements.txt` or `pyproject.toml`.
If building from source, ensure your Rust toolchain is updated to version `1.79` or newer using `rustup update`.
Adapt code to work with node/edge indices returned by `add_node()`/`add_edge()` methods. Access node data using the graph's mapping protocol (e.g., `graph[node_index]`). Use callback functions for algorithms that operate on node/edge data (e.g., `weight_fn`).
Always use the correct explicitly typed function for `PyGraph` (undirected) or `PyDiGraph` (directed) instances. Check the API documentation for the correct function signature.
After making changes to the Rust codebase, rerun `pip install .` in the repository root to recompile and update the installed library. Avoid `pip install -e` for Rust code changes.
pip install rustworkx
Install the Rust toolchain by following the instructions at https://rustup.rs/
Use `graph.add_nodes_from([data1, data2])` to add nodes and `graph.add_edges_from([(u, v, weight), ...])` for edges.
Use `graph.get_node_data(index)` to retrieve the payload of a node at a specific index.