home / skills / eyadsibai / ltk / networkx

This skill helps you analyze graphs and networks with NetworkX to compute centrality, shortest paths, communities, and visualize insights.

npx playbooks add skill eyadsibai/ltk --skill networkx

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
2.9 KB
---
name: networkx
description: Use when "NetworkX", "graph analysis", "network analysis", "graph algorithms", "shortest path", "centrality", "PageRank", "community detection", "social network", "knowledge graph"
version: 1.0.0
---

# NetworkX Graph Analysis

Python library for creating, analyzing, and visualizing networks and graphs.

## When to Use

- Social network analysis
- Knowledge graphs and ontologies
- Shortest path problems
- Community detection
- Citation/reference networks
- Biological networks (protein interactions)

---

## Graph Types

| Type | Edges | Multiple Edges |
|------|-------|----------------|
| `Graph` | Undirected | No |
| `DiGraph` | Directed | No |
| `MultiGraph` | Undirected | Yes |
| `MultiDiGraph` | Directed | Yes |

---

## Key Algorithms

### Centrality Measures

| Measure | What It Finds | Use Case |
|---------|---------------|----------|
| **Degree** | Most connections | Popular nodes |
| **Betweenness** | Bridge nodes | Information flow |
| **Closeness** | Fastest reach | Efficient spreaders |
| **PageRank** | Importance | Web pages, citations |
| **Eigenvector** | Influential connections | Who knows important people |

### Path Algorithms

| Algorithm | Purpose |
|-----------|---------|
| **Shortest path** | Minimum hops |
| **Weighted shortest** | Minimum cost |
| **All pairs shortest** | Full distance matrix |
| **Dijkstra** | Efficient weighted paths |

### Community Detection

| Method | Approach |
|--------|----------|
| **Louvain** | Modularity optimization |
| **Greedy modularity** | Hierarchical merging |
| **Label propagation** | Fast, scalable |

---

## Graph Generators

| Generator | Model |
|-----------|-------|
| **Erdős-Rényi** | Random edges |
| **Barabási-Albert** | Preferential attachment (scale-free) |
| **Watts-Strogatz** | Small-world |
| **Complete** | All connected |

---

## Layout Algorithms

| Layout | Best For |
|--------|----------|
| **Spring** | General purpose |
| **Circular** | Regular structure |
| **Kamada-Kawai** | Aesthetics |
| **Spectral** | Clustered graphs |

---

## I/O Formats

| Format | Preserves Attributes | Human Readable |
|--------|---------------------|----------------|
| **GraphML** | Yes | Yes (XML) |
| **Edge list** | No | Yes |
| **JSON** | Yes | Yes |
| **Pandas** | Yes | Via DataFrame |

---

## Performance Considerations

| Scale | Approach |
|-------|----------|
| < 10K nodes | Any algorithm |
| 10K - 100K | Use approximate algorithms |
| > 100K | Consider graph-tool or igraph |

**Key concept**: NetworkX is pure Python - great for prototyping, may need alternatives for production scale.

---

## Best Practices

- Set random seeds for reproducibility
- Choose correct graph type upfront
- Use pandas integration for data exchange
- Consider memory for large graphs

## Resources

- NetworkX docs: <https://networkx.org/documentation/latest/>
- Tutorial: <https://networkx.org/documentation/latest/tutorial.html>

Overview

This skill provides a practical interface to NetworkX for creating, analyzing, and visualizing graphs in Python. It targets graph and network analysis tasks like shortest paths, centrality, PageRank, and community detection. Use it for prototyping social networks, knowledge graphs, citation networks, and other relational data workflows.

How this skill works

The skill constructs NetworkX graph objects (Graph, DiGraph, MultiGraph, MultiDiGraph) and runs built-in algorithms for centrality, shortest paths, community detection, graph generation, and layout. It reads and writes common formats (GraphML, edge lists, JSON, pandas DataFrames) and returns results as Python-native structures for downstream processing or visualization. For large graphs it will recommend approximate methods or alternative libraries when performance limits are reached.

When to use it

  • Exploring social networks, influence, or diffusion patterns
  • Modeling knowledge graphs, ontologies, or citation networks
  • Computing shortest paths, distances, or routing costs
  • Identifying central nodes (degree, betweenness, closeness, PageRank)
  • Detecting communities or cluster structure in relational data
  • Prototyping graph algorithms before scaling to production

Best practices

  • Choose the correct graph type (directed vs undirected, multi-edge support) before loading data
  • Set random seeds for reproducible layouts, generators, and community methods
  • Use pandas integration to import/export node and edge attribute tables cleanly
  • Prefer approximate or streaming algorithms for 10k–100k nodes; use graph-tool/igraph for >100k
  • Persist important attributes in GraphML or JSON to preserve metadata

Example use cases

  • Compute PageRank and identify influential authors in a citation network
  • Find shortest weighted routes between locations for logistics or routing
  • Detect communities in a social graph using Louvain or label propagation
  • Generate synthetic networks (Erdős-Rényi, Barabási-Albert, Watts-Strogatz) for simulations
  • Analyze protein-protein interaction patterns to prioritize candidate genes

FAQ

Which graph type should I pick?

Use Graph for undirected, DiGraph for directed relationships, and MultiGraph/MultiDiGraph when multiple parallel edges must be represented.

Is NetworkX suitable for very large graphs?

NetworkX is excellent for prototyping and small-to-medium graphs. For >100k nodes consider igraph or graph-tool for performance and memory efficiency.