Networkx

Networkx

Automate complex network analysis and graph theory computations using the NetworkX library for

Category: productivity Source: K-Dense-AI/claude-scientific-skills

NetworkX is a community skill for analyzing and manipulating graph data structures using the NetworkX Python library, covering graph construction, algorithm application, centrality analysis, community detection, and network visualization for computational graph analysis.

What Is This?

Overview

NetworkX provides tools for working with complex network structures in Python. It covers graph construction that builds directed, undirected, and multigraph structures from edge lists, adjacency matrices, and external data sources, algorithm application that runs shortest path, connectivity, and traversal algorithms on graph structures, centrality analysis that computes node importance metrics including degree, betweenness, and PageRank scores, community detection that identifies densely connected groups within networks using partitioning algorithms, and network visualization that renders graph layouts for structural exploration and presentation. The skill enables analysts to study relational data computationally.

Who Should Use This

This skill serves data scientists analyzing social and communication networks, researchers studying biological or infrastructure networks, and engineers building graph-based features for machine learning models.

Why Use It?

Problems It Solves

Relational data stored in flat tables loses structural information that graph representations preserve. Implementing graph algorithms from scratch is error-prone and time-consuming for common analysis tasks. Identifying influential nodes and communities requires specialized algorithms beyond simple aggregation. Visualizing large network structures needs layout algorithms that produce readable displays.

Core Highlights

Graph builder constructs typed graph structures from various data formats. Algorithm engine runs shortest path, connectivity, and flow computations. Centrality analyzer computes node importance using multiple ranking metrics. Community finder detects densely connected subgroups within network structures.

How to Use It?

Basic Usage

import networkx as nx

G = nx.DiGraph()
edges = [
  ('A', 'B', {'w': 4}),
  ('A', 'C', {'w': 2}),
  ('B', 'D', {'w': 5}),
  ('C', 'B', {'w': 1}),
  ('C', 'D', {'w': 8}),
  ('D', 'E', {'w': 6}),
  ('B', 'E', {'w': 3})]
G.add_edges_from(edges)

path = nx.shortest_path(
  G, 'A', 'E',
  weight='w')
cost = nx\
  .shortest_path_length(
    G, 'A', 'E',
    weight='w')
print(f'{path}: {cost}')

degree = nx\
  .degree_centrality(G)
between = nx\
  .betweenness_centrality(
    G)
pagerank = nx.pagerank(G)

for node in G.nodes():
  print(
    f'{node}: '
    f'deg={degree[node]:.2f}'
    f' btw={between[node]:.2f}'
    f' pr={pagerank[node]:.2f}')

Real-World Examples

import networkx as nx
from networkx.algorithms\
  .community import (
    greedy_modularity\
      _communities)

class NetworkAnalyzer:
  def __init__(
    self,
    edges: list[tuple]
  ):
    self.G = nx.Graph()
    self.G.add_edges_from(
      edges)

  def top_nodes(
    self,
    metric: str = 'pr',
    n: int = 10
  ) -> list:
    funcs = {
      'degree': nx
        .degree_centrality,
      'between': nx
        .betweenness\
          _centrality,
      'pr': nx.pagerank}
    scores = funcs[
      metric](self.G)
    return sorted(
      scores.items(),
      key=lambda x: x[1],
      reverse=True)[:n]

  def communities(self):
    comms = (
      greedy_modularity\
        _communities(
          self.G))
    return [
      sorted(c)
      for c in comms]

  def summary(self) -> dict:
    return {
      'nodes': self.G
        .number_of_nodes(),
      'edges': self.G
        .number_of_edges(),
      'density': round(
        nx.density(self.G),
        4),
      'components': nx
        .number_connected\
          _components(
            self.G)}

Advanced Tips

Use generators instead of lists when iterating over neighbors and paths in large graphs to reduce memory consumption. Convert NetworkX graphs to sparse matrices for algorithm steps that benefit from NumPy or SciPy optimizations. Apply graph sampling techniques on networks with millions of nodes to make analysis computationally feasible.

When to Use It?

Use Cases

Analyze a social network to identify influential users based on centrality metrics and community membership. Compute shortest paths in a transportation network for route optimization. Detect communities in a collaboration graph to understand team structures within an organization.

Related Topics

Graph theory, network analysis, centrality metrics, community detection, Python, shortest path algorithms, and social network analysis.

Important Notes

Requirements

NetworkX Python package for graph construction and algorithms. Matplotlib for graph visualization rendering. NumPy and SciPy for matrix-based graph operations and performance.

Usage Recommendations

Do: choose the appropriate graph type between directed, undirected, and multigraph based on your data semantics. Use built-in algorithms rather than reimplementing standard graph operations. Profile memory usage on large graphs before running expensive algorithms.

Don't: use NetworkX for graphs with millions of edges where specialized libraries like graph-tool or NetworKit offer better performance. Store large attribute dictionaries on nodes when external lookup tables would use less memory. Assume algorithm results are deterministic since some algorithms use randomized initialization.

Limitations

NetworkX stores graphs in Python dictionaries which limits performance on graphs with millions of nodes and edges. Visualization becomes unreadable for dense graphs beyond a few hundred nodes without aggregation or sampling. Single-threaded execution means large graph algorithms cannot leverage multi-core hardware for parallel computation.