Professional Documents
Culture Documents
Kshitij
Kshitij
Kshitij
Experiment-3.3
Student Name: Kshitij Kashyap UID: 21BCS11493
Branch: CSE Section/Group: 21BCS_SN-911-B
Semester: 5th Date of Performance: 06-11-23
Subject Name: SOCIAL NETWORKS LAB
Subject Code: 21CSP-345
AIM: Demonstrate the emergence of the Power Law in World Wide Web (WWW)
graphs; generate a random graph with a power-law degree distribution.
Code / Script:
import networkx as nx
import matplotlib.pyplot as plt
# Generate a random graph with a power-law degree distribution
G = nx.powerlaw_cluster_graph(200, 3, 0.1)
# Get the degree distribution of the graph
degree_distribution = nx.degree_histogram(G)
# Create lists for degree values and their corresponding counts
degrees = [degree for degree, count in enumerate(degree_distribution) if count > 0]
counts = [count for count in degree_distribution if count > 0]
# Print the degree distribution
for degree, count in zip(degrees, counts):
print("Degree:", degree, "Count:", count)
# Plot the degree distribution
plt.bar(degrees, counts)
plt.xlabel("Degree")
plt.ylabel("Count")
plt.title("Degree Distribution")
plt.show()
# Draw the graph
pos = nx.spring_layout(G, seed=42) # Define a layout for the nodes for consistent visualization
nx.draw(G, pos, node_size=20, node_color='lightblue', with_labels=False)
plt.title("Random Graph with Power-Law Degree Distribution")
plt.show()
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
LEARNING OUTCOMES: