-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtualNetworkGraph.py
More file actions
375 lines (314 loc) · 9.79 KB
/
VirtualNetworkGraph.py
File metadata and controls
375 lines (314 loc) · 9.79 KB
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import sys
import argparse
import random
CONNECTED=True # Flag used to tell if graph is connected
KEEP_CHECKING=True # Flag used in functions: check_if_connected() and search_neighbors()
class Vertex:
def __init__(self, node, node_weight):
self.id = node
self.node_weight = node_weight
self.adjacent = {}
def __str__(self):
return str(self.id) + ' adjacent: ' + str([x.id for x in self.adjacent])
def add_neighbor(self, neighbor, weight=1):
self.adjacent[neighbor] = weight
def get_connections(self):
return self.adjacent.keys()
def get_id(self):
return self.id
def get_edge_weight(self, neighbor):
return self.adjacent[neighbor]
def get_node_weight(self):
return self.node_weight
class Graph():
def __init__(self):
self.vert_dict = {}
self.num_vertices = 0
self.num_nodes = 0
self.topology = ""
self.alpha = 0 # int of range = 0-100
self.node_min = 0
self.node_max = 0
self.link_min = 0
self.link_max = 0
def __iter__(self):
return iter(self.vert_dict.values())
def add_vertex(self, node, node_weight):
self.num_vertices = self.num_vertices + 1
new_vertex = Vertex(node, node_weight)
self.vert_dict[node] = new_vertex
#print "node= " + str(node) + " new_vertex= " + str(new_vertex)
return new_vertex
def remove_vertex(self, node):
if node in self.vert_dict:
return self.vert_dict.pop(node)
else:
return None
def remove_vertices(self):
self.vert_dict = {}
def get_vertex(self, n):
if n in self.vert_dict:
return self.vert_dict[n]
else:
return None
def get_vertices(self):
return self.vert_dict.keys()
def add_edge(self, src, dest, weight = 0):
if src not in self.vert_dict:
self.add_vertex(src)
if dest not in self.vert_dict:
self.add_vertex(dest)
self.vert_dict[src].add_neighbor(self.vert_dict[dest], weight)
### If directed graph, uncomment below... """
#self.vert_dict[dest].add_neighbor(self.vert_dict[src], weight)
def set_num_nodes(self, n):
self.num_nodes = n
def get_num_nodes(self):
return self.num_nodes
def set_topology(self, topo):
self.topology = topo
def get_topology(self):
return self.topology
def set_alpha(self, a):
self.alpha = a
def get_alpha(self):
return self.alpha
def set_node_min(self, n):
self.node_min = n
def get_node_min(self):
return self.node_min
def set_node_max(self, n):
self.node_max = n
def get_node_max(self):
return self.node_max
def set_link_min(self, n):
self.link_min = n
def get_link_min(self):
return self.link_min
def set_link_max(self, n):
self.link_max = n
def get_link_max(self):
return self.link_max
### END classes ###
### make full (mesh) connected graph
def make_full(g):
for node in g:
for other_node in g:
link_weight = int(random.uniform(g.get_link_min(), g.get_link_max()))
if (other_node == node):
continue
else:
g.add_edge(node.get_id(), other_node.get_id(), link_weight)
def biased_coin_flip(g):
alpha = g.get_alpha()
coin_bias = random.uniform(1,100)
if (coin_bias <= alpha):
return 1 # Head
return 0 # Tail
def print_graph(g):
for node in g:
for neighbor in node.get_connections():
nodeid = node.get_id()
neighborid = neighbor.get_id()
print '%3d %3d %4d' % ( nodeid, neighborid, node.get_edge_weight(neighbor))
for node in g:
print node.get_node_weight(),
print ""
""" Debug: Shows current node and adjacent node(s)
for node in g:
print 'g.vert_dict[%s]=%s' %(node.get_id(), g.vert_dict[node.get_id()])
print g.get_vertices()
for node in g:
print len(node.adjacent.keys()),
print ""
"""
### writes graph out to file w/ filename: <topology>.out
def save_graph(g):
f = open(g.get_topology()+'.out', 'w')
for node in g:
for neighbor in node.get_connections():
nodeid = node.get_id()
neighborid = neighbor.get_id()
f.writelines('%3d %3d %4d\n' % ( nodeid, neighborid, node.get_edge_weight(neighbor)))
for node in g:
f.write(str(node.get_node_weight())+ " " )
f.write("\n")
f.flush()
f.close()
# Part of quazi-DFS
def search_neighbors(node, unvisited, visited):
global CONNECTED
global KEEP_CHECKING
if KEEP_CHECKING:
if node in unvisited:
unvisited.remove(node)
visited.append(node)
if node.get_connections():
for neighbor in node.get_connections():
search_neighbors(neighbor, unvisited, visited)
elif ((len(unvisited)+1)>1): # Are we on the end node?
KEEP_CHECKING = False
CONNECTED = False
"""
Performs quazi-DFS tree traversal with help from search_neighbors()
Keeps 2 lists: unvisited (filed with all nodes) and visited (empty list)
unvisited: has nodes removed as they're visited; also used to get neighbors
of node and traverses them recursively.
visited: as nodes are visited they get added here. Used as to break
out of while loop by checking (# of nodes visited = # of total nodes)
KEEP_CHECKING: another check to know when to stop. Needed due to sloppiness
of quazi-DFS implementation.
CONNECTED: "Is graph connected?"
"""
def check_if_connected(g):
global CONNECTED
global KEEP_CHECKING
KEEP_CHECKING=True
visited = []
unvisited = [node for node in g]
num_vertices = len(g.get_vertices())
while (KEEP_CHECKING and (unvisited or (len(visited) != num_vertices))):
node = unvisited[0]
search_neighbors(node, unvisited, visited)
if not unvisited:
CONNECTED=True
else:
CONNECTED=False
return
def add_nodes_and_node_weights(g):
for i in range(g.get_num_nodes()):
node_weight = int(random.uniform(g.get_node_min(), g.get_node_max()))
g.add_vertex(i, node_weight)
return
""" Generates edges based on biased coin flip:
If heads: add edge
else: do not add edge
Iterates over each possible pair of nodes.
If alpha = 1, result is full graph.
If alpha = 0, result is graph with no links. """
def generate_random_graph(g):
for node in g:
for other_node in g:
if (other_node == node):
continue
else:
heads = biased_coin_flip(g)
if heads:
link_weight = int(random.uniform(g.get_link_min(), g.get_link_max()))
g.add_edge(node.get_id(), other_node.get_id(), link_weight)
### END functions ###
if __name__ == '__main__':
topology = ""
alpha = 0
link_max = 0
# Parse and check command line arguments...
parser = argparse.ArgumentParser(prog=sys.argv[0], usage="python %(prog)s <configfile>")
parser.add_argument('filename')
try:
args = parser.parse_args()
except IOError:
parser.print_help()
sys.exit(2)
try:
f = open(args.filename)
except IOError:
print "Cannot open config file! Aborting..."
sys.exit()
g = Graph()
num_nodes = 0
topology = ""
alpha = 0
node_min = node_max=0
link_min = link_max=0
attempts = 1
# Parse file line by line...
for line in f:
lhs, rhs = line.split(":")
lhs = lhs.lower()
#print lhs, rhs
if (lhs == "nodes"):
num_nodes = int(rhs)
g.set_num_nodes(num_nodes)
elif (lhs == "topology"):
topology = str(rhs).strip().lower()
#g.set_topology(str(rhs).strip().lower()) # removes leading and ending whitespace; makes lowercase
g.set_topology(topology)
elif ((lhs == "alpha") and (g.get_topology() == "random")):
alpha = int(float(rhs)*100)
g.set_alpha(alpha)
#print g.get_topology() + " " + str(alpha) + " " + str(g.get_alpha())
if ( (g.get_alpha() < 0) or (g.get_alpha() > 100) ):
print "Invalid range for alpha! Must be a decimal from 0.0-1.0"
sys.exit()
elif (lhs == "node-min"):
node_min = int(rhs)
g.set_node_min(node_min)
#g.set_node_min(int(rhs))
elif (lhs == "node-max"):
node_max = int(rhs)
g.set_node_max(node_max)
#g.set_node_max(int(rhs))
elif (lhs == "link-min"):
link_min = int(rhs)
g.set_link_min(link_min)
#g.set_link_min(int(rhs))
elif (lhs == "link-max"):
link_max = int(rhs)
g.set_link_max(link_max)
#g.set_link_max(int(rhs))
else:
continue
f.close()
""" Debug...
print "nodes: " + str(g.get_num_nodes())
print "topology: " + g.get_topology()
print "alpha: " + str(g.get_alpha())
print "link_max: " + str(g.get_link_max())
"""
add_nodes_and_node_weights(g)
if (g.get_topology() == "linear"):
for i in range(0, g.get_num_nodes()-1):
link_weight = int(random.uniform(g.get_link_min(), g.get_link_max()))
g.add_edge(i,i+1,link_weight)
elif (g.get_topology() == "full"):
make_full(g)
elif (g.get_topology() == "star"):
hub_node = 0
for i in range(1, g.get_num_nodes()):
link_weight = int(random.uniform(g.get_link_min(), g.get_link_max()))
g.add_edge(hub_node, i, link_weight)
elif (g.get_topology() == "random"):
if (g.get_alpha() == 100):
make_full(g)
elif (g.get_alpha() == 0):
""" Make no connections """
pass
else:
# Random edges based on alpha
generate_random_graph(g)
check_if_connected(g)
#attempts = 1
# Keep trying to generate a connected graph...
while not CONNECTED:
#print attempts, g.get_alpha(),
#print CONNECTED
attempts = attempts + 1
del g
g = Graph() # Start fresh....
g.set_num_nodes(num_nodes)
g.set_topology(topology)
g.set_alpha(alpha)
g.set_node_min(node_min)
g.set_node_max(node_max)
g.set_link_min(link_min)
g.set_link_max(link_max)
add_nodes_and_node_weights(g)
generate_random_graph(g)
check_if_connected(g)
# Write graph to file...
save_graph(g)
# Debug....
#print_graph(g)
#check_if_connected(g)
#print attempts, g.get_alpha()
#print CONNECTED