-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphGen.py
33 lines (29 loc) · 910 Bytes
/
GraphGen.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
import networkx as nx
import random
class GraphGen:
"""Random Max Degree 4 Graph Generator"""
def __init__(self, nodes):
self.n = nodes
def randomMaxDeg4Graph(self):
while True:
try:
seq = seqGen(self.n)
g = nx.random_degree_sequence_graph(seq)
break
except nx.NetworkXError:
pass
for (u, v) in g.edges():
g[u][v]['w'] = random.randint(1, 2**self.n)
return g
def random4RegularGraph(self):
g = nx.random_regular_graph(4, self.n)
for (u, v) in g.edges():
g[u][v]['w'] = random.randint(1, 2**self.n)
return g
def seqGen(n):
seq = [1, 2] #start with invalid deg seq
while not nx.is_valid_degree_sequence(seq):
seq = []
for x in range(0, n):
seq.append(random.randint(1, 4))
return seq