You are on page 1of 3

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 4
Student Name: Shivansh Vij UID: 21BCS3866
Branch: CSE Section/Group: 902/B
Semester: 5th Date of Performance: 01-09-2023
Subject Name: Social Networks Subject Code: 21CSP-345

1. Aim: Evaluate centrality measures such as degree centrality, betweenness


centrality, and closeness centrality to identify influential nodes or key connectors in
the network by using python.

2. Objective: The objective of this experiment is to evaluate centrality measures


such as degree centrality, betweenness centrality, and closeness centrality using
Python in order to identify influential nodes or key connectors in a network.

3. Requirements: Vs Code, Python IDE, install libraries NetworkX and


Matplotlib.

4. Code:
import networkx as nx
import matplotlib.pyplot as plt
# Step 1: Create a graph or load network data

G = nx.Graph()

# Add nodes and edges to the graph...

G.add_nodes_from([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16])
G.add_edges_from([(1,2),(2,3),(3,4),(4,1),(1,3),(2,4),(1,6),(2,6),(3,6),(4,6),(1,5),(2,
5),(1,7),(3,7),(6,8),(4,8),(6,9),(2,9),(4,10),(3,10),(11,12),(12,13),(13,14),(14,15),(15
,11),(15,16),(16,10)])
# Step 2: Compute centrality measures

degree_centrality = nx.degree_centrality(G)
betweenness_centrality = nx.betweenness_centrality(G)
closeness_centrality = nx.closeness_centrality(G)

# Step 3: Identify influential nodes

influential_nodes_degree = sorted(degree_centrality, key=degree_centrality.get,


reverse=True)[:5]

influential_nodes_betweenness = sorted(betweenness_centrality,
key=betweenness_centrality.get, reverse=True)[:5]

influential_nodes_closeness = sorted(closeness_centrality,
key=closeness_centrality.get, reverse=True)[:5]

# Step 4: Plot the network with node labels indicating centrality measures

print("21BCS3653 - Aaditya Narain Singh")


nx.draw(G, with_labels=True)
plt.show()

# Step 5: Print the results

print("Influential nodes based on degree centrality:", influential_nodes_degree)

print("Influential nodes based on betweenness centrality:",


influential_nodes_betweenness)

print("Influential nodes based on closeness centrality:",


influential_nodes_closeness)
5. Output:

Fig. 2.1.1: Showing graph using networkx and matplotlib and the
degree centrality, betweenness centrality,
closeness centrality

6. Learning Outcomes:
a) Centrality values for each node in the network.
b) Identification of nodes with the highest centrality scores, indicating their level of
influence or importance within the network.
c) Comparison of centrality values across different measures to determine the most
appropriate measure for identifying key connectors.

You might also like