Skip to content
Data
Skill

/networkx

Build, analyze, and visualize networks and graphs using NetworkX (Python). Use this skill whenever the user wants to: create graphs or networks, analyze graph properties, compute centrality measures, find shortest paths, detect communities, run graph algorithms, convert graphs

From plugin
mykg
844 skills4 agents1 MCP

Claude Code

$ npx -y skills add SenolIsci/mykg --skill networkx --agent claude-code

SKILL.md

networkx.SKILL.md
name: networkx
description: "Build, analyze, and visualize networks and graphs using NetworkX (Python). Use this skill whenever the user wants to: create graphs or networks, analyze graph properties, compute centrality measures, find shortest paths, detect communities, run graph algorithms, convert graphs to/from matrices or dataframes, visualize networks with matplotlib, import/export graph files (GML, GraphML, GEXF, edgelist, etc.), work with directed or undirected graphs, weighted or multigraphs, perform social network analysis, or do any graph theory computation. Trigger on keywords: networkx, graph, network, nodes, edges, adjacency, shortest path, centrality, community detection, spanning tree, flow, clique, PageRank, bipartite, DAG, topology, graph analysis, social network."
allowed-tools: Bash Read Write Edit

NetworkX Skill — Create and Manipulate Networks

NetworkX (v3.6+) is the standard Python library for graph analysis. This skill covers everything from basic graph creation to advanced algorithms. When in doubt, prefer simple explicit code over clever one-liners — graphs are complex enough on their own.

**References:**

  • [algorithms.md](references/algorithms.md) — Algorithm reference by category (centrality, community, flow, etc.)
  • [io.md](references/io.md) — File I/O and format conversion reference

---

1. Choosing a Graph Class

Pick the right class first — it cannot easily be changed after construction.

import networkx as nx

G  = nx.Graph()          # undirected, no parallel edges
DG = nx.DiGraph()        # directed, no parallel edges
MG = nx.MultiGraph()     # undirected + parallel edges allowed
MD = nx.MultiDiGraph()   # directed + parallel edges allowed

| Need | Class | |---|---| | Social networks, protein interactions | `Graph` | | Web graphs, citation networks, DAGs | `DiGraph` | | Transport networks (multiple routes) | `MultiGraph` | | Dependency graphs with typed edges | `MultiDiGraph` |

Convert between types:

DG = G.to_directed()    # Graph → DiGraph (each edge becomes two arcs)
G2 = DG.to_undirected() # DiGraph → Graph

---

2. Building Graphs

Add Nodes

Any hashable Python object is a valid node: int, str, tuple, frozenset.

G.add_node(1)
G.add_node("Alice", age=30, role="engineer")   # node with attributes
G.add_nodes_from([2, 3, 4])
G.add_nodes_from([
    ("Bob",   {"age": 25, "role": "designer"}),
    ("Carol", {"age": 35, "role": "manager"}),
])

Add Edges

G.add_edge(1, 2)
G.add_edge("Alice", "Bob", weight=0.9, relation="colleague")
G.add_edges_from([(1, 2), (2, 3), (3, 4)])
G.add_edges_from([
    (1, 2, {"weight": 1.5}),
    (2, 3, {"weight": 0.8}),
])
G.add_weighted_edges_from([(1, 2, 1.5), (2, 3, 0.8)])  # shorthand

For MultiGraph, `add_edge` returns the edge key (int):

k = MG.add_edge(1, 2, weight=0.5)   # k=0
k = MG.add_edge(1, 2, weight=0.75)  # k=1 (parallel edge)

Remove Nodes and Edges

G.remove_node(1)
G.remove_nodes_from([2, 3])
G.remove_edge(1, 2)
G.remove_edges_from([(1, 2), (2, 3)])
G.clear()  # remove everything

Graph-Level Attributes

G = nx.Graph(name="Social Network", created="2025")
G.graph["description"] = "Friendship graph"

---

3. Inspecting a Graph

# Size
G.number_of_nodes()   # or len(G)
G.number_of_edges()   # or G.size()

# Nodes and edges (views, not copies)
list(G.nodes)
list(G.nodes(data=True))                   # with attributes
list(G.nodes(data="weight", default=1.0))  # specific attribute

list(G.edges)
list(G.edges(data=True))       # with attributes
list(G.edges(data="weight"))   # specific attribute

# Adjacency
list(G.neighbors(1))         # undirected neighbors
list(G.adj[1])               # same, dict-style
G.adj[1][2]["weight"]        # edge attribute lookup

