Exploring Graph Analytics with NetworkX

Subash Palvel
3 min readSep 13, 2023

--

Graph analytics is a powerful tool for analyzing and understanding complex relationships and structures in data. NetworkX is a Python library that provides a simple yet flexible way to work with graphs and perform various graph analytics tasks. In this post, we will explore the basics of graph analytics using NetworkX and demonstrate its capabilities through practical examples.

What is NetworkX?

NetworkX is a Python package for the creation, manipulation, and study of the structure, dynamics, and functions of complex networks. It provides an easy-to-use interface for constructing, analyzing, and visualizing graphs and networks. NetworkX is widely used in various domains, including social network analysis, biological network analysis, and infrastructure network analysis.

Getting Started

To get started with NetworkX, you need to have Python installed on your system. You can install NetworkX using pip, the Python package installer, by running the following command:

pip install networkx

Once installed, you can import NetworkX in your Python script or Jupyter notebook using the following import statement:

import networkx as nx

Creating a Graph

The first step in working with NetworkX is to create a graph. NetworkX provides various graph classes, such as Graph, DiGraph, MultiGraph, and MultiDiGraph, to represent different types of graphs. Let's create a simple undirected graph using the Graph class:

G = nx.Graph()

Adding Nodes and Edges

Once we have created a graph, we can add nodes and edges to it. Nodes can be any hashable object, such as numbers, strings, or even other Python objects. Edges are represented as tuples of nodes. Let’s add some nodes and edges to our graph:

G.add_node(1)
G.add_nodes_from([2, 3, 4])
G.add_edge(1, 2)
G.add_edges_from([(2, 3), (3, 4), (4, 1)])

Analyzing the Graph

NetworkX provides a wide range of functions and algorithms for analyzing graphs. Let’s explore some of the basic graph analytics tasks using our created graph.

Basic Graph Information

We can obtain basic information about the graph, such as the number of nodes and edges, using the following functions:

print("Number of nodes:", G.number_of_nodes())
print("Number of edges:", G.number_of_edges())

Degree Distribution

The degree of a node in a graph is the number of edges connected to it. We can compute the degree distribution of the graph using the degree function:

degrees = [degree for node, degree in G.degree()]
print("Degree distribution:", degrees)

Shortest Path

We can find the shortest path between two nodes in the graph using the shortest_path function:

shortest_path = nx.shortest_path(G, source=1, target=4)
print("Shortest path:", shortest_path)

Centrality Measures

Centrality measures help us identify the most important nodes in a graph. NetworkX provides various centrality measures, such as degree centrality, closeness centrality, and betweenness centrality. Let’s compute the degree centrality of our graph:

degree_centrality = nx.degree_centrality(G)
print("Degree centrality:", degree_centrality)

Visualizing the Graph

NetworkX provides a simple way to visualize graphs using Matplotlib. Let’s visualize our graph:

import matplotlib.pyplot as plt
nx.draw(G, with_labels=True)
plt.show()

Conclusion

In this post, we explored the basics of graph analytics using NetworkX. We learned how to create a graph, add nodes and edges, and perform various graph analytics tasks, such as computing degree distribution, finding shortest paths, and computing centrality measures. We also visualized our graph using Matplotlib. NetworkX provides a powerful and flexible framework for working with graphs and performing graph analytics tasks. It is a valuable tool for anyone working with complex networks and relationships in data.

Happy graph analytics with NetworkX!

Follow me at LinkedIn:

https://www.linkedin.com/in/subashpalvel/

Follow me at Medium:

https://subashpalvel.medium.com/

--

--