-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_graph.py
104 lines (90 loc) · 3.58 KB
/
generate_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
from decentralizepy.graphs.Regular import Regular
from decentralizepy.graphs.FullyConnected import FullyConnected
from decentralizepy.graphs.Ring import Ring
from decentralizepy.graphs.SmallWorld import SmallWorld
from decentralizepy.graphs.Star import Star
from decentralizepy.graphs.Bipartite import Bipartite
import getopt, sys
if __name__ == "__main__":
"""
Script to generate a graph file.
Usage
-----
python generate_graph.py -g <graph_type> -n <num_nodes> -s <seed> -d <degree> -k <k_over_2> -b <beta> -f <file_name> -a
Parameters
----------
graph_type : str
One of {"Regular", "FullyConnected", "Ring", "SmallWorld", "Star"i, "Bipartite"}
num_nodes : int
Number of nodes in the graph
seed : int, optional
Seed for random number generator
degree : int, optional
Degree of the graph
k_over_2 : int, optional
Parameter for smallworld
beta : float, optional
Parameter for smallworld
file_name : str, optional
Name of the file to write the graph to
a : flag, optional
If set, the graph is written in adjacency list format, otherwise in edge list format
h : flag, optional
Prints this help message
"""
__doc__ = "Usage: python3 generate_graph.py -g <graph_type> -n <num_nodes> -s <seed> -d <degree> -k <k_over_2> -b <beta> -f <file_name> -a -h"
assert len(sys.argv) >= 2, __doc__
argumentList = sys.argv[1:]
options = "hg:n:s:d:k:b:f:a"
long_options = ["graph=", "nodes=", "seed=", "degree=", "kover2=", "beta=", "file=", "adjacency", "help"]
try:
arguments, values = getopt.getopt(argumentList, options, long_options)
graph_type = None
num_nodes = None
seed = None
degree = None
k_over_2 = None
beta = None
file_name = None
type_adjacency = "edges"
for currentArgument, currentValue in arguments:
if currentArgument in ("-h", "--help"):
print(__doc__)
exit(0)
elif currentArgument in ("-g", "--graph"):
graph_type = currentValue
elif currentArgument in ("-n", "--nodes"):
num_nodes = int(currentValue)
elif currentArgument in ("-s", "--seed"):
seed = int(currentValue)
elif currentArgument in ("-d", "--degree"):
degree = int(currentValue)
elif currentArgument in ("-k", "--kover2"):
k_over_2 = int(currentValue)
elif currentArgument in ("-b", "--beta"):
beta = float(currentValue)
elif currentArgument in ("-f", "--file"):
file_name = currentValue
elif currentArgument in ("-a", "--adjacency"):
type_adjacency = "adjacency"
if graph_type == 'Regular':
g = Regular(num_nodes, degree, seed)
elif graph_type == 'FullyConnected':
g = FullyConnected(num_nodes)
elif graph_type == 'Ring':
g = Ring(num_nodes)
elif graph_type == 'SmallWorld':
g = SmallWorld(num_nodes, k_over_2, beta)
elif graph_type == 'Star':
g = Star(num_nodes)
elif graph_type == 'Bipartite':
g = Bipartite(num_nodes)
else:
raise ValueError("Invalid graph type: " + graph_type)
if file_name is not None:
g.write_graph_to_file(file_name, type=type_adjacency)
else:
raise ValueError("No file name. " + __doc__)
except getopt.error as err:
print(str(err))
sys.exit(2)