-
Notifications
You must be signed in to change notification settings - Fork 2
/
random_graph_generator.py
59 lines (47 loc) · 1.99 KB
/
random_graph_generator.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
import networkx as nx
import os
import numpy as np
import shutil
OUT_DIR = os.path.join('data', 'random_graphs')
def generate_graphs(n=20, p=0.2, start=10, stop=1000, nb=100, n_constant=False, OUT_DIR=OUT_DIR):
# cleans the output folders
shutil.rmtree(os.path.join(OUT_DIR, 'gexf'))
shutil.rmtree(os.path.join(OUT_DIR, 'clq'))
# creates the output folders if needed
if not os.path.exists(OUT_DIR):
os.mkdir(OUT_DIR)
if not os.path.exists(os.path.join(OUT_DIR, 'gexf')):
os.mkdir(os.path.join(OUT_DIR, 'gexf'))
if not os.path.exists(os.path.join(OUT_DIR, 'clq')):
os.mkdir(os.path.join(OUT_DIR, 'clq'))
# n varies, p constant
if not n_constant:
for n in np.linspace(start, stop, nb):
n = int(n)
g = nx.fast_gnp_random_graph(n, p)
filename = 'Gnp' + str(n) + '_' + str(p).strip('.')
nx.write_gexf(g, os.path.join(OUT_DIR, 'gexf', filename + '.gexf'))
with open(os.path.join(OUT_DIR, 'clq', filename + '.clq'), "w") as f:
# writes the header
f.write("p EDGE {} {}\n".format(
g.number_of_nodes(), g.number_of_edges()))
# writes all edges
for u, v in g.edges():
f.write("e {} {}\n".format(u, v))
# p varies, n constant
else:
for p in np.linspace(start, stop, nb):
g = nx.fast_gnp_random_graph(n, p)
filename = 'Gnp' + str(n) + '_' + str(p).strip('.')
nx.write_gexf(g, os.path.join(OUT_DIR, 'gexf', filename + '.gexf'))
with open(os.path.join(OUT_DIR, 'clq', filename + '.clq'), "w") as f:
# writes the header
f.write("p EDGE {} {}\n".format(
g.number_of_nodes(), g.number_of_edges()))
# writes all edges
for u, v in g.edges():
f.write("e {} {}\n".format(u, v))
def main():
generate_graphs()
if __name__ == '__main__':
main()