retworkx

raw JSON →
0.17.1 verified Mon Apr 27 auth: no python

A high-performance graph library for Python, written in Rust, providing directed and undirected graph data structures with efficient algorithms. Current version 0.17.1, release cadence irregular.

pip install retworkx
error ModuleNotFoundError: No module named 'retworkx'
cause retworkx not installed or environment not activated
fix
Run pip install retworkx in the correct environment (Python >=3.9).
error TypeError: add_child() missing 1 required positional argument: 'obj'
cause Missing the edge weight argument in add_child
fix
Provide a third argument, e.g., dag.add_child(parent, child, None).
error AttributeError: module 'retworkx' has no attribute 'digraph'
cause Using the deprecated function name from old versions (<0.14.0)
fix
Use retworkx.PyDiGraph() instead of retworkx.digraph().
breaking Version 0.14.0 replaced the original function-based API with the class-based API (PyGraph, PyDiGraph). Code using `from retworkx import *` or older function names will break.
fix Migrate to using PyGraph and PyDiGraph classes. See migration guide at https://qiskit.org/documentation/retworkx/
deprecated The `digraph` and `graph` constructor functions are deprecated and removed in 0.14.0.
fix Replace `retworkx.digraph()` with `retworkx.PyDiGraph()` and similarly for undirected graphs.
gotcha Node indices are integers returned by add_node/add_child; do not rely on them being sequential or stable after deletions.
fix Assign custom node data or use node indices immediately; do not assume order.
gotcha Edge weights are optional but must be passed as the third argument to add_child; passing None is acceptable but can lead to type confusion.
fix Always provide an explicit weight (or None) to avoid unexpected behavior.

Create a directed acyclic graph, add nodes and edges, and print them.

import retworkx as rx

dag = rx.PyDiGraph()
node_a = dag.add_node('A')
node_b = dag.add_child(node_a, 'B', None)
print(dag.nodes())
print(dag.edges())