Introduction to Social Networks using NetworkX in Python
Last Updated :
30 Jan, 2023
Prerequisite – Python Basics
Ever wondered how the most popular social networking site Facebook works? How we are connected with friends using just Facebook? So, Facebook and other social networking sites work on a methodology called social networks. Social networking is used in mostly all social media sites such as Facebook, Instagram, and LinkedIn, etc. It has a significant effect on marketers to engage customers. Social networks use graphs for creating a network. Their nodes are people and edges are their connection between each other. Two nodes with edges connected are friends. Now let’s see an example for understanding what is social networks.
A social network is a collection of individuals (or “nodes”) and the relationships (or “edges”) between them. Social network analysis is the process of analyzing these networks to understand patterns of interaction and communication among individuals.
NetworkX is a popular Python library for working with graphs and networks. It provides a wide range of graph algorithms and functions for creating, manipulating, and analyzing networks.
The network of 50 students in a class

The network of 50 people
The most important python library used in social networking is Networkx.
NetworkX
NetworkX is a graph package that is used to create and modify different types of graphs. It provides a rapid development environment for collaborative, multidisciplinary projects.
Installation:
pip install networkx
After starting python, we have to import networkx module:
import networkx as nx
Basic inbuilt graph types are:
- Graph: This type of graph stores nodes and edges and edges are un-directed. It can have self-loops but cannot have parallel edges.
- Di-Graph: This type of graph is the base class for directed graphs. It can have nodes and edges and edges are directed in nature. It can have self-loops but parallel edges are not allowed in Di-Graph.
- Multi-Graph: This type of graph is an undirected graph class that can store multi or parallel edges. It can have self-loops as well. Multi-edges are multiple edges between 2 nodes.
- Multi-DiGraph: This type of graph is a directed graph class that can store multi edges. It can have self-loops as well. Multi-edges are multiple edges between 2 nodes.
Example of Graph creation :
Python3
import networkx as nx
G = nx.Graph()
G.add_edge( 1 , 2 )
G.add_edge( 2 , 3 , weight = 0.9 )
|
Drawing of graph:
Drawing can be done using Matplotlib.pyplot library.
Python3
import matplotlib.pyplot as plt
import networkx as nx
G = nx.cubical_graph()
plt.subplot( 122 )
nx.draw(G, pos = nx.circular_layout(G),
node_color = 'r' ,
edge_color = 'b' )
|
Output:

Circular Graph
Graph Edge Removal:
To remove an edge from the graph, use the remove_edge() method of graph object.
Syntax: G.remove_edge(u, v)
Parameters:
- u: first node
- v: second node
Return: None
Graph Node Removal:
To remove a node from the graph, use the remove_node() method of graph object.
Syntax: G.remove_node(u)
Parameter: Node to remove
Return: None
Show the Adjacent vertices:
Python3
import netwokx as nx
G = nx.Graph()
G.add_edge( '1' , '2' )
G.add_edge( '2' , '3' )
print (G.adj)
|
Output:
{'1': {'2': {}}, '2': {'1': {}, '3': {}}, '3': {'2': {}}}
Advantages and Disadvantages :
Advantages of using NetworkX for social network analysis include:
- A wide range of built-in algorithms: NetworkX provides a wide range of algorithms for analyzing and manipulating graphs, which makes it a powerful tool for social network analysis.
- Easy to use: NetworkX has a simple and intuitive API, which makes it easy to use even for beginners.
- Flexibility: NetworkX is built on top of the popular Python programming language, which means that it can be easily integrated with other Python libraries, such as NumPy and Matplotlib, making it a flexible tool for data analysis.
- Support for large graphs: NetworkX is built for handling large scale network analysis and visualization, it can handle large graphs with millions of nodes and edges.
Disadvantages of using NetworkX for social network analysis include:
- Limited visualization capabilities: While NetworkX provides basic visualization capabilities, it is not as advanced as other libraries such as Gephi, which is more specialized in graph visualization.
- Limited scalability: NetworkX is not designed to handle extremely large scale graph analysis, for that other libraries such as igraph, boost.python and SNAP can be used.
- Limited support for directed and weighted edges: NetworkX is primarily designed for undirected, unweighted graphs, although it does support directed and weighted edges.
Reference :
“Python NetworkX: A Practical Overview” by Shai Vaingast is a good reference book for learning NetworkX and its application in social network analysis. The book covers the basics of NetworkX and its use in solving real-world problems such as community detection, centrality measures, and graph visualization.
Similar Reads
NetworkX : Python software package for study of complex networks
NetworkX is a Python language software package for the creation, manipulation, and study of the structure, dynamics, and function of complex networks. It is used to study large complex networks represented in form of graphs with nodes and edges. Using networkx we can load and store complex networks.
3 min read
Python | Visualize graphs generated in NetworkX using Matplotlib
Prerequisites: Generating Graph using Network X, Matplotlib IntroIn this article, we will be discussing how to plot a graph generated by NetworkX in Python using Matplotlib. NetworkX is not a graph visualizing package but basic drawing with Matplotlib is included in the software package. Step 1 : Im
3 min read
Directed Graphs, Multigraphs and Visualization in Networkx
Prerequisite: Basic visualization technique for a Graph In the previous article, we have learned about the basics of Networkx module and how to create an undirected graph. Note that Networkx module easily outputs the various Graph parameters easily, as shown below with an example. import networkx as
9 min read
Complete Graph using Networkx in Python
A complete graph also called a Full Graph it is a graph that has n vertices where the degree of each vertex is n-1. In other words, each vertex is connected with every other vertex. Example: Complete Graph with 6 edges: Properties of Complete Graph: The degree of each vertex is n-1.The total number
3 min read
Introduction to Social Networks using NetworkX in Python
Prerequisite - Python Basics Ever wondered how the most popular social networking site Facebook works? How we are connected with friends using just Facebook? So, Facebook and other social networking sites work on a methodology called social networks. Social networking is used in mostly all social me
4 min read
Wheel Graph using Networkx Python
A wheel graph is a type of graph in which if we connect each node in an n-1 node cycle graph to nth node kept at the centre we get a wheel graph. The definition would be more clear after seeing the example below. Wheel Graph with n nodes is represented by Wn . Example: W5: W6: Properties of Wheel Gr
2 min read
Small World Model - Using Python Networkx
In this article, we will learn how to create a Small World Network using Networx module in Python. Before proceeding that let's first understand some basics about Small World Phenomenon. What is Small World Phenomenon ? Small World Phenomenon is the study and notion that we are all connected via a s
4 min read
Ego graph Using Networkx in Python
Prerequisite - Graphs, Networkx Basics Ego network is a special type of network consisting of one central node and all other nodes directly connected to it. The central node is known as ego, while the other surrounding nodes directly connected to it are known as alters. Ego networks are mostly used
4 min read
Star Graph using Networkx Python
In this article, we are going to see Star Graph using Networkx Python. A Star graph is a special type of graph in which n-1 vertices have degree 1 and a single vertex have degree n â 1. This looks like that n â 1 vertex is connected to a single central vertex. A star graph with total n â vertex is t
2 min read
Network Centrality Measures in a Graph using Networkx | Python
Centrality Measures allows us to pinpoint the most important nodes of a Graph. This essentially helps us to identify : Influential nodes in a Social Network. Nodes that disseminate information to many nodes Hubs in a transportation network Important pages in the Web Nodes that prevent the Network fr
6 min read