Professional Documents
Culture Documents
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
4. Code:
import networkx as nx
import matplotlib.pyplot as plt
# Step 1: Create a graph or load network data
G = nx.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)
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
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.