-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph_generator.py
52 lines (32 loc) · 1.62 KB
/
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
import networkx as nx
import random
# open file to append graphs
output_file = open("graphs.txt", "a")
#####################################################################################################
# #
# Randomly generating directed graphs with order within given range #
# #
#####################################################################################################
# adding another graph to file
# input: minimum and maximum order of graph (integer each), minimum and maximum probability to have edge between vertices (float between 0 an 1 each)
def append_graph(min_order, max_order, min_p, max_p):
# get random graph order
n = random.randrange(min_order, max_order + 1)
# print(n) # nice for running script to check output
# random likilihood to create edges
p = random.randrange(min_p, max_p + 1)
# create directed graph
D = nx.gnp_random_graph(n, p/100.0, directed=True)
# write graph order to file
output_file.write(str(n) + '\n')
# write adjacency list to file
for u in range(n):
for v in D[u]:
output_file.write(str(v) + ' ')
output_file.write('\n')
# main where multiple graphs get written to file
for i in range(0, 10):
append_graph(5, 50, 30, 40)
output_file.write('0\n') #closing output with 0
#closing file
output_file.close()