-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphGeneration.py
328 lines (245 loc) · 9.27 KB
/
graphGeneration.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import networkx as nx
import torch_geometric.datasets as ds
import random
import ndlib
import ndlib.models.epidemics as ep
import ndlib.models.ModelConfig as mc
from torch_geometric.datasets import Planetoid
def connSW(n):
g = nx.connected_watts_strogatz_graph(n, int(n/200), 0.1)
config = mc.Configuration()
for a, b in g.edges():
weight = random.randrange(40,80)
weight = round(weight / 100, 2)
g[a][b]['weight'] = weight
config.add_edge_configuration("threshold", (a, b), weight)
return g, config
def BA(n):
g = nx.barabasi_albert_graph(n, 10)
config = mc.Configuration()
for a, b in g.edges():
weight = random.randrange(40,80)
weight = round(weight / 100, 2)
g[a][b]['weight'] = weight
config.add_edge_configuration("threshold", (a, b), weight)
return g, config
def ER(n):
g = nx.erdos_renyi_graph(n, 20/n)
while nx.is_connected(g) == False:
g = nx.erdos_renyi_graph(n, 0.05)
config = mc.Configuration()
for a, b in g.edges():
weight = random.randrange(40, 80)
weight = round(weight / 100, 2)
config.add_edge_configuration("threshold", (a, b), weight)
g[a][b]['weight'] = weight
return g, config
def CiteSeer():
dataset = Planetoid(root='./Planetoid', name='CiteSeer') # Cora, CiteSeer, PubMed
data = dataset[0]
edges = (data.edge_index.numpy()).T.tolist()
G = nx.from_edgelist(edges)
c = max(nx.connected_components(G), key=len)
g = G.subgraph(c).copy()
g = nx.convert_node_labels_to_integers(g, first_label=0, ordering='default', label_attribute=None)
config = mc.Configuration()
for a, b in g.edges():
weight = random.randrange(40,80)
weight = round(weight / 100, 2)
config.add_edge_configuration("threshold", (a, b), weight)
g[a][b]['weight'] = weight
return g, config
def PubMed():
dataset = Planetoid(root='./Planetoid', name='PubMed') # Cora, CiteSeer, PubMed
data = dataset[0]
edges = (data.edge_index.numpy()).T.tolist()
G = nx.from_edgelist(edges)
c = max(nx.connected_components(G), key=len)
g = G.subgraph(c).copy()
g = nx.convert_node_labels_to_integers(g, first_label=0, ordering='default', label_attribute=None)
config = mc.Configuration()
for a, b in g.edges():
weight = random.randrange(40,80)
weight = round(weight / 100, 2)
config.add_edge_configuration("threshold", (a, b), weight)
g[a][b]['weight'] = weight
return g, config
def Cora():
dataset = Planetoid(root='./Planetoid', name='Cora') # Cora, CiteSeer, PubMed
data = dataset[0]
# data.num_classes = dataset.num_classes
edges = (data.edge_index.numpy()).T.tolist()
G = nx.from_edgelist(edges)
c = max(nx.connected_components(G), key=len)
g = G.subgraph(c).copy()
g = nx.convert_node_labels_to_integers(g, first_label=0, ordering='default', label_attribute=None)
config = mc.Configuration()
for a, b in g.edges():
weight = random.randrange(40,80)
weight = round(weight / 100, 2)
config.add_edge_configuration("threshold", (a, b), weight)
g[a][b]['weight'] = weight
# return g, config, data
return g, config
def photo():
dataset = ds.Amazon(root='./geo', name = 'Photo')
data = dataset[0]
edges = (data.edge_index.numpy()).T.tolist()
G = nx.from_edgelist(edges)
g = nx.convert_node_labels_to_integers(G, first_label=0, ordering='default', label_attribute=None)
config = mc.Configuration()
for a, b in g.edges():
weight = random.randrange(40,80)
weight = round(weight / 100, 2)
config.add_edge_configuration("threshold", (a, b), weight)
g[a][b]['weight'] = weight
return g, config
def coms():
dataset = ds.Amazon(root='./geo', name = 'Computers')
data = dataset[0]
edges = (data.edge_index.numpy()).T.tolist()
G = nx.from_edgelist(edges)
g = nx.convert_node_labels_to_integers(G, first_label=0, ordering='default', label_attribute=None)
config = mc.Configuration()
for a, b in g.edges():
weight = random.randrange(40,80)
weight = round(weight / 100, 2)
config.add_edge_configuration("threshold", (a, b), weight)
g[a][b]['weight'] = weight
return g, config
def synn():
edgelist = []
for i in range(3):
for j in range(i+1,3):
edgelist.append((i,j))
for i in range(3,21):
edgelist.append((0,i))
edgelist.append((1,i+18))
edgelist.append((2,i+36))
for i in range(18):
edgelist.append((i+3,i+57))
edgelist.append((i+21,i+75))
edgelist.append((i+39,i+93))
for i in range(5):
edgelist.append((94, 95+i))
edgelist.append((95+i, 100+i))
edgelist.append((105, 106+i))
edgelist.append((106+i, 111+i))
edgelist.append((116, 117+i))
edgelist.append((117+i, 122+i))
edgelist.append((127, 128+i))
edgelist.append((128+i, 133+i))
edgelist.append((57,100))
edgelist.append((58,111))
edgelist.append((75,122))
edgelist.append((93,133))
g=nx.from_edgelist(edgelist)
config = mc.Configuration()
for a, b in g.edges():
weight = random.randrange(40,80)
weight = round(weight / 100, 2)
config.add_edge_configuration("threshold", (a, b), weight)
g[a][b]['weight'] = weight
return g, config
def synn_small():
edgelist = []
# Reduced nodes in each range
for i in range(2): # Reduced from 3
for j in range(i+1, 2): # Reduced from 3
edgelist.append((i, j))
# Smaller outer loops
for i in range(2, 7): # Reduced from 21, smaller increments
edgelist.append((0, i))
edgelist.append((1, i + 5)) # Adjusted the offsets
# Further reduced nested loops
for i in range(5): # Reduced drastically from 18
edgelist.append((i+2, i + 7)) # Adjusted indices
edgelist.append((i+7, i + 12)) # Adjusted further
# Very simplified connections at the end
edgelist.append((7, 12))
edgelist.append((8, 13))
g = nx.from_edgelist(edgelist)
config = mc.Configuration()
# Adding weights as in the original function
for a, b in g.edges():
weight = random.randrange(40, 80)
weight = round(weight / 100, 2)
config.add_edge_configuration("threshold", (a, b), weight)
g[a][b]['weight'] = weight
return g, config
def power_law(n, m=2):
"""
Generate a power law graph using the Barabási-Albert model.
:param n: Number of nodes
:param m: Number of edges to attach from a new node to existing nodes
:return: NetworkX graph and Configuration object
"""
g = nx.barabasi_albert_graph(n, m)
config = mc.Configuration()
for a, b in g.edges():
weight = random.randrange(40, 80)
weight = round(weight / 100, 2)
g[a][b]['weight'] = weight
config.add_edge_configuration("threshold", (a, b), weight)
return g, config
def bipartite_graph(n1, n2):
"""
Generate a bipartite graph.
:param n1: Number of nodes in set 1
:param n2: Number of nodes in set 2
:return: NetworkX graph and Configuration object
"""
g = nx.bipartite.random_graph(n1, n2, 0.1)
config = mc.Configuration()
for a, b in g.edges():
weight = random.randrange(40, 80)
weight = round(weight / 100, 2)
g[a][b]['weight'] = weight
config.add_edge_configuration("threshold", (a, b), weight)
return g, config
def highly_clustered_graph(n, p):
"""
Generate a highly clustered graph using the Watts-Strogatz model.
:param n: Number of nodes
:param p: Rewiring probability
:return: NetworkX graph and Configuration object
"""
g = nx.watts_strogatz_graph(n, k=int(n/10), p=p)
config = mc.Configuration()
for a, b in g.edges():
weight = random.randrange(40, 80)
weight = round(weight / 100, 2)
g[a][b]['weight'] = weight
config.add_edge_configuration("threshold", (a, b), weight)
return g, config
def disconnected_graph(n, m, num_components):
"""
Generate a disconnected graph composed of multiple components.
:param n: Total number of nodes
:param m: Number of edges per component
:param num_components: Number of components
:return: NetworkX graph and Configuration object
"""
components = [nx.barabasi_albert_graph(int(n/num_components), m) for _ in range(num_components)]
g = nx.disjoint_union_all(components)
config = mc.Configuration()
for a, b in g.edges():
weight = random.randrange(40, 80)
weight = round(weight / 100, 2)
g[a][b]['weight'] = weight
config.add_edge_configuration("threshold", (a, b), weight)
return g, config
def star_graph(n):
"""
Generate a star graph.
:param n: Number of nodes
:return: NetworkX graph and Configuration object
"""
g = nx.star_graph(n-1) # nx.star_graph creates a star graph with n nodes (0 to n-1, with 0 as the center)
config = mc.Configuration()
for a, b in g.edges():
weight = random.randrange(40, 80)
weight = round(weight / 100, 2)
g[a][b]['weight'] = weight
config.add_edge_configuration("threshold", (a, b), weight)
return g, config