Guide/Start here
Quick Start
1 min readFrom the README
Install
# from PyPI (pre-built wheels, no Rust toolchain required)
pip install franken-networkx
# with SciPy for the spectral / sparse-matrix functions (NumPy is always installed)
pip install 'franken-networkx[all]'
# from source (requires Rust nightly + maturin)
git clone https://github.com/Dicklesworthstone/franken_networkx
cd franken_networkx
pip install maturin
maturin develop --release --features pyo3/abi3-py310
Run an algorithm
import franken_networkx as fnx
# Build a small weighted graph
G = fnx.Graph()
G.add_weighted_edges_from([
("a", "b", 1.0),
("b", "c", 2.0),
("a", "c", 5.0),
("c", "d", 1.0),
("b", "d", 4.0),
])
print(fnx.shortest_path(G, "a", "d", weight="weight")) # ['a', 'b', 'c', 'd']
print(fnx.shortest_path_length(G, "a", "d", weight="weight")) # 4.0
# pagerank needs scipy (pip install 'franken-networkx[scipy]'), exactly as nx.pagerank does.
print(sorted(fnx.pagerank(G).items()))
# [('a', 0.229...), ('b', 0.270...), ('c', 0.299...), ('d', 0.200...)]
# (b and c are the degree-3 hubs; a and d are degree-2 leaves of the cluster.)
print(list(fnx.connected_components(G)))
# [{'a', 'b', 'c', 'd'}]
Drop in as a backend
import networkx as nx
# Enable the backend once, application-wide.
nx.config.backend_priority = ["franken_networkx"]
G = nx.erdos_renyi_graph(1_000, 0.01, seed=42)
cc = nx.connected_components(G) # routes to FrankenNetworkX
pr = nx.pagerank(G) # routes to FrankenNetworkX
diam = nx.diameter(G) # routes to FrankenNetworkX
Round-trip I/O
import franken_networkx as fnx
from pathlib import Path
from tempfile import TemporaryDirectory
G = fnx.path_graph(5)
with TemporaryDirectory() as directory:
path = Path(directory) / "graph.xml"
fnx.write_graphml(G, path)
H = fnx.read_graphml(path)
assert sorted(map(str, H.nodes())) == sorted(map(str, G.nodes()))
assert sorted(tuple(map(str, edge)) for edge in H.edges()) == sorted(
tuple(map(str, edge)) for edge in G.edges()
)