leonardodalinky/scider/network-graph-analysis

Claude Code and agent skill by leonardodalinky/scider

Trust tier 2
  • community
LLM review pending
Source check pending

Checking that the SKILL.md file is still in the repository.

Graph and network analysis — graph construction, centrality measures, community detection, graph ML basics, and visualization. Use when data has relational/network structure.

Install

npx vskill@latest install leonardodalinky/scider/network-graph-analysis

SKILL.md

Network Graph Analysis

Overview

Graph and network analysis extracts structural information from relational data. Use this skill when your data describes connections between entities — citations, protein interactions, social ties, trade flows, co-occurrence matrices, or any adjacency structure. The key insight: if relationships carry information that tabular rows cannot, model the structure explicitly.

When to Use This Skill

Use this skill when:

  • Your data is fundamentally relational (entities linked by edges)
  • You need to identify influential nodes, bridging nodes, or tightly-knit communities
  • You are building or evaluating a graph neural network (GNN)
  • You need to visualize network structure for publication or exploration
  • Your dataset is a co-occurrence matrix, adjacency matrix, or edge list

Do not reach for graph methods if rows in your tabular dataset are independent (no meaningful pairwise relationships). First run EDA (EDA skill) to understand data shape, then apply this skill.


Graph Construction from Data

From a NumPy/SciPy Adjacency Matrix
python
import numpy as np
import networkx as nx
import scipy.sparse as sp
# Dense adjacency matrix
A = np.array([[0, 1, 0],
[1, 0, 1],
[0, 1, 0]])
G = nx.from_numpy_array(A) # undirected
G_directed = nx.from_numpy_array(A, create_using=nx.DiGraph()) # directed
# SciPy sparse matrix (memory-efficient for large graphs)
A_sparse = sp.csr_matrix(A)
G_sparse = nx.from_scipy_sparse_array(A_sparse)
From an Edge List CSV
python
import pandas as pd
# CSV with columns: source, target (and optionally weight)
edges_df = pd.read_csv('edges.csv')
# Unweighted, undirected
G = nx.from_pandas_edgelist(edges_df, source='source', target='target')
# Weighted, directed
G_weighted = nx.from_pandas_edgelist(
edges_df,
source='source',
target='target',
edge_attr='weight',
create_using=nx.DiGraph()
)
From Bipartite Data (e.g., users x items)
python
from networkx.algorithms import bipartite
# Build bipartite graph
B = nx.Graph()
B.add_nodes_from(['u1', 'u2', 'u3'], bipartite=0) # user nodes
B.add_nodes_from(['i1', 'i2'], bipartite=1) # item nodes
B.add_edges_from([('u1','i1'), ('u2','i1'), ('u2','i2'), ('u3','i2')])
# Project onto one set (user-user co-occurrence)
user_nodes = {n for n, d in B.nodes(data=True) if d['bipartite'] == 0}
G_projected = bipartite.weighted_projected_graph(B, user_nodes)
Weighted vs Unweighted / Directed vs Undirected
ChoiceWhen to use
UndirectedSymmetric relationships (co-authorship, friendship)
DirectedAsymmetric relationships (citations, follows, flows)
UnweightedOnly presence/absence of edge matters
WeightedEdge strength matters (call frequency, correlation strength)

Use nx.DiGraph() for directed, nx.Graph() for undirected. Add weight edge attributes for weighted graphs — most centrality functions respect the weight keyword.


Basic Graph Statistics

python
G = nx.karate_club_graph() # example
print(f"Nodes: {G.number_of_nodes()}")
print(f"Edges: {G.number_of_edges()}")
print(f"Density: {nx.density(G):.4f}") # edges / possible edges
degrees = [d for _, d in G.degree()]
print(f"Average degree: {sum(degrees)/len(degrees):.2f}")
print(f"Max degree: {max(degrees)}")
# Connected components
components = list(nx.connected_components(G))
print(f"Connected components: {len(components)}")
print(f"Largest component: {max(len(c) for c in components)} nodes")
# Diameter (only valid for connected graphs)
if nx.is_connected(G):
print(f"Diameter: {nx.diameter(G)}")
else:
# Diameter of largest component
largest = G.subgraph(max(components, key=len))
print(f"Diameter (LCC): {nx.diameter(largest)}")
# Clustering coefficient
print(f"Global clustering: {nx.transitivity(G):.4f}")
print(f"Avg local clustering:{nx.average_clustering(G):.4f}")

