Skip to content

NetworkX Backend Interface

nx-neptune serves as a NetworkX-compatible backend for Neptune Analytics. This enables you to offload graph algorithm workloads to AWS with no code changes — use familiar NetworkX APIs to seamlessly scale graph computations on-demand.

For more on NetworkX backends, see the NetworkX backends documentation.

  1. You write standard NetworkX code
  2. Add backend="neptune" to any supported algorithm call
  3. nx-neptune transparently syncs your graph to Neptune Analytics, executes the algorithm there, and returns results in NetworkX-compatible format
import networkx as nx
# Build a small social network
G = nx.DiGraph()
G.add_edges_from([
("Alice", "Bob"),
("Alice", "Carol"),
("Bob", "Carol"),
("Carol", "Dave"),
("Dave", "Alice"),
])
# Runs on Neptune Analytics, returns standard NetworkX result format
scores = nx.pagerank(G, backend="neptune")
# Who's most influential?
top = sorted(scores.items(), key=lambda x: x[1], reverse=True)
for name, score in top:
print(f"{name}: {score:.4f}")

Set the NETWORKX_GRAPH_ID environment variable to point at your Neptune Analytics instance:

Terminal window
export NETWORKX_GRAPH_ID=your-neptune-analytics-graph-id
python ./nx_example.py

Or inline:

Terminal window
NETWORKX_GRAPH_ID=your-neptune-analytics-graph-id python ./nx_example.py

The backend is registered via the nx_plugin entry point in pyproject.toml. When you install nx_neptune, NetworkX automatically discovers the "neptune" backend.

# No explicit import needed — just use the backend parameter
result = nx.pagerank(G, backend="neptune")

Each algorithm follows this pattern:

from nx_neptune.utils.decorators import configure_if_nx_active
@configure_if_nx_active()
def algorithm_name(neptune_graph: NeptuneGraph, **kwargs):
# Build openCypher query
# Execute on Neptune Analytics
# Transform results to NetworkX format
return result

nx-neptune supports algorithms across four categories:

CategoryAlgorithms
Traversalbfs_edges, descendants_at_distance, bfs_layers
Link Analysispagerank
Centralitydegree_centrality, in_degree_centrality, out_degree_centrality, closeness_centrality
Communitylouvain_communities, label_propagation_communities, fast_label_propagation_communities, asyn_lpa_communities

Full algorithm reference

Beyond standard NetworkX parameters, nx-neptune exposes Neptune Analytics-specific parameters on each algorithm:

  • vertex_label — filter by vertex label
  • edge_labels — filter by edge labels
  • concurrency — control thread count
  • traversal_direction"outbound" or "inbound"
  • write_property — persist results as node properties
  • edge_weight_property / edge_weight_type — weighted computation

These are passed as additional keyword arguments:

r = nx.pagerank(
G,
backend="neptune",
vertex_label="Person",
edge_labels=["KNOWS"],
write_property="pr_score",
)