Clustering, Connectivity and other Graph properties using Python Networkx

Python NetworkX is a popular open-source Python library used for the creation, manipulation, and analysis of complex networks or graphs. It provides a wide range of tools, algorithms, and functions to work with graphs, making it a valuable resource for network analysis and research.

Python NetworkX allows us to represent and work with different types of graphs, such as directed graphs, undirected graphs, multigraphs (graphs with multiple edges between nodes), and weighted graphs (graphs with edge weights). It also provides a simple and intuitive interface for creating, adding nodes and edges, and performing various operations on graphs.

Installation and Importing NetworkX

To use NetworkX, we have to install it using the Python package manager pip by running the following command in the command prompt.

pip install networkx

Once NetworkX is installed in our Python environment, we can import the library in our Python script and start using its functionalities ?

import networkx as nx
import matplotlib.pyplot as plt

NetworkX library offers a wide range of properties such as clustering, connectivity, and other graph properties. Let's explore these key concepts in detail.

Clustering Coefficient

The clustering coefficient is a measure of the degree to which nodes in a graph tend to cluster together. It quantifies the presence of triangles in the graph, where a triangle consists of three nodes that are all connected to each other. A high clustering coefficient indicates that nodes in the graph are more likely to form tightly connected clusters.

In NetworkX, we can calculate the clustering coefficient using the average_clustering() function to obtain the average clustering coefficient of a graph, or the clustering() function to compute the clustering coefficient of individual nodes.

Average Clustering Coefficient

The average_clustering() function calculates the average clustering coefficient of a graph. It returns a single value representing the average clustering coefficient across all nodes ?

import networkx as nx
import matplotlib.pyplot as plt

# Create a simple undirected graph
G = nx.Graph()
G.add_edges_from([(1, 2), (1, 3), (2, 3), (2, 4), (3, 4), (4, 5), (4, 6)])

# Calculate average clustering coefficient
avg_clustering = nx.average_clustering(G)
print("Average Clustering Coefficient:", avg_clustering)

# Visualize the graph
nx.draw_networkx(G, with_labels=True, node_color='lightblue', node_size=500)
plt.title("Graph with Clustering")
plt.axis('off')
plt.show()
Average Clustering Coefficient: 0.4166666666666666

Node Clustering Coefficient

The clustering() function calculates the clustering coefficient of individual nodes. It returns a dictionary where keys are nodes and values are their clustering coefficients ?

import networkx as nx

# Create a graph
G = nx.Graph()
G.add_edges_from([(1, 2), (1, 3), (2, 3), (2, 4), (3, 4), (4, 5), (4, 6)])

# Calculate clustering coefficient for each node
node_clustering = nx.clustering(G)
print("Node Clustering Coefficients:")
for node, clustering_coefficient in node_clustering.items():
    print(f"Node {node}: {clustering_coefficient:.3f}")
Node Clustering Coefficients:
Node 1: 1.000
Node 2: 0.667
Node 3: 0.667
Node 4: 0.167
Node 5: 0.000
Node 6: 0.000

Connectivity

Connectivity refers to the property of a graph that determines whether all nodes are reachable from any other node. NetworkX offers functions like is_connected() and is_strongly_connected() to analyze graph connectivity.

Example

Let's check if our graph is connected using is_connected() ?

import networkx as nx

# Create a connected graph
G1 = nx.Graph()
G1.add_edges_from([(1, 2), (2, 3), (3, 4)])

# Create a disconnected graph
G2 = nx.Graph()
G2.add_edges_from([(1, 2), (3, 4)])  # Two separate components

print("Graph 1 connected:", nx.is_connected(G1))
print("Graph 2 connected:", nx.is_connected(G2))

# Check number of connected components
print("Connected components in G2:", nx.number_connected_components(G2))
Graph 1 connected: True
Graph 2 connected: False
Connected components in G2: 2

Degree Centrality

Degree centrality measures the importance of a node based on its degree (number of connections). The degree_centrality() function calculates the normalized degree centrality for each node ?

import networkx as nx

# Create a graph
G = nx.Graph()
G.add_edges_from([(1, 2), (2, 3), (2, 4), (3, 4), (4, 5), (4, 6)])

# Calculate degree centrality
centrality = nx.degree_centrality(G)
print("Degree Centrality:")
for node, cent in centrality.items():
    print(f"Node {node}: {cent:.3f}")

# Find the most central node
most_central = max(centrality, key=centrality.get)
print(f"\nMost central node: {most_central}")
Degree Centrality:
Node 1: 0.200
Node 2: 0.600
Node 3: 0.400
Node 4: 0.800
Node 5: 0.200
Node 6: 0.200

Most central node: 4

Other Graph Properties

NetworkX provides many additional graph properties for comprehensive analysis ?

import networkx as nx

# Create a sample graph
G = nx.Graph()
G.add_edges_from([(1, 2), (2, 3), (3, 4), (4, 1), (2, 4)])

# Calculate various graph properties
density = nx.density(G)
diameter = nx.diameter(G) if nx.is_connected(G) else "Graph not connected"
avg_path_length = nx.average_shortest_path_length(G) if nx.is_connected(G) else "N/A"

print(f"Number of nodes: {G.number_of_nodes()}")
print(f"Number of edges: {G.number_of_edges()}")
print(f"Graph density: {density:.3f}")
print(f"Diameter: {diameter}")
print(f"Average shortest path length: {avg_path_length:.3f}")
Number of nodes: 4
Number of edges: 5
Graph density: 0.833
Diameter: 2
Average shortest path length: 1.167

Conclusion

NetworkX provides powerful tools for analyzing graph properties including clustering coefficients, connectivity measures, and centrality metrics. These properties help understand network structure and identify important nodes, making NetworkX essential for network analysis and research.

Updated on: 2026-03-27T11:26:19+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements