| name | apply-graph-theory-analysis |
| description | Use when analyzing networks, relationships, or connectivity problems using graph theory — including graph representation, traversal algorithms, shortest path, minimum spanning tree, centrality measures, and community detection. |
Apply Graph Theory Analysis
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.
Why This Is Best Practice
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.
Steps
1. Choose the correct graph representation
Define the graph type based on the problem:
- Undirected graph G = (V, E): edges have no direction; friendship networks, road networks (bidirectional)
- Directed graph (digraph) G = (V, E): edges have direction; web links, citation networks, supply chains
- Weighted graph: each edge has a weight (distance, capacity, cost); required for shortest path problems
- Bipartite graph: vertices split into two disjoint sets with edges only between sets; user-item interactions, job-worker assignment
- Multigraph: multiple edges between the same pair of vertices; airline routes with multiple flights
import networkx as nx
G = nx.Graph()
G = nx.DiGraph()
G = nx.Graph()
G.add_edge('A', 'B', weight=5.0)
2. Analyze basic graph properties
Compute structural metrics before any algorithm:
n = G.number_of_nodes()
m = G.number_of_edges()
density = nx.density(G)
is_connected = nx.is_connected(G)
is_strongly = nx.is_strongly_connected(G)
degrees = [d for n, d in G.degree()]
avg_degree = sum(degrees) / len(degrees)
Degree distribution shape:
- Normal → random network
- Power law (many low-degree, few hubs) → scale-free network (most real-world networks)
3. Apply traversal and search algorithms
Breadth-First Search (BFS): finds shortest path in unweighted graphs; explores level by level
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')
For negative weights — Bellman-Ford:
path = nx.bellman_ford_path(G, source='A', target='B', weight='weight')
4. Compute centrality measures
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)
pr = nx.pagerank(G, alpha=0.85)
ec = nx.eigenvector_centrality(G)
5. Find minimum spanning tree and connectivity
Minimum spanning tree (MST): connect all nodes with minimum total edge weight
mst = nx.minimum_spanning_tree(G, weight='weight', algorithm='kruskal')
total_weight = mst.size(weight='weight')
Connectivity:
cut_vertices = list(nx.articulation_points(G))
bridges = list(nx.bridges(G))
min_cut = nx.minimum_node_cut(G, s, t)
6. Detect communities
Community detection identifies groups of densely connected nodes:
from networkx.algorithms import community
import community as community_louvain
partition = community_louvain.best_partition(G)
modularity = community_louvain.modularity(partition, G)
communities = community.girvan_newman(G)
Modularity Q: ranges from 0 to 1; Q > 0.3 indicates meaningful community structure.
Common Mistakes
- Choosing undirected when directed matters: Web links, citations, and supply chains are directed — using undirected loses all directional information and produces wrong centrality, reachability, and path results.
- Using Dijkstra on a graph with negative edge weights: Dijkstra's algorithm produces incorrect results with negative weights. Use Bellman-Ford or Johnson's algorithm.
- Computing all-pairs shortest paths on large graphs: Floyd-Warshall is O(V³) — on a 10,000-node network this is 10¹² operations. Use BFS/Dijkstra from a sample of source nodes for large graphs.
When NOT to Use
- Hypergraph problems (edges connect >2 nodes simultaneously, e.g., collaboration groups): standard graph models force artificial pairwise decomposition; use hypergraph libraries (HyperNetX) or simplicial complexes instead.