# networkx

> Comprehensive toolkit for creating, analyzing, and visualizing complex networks and graphs in Python — covering graph algorithms (shortest paths, centrality, clustering), community detection, synthetic network generation, and network visualization.

**Use case**: Analyze and visualize graph and network data structures

**Canonical URL**: https://agentcookbooks.com/skills/networkx/

**Topics**: claude-code, skills, science, data-science

**Trigger phrases**: "analyze this network", "graph centrality", "shortest path in a graph", "community detection", "networkx visualization"

**Source**: [K-Dense AI](https://github.com/K-Dense-AI/scientific-agent-skills/tree/main/scientific-skills/networkx)

**License**: MIT

---

## What it does

`networkx` is a Claude Code skill from K-Dense AI's [scientific-agent-skills repo](https://github.com/K-Dense-AI/scientific-agent-skills). It turns Claude into a NetworkX expert covering graph creation (directed, undirected, multigraph, bipartite), graph algorithms (shortest paths, centrality measures, clustering coefficients, community detection), synthetic network generation (Erdős–Rényi, Barabási–Albert, Watts–Strogatz), and visualization with matplotlib layouts.

A session produces Python code that builds, analyzes, and visualizes the graph structure from your data — from a simple node-edge list to a full network analysis pipeline with centrality rankings and community partition.

## When to use it

Reach for it when:

- You have relational data (citation networks, biological networks, social graphs, transportation networks) and want graph-theoretic analysis
- You need standard graph algorithms: shortest path, betweenness centrality, PageRank, connected components, or community detection
- You're teaching or prototyping graph algorithms and want a Pythonic library with good documentation coverage

When *not* to reach for it:

- Very large graphs (millions of nodes/edges) where NetworkX's pure-Python implementation becomes slow — consider `igraph` or GPU-based graph libraries via `optimize-for-gpu`
- Graph neural networks — use `torch-geometric`

## Install

Copy the `SKILL.md` from K-Dense AI's [networkx folder](https://github.com/K-Dense-AI/scientific-agent-skills/tree/main/scientific-skills/networkx) into `.claude/skills/networkx/` in your project.

Trigger phrases: "analyze this network", "graph centrality", "shortest path in a graph", "community detection", "networkx visualization".

## What a session looks like

A typical session has three phases:

1. **Graph construction.** Provide the edge list, adjacency matrix, or DataFrame with source/target columns. Claude constructs the appropriate NetworkX graph type and validates connectivity.
2. **Analysis.** Claude runs the requested algorithms — centrality measures, path analysis, clustering, community detection (Louvain, Girvan-Newman, label propagation) — and returns structured results with the top-N nodes or statistics.
3. **Visualization.** The graph is drawn with a layout appropriate to the structure (spring layout for small graphs, shell or circular for structured graphs) with node size or color mapped to the computed metric.

## Receipts

**Where it works well:**
- Citation network analysis — building a graph from a BibTeX file and computing betweenness centrality to find bridging papers takes a dozen lines of NetworkX code
- Small-to-medium biological networks (protein-protein interaction, metabolic pathways) where the graph fits in memory and standard centrality measures surface meaningful biology

**Where it backfires:**
- Graphs with millions of edges become slow or run out of memory in NetworkX's pure-Python data structures — at that scale, cuGraph (via `optimize-for-gpu`) or specialized libraries are needed
- Graph visualization for dense networks produces unreadable hairball plots; NetworkX's matplotlib integration doesn't handle graph sparsification automatically

**Pattern that works:** always check graph size (number of nodes and edges) before running expensive algorithms like betweenness centrality — it scales as O(VE) and will hang on large dense graphs without warning.

## Source and attribution

Originally authored by [K-Dense Inc.](https://github.com/K-Dense-AI). The canonical SKILL.md lives in the [`networkx` folder](https://github.com/K-Dense-AI/scientific-agent-skills/tree/main/scientific-skills/networkx) of their public scientific-agent-skills repository.

License: MIT. Install, adapt, and redistribute with attribution preserved.

This page documents the skill from a practitioner's perspective. For the formal spec and any updates, defer to the source repo.