Centrality Measures

Degree Centrality — Who Has the Most Connections?

The simplest measure. Node importance = fraction of other nodes it connects to. Use as a baseline; fast to compute.

python
degree_cent = nx.degree_centrality(G)
# For weighted graphs, use strength (sum of weights)
strength = dict(G.degree(weight='weight'))
# Top 5 nodes by degree centrality
top_degree = sorted(degree_cent.items(), key=lambda x: x[1], reverse=True)[:5]
print("Top nodes by degree centrality:", top_degree)
Betweenness Centrality — Bridges Between Communities

Fraction of shortest paths passing through a node. High betweenness = bottleneck / broker. Expensive: O(VE) for unweighted, slower for weighted. Sample for large graphs.

python
# Exact
between_cent = nx.betweenness_centrality(G, normalized=True, weight='weight')
# Approximate for large graphs (sample k sources)
between_approx = nx.betweenness_centrality(G, k=200, normalized=True)
top_between = sorted(between_cent.items(), key=lambda x: x[1], reverse=True)[:5]
print("Top nodes by betweenness:", top_between)
Closeness Centrality — Reaches Others Quickly

Average shortest path distance to all other nodes (inverted). High closeness = spreads information fast. Meaningful only in connected graphs (or apply per component).

python
closeness_cent = nx.closeness_centrality(G)
top_close = sorted(closeness_cent.items(), key=lambda x: x[1], reverse=True)[:5]
print("Top nodes by closeness:", top_close)
Eigenvector Centrality — Connected to Important Nodes

Score proportional to scores of neighbors. Being connected to high-scoring nodes raises your own score. PageRank is a damped variant. Use for undirected or directed networks where prestige propagates.

python
eigen_cent = nx.eigenvector_centrality(G, max_iter=1000, weight='weight')
# PageRank for directed graphs
pagerank = nx.pagerank(G, alpha=0.85, weight='weight')
top_eigen = sorted(eigen_cent.items(), key=lambda x: x[1], reverse=True)[:5]
print("Top nodes by eigenvector centrality:", top_eigen)
When to Use Each
MeasureBest for
DegreeQuick scan, finding hubs, baseline
BetweennessFinding brokers, bridges, critical infrastructure nodes
ClosenessInformation diffusion, finding broadcast centers
Eigenvector/PageRankInfluence, authority, prestige propagation

Compute all four and compare — they often disagree and the disagreement is informative.


Community Detection

Louvain — Fast, Large Graphs (Recommended Default)

Maximizes modularity greedily. Non-deterministic; run multiple times and pick highest modularity. Available via python-louvain (community package) or igraph.

python
# Using python-louvain
import community as community_louvain
partition = community_louvain.best_partition(G, weight='weight', random_state=42)
modularity = community_louvain.modularity(partition, G)
num_communities = len(set(partition.values()))
print(f"Communities: {num_communities}, Modularity: {modularity:.4f}")
# Assign as node attribute
nx.set_node_attributes(G, partition, 'community')
# Using igraph (faster on very large graphs)
import igraph as ig
G_ig = ig.Graph.from_networkx(G)
louvain_result = G_ig.community_multilevel(weights='weight')
print(f"Modularity (igraph): {louvain_result.modularity:.4f}")
Girvan-Newman — Hierarchical, Small Graphs

Removes edges with highest betweenness iteratively, revealing community hierarchy. Slow (O(E²V)); use only when n < 1000 and you need a dendrogram.

python
from networkx.algorithms.community import girvan_newman
comp = girvan_newman(G)
# Get partitions at different resolution levels
for communities in itertools.islice(comp, 3):
print(f"k={len(communities)}: {[len(c) for c in communities]}")
Spectral Clustering — Exactly k Communities

Use when you know k in advance. Builds graph Laplacian, finds k eigenvectors, runs k-means in spectral space.

python
from sklearn.cluster import SpectralClustering
import numpy as np
A = nx.to_numpy_array(G)
sc = SpectralClustering(
n_clusters=5,
affinity='precomputed',
assign_labels='kmeans',
random_state=42
)
labels = sc.fit_predict(A)
Label Propagation — Very Fast, Non-Deterministic

Each node inherits the most common label of its neighbors. Converges in O(E). Results vary between runs; use when speed matters more than stability.

python
from networkx.algorithms.community import label_propagation_communities
communities = list(label_propagation_communities(G))
print(f"Found {len(communities)} communities")

