NetworkX library in Python

In Python, we can create, manipulate and analyze networks or graphs with the help of the NetworkX library. This library provides functions to work with multigraphs and directed, undirected and weighted graphs. NetworkX is widely used in transportation, infrastructure planning, and biological networks.

To work with this library, install it through the following command.

pip3 install networkx
Installation of Networkx

Let us discuss the functionalities provided by the NetworkX library.

Graph creation

We can create an undirected graph, as shown below in the code. nx.Graph() creates an empty undirected graph named G and the node can be added through G.add_node(). Similarly, adding an edge between two nodes is simple with the help of a function G.add_edges_from().

import networkx as nx
# Create an undirected graph
graph = nx.Graph()
# Add nodes
graph.add_node(1)
graph.add_nodes_from([2, 3])
# Add edges
graph.add_edge(1, 2)
graph.add_edges_from([(2, 3), (3, 1)])

Graph information

We can retrieve the information from the graph in NetworkX. Look at the code below, where we are finding the number of nodes, edges, and neighbors of the node in the graph. It is very easy to understand the code.

import networkx as nx
graph = nx.Graph()
graph.add_node(1)
graph.add_nodes_from([2, 3])
graph.add_edge(1, 2)
graph.add_edges_from([(2, 3), (3, 1)])
# Number of nodes and edges
num_nodes = graph.number_of_nodes()
num_edges = graph.number_of_edges()
# List of nodes and edges
nodes_list = list(graph.nodes())
edges_list = list(graph.edges())
# Neighbors of a node
neighbors_of_node = list(graph.neighbors(1))

Graph algorithms

We can perform various algorithms on graphs for better analysis of the information. Most importantly, we have an opportunity to find the shortest path between source and destination in the graph. Moreover, we also find PageRank, betweenness centrality for each node, clustering coefficient, and the graph's minimum spanning tree. All these algorithms are used in real-world applications for extracting insightful information from the graph.

import networkx as nx
graph = nx.Graph()
graph.add_node(1)
graph.add_nodes_from([2, 3])
graph.add_edge(1, 2)
graph.add_edges_from([(2, 3), (3, 1)])
# Shortest path between two nodes
shortest_path = nx.shortest_path(graph, source=1, target=3)
# All shortest paths between two nodes
all_shortest_paths = list(nx.all_shortest_paths(graph, source=1, target=3))
# PageRank centrality
pagerank = nx.pagerank(graph)
# Betweenness centrality
betweenness_centrality = nx.betweenness_centrality(graph)
# Clustering coefficient of nodes
clustering_coefficient = nx.clustering(graph)
# Minimum spanning tree
minimum_spanning_tree = nx.minimum_spanning_tree(graph)

Graph visualization

The graph can be plotted with the help of the Matplotlib library. The nx.draw() function in NetworkX provides the utility to visualize a graph. While calling the function, we pass the graph as a parameter and specify whether to display the labels. Additionally, we can set the node's size and the font size of the graph.

import matplotlib.pyplot as plt
import networkx as nx
graph = nx.Graph()
graph.add_node(1)
graph.add_nodes_from([2, 3])
graph.add_edge(1, 2)
graph.add_edges_from([(2, 3), (3, 1)])
# Draw the graph
nx.draw(graph, with_labels=True, node_color='skyblue', node_size=3500, font_size=12)
plt.savefig("./output/plot.png")
plt.show()

Connected components

Sometimes we need to find out if two nodes are connected or if we want the graph's component in which all the nodes are connected. This feature helps us establish a map to find the connected roads and analyze the destination route. nx.connected_components() returns all the components that are connected. Remember, there can be any number of connected components in the graph.

import networkx as nx
graph = nx.Graph()
graph.add_node(1)
graph.add_nodes_from([2, 3])
graph.add_edge(1, 2)
graph.add_edges_from([(2, 3), (3, 1)])
# Get the connected components of the graph
connected_components = list(nx.connected_components(graph))
# Get the largest connected component
largest_component = max(connected_components, key=len)
# Create a subgraph of the largest component
largest_component_subgraph = graph.subgraph(largest_component)

Degree distribution

The degree distribution determines how connected or centralized the graph is. By examining the degree distribution, we can identify patterns and characteristics of the network, such as nodes with high or low connectivity. This information is valuable while detecting anomalies and identifying the influential nodes of the network.

import networkx as nx
graph = nx.Graph()
graph.add_node(1)
graph.add_nodes_from([2, 3])
graph.add_edge(1, 2)
graph.add_edges_from([(2, 3), (3, 1)])
#===============================
# Degree of a node (number of edges connected to the node)
degree_of_node = graph.degree(1)
# Degree sequence of the graph
degree_sequence = [degree for node, degree in graph.degree()]
# Degree histogram
print(nx.degree_histogram(graph))

Triangles

We can find the number of triangles in the graph with the help of nx.triangles(). Triangles in the graph represent the connections of nodes and dependencies. In real-life applications, it is also used to represent a group or friends. Run the below code.

import networkx as nx
# Create an undirected graph
graph = nx.Graph()
# Add nodes
graph.add_node(1)
graph.add_nodes_from([2, 3])
# Add edges
graph.add_edge(1, 2)
graph.add_edges_from([(2, 3), (3, 1)])
# Number of triangles in the graph
num_triangles = sum(nx.triangles(graph).values()) // 3
print(num_triangles)

Graph generators

We can generate a random graph through this library. To generate a random Erdos-Renyi graph, we use the function nx.erdos_renyi_graph(). In the below code, we plotted the graph as well. Running the below code generates the graph with a different shape every time.

import networkx as nx
import matplotlib.pyplot as plt
# Generate a complete graph
complete_graph = nx.complete_graph(5)
# Generate a cycle graph
cycle_graph = nx.cycle_graph(6)
# Generate a random graph (Erdos-Renyi graph)
random_graph = nx.erdos_renyi_graph(20, 0.3)
nx.draw(random_graph, with_labels=True, node_color='skyblue', node_size=1000, font_size=12)
plt.savefig("./output/plot.png")
plt.show()

Conclusions

NetworkX enables tasks like adding/removing nodes and edges, finding shortest paths, and calculating centrality measures. This is widely used in social sciences, biology, transportation planning, and computer networks. Researchers, data analysts, and developers benefit from NetworkX's efficiency and effectiveness in analyzing complex network data.

Q

How can you visualize a NetworkX graph using Matplotlib?

A)

G.visualize()

B)

nx.draw(G)

C)

G.plot()

D)

nx.show(G)

Copyright ©2024 Educative, Inc. All rights reserved