Skip to main content

Working with NetworkX

NetworkX is a Python library for creating, manipulating, and studying networks. Its graph objects are convenient in-memory containers and algorithm interfaces. They do not replace a data contract, quality checks, or a storage system designed for a production-scale graph.

Prerequisites​

Complete edge lists, adjacency matrices, and sparse storage. You should understand why this fixture needs declared nodes and parallel edge keys.

Learning goals​

  • Build a MultiDiGraph without dropping isolates or repeated events.
  • Attach node and edge attributes without making them part of identity accidentally.
  • Inspect nodes, edges, keys, and attributes through maintained public methods.
  • Separate NetworkX-specific conveniences from graph semantics.

Version and construction contract​

The repository pins NetworkX 3.7, the stable release checked on 2026-10-18. NetworkX point releases may include API changes, so examples are verified against the exact pin rather than an open-ended dependency.

Construction order matters for reproducible display and iteration, but insertion order is not a graph-theoretic ranking. Add every declared node first; then add only accepted relationships with an explicit event ID as the multiedge key. Algorithms receive a graph object, but their mathematical preconditions still depend on direction, weights, self-loops, and connectivity.

Worked example​

import csv
from pathlib import Path

import networkx as nx

fixture = Path("tests/fixtures/graph-data-science")
graph = nx.MultiDiGraph(observation="local teaching fixture")
with (fixture / "interaction-nodes.csv").open(encoding="utf-8") as handle:
for row in csv.DictReader(handle):
graph.add_node(row["node_id"], label=row["label"], node_type=row["node_type"])
with (fixture / "interaction-relationships.csv").open(encoding="utf-8") as handle:
for row in csv.DictReader(handle):
if row["record_status"] == "valid":
graph.add_edge(
row["source"], row["target"], key=row["relationship_id"],
weight=int(row["weight"]), interaction_type=row["interaction_type"],
observed_at=row["observed_at"],
)

print(f"networkx={nx.__version__} graph_type={type(graph).__name__}")
print(
f"nodes={graph.number_of_nodes()} edges={graph.number_of_edges()} "
f"ada_ben_keys={list(graph['ada']['ben'])}"
)

Expected output:

networkx=3.7 graph_type=MultiDiGraph
nodes=7 edges=10 ada_ben_keys=['r01', 'r02']

number_of_edges() counts event edges, including parallel edges and the self-loop. Distinct ordered node pairs are a different quantity.

Limitations​

  • NetworkX stores graph data in Python objects; very large networks may require sparse, database, distributed, or specialized systems.
  • A convenient default does not establish semantic correctness. For example, an algorithm's weight parameter may treat weight as cost rather than strength.
  • Backend support and return types vary by function; consult the exact stable API before relying on acceleration.
  • Iteration order can make output repeatable, but it is not importance, centrality, or evidence.

Practice​

Add a new declared isolate and verify that the node count changes while the edge count does not. Then inspect one edge's interaction_type and observed_at without reading private attributes.

Next steps​

Next, reject bad records and document uncertainty in graph data quality and missing relationships.

Primary sources​

Checked 2026-10-18: NetworkX, stable 3.7 documentation; NetworkX, 3.7 release notes; NetworkX, tutorial; NetworkX, MultiDiGraph reference.