Rustworkx: A High-Performance Graph Library for Python

0.17.1 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

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.

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]}")

view raw JSON →