From grimoire
Analyzes networks and connectivity using graph theory: representation, traversal, shortest paths, centrality, and community detection. Applies algorithms from NetworkX/Python.
How this skill is triggered — by the user, by Claude, or both
Slash command
/grimoire:apply-graph-theory-analysisThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Represent and analyze networks using graph theory — selecting appropriate graph models, applying correct traversal and search algorithms, computing centrality and connectivity metrics, and detecting community structure to answer structural questions about the network.
Represent and analyze networks using graph theory — selecting appropriate graph models, applying correct traversal and search algorithms, computing centrality and connectivity metrics, and detecting community structure to answer structural questions about the network.
Adopted by: Google (PageRank = eigenvector centrality of the web graph), social network analysis (Facebook, LinkedIn graph algorithms), logistics (UPS/FedEx routing = shortest path on road networks), computational biology (protein interaction networks, metabolic pathways), and electrical engineering (circuit analysis = Kirchhoff's laws on a graph). NetworkX (Python) and igraph (R/Python/C) are the dominant open-source graph analysis libraries. Impact: Newman (2010) established the theoretical foundations of network science — demonstrating that most real networks have scale-free degree distributions (hubs), small-world properties (short paths despite large size), and community structure. These properties determine resilience, information spread, and vulnerability in ways that aggregate statistics miss entirely. The PageRank algorithm (Brin & Page, 1998) — a specialization of eigenvector centrality — generates ~$140B+ annual revenue for Google by identifying authoritative web pages.
Define the graph type based on the problem:
import networkx as nx
G = nx.Graph() # undirected, unweighted
G = nx.DiGraph() # directed
G = nx.Graph()
G.add_edge('A', 'B', weight=5.0)
Compute structural metrics before any algorithm:
n = G.number_of_nodes() # |V|
m = G.number_of_edges() # |E|
density = nx.density(G) # 2m / n(n-1) for undirected
is_connected = nx.is_connected(G) # for undirected
is_strongly = nx.is_strongly_connected(G) # for directed (all pairs reachable)
# Degree distribution
degrees = [d for n, d in G.degree()]
avg_degree = sum(degrees) / len(degrees)
Degree distribution shape:
Breadth-First Search (BFS): finds shortest path in unweighted graphs; explores level by level
# Shortest path in unweighted graph (BFS-based)
path = nx.shortest_path(G, source='A', target='B')
length = nx.shortest_path_length(G, source='A', target='B')
Depth-First Search (DFS): explores as deep as possible; used for cycle detection, topological sort, connected components
For weighted graphs — Dijkstra's algorithm:
path = nx.shortest_path(G, source='A', target='B', weight='weight')
length = nx.shortest_path_length(G, source='A', target='B', weight='weight')
# O(E log V); requires non-negative weights
For negative weights — Bellman-Ford:
path = nx.bellman_ford_path(G, source='A', target='B', weight='weight')
# O(VE); handles negative edges; detects negative cycles
Select centrality metric based on what "importance" means in context:
| Centrality | Measures | Best for |
|---|---|---|
| Degree centrality | Number of connections | Local popularity |
| Betweenness centrality | Fraction of shortest paths through v | Bridge/bottleneck nodes |
| Closeness centrality | Average distance to all other nodes | Broadcast efficiency |
| Eigenvector centrality | Influence accounting for neighbor influence | Authority (PageRank variant) |
| PageRank | Directed version of eigenvector | Web ranking, citation impact |
bc = nx.betweenness_centrality(G) # normalized by default
pr = nx.pagerank(G, alpha=0.85) # damping factor 0.85 = Google's original
ec = nx.eigenvector_centrality(G)
Minimum spanning tree (MST): connect all nodes with minimum total edge weight
# Kruskal's algorithm (good for sparse graphs)
mst = nx.minimum_spanning_tree(G, weight='weight', algorithm='kruskal')
# Prim's algorithm (good for dense graphs): algorithm='prim'
total_weight = mst.size(weight='weight')
Connectivity:
# Cut vertices (removing disconnects the graph)
cut_vertices = list(nx.articulation_points(G))
# Bridges (removing disconnects an edge)
bridges = list(nx.bridges(G))
# Minimum vertex cut (fewest vertices to remove to disconnect)
min_cut = nx.minimum_node_cut(G, s, t)
Community detection identifies groups of densely connected nodes:
from networkx.algorithms import community
# Louvain algorithm (fast, widely used)
# install: pip install python-louvain
import community as community_louvain
partition = community_louvain.best_partition(G)
modularity = community_louvain.modularity(partition, G)
# Girvan-Newman (hierarchical, slower)
communities = community.girvan_newman(G)
Modularity Q: ranges from 0 to 1; Q > 0.3 indicates meaningful community structure.
npx claudepluginhub jeffreytse/grimoire --plugin grimoireNetworkX toolkit for graph creation, centrality, shortest paths, community detection, generators, and I/O. Use for 100K+ node graphs with igraph/graph-tool; for GNNs use PyG.
Creates, manipulates, and analyzes complex networks and graphs using Python. Covers shortest paths, centrality, community detection, PageRank, graph I/O, and visualization.
Creates, analyzes, and visualizes complex networks and graphs in Python. Use for graph algorithms, centrality, community detection, and network generation.