Open In App

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 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:

Example of Graph creation :




# import networkx library
import networkx as nx
 
# create an empty undirected graph
G = nx.Graph()
 
# adding edge in graph G
G.add_edge(1, 2)
G.add_edge(2, 3, weight=0.9)

Drawing of graph:

Drawing can be done using  Matplotlib.pyplot library. 




# import matplotlib.pyplot library
import matplotlib.pyplot as plt
 
# import networkx library
import networkx as nx
 
# create a cubical empty graph
G = nx.cubical_graph()
 
# plotting the graph
plt.subplot(122)
 
# draw a graph with red
# node and value edge color
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:




# import networkx library
import netwokx as nx
 
# create an empty undirected graph
G = nx.Graph()
 
# add edge to the graph
G.add_edge('1', '2')
G.add_edge('2', '3')
 
# print the adjacent vertices
print(G.adj)

Output: 

{'1': {'2': {}}, '2': {'1': {}, '3': {}}, '3': {'2': {}}}

Advantages and Disadvantages :

Advantages of using NetworkX for social network analysis include:

Disadvantages of using NetworkX for social network analysis include:

 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.


Article Tags :