-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgengraphs.py
157 lines (147 loc) · 5.03 KB
/
gengraphs.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import networkx as nx
import sys
import random
import numpy as np
linearGraph = "LINEAR"
ringGraph = "RING"
unitDisk = "UNIT"
gridGraph = "GRID"
meshGraph = "MESH" # grid with diagonals
regularGraph = "REGULAR"
plainGrid = "TEST"
powerLaw = "PLAW"
allowedGraphs = [linearGraph, unitDisk, gridGraph, regularGraph,\
plainGrid, ringGraph, powerLaw, meshGraph]
def loadGraph(fname, remap=False, connected=True, silent=False):
""" Parameters
--------------
fname : string
filname to open
remap : bool
remap the labels to a sequence of integers
connected : bool
return only the larges component subgraph
"""
G=nx.Graph()
if not silent:
print "Loading/Generating Graph"
# load a file using networkX adjacency matrix structure
if fname.lower().endswith(".adj"):
try:
G = nx.read_adjlist(fname, nodetype=int)
except IOError as err:
print
print err
sys.exit(1)
elif fname.lower().endswith(".edges"):
try:
G = nx.read_weighted_edgelist(fname, nodetype=int)
except IOError as err:
print
print err
sys.exit(1)
else:
print >> sys.stderr, "Error: Allowed file extensions are .adj for",\
"adjacency matrix and .edges for edge-list"
sys.exit(1)
if connected:
C = sorted(list(nx.connected_component_subgraphs(G)),
key=len, reverse=True)[0]
G = C
if not silent:
print >> sys.stderr, "Graph", fname, "loaded",
# remap node labels so we don't have "holes" in the numbering
if remap:
mapping=dict(zip(G.nodes(),range(G.order())))
H = nx.relabel_nodes(G,mapping)
return H
return G
def genGraph(graphKind, numNodes):
""" Parameters
--------------
graphKind: string
numNodes: integer
"""
G=nx.Graph()
print "Loading Graph"
if graphKind == linearGraph:
G.add_nodes_from(range(0,numNodes))
for i in range(0,numNodes-1):
G.add_edge(i,i+1)
elif graphKind == ringGraph:
G.add_nodes_from(range(0,numNodes))
for i in range(0,numNodes):
G.add_edge(i,(i+1)%numNodes)
elif graphKind == unitDisk:
r = 20
# 90 nodes with 400*400 is ok, try to keep the same density
#density = 90.0/(400*400)
#area = numNodes/density
#xSize = np.sqrt(area)
xSize = 150 # use this to keep area fixed
w = dict((i,r/2) for i in range(numNodes))
for i in range(1000):
random.jumpahead(i)
p = dict((i,(random.uniform(0,xSize),random.uniform(0,xSize)))
for i in range(numNodes))
G = nx.geographical_threshold_graph(numNodes,theta=1,alpha=1,
pos=p, weight=w)
if nx.is_connected(G):
break
if not nx.is_connected(G):
print >> sys.stderr, "Could not find a connected graph \
with the given features in 1000 attempts!"
sys.exit(1)
elif graphKind == regularGraph:
degree = 6
G= nx.random_regular_graph(degree,numNodes)
elif graphKind == gridGraph or graphKind == meshGraph:
if graphKind == meshGraph:
radius = 90.0 # with diagonals
else:
radius = 70.0 # without diagonals
side = int(np.sqrt(numNodes))
if side*side != numNodes:
print >> sys.stderr, "Error, you want a squared \
grid with",numNodes,"nodes, it's not a square"
sys.exit(1)
distance = 60
positions = {}
w = {}
for i in range(numNodes):
positions[i] = (distance*(i%side), distance*(i/side))
w[i] = radius/2
G = nx.geographical_threshold_graph(numNodes, theta=1,
alpha=1, pos=positions, weight=w)
if not nx.is_connected(G):
print >> sys.stderr, "Error, something is \
wrong with the graph definition"
sys.exit(1)
elif graphKind == plainGrid:
side = int(np.sqrt(float(numNodes)))
G = nx.grid_2d_graph(side, side)
numNodes = side*side
elif graphKind == powerLaw:
gamma=2
powerlaw_gamma = lambda x: nx.utils.powerlaw_sequence(x, exponent=gamma)
loop = True
for i in range(1000):
z = nx.utils.create_degree_sequence(numNodes,
powerlaw_gamma, max_tries=5000)
G = nx.configuration_model(z)
G = nx.Graph(G)
G.remove_edges_from(G.selfloop_edges())
mainC = nx.connected_component_subgraphs(G)[0]
if len(mainC.nodes()) >= numNodes*0.9:
loop = False
break
if loop:
print "ERROR: generating powerLow graph with \
impossible parameters"
sys.exit()
else:
errMsg = "Unknown graph type " + graphKind
print >> sys.stderr, errMsg
sys.exit(1)
print >> sys.stderr, "ok"
return G