Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Introduction to Network Analysis with NetworkX

Graph Data Structures and Operations

In this Jupyter notebook, we will explore the basics of graph data structures and operations using the NetworkX library in Python. NetworkX is a powerful library for creating, manipulating, and studying the structure and dynamics of complex networks.

We will start by creating simple directed and undirected graphs, and then explore some basic graph operations, such as breadth-first search (BFS).

Importing Necessary Libraries

Creating an un-directed Graph

First, let’s create a simple un-directed graph and visualize it:

f:\conda\envs\pth-gpu\lib\site-packages\networkx\drawing\nx_pylab.py:433: UserWarning: No data for colormapping provided via 'c'. Parameters 'cmap' will be ignored
  node_collection = ax.scatter(
<Figure size 640x480 with 1 Axes>

Creating a Directed Graph

Now, let’s create a simple directed graph (DiGraph) and visualize it:

<Figure size 640x480 with 1 Axes>

Create a weighted graph WG and visualize it with edge weights

{('A', 'B'): Text(-0.17063150397490368, -0.25785521234238246, '10'), ('A', 'C'): Text(0.16908485308194388, 0.2584267536065791, '20'), ('B', 'D'): Text(-0.5751319512575159, -0.5819114840781281, '30'), ('B', 'E'): Text(-0.3070875406419613, -0.7577069745265291, '40'), ('C', 'F'): Text(0.5743060275721562, 0.5815239318372771, '50'), ('C', 'G'): Text(0.3081446719975651, 0.7583070963940803, '60')}
<Figure size 640x480 with 1 Axes>

Check the connectivity of two different graphs

Is graph 1 connected? False
Is graph 2 connected? True

Visualize the two graphs side-by-side

<Figure size 800x800 with 2 Axes>

Calculate degree of node A for an undirected graph G and a directed graph DG

deg(A) = 2
deg^-(A) = 0
deg^+(A) = 2
<Figure size 640x480 with 1 Axes>

Calculate degree centrality measures for an undirected graph G

Degree centrality      = {'A': 0.3333333333333333, 'B': 0.5, 'C': 0.5, 'D': 0.16666666666666666, 'E': 0.16666666666666666, 'F': 0.16666666666666666, 'G': 0.16666666666666666}
Closeness centrality   = {'A': 0.6, 'B': 0.5454545454545454, 'C': 0.5454545454545454, 'D': 0.375, 'E': 0.375, 'F': 0.375, 'G': 0.375}
Betweenness centrality = {'A': 0.6, 'B': 0.6, 'C': 0.6, 'D': 0.0, 'E': 0.0, 'F': 0.0, 'G': 0.0}

Represent graphs using different data structures (adjacency matrix, edge list, adjacency list)

Creating an Undirected Graph and Performing Breadth-First Search (BFS)

Next, let’s create an undirected graph G and perform a BFS traversal starting from node ‘A’:

['A', 'B', 'C', 'D', 'E', 'F', 'G']

Perform Depth-First Search (DFS) on an undirected graph G

['A', 'B', 'D', 'E', 'C', 'F', 'G']

Erdős-Rényi Model

nx.erdos_renyi_graph is a function from the NetworkX library in Python that generates a random graph based on the Erdős-Rényi model. This model is one of the simplest and most widely studied random graph models.

Overview of the Erdős-Rényi Model

There are two primary formulations of the Erdős-Rényi model:

  1. G(n, p): In this model, a graph is constructed by adding nodes one at a time (where ( n ) is the total number of nodes). Each possible edge between any pair of nodes is included independently with a probability ( p ).

  2. G(n, m): In this model, ( n ) nodes are connected by creating exactly ( m ) edges chosen uniformly at random from all possible edges that could exist between the nodes.

The Erdős-Rényi random graphs are often used in various fields, including:

  • Network theory: To study the properties of random graphs.

  • Computer science: In algorithms related to networks and to simulate networks with random connections.

Random graphs can help researchers understand the behavior of more complex structures and networks in real-world applications.

<Figure size 640x480 with 1 Axes>