-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgraph_generator.py
97 lines (75 loc) · 2.59 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
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
#!/usr/bin/python2.7
import sys, random, math, snap;
from snap import *;
from operator import itemgetter
### Generates a random directed graph with edge labels using preferential attachment
### The labels can follow either a uniform or normal distribution
def exitMain():
sys.exit(1);
if(len(sys.argv) < 6):
print("usage python2.7 " + sys.argv[0] + " |V| <avg. degree per node> |L| {uni|norm|exp} {pa|ff|pl|er}");
exitMain();
try:
V = int(sys.argv[1]);
degree = int(sys.argv[2]);
L = int(sys.argv[3]);
dist = sys.argv[4];
model = sys.argv[5];
is_directed = sys.argv[6];
except:
print("first three params need to be integers");
exitMain();
E = degree * V;
if( E > (V*(V-1)) ):
print("too many edges for the number of nodes");
exitMain();
Rnd = TRnd()
name = "";
if( model == "er" ):
UGraph = GenRndGnm(snap.PNGraph, V, E, True, Rnd); isDirected = True; # We use Erdos-Renyi
name = model + "V" + str(V/1000) + "kD" + str(degree) + "L" + str(L) + dist + ".edge";
if( model == "pa" ):
name = "V" + str(V/1000) + "kD" + str(degree) + "L" + str(L) + dist + ".edge";
UGraph = GenPrefAttach(V, degree, Rnd); isDirected = False; # We use preferential attachment
if( model == "ff" ):
name = model + "V" + str(V/1000) + "k" + str(degree) + "L" + str(L) + dist + ".edge";
UGraph = GenForestFire(V, 0.4, 0.2); isDirected = True; # We use forest fire
if( model == "pl" ):
alpha = 1.95;
UGraph = GenRndPowerLaw (V, alpha); isDirected = False;
name = model + "V" + str(V/1000) + "ka" + str(alpha) + "L" + str(L) + dist + ".edge";
f = open(name, 'w');
if( L == 0 ):
exitMain();
random.seed();
mean = int(math.floor(L/2));
sd = int( max(1, math.floor(L/4)) );
dgraph = [];
for EI in UGraph.Edges():
if dist == "norm":
label = random.normalvariate(mean, sd);
if dist == "uni":
label = random.uniform(0,L);
if dist == "exp":
label = random.expovariate( 1.0 / L/1.7 )
label = max(0,label);
label = min(L-1,label);
label = int(math.floor(label));
#label = 2**label;
#print(label);
if( isDirected == is_directed ):
direction = random.uniform(0,1);
else:
direction = 0.0;
if( direction < 0.5 ):
triple = (EI.GetSrcNId(), EI.GetDstNId(), label);
else:
triple = (EI.GetDstNId(), EI.GetSrcNId(), label);
dgraph.append(triple);
dgraph = sorted(dgraph,key=lambda x: x[0], reverse=False);
for triple in dgraph:
(s,t,l) = triple;
base = 0;
ss = str(base+s) + " " + str(base+t) + " " + str(l);
f.write(ss + "\n");
f.close();