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
Let us discuss the functionalities provided by the NetworkX library.
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 graphgraph = nx.Graph()# Add nodesgraph.add_node(1)graph.add_nodes_from([2, 3])# Add edgesgraph.add_edge(1, 2)graph.add_edges_from([(2, 3), (3, 1)])
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 nxgraph = 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 edgesnum_nodes = graph.number_of_nodes()num_edges = graph.number_of_edges()# List of nodes and edgesnodes_list = list(graph.nodes())edges_list = list(graph.edges())# Neighbors of a nodeneighbors_of_node = list(graph.neighbors(1))
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 nxgraph = 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 nodesshortest_path = nx.shortest_path(graph, source=1, target=3)# All shortest paths between two nodesall_shortest_paths = list(nx.all_shortest_paths(graph, source=1, target=3))# PageRank centralitypagerank = nx.pagerank(graph)# Betweenness centralitybetweenness_centrality = nx.betweenness_centrality(graph)# Clustering coefficient of nodesclustering_coefficient = nx.clustering(graph)# Minimum spanning treeminimum_spanning_tree = nx.minimum_spanning_tree(graph)
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 pltimport networkx as nxgraph = 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 graphnx.draw(graph, with_labels=True, node_color='skyblue', node_size=3500, font_size=12)plt.savefig("./output/plot.png")plt.show()
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 nxgraph = 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 graphconnected_components = list(nx.connected_components(graph))# Get the largest connected componentlargest_component = max(connected_components, key=len)# Create a subgraph of the largest componentlargest_component_subgraph = graph.subgraph(largest_component)
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 nxgraph = 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 graphdegree_sequence = [degree for node, degree in graph.degree()]# Degree histogramprint(nx.degree_histogram(graph))
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 graphgraph = nx.Graph()# Add nodesgraph.add_node(1)graph.add_nodes_from([2, 3])# Add edgesgraph.add_edge(1, 2)graph.add_edges_from([(2, 3), (3, 1)])# Number of triangles in the graphnum_triangles = sum(nx.triangles(graph).values()) // 3print(num_triangles)
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 nximport matplotlib.pyplot as plt# Generate a complete graphcomplete_graph = nx.complete_graph(5)# Generate a cycle graphcycle_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()
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.
How can you visualize a NetworkX graph using Matplotlib?
G.visualize()
nx.draw(G)
G.plot()
nx.show(G)