Guide/How it works
NetworkX Backend Protocol Primer
2 min readFrom the README
NetworkX 3.0+ ships a backend protocol that lets third-party libraries accelerate or replace algorithm implementations. FrankenNetworkX implements it through two Python entry points wired up in pyproject.toml:
[project.entry-points."networkx.backends"]
franken_networkx = "franken_networkx.backend:backend_interface"
[project.entry-points."networkx.backend_info"]
franken_networkx = "fnx_backend_info:get_backend_info"
(fnx_backend_info is a small top-level module shipped in the wheel, so NetworkX can read the backend's metadata without importing the extension while NetworkX itself is still initialising.)
Once the package is installed, NetworkX picks up these entry points automatically. There is nothing to import on the user's side beyond networkx.
What the dispatcher does on each call
For each nx.<algorithm>(...) call when franken_networkx is in the priority list (or supplied as backend="franken_networkx"):
- NetworkX asks
backend_info.get_backend_info()for the list of supported algorithms (parsed from_SUPPORTED_ALGORITHMSvia AST so the metadata side is safe to import while NetworkX itself is still initializing). - NetworkX asks
BackendInterface.can_run(name, args, kwargs)whether fnx will accept the call shape.can_runreturnsFalsefor:- unsupported algorithm names,
- argument shapes that fail
inspect.signature(...).bind(...), average_shortest_path_lengthwith a non-defaultmethod=...,node_connectivity/edge_connectivity/minimum_node_cut/minimum_edge_cutwhen a customflow_func=...is supplied (fnx's native flow implementation can't honor arbitrary user callables).
- If
can_runreturnsTrue, NetworkX callsBackendInterface.convert_from_nx(G)to materialize an fnx graph. The conversion preserves node insertion order via the_topo_emit_edges_by_adjhelper so adjacency order matches what nx would have iterated. - The bound fnx function runs.
- NetworkX calls
BackendInterface.convert_to_nx(result). This recursively unwraps fnx graphs hiding inside dicts, lists, tuples, and sets, so an algorithm that returnsdict[str, fnx.Graph](e.g.gomory_hu_treesubgraphs) returnsdict[str, nx.Graph]to the caller. - Functions that mutate their input (
relabel_nodes(copy=False),contracted_nodes(copy=False),set_node_attributes,double_edge_swap, ...) follow NetworkX's dispatcher rules. Underbackend_priorityNetworkX does not auto-dispatch them, so they run in NetworkX on your graph. With an explicitbackend="franken_networkx"on a NetworkX graph, NetworkX converts the graph and fnx mutates the converted copy — NetworkX documents this ("may change behavior by not mutating inputs") for every backend. On an fnx graph they mutate that graph in place, exactly as NetworkX would (tests/python/test_mutating_dispatchables_state_parity.py).
Per-call vs application-wide
import networkx as nx
# Application-wide:
nx.config.backend_priority = ["franken_networkx"]
# Per-call:
demo = nx.path_graph(10)
s, t = 0, 9
nx.shortest_path(demo, s, t, backend="franken_networkx")
The application-wide form still falls back to NetworkX for any algorithm fnx doesn't claim. The per-call form raises NotImplementedError if fnx doesn't claim the algorithm (you can wrap it yourself if you want a softer fallback).
Fallback semantics
When can_run returns False, NetworkX uses the next backend in backend_priority, or pure-Python NetworkX itself. This is why FrankenNetworkX is safe to install in an existing nx codebase: in the worst case, you're back to vanilla nx.