-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_graph.py
executable file
·259 lines (228 loc) · 10.3 KB
/
gen_graph.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
# Function to generate graphs given topological parameters
# Input: Params
# Outputs: gn - graph as list of neighbors
# numNbrDict - dictionary of number of neighbors of each node
# g - networkx graph
import networkx as nx
import numpy as np
import pdb
import copy
def gen_graph(Params):
# 2D grid graph
if Params['graphName'] == 'grid2d':
#g = nx.grid_2d_graph(Params['numNodesRow'], Params['numNodesCol'])
g = nx.grid_graph(dim=[Params['numNodesRow'], Params['numNodesCol']])
# graph as neighbor list
gn = {}
nnList = []
numNbrDict = {}
gnodes = [xx for xx in g.nodes()]
for ind in range(g.number_of_nodes()):
nodeRow = gnodes[ind][0]
nodeCol = gnodes[ind][1]
nodeIndex = nodeRow*Params['numNodesRow']+nodeCol
neighborList = [xx for xx in nx.neighbors(g, gnodes[ind])]
for indnn in range(len(neighborList)):
nnList.append(neighborList[indnn][0]*Params['numNodesRow']+neighborList[indnn][1])
# check which one is convenient
#gn[nodeIndex] = np.array(nnList) # gn as dictionary of numpy arrays
gn[nodeIndex] = copy.copy(nnList) # gn as dictionary of lists
numNbrDict[nodeIndex] = len(nnList)
del nnList[:]
# 3D grid graph
elif Params['graphName'] == 'grid3d':
g = nx.grid_graph(dim=[Params['numNodesDim1'], Params['numNodesDim2'], Params['numNodesDim3']])
# graph as neighbor list
gn = {}
nnList = []
numNbrDict = {}
gnodes = [xx for xx in g.nodes()]
for ind in range(g.number_of_nodes()):
nodeDim1 = gnodes[ind][0]
nodeDim2 = gnodes[ind][1]
nodeDim3 = gnodes[ind][2]
nodeIndex = nodeDim1*Params['numNodesDim2']*Params['numNodesDim3']+nodeDim2*Params['numNodesDim3']+nodeDim3
neighborList = nx.neighbors(g, gnodes[ind])
for indnn in range(len(neighborList)):
nnList.append(neighborList[indnn][0]*Params['numNodesDim2']*Params['numNodesDim3']+neighborList[indnn][1]*Params['numNodesDim3']+neighborList[indnn][2])
# check which one is convenient
#gn[nodeIndex] = np.array(nnList) # gn as dictionary of numpy arrays
gn[nodeIndex] = copy.copy(nnList) # gn as dictionary of lists
numNbrDict[nodeIndex] = len(nnList)
del nnList[:]
# Random Geometric Graph
elif Params['graphName'] == 'rgg':
g = nx.random_geometric_graph(Params['numNodes'], radius = Params['rggRad'], seed=0)
# graph as neighbor list
gn = {}
nnList = []
numNbrDict = {}
for ind in range(g.number_of_nodes()):
#neighborList = nx.neighbors(g, g.nodes()[ind])
neighborListPrime = nx.neighbors(g, ind)
neighborList = [x for x in neighborListPrime]
for indnn in range(len(neighborList)):
nnList.append(neighborList[indnn])
# check which one is convenient
#gn[nodeIndex] = np.array(nnList) # gn as dictionary of numpy arrays
gn[ind] = copy.copy(nnList) # gn as dictionary of lists
numNbrDict[ind] = len(nnList)
del nnList[:]
# for ind in range(Params['numNodes']):
# print(gn[ind])
# Power-law tree graph
elif Params['graphName'] == 'pltree':
g = nx.random_powerlaw_tree(Params['numNodes'], gamma=Params['pltreegamma'], seed=None, tries = 10000)
# graph as neighbor list
gn = {}
nnList = []
numNbrDict = {}
for ind in range(g.number_of_nodes()):
neighborListPrime = nx.neighbors(g, ind)
neighborList = [x for x in neighborListPrime]
for indnn in range(len(neighborList)):
nnList.append(neighborList[indnn])
# check which one is convenient
#gn[nodeIndex] = np.array(nnList) # gn as dictionary of numpy arrays
gn[ind] = copy.copy(nnList) # gn as dictionary of lists
numNbrDict[ind] = len(nnList)
del nnList[:]
# Preferential attachment graph
elif Params['graphName'] == 'pa':
g = nx.barabasi_albert_graph(n=Params['numNodes'], m=Params['mVal'], seed=None)
# graph as neighbor list
gn = {}
nnList = []
numNbrDict = {}
for ind in range(g.number_of_nodes()):
neighborListPrime = nx.neighbors(g, ind)
neighborList = [x for x in neighborListPrime]
for indnn in range(len(neighborList)):
nnList.append(neighborList[indnn])
# check which one is convenient
#gn[nodeIndex] = np.array(nnList) # gn as dictionary of numpy arrays
gn[ind] = copy.copy(nnList) # gn as dictionary of lists
numNbrDict[ind] = len(nnList)
del nnList[:]
elif Params['graphName'] == 'er':
pEr = (2*np.log(Params['numNodes']))/Params['numNodes']
g = nx.gnp_random_graph(Params['numNodes'], p=pEr, seed=None, directed=False)
# graph as neighbor list
gn = {}
nnList = []
numNbrDict = {}
for ind in range(g.number_of_nodes()):
neighborListPrime = nx.neighbors(g, ind)
neighborList = [x for x in neighborListPrime]
for indnn in range(len(neighborList)):
nnList.append(neighborList[indnn])
# check which one is convenient
#gn[nodeIndex] = np.array(nnList) # gn as dictionary of numpy arrays
gn[ind] = copy.copy(nnList) # gn as dictionary of lists
numNbrDict[ind] = len(nnList)
del nnList[:]
# graph as neighbor list
gn = {}
nnList = []
numNbrDict = {}
for ind in range(g.number_of_nodes()):
neighborListPrime = nx.neighbors(g, ind)
neighborList = [x for x in neighborListPrime]
for indnn in range(len(neighborList)):
nnList.append(neighborList[indnn])
# check which one is convenient
#gn[nodeIndex] = np.array(nnList) # gn as dictionary of numpy arrays
gn[ind] = copy.copy(nnList) # gn as dictionary of lists
numNbrDict[ind] = len(nnList)
del nnList[:]
elif Params['graphName'] == 'wxrnd':
g = nx.waxman_graph(Params['numNodes'], alpha=Params['alphaWxRnd'], beta=Params['betaWxRnd'], L=None, domain=(0,0,1,1))
# graph as neighbor list
gn = {}
nnList = []
numNbrDict = {}
for ind in range(g.number_of_nodes()):
neighborListPrime = nx.neighbors(g, ind)
neighborList = [x for x in neighborListPrime]
for indnn in range(len(neighborList)):
nnList.append(neighborList[indnn])
# check which one is convenient
#gn[nodeIndex] = np.array(nnList) # gn as dictionary of numpy arrays
gn[ind] = copy.copy(nnList) # gn as dictionary of lists
numNbrDict[ind] = len(nnList)
del nnList[:]
# graph as neighbor list
gn = {}
nnList = []
numNbrDict = {}
for ind in range(g.number_of_nodes()):
neighborListPrime = nx.neighbors(g, ind)
neighborList = [x for x in neighborListPrime]
for indnn in range(len(neighborList)):
nnList.append(neighborList[indnn])
# check which one is convenient
#gn[nodeIndex] = np.array(nnList) # gn as dictionary of numpy arrays
gn[ind] = copy.copy(nnList) # gn as dictionary of lists
numNbrDict[ind] = len(nnList)
del nnList[:]
elif Params['graphName'] == 'plclust':
g = nx.powerlaw_cluster_graph(Params['numNodes'], m=Params['mVal'],p=Params['pTriangle'], seed=None)
# graph as neighbor list
gn = {}
nnList = []
numNbrDict = {}
for ind in range(g.number_of_nodes()):
neighborListPrime = nx.neighbors(g, ind)
neighborList = [x for x in neighborListPrime]
for indnn in range(len(neighborList)):
nnList.append(neighborList[indnn])
# check which one is convenient
#gn[nodeIndex] = np.array(nnList) # gn as dictionary of numpy arrays
gn[ind] = copy.copy(nnList) # gn as dictionary of lists
numNbrDict[ind] = len(nnList)
del nnList[:]
# graph as neighbor list
gn = {}
nnList = []
numNbrDict = {}
for ind in range(g.number_of_nodes()):
neighborListPrime = nx.neighbors(g, ind)
neighborList = [x for x in neighborListPrime]
for indnn in range(len(neighborList)):
nnList.append(neighborList[indnn])
# check which one is convenient
#gn[nodeIndex] = np.array(nnList) # gn as dictionary of numpy arrays
gn[ind] = copy.copy(nnList) # gn as dictionary of lists
numNbrDict[ind] = len(nnList)
del nnList[:]
elif Params['graphName'] == 'dgm':
g = nx.dorogovtsev_goltsev_mendes_graph(Params['depthParam'], create_using=None)
# graph as neighbor list
gn = {}
nnList = []
numNbrDict = {}
for ind in range(g.number_of_nodes()):
neighborListPrime = nx.neighbors(g, ind)
neighborList = [x for x in neighborListPrime]
for indnn in range(len(neighborList)):
nnList.append(neighborList[indnn])
# check which one is convenient
#gn[nodeIndex] = np.array(nnList) # gn as dictionary of numpy arrays
gn[ind] = copy.copy(nnList) # gn as dictionary of lists
numNbrDict[ind] = len(nnList)
del nnList[:]
# graph as neighbor list
gn = {}
nnList = []
numNbrDict = {}
for ind in range(g.number_of_nodes()):
neighborListPrime = nx.neighbors(g, ind)
neighborList = [x for x in neighborListPrime]
for indnn in range(len(neighborList)):
nnList.append(neighborList[indnn])
# check which one is convenient
#gn[nodeIndex] = np.array(nnList) # gn as dictionary of numpy arrays
gn[ind] = copy.copy(nnList) # gn as dictionary of lists
numNbrDict[ind] = len(nnList)
del nnList[:]
return (gn, numNbrDict, g)