FrankenNetworkX

Guide/How it works

NetworkX Backend Protocol Primer

2 min readFrom the README

On this page 3
What the dispatcher does on each callPer-call vs application-wideFallback semantics

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"):

  1. NetworkX asks backend_info.get_backend_info() for the list of supported algorithms (parsed from _SUPPORTED_ALGORITHMS via AST so the metadata side is safe to import while NetworkX itself is still initializing).
  2. NetworkX asks BackendInterface.can_run(name, args, kwargs) whether fnx will accept the call shape. can_run returns False for:
    • unsupported algorithm names,
    • argument shapes that fail inspect.signature(...).bind(...),
    • average_shortest_path_length with a non-default method=...,
    • node_connectivity / edge_connectivity / minimum_node_cut / minimum_edge_cut when a custom flow_func=... is supplied (fnx's native flow implementation can't honor arbitrary user callables).
  3. If can_run returns True, NetworkX calls BackendInterface.convert_from_nx(G) to materialize an fnx graph. The conversion preserves node insertion order via the _topo_emit_edges_by_adj helper so adjacency order matches what nx would have iterated.
  4. The bound fnx function runs.
  5. NetworkX calls BackendInterface.convert_to_nx(result). This recursively unwraps fnx graphs hiding inside dicts, lists, tuples, and sets, so an algorithm that returns dict[str, fnx.Graph] (e.g. gomory_hu_tree subgraphs) returns dict[str, nx.Graph] to the caller.
  6. Functions that mutate their input (relabel_nodes(copy=False), contracted_nodes(copy=False), set_node_attributes, double_edge_swap, ...) follow NetworkX's dispatcher rules. Under backend_priority NetworkX does not auto-dispatch them, so they run in NetworkX on your graph. With an explicit backend="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.