forked from navidadkhah/Graph_Simulation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
4-eigenvalue and degree distirbution plots.py
138 lines (116 loc) · 4.81 KB
/
4-eigenvalue and degree distirbution plots.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import networkx as nx
import numpy as np
import random
import matplotlib.pyplot as plt
import math
from collections import defaultdict
# Function to plot the degree distribution PDF
def plot_degree_distribution_pdf(graph_type, degrees, title):
degree_count = np.bincount(degrees)
pdf = degree_count / len(degrees)
plt.bar(range(len(pdf)), pdf, width=0.8, color='b')
plt.title(title)
plt.xlabel('Degree')
plt.ylabel('Probability Density')
plt.xticks(np.arange(0, max(degrees)+1, 1))
plt.xlim([-0.5, max(degrees)+0.5])
plt.show()
# Random Scale-Free Recursive Graph (RSRG)
def plot_rsr_graph():
graph = nx.barabasi_albert_graph(100, 3, seed=42)
degrees = [degree for _, degree in graph.degree()]
degree_hist = np.histogram(degrees, bins=range(max(degrees) + 2))[0]
degree_pdf = degree_hist / sum(degree_hist)
plt.plot(range(len(degree_pdf)), degree_pdf, label='RSRG')
# Random Scale-Free Recursive Bipartite Graph (RSRBG)
def plot_rsr_bipartite_graph():
graph = nx.bipartite.random_graph(10, 5, 0.5, seed=42)
projection = nx.bipartite.projected_graph(graph, list(range(10)))
degrees = [degree for _, degree in projection.degree()]
degree_hist = np.histogram(degrees, bins=range(max(degrees) + 2))[0]
degree_pdf = degree_hist / sum(degree_hist)
plt.plot(range(len(degree_pdf)), degree_pdf, label='RSRBG')
# Watts-Strogatz (WS)
def plot_ws_graph():
graph = nx.watts_strogatz_graph(100, 4, 0.3, seed=42)
degrees = [degree for _, degree in graph.degree()]
degree_hist = np.histogram(degrees, bins=range(max(degrees) + 2))[0]
degree_pdf = degree_hist / sum(degree_hist)
plt.plot(range(len(degree_pdf)), degree_pdf, label='WS')
# Scale-Free (SF)
def plot_sf_graph():
graph = nx.scale_free_graph(100, seed=42)
degrees = [degree for _, degree in graph.degree()]
degree_hist = np.histogram(degrees, bins=range(max(degrees) + 2))[0]
degree_pdf = degree_hist / sum(degree_hist)
plt.plot(range(len(degree_pdf)), degree_pdf, label='SF')
# Erdos-Renyi (ER)
def plot_er_graph():
graph = nx.erdos_renyi_graph(100, 0.15, seed=42)
degrees = [degree for _, degree in graph.degree()]
degree_hist = np.histogram(degrees, bins=range(max(degrees) + 2))[0]
degree_pdf = degree_hist / sum(degree_hist)
plt.plot(range(len(degree_pdf)), degree_pdf, label='ER')
# Plotting all graphs
plot_rsr_graph()
plot_rsr_bipartite_graph()
plot_ws_graph()
plot_sf_graph()
plot_er_graph()
# Setting plot properties
plt.xlabel('Degree')
plt.ylabel('Probability Density')
plt.title('Degree Distribution PDF')
plt.legend()
plt.grid(True)
# Display the plot
plt.show()
# Random Scale-Free Recursive Graph (RSRG)
def plot_rsr_graph():
graph = nx.barabasi_albert_graph(100, 3, seed=42)
eigenvalues = np.linalg.eigvals(nx.to_numpy_array(graph))
eigenvalue_hist = np.histogram(eigenvalues, bins=30)[0]
eigenvalue_pdf = eigenvalue_hist / sum(eigenvalue_hist)
plt.plot(range(len(eigenvalue_pdf)), eigenvalue_pdf, label='RSRG')
# Random Scale-Free Recursive Bipartite Graph (RSRBG)
def plot_rsr_bipartite_graph():
graph = nx.bipartite.random_graph(10, 5, 0.5, seed=42)
eigenvalues = np.linalg.eigvals(nx.to_numpy_array(graph))
eigenvalue_hist = np.histogram(eigenvalues, bins=30)[0]
eigenvalue_pdf = eigenvalue_hist / sum(eigenvalue_hist)
plt.plot(range(len(eigenvalue_pdf)), eigenvalue_pdf, label='RSRBG')
# Watts-Strogatz (WS)
def plot_ws_graph():
graph = nx.watts_strogatz_graph(100, 4, 0.3, seed=42)
eigenvalues = np.linalg.eigvals(nx.to_numpy_array(graph))
eigenvalue_hist = np.histogram(eigenvalues, bins=30)[0]
eigenvalue_pdf = eigenvalue_hist / sum(eigenvalue_hist)
plt.plot(range(len(eigenvalue_pdf)), eigenvalue_pdf, label='WS')
# Scale-Free (SF)
def plot_sf_graph():
graph = nx.scale_free_graph(100, seed=42)
eigenvalues = np.linalg.eigvals(nx.to_numpy_array(graph))
eigenvalue_hist = np.histogram(eigenvalues, bins=30)[0]
eigenvalue_pdf = eigenvalue_hist / sum(eigenvalue_hist)
plt.plot(range(len(eigenvalue_pdf)), eigenvalue_pdf, label='SF')
# Erdos-Renyi (ER)
def plot_er_graph():
graph = nx.erdos_renyi_graph(100, 0.15, seed=42)
eigenvalues = np.linalg.eigvals(nx.to_numpy_array(graph))
eigenvalue_hist = np.histogram(eigenvalues, bins=30)[0]
eigenvalue_pdf = eigenvalue_hist / sum(eigenvalue_hist)
plt.plot(range(len(eigenvalue_pdf)), eigenvalue_pdf, label='ER')
# Plotting all graphs
plot_rsr_graph()
plot_rsr_bipartite_graph()
plot_ws_graph()
plot_sf_graph()
plot_er_graph()
# Setting plot properties
plt.xlabel('Eigenvalue Index')
plt.ylabel('Probability Density')
plt.title('Eigenvalue Distribution PDF')
plt.legend()
plt.grid(True)
# Display the plot
plt.show()