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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
# -*- coding: utf-8 -*-
"""
Generators for some directed graphs, including growing network (GN) graphs and
scale-free graphs.
"""
# Copyright (C) 2006-2009 by
# Aric Hagberg <hagberg@lanl.gov>
# Dan Schult <dschult@colgate.edu>
# Pieter Swart <swart@lanl.gov>
# All rights reserved.
# BSD license.
__author__ ="""Aric Hagberg (hagberg@lanl.gov)\nWillem Ligtenberg (W.P.A.Ligtenberg@tue.nl)"""
__all__ = ['gn_graph', 'gnc_graph', 'gnr_graph','scale_free_graph']
import random
import networkx as nx
from networkx.generators.classic import empty_graph
from networkx.utils import discrete_sequence
def gn_graph(n, kernel=None, create_using=None, seed=None):
"""Return the growing network (GN) digraph with ``n`` nodes.
The GN graph is built by adding nodes one at a time with a link to one
previously added node. The target node for the link is chosen with
probability based on degree. The default attachment kernel is a linear
function of the degree of a node.
The graph is always a (directed) tree.
Parameters
----------
n : int
The number of nodes for the generated graph.
kernel : function
The attachment kernel.
create_using : graph, optional (default DiGraph)
Return graph of this type. The instance will be cleared.
seed : hashable object, optional
The seed for the random number generator.
Examples
--------
To create the undirected GN graph, use the :meth:`~DiGraph.to_directed`
method::
>>> D = nx.gn_graph(10) # the GN graph
>>> G = D.to_undirected() # the undirected version
To specify an attachment kernel, use the ``kernel`` keyword argument::
>>> D = nx.gn_graph(10, kernel=lambda x: x ** 1.5) # A_k = k^1.5
References
----------
.. [1] P. L. Krapivsky and S. Redner,
Organization of Growing Random Networks,
Phys. Rev. E, 63, 066123, 2001.
"""
if create_using is None:
create_using = nx.DiGraph()
elif not create_using.is_directed():
raise nx.NetworkXError("Directed Graph required in create_using")
if kernel is None:
kernel = lambda x: x
if seed is not None:
random.seed(seed)
G=empty_graph(1,create_using)
G.name="gn_graph(%s)"%(n)
if n==1:
return G
G.add_edge(1,0) # get started
ds=[1,1] # degree sequence
for source in range(2,n):
# compute distribution from kernel and degree
dist=[kernel(d) for d in ds]
# choose target from discrete distribution
target=discrete_sequence(1,distribution=dist)[0]
G.add_edge(source,target)
ds.append(1) # the source has only one link (degree one)
ds[target]+=1 # add one to the target link degree
return G
def gnr_graph(n, p, create_using=None, seed=None):
"""Return the growing network with redirection (GNR) digraph with ``n``
nodes and redirection probability ``p``.
The GNR graph is built by adding nodes one at a time with a link to one
previously added node. The previous target node is chosen uniformly at
random. With probabiliy ``p`` the link is instead "redirected" to the
successor node of the target.
The graph is always a (directed) tree.
Parameters
----------
n : int
The number of nodes for the generated graph.
p : float
The redirection probability.
create_using : graph, optional (default DiGraph)
Return graph of this type. The instance will be cleared.
seed : hashable object, optional
The seed for the random number generator.
Examples
--------
To create the undirected GNR graph, use the :meth:`~DiGraph.to_directed`
method::
>>> D = nx.gnr_graph(10, 0.5) # the GNR graph
>>> G = D.to_undirected() # the undirected version
References
----------
.. [1] P. L. Krapivsky and S. Redner,
Organization of Growing Random Networks,
Phys. Rev. E, 63, 066123, 2001.
"""
if create_using is None:
create_using = nx.DiGraph()
elif not create_using.is_directed():
raise nx.NetworkXError("Directed Graph required in create_using")
if not seed is None:
random.seed(seed)
G=empty_graph(1,create_using)
G.name="gnr_graph(%s,%s)"%(n,p)
if n==1:
return G
for source in range(1,n):
target=random.randrange(0,source)
if random.random() < p and target !=0:
target=G.successors(target)[0]
G.add_edge(source,target)
return G
def gnc_graph(n, create_using=None, seed=None):
"""Return the growing network with copying (GNC) digraph with ``n`` nodes.
The GNC graph is built by adding nodes one at a time with a link to one
previously added node (chosen uniformly at random) and to all of that
node's successors.
Parameters
----------
n : int
The number of nodes for the generated graph.
create_using : graph, optional (default DiGraph)
Return graph of this type. The instance will be cleared.
seed : hashable object, optional
The seed for the random number generator.
References
----------
.. [1] P. L. Krapivsky and S. Redner,
Network Growth by Copying,
Phys. Rev. E, 71, 036118, 2005k.},
"""
if create_using is None:
create_using = nx.DiGraph()
elif not create_using.is_directed():
raise nx.NetworkXError("Directed Graph required in create_using")
if not seed is None:
random.seed(seed)
G=empty_graph(1,create_using)
G.name="gnc_graph(%s)"%(n)
if n==1:
return G
for source in range(1,n):
target=random.randrange(0,source)
for succ in G.successors(target):
G.add_edge(source,succ)
G.add_edge(source,target)
return G
def scale_free_graph(n, alpha=0.41, beta=0.54, gamma=0.05, delta_in=0.2,
delta_out=0, create_using=None, seed=None):
"""Returns a scale-free directed graph.
Parameters
----------
n : integer
Number of nodes in graph
alpha : float
Probability for adding a new node connected to an existing node
chosen randomly according to the in-degree distribution.
beta : float
Probability for adding an edge between two existing nodes.
One existing node is chosen randomly according the in-degree
distribution and the other chosen randomly according to the out-degree
distribution.
gamma : float
Probability for adding a new node conecgted to an existing node
chosen randomly according to the out-degree distribution.
delta_in : float
Bias for choosing ndoes from in-degree distribution.
delta_out : float
Bias for choosing ndoes from out-degree distribution.
create_using : graph, optional (default MultiDiGraph)
Use this graph instance to start the process (default=3-cycle).
seed : integer, optional
Seed for random number generator
Examples
--------
Create a scale-free graph on one hundred nodes::
>>> G = nx.scale_free_graph(100)
Notes
-----
The sum of ``alpha``, ``beta``, and ``gamma`` must be 1.
References
----------
.. [1] B. Bollobás, C. Borgs, J. Chayes, and O. Riordan,
Directed scale-free graphs,
Proceedings of the fourteenth annual ACM-SIAM Symposium on
Discrete Algorithms, 132--139, 2003.
"""
def _choose_node(G,distribution,delta):
cumsum=0.0
# normalization
psum=float(sum(distribution.values()))+float(delta)*len(distribution)
r=random.random()
for i in range(0,len(distribution)):
cumsum+=(distribution[i]+delta)/psum
if r < cumsum:
break
return i
if create_using is None:
# start with 3-cycle
G = nx.MultiDiGraph()
G.add_edges_from([(0,1),(1,2),(2,0)])
else:
# keep existing graph structure?
G = create_using
if not (G.is_directed() and G.is_multigraph()):
raise nx.NetworkXError(\
"MultiDiGraph required in create_using")
if alpha <= 0:
raise ValueError('alpha must be >= 0.')
if beta <= 0:
raise ValueError('beta must be >= 0.')
if gamma <= 0:
raise ValueError('beta must be >= 0.')
if alpha+beta+gamma !=1.0:
raise ValueError('alpha+beta+gamma must equal 1.')
G.name="directed_scale_free_graph(%s,alpha=%s,beta=%s,gamma=%s,delta_in=%s,delta_out=%s)"%(n,alpha,beta,gamma,delta_in,delta_out)
# seed random number generated (uses None as default)
random.seed(seed)
while len(G)<n:
r = random.random()
# random choice in alpha,beta,gamma ranges
if r<alpha:
# alpha
# add new node v
v = len(G)
# choose w according to in-degree and delta_in
w = _choose_node(G, G.in_degree(),delta_in)
elif r < alpha+beta:
# beta
# choose v according to out-degree and delta_out
v = _choose_node(G, G.out_degree(),delta_out)
# choose w according to in-degree and delta_in
w = _choose_node(G, G.in_degree(),delta_in)
else:
# gamma
# choose v according to out-degree and delta_out
v = _choose_node(G, G.out_degree(),delta_out)
# add new node w
w = len(G)
G.add_edge(v,w)
return G
|