# DiGraph-specific
list(DG.successors(n))
list(DG.predecessors(n))
list(DG.in_edges(n))
list(DG.out_edges(n))

# Degree
G.degree(1)               # single node
dict(G.degree())          # all nodes → {node: degree}
dict(G.degree(weight="weight"))  # weighted degree

# MultiDiGraph
MD.in_degree(n)
MD.out_degree(n)

Node/Edge Membership

1 in G           # node membership
(1, 2) in G.edges  # edge membership
G.has_node(1)
G.has_edge(1, 2)

---

4. Attributes: Read and Write

# Read node attribute
G.nodes[1]["color"]

# Write node attribute
G.nodes[1]["color"] = "red"

# Read edge attribute
G[1][2]["weight"]          # Graph / DiGraph
MG[1][2][0]["weight"]      # MultiGraph (key=0)

# Write edge attribute
G[1][2]["weight"] = 4.7

# Bulk set / get with dict or scalar
nx.set_node_attributes(G, {1: "red", 2: "blue"}, name="color")
nx.set_node_attributes(G, 0.0, name="score")          # same value for all

nx.set_edge_attributes(G, {(1,2): 1.5, (2,3): 0.8}, name="weight")

colors = nx.get_node_attributes(G, "color")           # {node: value}
weights = nx.get_edge_attributes(G, "weight")         # {(u,v): value}

---

5. Graph Views and Subgraphs

Views are live windows — they reflect changes to the original graph without copying data.

# Node-induced subgraph
sub = G.subgraph([1, 2, 3])   # view
sub = G.subgraph([1, 2, 3]).copy()  # independent copy

# Edge-induced subgraph
esub = G.edge_subgraph([(1, 2), (2, 3)])

# Filter view (no copy)
import networkx as nx
heavy = nx.subgraph_view(G, filter_edge=lambda u, v: G[u][v]["weight"] > 0.5)

# Reverse a DiGraph
R = DG.reverse()         # view
R = DG.reverse(copy=True)

# Add useful structural paths/cycles
nx.add_path(G, [10, 11, 12, 13])
nx.add_cycle(G, [20, 21, 22])
nx.add_star(G, [0, 1, 2, 3])  # 0 is the hub

---

6. Graph Generators

Classic

nx.complete_graph(5)           # K5
nx.complete_bipartite_graph(3, 4)
nx.cycle_graph(6)
nx.path_graph(5)
nx.star_graph(4)               # hub + 4 leaves
nx.wheel_graph(6)
nx.petersen_graph()
nx.balanced_tree(r=3, h=2)    # 3-ary tree, height 2
nx.barbell_grap
Read more
@senolisci
Ships withmykg

myKG automatically generates a confidence-scored knowledge graph from a set of mixed documents — Markdown, plain text, PDF, Word, PowerPoint, Excel, HTML, and images — grounded in an induced RDFS/OWL ontology.

844 skills4 agents1 MCP
Get the whole plugin

Stars on GitHub

Momentum

stars in total
84
Star history of SenolIsci/mykg on GitHub, a chart by star-history.com

Other skills on mykg.

design-architecture

design-architecture

Reviews the current codebase architecture and proposes improvements using four parallel specialist subagents: System Architect,…

@senolisci@senolisciInstall Skill
mykg-github-pages

mykg-github-pages

Set up and maintain the GitHub Pages site for the mykg repo (SenolIsci/mykg) — a purpose-built pages/ folder (landing page…

@senolisci@senolisciInstall Skill
mykg

mykg

Run mykg knowledge-graph commands inside Claude Code from one slash command `/mykg`. The user describes intent in natural…

@senolisci@senolisciInstall Skill

More skills from other plugins.

context-loader
Auto-invoked

context-loader

Search memories from earlier Pi sessions in this repository. Use it when earlier work may already explain the code, error,…

mem05d69k
@mem0ai@mem0aiInstall Skill
analyzing-ai-citations

analyzing-ai-citations

Answer AI citation and answer engine optimization (AEO) questions in PostHog: whether AI answer engines (ChatGPT, Claude, and an…

posthog12d41k
@posthog@posthogInstall Skill

Questions about networkx.

Which coding agents can use networkx?
networkx installs in Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot, OpenCode and Windsurf with the skills CLI, using each agent's flag shown on this page. Flowy's auto-invocation, which fires a skill when your prompt matches, runs in Claude Code only.