Graph ML Basics

When Does Graph Structure Help?

Graph structure adds value beyond tabular features when:

  • Nodes are connected in ways that predict the target (social influence, protein proximity)
  • Labels propagate through edges (homophily: connected nodes share labels)
  • Global topology matters (scale-free vs random network properties)

If nodes have no meaningful connections or edges are arbitrary, stick with tabular models.

GNN Overview: Message Passing

At each layer, each node aggregates feature vectors from its neighbors, then updates its own representation. After k layers, a node sees its k-hop neighborhood.

Key architectures:

  • GCN (Graph Convolutional Network): normalized sum aggregation — simple, effective
  • GraphSAGE: sample-and-aggregate — scales to large graphs via mini-batching
  • GAT (Graph Attention Network): learns attention weights over neighbors — best when neighbor importance varies
Task Types
TaskInputOutputExample
Node classificationNode features + graphLabel per nodeClassify paper topics in citation network
Link predictionNode features + partial graphEdge probabilityRecommend friends, predict protein interactions
Graph classificationFull graphLabel per graphClassify molecules, detect fraud subgraphs
Libraries
python
# PyTorch Geometric (PyG) — most popular, extensive model zoo
from torch_geometric.data import Data
from torch_geometric.nn import GCNConv, GATConv, SAGEConv
import torch
data = Data(
x=node_features, # [num_nodes, num_features]
edge_index=edge_index, # [2, num_edges] — COO format
y=labels # [num_nodes] for node classification
)
class GCN(torch.nn.Module):
def __init__(self, in_channels, hidden, out_channels):
super().__init__()
self.conv1 = GCNConv(in_channels, hidden)
self.conv2 = GCNConv(hidden, out_channels)
def forward(self, data):
x, edge_index = data.x, data.edge_index
x = self.conv1(x, edge_index).relu()
x = torch.nn.functional.dropout(x, p=0.5, training=self.training)
x = self.conv2(x, edge_index)
return x
# DGL — alternative with strong support for heterogeneous graphs
import dgl
g = dgl.from_networkx(G)

Visualization

NetworkX + Matplotlib — Small Graphs (<500 nodes)
python
import matplotlib.pyplot as plt
import networkx as nx
fig, ax = plt.subplots(figsize=(12, 8))
# Layout algorithms
pos = nx.spring_layout(G, seed=42) # force-directed — general purpose
# pos = nx.kamada_kawai_layout(G) # minimizes edge crossings — slower but cleaner
# pos = nx.circular_layout(G) # nodes on a circle — good for small n
# pos = nx.spectral_layout(G) # based on graph Laplacian eigenvectors
# Color by community
communities = community_louvain.best_partition(G)
colors = [communities[n] for n in G.nodes()]
# Size by degree
sizes = [300 * nx.degree_centrality(G)[n] + 50 for n in G.nodes()]

This is the start of the file. Read the full SKILL.md on GitHub.

Security

  • Security scanPassed, 100 of 100Pattern checks on the skill's files.
  • Model reviewNot runA model review is scheduled for the next rescan.

Quality evaluation

  • Task evaluationNot runNo evaluation yet. Run one in Skill Studio.

Source

Popularity

Repository stars
88
Repository forks
7
7-day trend score
22

Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.

Works with

Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.

  • Claude Code
  • Cursor
  • GitHub Copilot
  • Windsurf
  • Codex
  • Gemini CLI
  • Cline
  • Amp
  • Roo Code
  • Goose
  • Continue
  • Kilo Code
  • Junie
  • Trae
  • OpenHands
  • Qwen Code
  • AI Agent
  • Tabnine Chat
  • Sourcegraph Cody
  • Amazon Q
  • Firebase Genkit
  • Sweep
  • Devin
  • Phind
  • Mentat
  • Claude Desktop
  • Aider
  • GPT Pilot
  • Plandex
  • bolt.new
  • v0
  • Replit Agent
  • Anse
  • TypingMind
  • BetterChatGPT
  • LibreChat
  • Warp AI
  • Pieces
  • Fig AI
Embed badge
vskill VERIFIED badge
[![vskill VERIFIED](https://verified-skill.com/api/v1/skills/leonardodalinky/scider/network-graph-analysis/badge)](https://verified-skill.com/skills/leonardodalinky/scider/network-graph-analysis)

Something wrong with this skill or its page? Tell us.