random_graphs.py 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  1. """
  2. Generators for random graphs.
  3. """
  4. import itertools
  5. import math
  6. from collections import defaultdict
  7. import networkx as nx
  8. from networkx.utils import py_random_state
  9. from ..utils.misc import check_create_using
  10. from .classic import complete_graph, empty_graph, path_graph, star_graph
  11. from .degree_seq import degree_sequence_tree
  12. __all__ = [
  13. "fast_gnp_random_graph",
  14. "gnp_random_graph",
  15. "dense_gnm_random_graph",
  16. "gnm_random_graph",
  17. "erdos_renyi_graph",
  18. "binomial_graph",
  19. "newman_watts_strogatz_graph",
  20. "watts_strogatz_graph",
  21. "connected_watts_strogatz_graph",
  22. "random_regular_graph",
  23. "barabasi_albert_graph",
  24. "dual_barabasi_albert_graph",
  25. "extended_barabasi_albert_graph",
  26. "powerlaw_cluster_graph",
  27. "random_lobster",
  28. "random_lobster_graph",
  29. "random_shell_graph",
  30. "random_powerlaw_tree",
  31. "random_powerlaw_tree_sequence",
  32. "random_kernel_graph",
  33. ]
  34. @py_random_state(2)
  35. @nx._dispatchable(graphs=None, returns_graph=True)
  36. def fast_gnp_random_graph(n, p, seed=None, directed=False, *, create_using=None):
  37. """Returns a $G_{n,p}$ random graph, also known as an Erdős-Rényi graph or
  38. a binomial graph.
  39. Parameters
  40. ----------
  41. n : int
  42. The number of nodes.
  43. p : float
  44. Probability for edge creation.
  45. seed : integer, random_state, or None (default)
  46. Indicator of random number generation state.
  47. See :ref:`Randomness<randomness>`.
  48. directed : bool, optional (default=False)
  49. If True, this function returns a directed graph.
  50. create_using : Graph constructor, optional (default=nx.Graph or nx.DiGraph)
  51. Graph type to create. If graph instance, then cleared before populated.
  52. Multigraph types are not supported and raise a ``NetworkXError``.
  53. By default NetworkX Graph or DiGraph are used depending on `directed`.
  54. Notes
  55. -----
  56. The $G_{n,p}$ graph algorithm chooses each of the $[n (n - 1)] / 2$
  57. (undirected) or $n (n - 1)$ (directed) possible edges with probability $p$.
  58. This algorithm [1]_ runs in $O(n + m)$ time, where `m` is the expected number of
  59. edges, which equals $p n (n - 1) / 2$. This should be faster than
  60. :func:`gnp_random_graph` when $p$ is small and the expected number of edges
  61. is small (that is, the graph is sparse).
  62. See Also
  63. --------
  64. gnp_random_graph
  65. References
  66. ----------
  67. .. [1] Vladimir Batagelj and Ulrik Brandes,
  68. "Efficient generation of large random networks",
  69. Phys. Rev. E, 71, 036113, 2005.
  70. """
  71. default = nx.DiGraph if directed else nx.Graph
  72. create_using = check_create_using(
  73. create_using, directed=directed, multigraph=False, default=default
  74. )
  75. if p <= 0 or p >= 1:
  76. return nx.gnp_random_graph(
  77. n, p, seed=seed, directed=directed, create_using=create_using
  78. )
  79. G = empty_graph(n, create_using=create_using)
  80. lp = math.log(1.0 - p)
  81. if directed:
  82. v = 1
  83. w = -1
  84. while v < n:
  85. lr = math.log(1.0 - seed.random())
  86. w = w + 1 + int(lr / lp)
  87. while w >= v and v < n:
  88. w = w - v
  89. v = v + 1
  90. if v < n:
  91. G.add_edge(w, v)
  92. # Nodes in graph are from 0,n-1 (start with v as the second node index).
  93. v = 1
  94. w = -1
  95. while v < n:
  96. lr = math.log(1.0 - seed.random())
  97. w = w + 1 + int(lr / lp)
  98. while w >= v and v < n:
  99. w = w - v
  100. v = v + 1
  101. if v < n:
  102. G.add_edge(v, w)
  103. return G
  104. @py_random_state(2)
  105. @nx._dispatchable(graphs=None, returns_graph=True)
  106. def gnp_random_graph(n, p, seed=None, directed=False, *, create_using=None):
  107. """Returns a $G_{n,p}$ random graph, also known as an Erdős-Rényi graph
  108. or a binomial graph.
  109. The $G_{n,p}$ model chooses each of the possible edges with probability $p$.
  110. Parameters
  111. ----------
  112. n : int
  113. The number of nodes.
  114. p : float
  115. Probability for edge creation.
  116. seed : integer, random_state, or None (default)
  117. Indicator of random number generation state.
  118. See :ref:`Randomness<randomness>`.
  119. directed : bool, optional (default=False)
  120. If True, this function returns a directed graph.
  121. create_using : Graph constructor, optional (default=nx.Graph or nx.DiGraph)
  122. Graph type to create. If graph instance, then cleared before populated.
  123. Multigraph types are not supported and raise a ``NetworkXError``.
  124. By default NetworkX Graph or DiGraph are used depending on `directed`.
  125. See Also
  126. --------
  127. fast_gnp_random_graph
  128. Notes
  129. -----
  130. This algorithm [2]_ runs in $O(n^2)$ time. For sparse graphs (that is, for
  131. small values of $p$), :func:`fast_gnp_random_graph` is a faster algorithm.
  132. :func:`binomial_graph` and :func:`erdos_renyi_graph` are
  133. aliases for :func:`gnp_random_graph`.
  134. >>> nx.binomial_graph is nx.gnp_random_graph
  135. True
  136. >>> nx.erdos_renyi_graph is nx.gnp_random_graph
  137. True
  138. References
  139. ----------
  140. .. [1] P. Erdős and A. Rényi, On Random Graphs, Publ. Math. 6, 290 (1959).
  141. .. [2] E. N. Gilbert, Random Graphs, Ann. Math. Stat., 30, 1141 (1959).
  142. """
  143. default = nx.DiGraph if directed else nx.Graph
  144. create_using = check_create_using(
  145. create_using, directed=directed, multigraph=False, default=default
  146. )
  147. if p >= 1:
  148. return complete_graph(n, create_using=create_using)
  149. G = nx.empty_graph(n, create_using=create_using)
  150. if p <= 0:
  151. return G
  152. edgetool = itertools.permutations if directed else itertools.combinations
  153. for e in edgetool(range(n), 2):
  154. if seed.random() < p:
  155. G.add_edge(*e)
  156. return G
  157. # add some aliases to common names
  158. binomial_graph = gnp_random_graph
  159. erdos_renyi_graph = gnp_random_graph
  160. @py_random_state(2)
  161. @nx._dispatchable(graphs=None, returns_graph=True)
  162. def dense_gnm_random_graph(n, m, seed=None, *, create_using=None):
  163. """Returns a $G_{n,m}$ random graph.
  164. In the $G_{n,m}$ model, a graph is chosen uniformly at random from the set
  165. of all graphs with $n$ nodes and $m$ edges.
  166. This algorithm should be faster than :func:`gnm_random_graph` for dense
  167. graphs.
  168. Parameters
  169. ----------
  170. n : int
  171. The number of nodes.
  172. m : int
  173. The number of edges.
  174. seed : integer, random_state, or None (default)
  175. Indicator of random number generation state.
  176. See :ref:`Randomness<randomness>`.
  177. create_using : Graph constructor, optional (default=nx.Graph)
  178. Graph type to create. If graph instance, then cleared before populated.
  179. Multigraph and directed types are not supported and raise a ``NetworkXError``.
  180. See Also
  181. --------
  182. gnm_random_graph
  183. Notes
  184. -----
  185. Algorithm by Keith M. Briggs Mar 31, 2006.
  186. Inspired by Knuth's Algorithm S (Selection sampling technique),
  187. in section 3.4.2 of [1]_.
  188. References
  189. ----------
  190. .. [1] Donald E. Knuth, The Art of Computer Programming,
  191. Volume 2/Seminumerical algorithms, Third Edition, Addison-Wesley, 1997.
  192. """
  193. create_using = check_create_using(create_using, directed=False, multigraph=False)
  194. mmax = n * (n - 1) // 2
  195. if m >= mmax:
  196. return complete_graph(n, create_using)
  197. G = empty_graph(n, create_using)
  198. if n == 1:
  199. return G
  200. u = 0
  201. v = 1
  202. t = 0
  203. k = 0
  204. while True:
  205. if seed.randrange(mmax - t) < m - k:
  206. G.add_edge(u, v)
  207. k += 1
  208. if k == m:
  209. return G
  210. t += 1
  211. v += 1
  212. if v == n: # go to next row of adjacency matrix
  213. u += 1
  214. v = u + 1
  215. @py_random_state(2)
  216. @nx._dispatchable(graphs=None, returns_graph=True)
  217. def gnm_random_graph(n, m, seed=None, directed=False, *, create_using=None):
  218. """Returns a $G_{n,m}$ random graph.
  219. In the $G_{n,m}$ model, a graph is chosen uniformly at random from the set
  220. of all graphs with $n$ nodes and $m$ edges.
  221. This algorithm should be faster than :func:`dense_gnm_random_graph` for
  222. sparse graphs.
  223. Parameters
  224. ----------
  225. n : int
  226. The number of nodes.
  227. m : int
  228. The number of edges.
  229. seed : integer, random_state, or None (default)
  230. Indicator of random number generation state.
  231. See :ref:`Randomness<randomness>`.
  232. directed : bool, optional (default=False)
  233. If True return a directed graph
  234. create_using : Graph constructor, optional (default=nx.Graph or nx.DiGraph)
  235. Graph type to create. If graph instance, then cleared before populated.
  236. Multigraph types are not supported and raise a ``NetworkXError``.
  237. By default NetworkX Graph or DiGraph are used depending on `directed`.
  238. See also
  239. --------
  240. dense_gnm_random_graph
  241. """
  242. default = nx.DiGraph if directed else nx.Graph
  243. create_using = check_create_using(
  244. create_using, directed=directed, multigraph=False, default=default
  245. )
  246. if n == 1:
  247. return nx.empty_graph(n, create_using=create_using)
  248. max_edges = n * (n - 1) if directed else n * (n - 1) / 2.0
  249. if m >= max_edges:
  250. return complete_graph(n, create_using=create_using)
  251. G = nx.empty_graph(n, create_using=create_using)
  252. nlist = list(G)
  253. edge_count = 0
  254. while edge_count < m:
  255. # generate random edge,u,v
  256. u = seed.choice(nlist)
  257. v = seed.choice(nlist)
  258. if u == v or G.has_edge(u, v):
  259. continue
  260. else:
  261. G.add_edge(u, v)
  262. edge_count = edge_count + 1
  263. return G
  264. @py_random_state(3)
  265. @nx._dispatchable(graphs=None, returns_graph=True)
  266. def newman_watts_strogatz_graph(n, k, p, seed=None, *, create_using=None):
  267. """Returns a Newman–Watts–Strogatz small-world graph.
  268. Parameters
  269. ----------
  270. n : int
  271. The number of nodes.
  272. k : int
  273. Each node is joined with its `k` nearest neighbors in a ring
  274. topology.
  275. p : float
  276. The probability of adding a new edge for each edge.
  277. seed : integer, random_state, or None (default)
  278. Indicator of random number generation state.
  279. See :ref:`Randomness<randomness>`.
  280. create_using : Graph constructor, optional (default=nx.Graph)
  281. Graph type to create. If graph instance, then cleared before populated.
  282. Multigraph and directed types are not supported and raise a ``NetworkXError``.
  283. Notes
  284. -----
  285. First create a ring over $n$ nodes [1]_. Then each node in the ring is
  286. connected with its $k$ nearest neighbors (or $k - 1$ neighbors if $k$
  287. is odd). Then shortcuts are created by adding new edges as follows: for
  288. each edge $(u, v)$ in the underlying "$n$-ring with $k$ nearest
  289. neighbors" with probability $p$ add a new edge $(u, w)$ with
  290. randomly-chosen existing node $w$. In contrast with
  291. :func:`watts_strogatz_graph`, no edges are removed.
  292. See Also
  293. --------
  294. watts_strogatz_graph
  295. References
  296. ----------
  297. .. [1] M. E. J. Newman and D. J. Watts,
  298. Renormalization group analysis of the small-world network model,
  299. Physics Letters A, 263, 341, 1999.
  300. https://doi.org/10.1016/S0375-9601(99)00757-4
  301. """
  302. create_using = check_create_using(create_using, directed=False, multigraph=False)
  303. if k > n:
  304. raise nx.NetworkXError("k>=n, choose smaller k or larger n")
  305. # If k == n the graph return is a complete graph
  306. if k == n:
  307. return nx.complete_graph(n, create_using)
  308. G = empty_graph(n, create_using)
  309. nlist = list(G.nodes())
  310. fromv = nlist
  311. # connect the k/2 neighbors
  312. for j in range(1, k // 2 + 1):
  313. tov = fromv[j:] + fromv[0:j] # the first j are now last
  314. for i in range(len(fromv)):
  315. G.add_edge(fromv[i], tov[i])
  316. # for each edge u-v, with probability p, randomly select existing
  317. # node w and add new edge u-w
  318. e = list(G.edges())
  319. for u, v in e:
  320. if seed.random() < p:
  321. w = seed.choice(nlist)
  322. # no self-loops and reject if edge u-w exists
  323. # is that the correct NWS model?
  324. while w == u or G.has_edge(u, w):
  325. w = seed.choice(nlist)
  326. if G.degree(u) >= n - 1:
  327. break # skip this rewiring
  328. else:
  329. G.add_edge(u, w)
  330. return G
  331. @py_random_state(3)
  332. @nx._dispatchable(graphs=None, returns_graph=True)
  333. def watts_strogatz_graph(n, k, p, seed=None, *, create_using=None):
  334. """Returns a Watts–Strogatz small-world graph.
  335. Parameters
  336. ----------
  337. n : int
  338. The number of nodes
  339. k : int
  340. Each node is joined with its `k` nearest neighbors in a ring
  341. topology.
  342. p : float
  343. The probability of rewiring each edge
  344. seed : integer, random_state, or None (default)
  345. Indicator of random number generation state.
  346. See :ref:`Randomness<randomness>`.
  347. create_using : Graph constructor, optional (default=nx.Graph)
  348. Graph type to create. If graph instance, then cleared before populated.
  349. Multigraph and directed types are not supported and raise a ``NetworkXError``.
  350. See Also
  351. --------
  352. newman_watts_strogatz_graph
  353. connected_watts_strogatz_graph
  354. Notes
  355. -----
  356. First create a ring over $n$ nodes [1]_. Then each node in the ring is joined
  357. to its $k$ nearest neighbors (or $k - 1$ neighbors if $k$ is odd).
  358. Then shortcuts are created by replacing some edges as follows: for each
  359. edge $(u, v)$ in the underlying "$n$-ring with $k$ nearest neighbors"
  360. with probability $p$ replace it with a new edge $(u, w)$ with uniformly
  361. random choice of existing node $w$.
  362. In contrast with :func:`newman_watts_strogatz_graph`, the random rewiring
  363. does not increase the number of edges. The rewired graph is not guaranteed
  364. to be connected as in :func:`connected_watts_strogatz_graph`.
  365. References
  366. ----------
  367. .. [1] Duncan J. Watts and Steven H. Strogatz,
  368. Collective dynamics of small-world networks,
  369. Nature, 393, pp. 440--442, 1998.
  370. """
  371. create_using = check_create_using(create_using, directed=False, multigraph=False)
  372. if k > n:
  373. raise nx.NetworkXError("k>n, choose smaller k or larger n")
  374. # If k == n, the graph is complete not Watts-Strogatz
  375. if k == n:
  376. G = nx.complete_graph(n, create_using)
  377. return G
  378. G = nx.empty_graph(n, create_using=create_using)
  379. nodes = list(range(n)) # nodes are labeled 0 to n-1
  380. # connect each node to k/2 neighbors
  381. for j in range(1, k // 2 + 1):
  382. targets = nodes[j:] + nodes[0:j] # first j nodes are now last in list
  383. G.add_edges_from(zip(nodes, targets))
  384. # rewire edges from each node
  385. # loop over all nodes in order (label) and neighbors in order (distance)
  386. # no self loops or multiple edges allowed
  387. for j in range(1, k // 2 + 1): # outer loop is neighbors
  388. targets = nodes[j:] + nodes[0:j] # first j nodes are now last in list
  389. # inner loop in node order
  390. for u, v in zip(nodes, targets):
  391. if seed.random() < p:
  392. w = seed.choice(nodes)
  393. # Enforce no self-loops or multiple edges
  394. while w == u or G.has_edge(u, w):
  395. w = seed.choice(nodes)
  396. if G.degree(u) >= n - 1:
  397. break # skip this rewiring
  398. else:
  399. G.remove_edge(u, v)
  400. G.add_edge(u, w)
  401. return G
  402. @py_random_state(4)
  403. @nx._dispatchable(graphs=None, returns_graph=True)
  404. def connected_watts_strogatz_graph(n, k, p, tries=100, seed=None, *, create_using=None):
  405. """Returns a connected Watts–Strogatz small-world graph.
  406. Attempts to generate a connected graph by repeated generation of
  407. Watts–Strogatz small-world graphs. An exception is raised if the maximum
  408. number of tries is exceeded.
  409. Parameters
  410. ----------
  411. n : int
  412. The number of nodes
  413. k : int
  414. Each node is joined with its `k` nearest neighbors in a ring
  415. topology.
  416. p : float
  417. The probability of rewiring each edge
  418. tries : int
  419. Number of attempts to generate a connected graph.
  420. seed : integer, random_state, or None (default)
  421. Indicator of random number generation state.
  422. See :ref:`Randomness<randomness>`.
  423. create_using : Graph constructor, optional (default=nx.Graph)
  424. Graph type to create. If graph instance, then cleared before populated.
  425. Multigraph and directed types are not supported and raise a ``NetworkXError``.
  426. Notes
  427. -----
  428. First create a ring over $n$ nodes [1]_. Then each node in the ring is joined
  429. to its $k$ nearest neighbors (or $k - 1$ neighbors if $k$ is odd).
  430. Then shortcuts are created by replacing some edges as follows: for each
  431. edge $(u, v)$ in the underlying "$n$-ring with $k$ nearest neighbors"
  432. with probability $p$ replace it with a new edge $(u, w)$ with uniformly
  433. random choice of existing node $w$.
  434. The entire process is repeated until a connected graph results.
  435. See Also
  436. --------
  437. newman_watts_strogatz_graph
  438. watts_strogatz_graph
  439. References
  440. ----------
  441. .. [1] Duncan J. Watts and Steven H. Strogatz,
  442. Collective dynamics of small-world networks,
  443. Nature, 393, pp. 440--442, 1998.
  444. """
  445. for i in range(tries):
  446. # seed is an RNG so should change sequence each call
  447. G = watts_strogatz_graph(n, k, p, seed, create_using=create_using)
  448. if nx.is_connected(G):
  449. return G
  450. raise nx.NetworkXError("Maximum number of tries exceeded")
  451. @py_random_state(2)
  452. @nx._dispatchable(graphs=None, returns_graph=True)
  453. def random_regular_graph(d, n, seed=None, *, create_using=None):
  454. r"""Returns a random $d$-regular graph on $n$ nodes.
  455. A regular graph is a graph where each node has the same number of neighbors.
  456. The resulting graph has no self-loops or parallel edges.
  457. Parameters
  458. ----------
  459. d : int
  460. The degree of each node.
  461. n : integer
  462. The number of nodes. The value of $n \times d$ must be even.
  463. seed : integer, random_state, or None (default)
  464. Indicator of random number generation state.
  465. See :ref:`Randomness<randomness>`.
  466. create_using : Graph constructor, optional (default=nx.Graph)
  467. Graph type to create. If graph instance, then cleared before populated.
  468. Multigraph and directed types are not supported and raise a ``NetworkXError``.
  469. Notes
  470. -----
  471. The nodes are numbered from $0$ to $n - 1$.
  472. Kim and Vu's paper [2]_ shows that this algorithm samples in an
  473. asymptotically uniform way from the space of random graphs when
  474. $d = O(n^{1 / 3 - \epsilon})$.
  475. Raises
  476. ------
  477. NetworkXError
  478. If $n \times d$ is odd or $d$ is greater than or equal to $n$.
  479. References
  480. ----------
  481. .. [1] A. Steger and N. Wormald,
  482. Generating random regular graphs quickly,
  483. Probability and Computing 8 (1999), 377-396, 1999.
  484. https://doi.org/10.1017/S0963548399003867
  485. .. [2] Jeong Han Kim and Van H. Vu,
  486. Generating random regular graphs,
  487. Proceedings of the thirty-fifth ACM symposium on Theory of computing,
  488. San Diego, CA, USA, pp 213--222, 2003.
  489. http://portal.acm.org/citation.cfm?id=780542.780576
  490. """
  491. create_using = check_create_using(create_using, directed=False, multigraph=False)
  492. if (n * d) % 2 != 0:
  493. raise nx.NetworkXError("n * d must be even")
  494. if not 0 <= d < n:
  495. raise nx.NetworkXError("the 0 <= d < n inequality must be satisfied")
  496. G = nx.empty_graph(n, create_using=create_using)
  497. if d == 0:
  498. return G
  499. def _suitable(edges, potential_edges):
  500. # Helper subroutine to check if there are suitable edges remaining
  501. # If False, the generation of the graph has failed
  502. if not potential_edges:
  503. return True
  504. for s1 in potential_edges:
  505. for s2 in potential_edges:
  506. # Two iterators on the same dictionary are guaranteed
  507. # to visit it in the same order if there are no
  508. # intervening modifications.
  509. if s1 == s2:
  510. # Only need to consider s1-s2 pair one time
  511. break
  512. if s1 > s2:
  513. s1, s2 = s2, s1
  514. if (s1, s2) not in edges:
  515. return True
  516. return False
  517. def _try_creation():
  518. # Attempt to create an edge set
  519. edges = set()
  520. stubs = list(range(n)) * d
  521. while stubs:
  522. potential_edges = defaultdict(lambda: 0)
  523. seed.shuffle(stubs)
  524. stubiter = iter(stubs)
  525. for s1, s2 in zip(stubiter, stubiter):
  526. if s1 > s2:
  527. s1, s2 = s2, s1
  528. if s1 != s2 and ((s1, s2) not in edges):
  529. edges.add((s1, s2))
  530. else:
  531. potential_edges[s1] += 1
  532. potential_edges[s2] += 1
  533. if not _suitable(edges, potential_edges):
  534. return None # failed to find suitable edge set
  535. stubs = [
  536. node
  537. for node, potential in potential_edges.items()
  538. for _ in range(potential)
  539. ]
  540. return edges
  541. # Even though a suitable edge set exists,
  542. # the generation of such a set is not guaranteed.
  543. # Try repeatedly to find one.
  544. edges = _try_creation()
  545. while edges is None:
  546. edges = _try_creation()
  547. G.add_edges_from(edges)
  548. return G
  549. def _random_subset(seq, m, rng):
  550. """Return m unique elements from seq.
  551. This differs from random.sample which can return repeated
  552. elements if seq holds repeated elements.
  553. Note: rng is a random.Random or numpy.random.RandomState instance.
  554. """
  555. targets = set()
  556. while len(targets) < m:
  557. x = rng.choice(seq)
  558. targets.add(x)
  559. return targets
  560. @py_random_state(2)
  561. @nx._dispatchable(graphs=None, returns_graph=True)
  562. def barabasi_albert_graph(n, m, seed=None, initial_graph=None, *, create_using=None):
  563. """Returns a random graph using Barabási–Albert preferential attachment
  564. A graph of $n$ nodes is grown by attaching new nodes each with $m$
  565. edges that are preferentially attached to existing nodes with high degree.
  566. Parameters
  567. ----------
  568. n : int
  569. Number of nodes
  570. m : int
  571. Number of edges to attach from a new node to existing nodes
  572. seed : integer, random_state, or None (default)
  573. Indicator of random number generation state.
  574. See :ref:`Randomness<randomness>`.
  575. initial_graph : Graph or None (default)
  576. Initial network for Barabási–Albert algorithm.
  577. It should be a connected graph for most use cases.
  578. A copy of `initial_graph` is used.
  579. If None, starts from a star graph on (m+1) nodes.
  580. create_using : Graph constructor, optional (default=nx.Graph)
  581. Graph type to create. If graph instance, then cleared before populated.
  582. Multigraph and directed types are not supported and raise a ``NetworkXError``.
  583. Returns
  584. -------
  585. G : Graph
  586. Raises
  587. ------
  588. NetworkXError
  589. If `m` does not satisfy ``1 <= m < n``, or
  590. the initial graph number of nodes m0 does not satisfy ``m <= m0 <= n``.
  591. References
  592. ----------
  593. .. [1] A. L. Barabási and R. Albert "Emergence of scaling in
  594. random networks", Science 286, pp 509-512, 1999.
  595. """
  596. create_using = check_create_using(create_using, directed=False, multigraph=False)
  597. if m < 1 or m >= n:
  598. raise nx.NetworkXError(
  599. f"Barabási–Albert network must have m >= 1 and m < n, m = {m}, n = {n}"
  600. )
  601. if initial_graph is None:
  602. # Default initial graph : star graph on (m + 1) nodes
  603. G = star_graph(m, create_using)
  604. else:
  605. if len(initial_graph) < m or len(initial_graph) > n:
  606. raise nx.NetworkXError(
  607. f"Barabási–Albert initial graph needs between m={m} and n={n} nodes"
  608. )
  609. G = initial_graph.copy()
  610. # List of existing nodes, with nodes repeated once for each adjacent edge
  611. repeated_nodes = [n for n, d in G.degree() for _ in range(d)]
  612. # Start adding the other n - m0 nodes.
  613. source = len(G)
  614. while source < n:
  615. # Now choose m unique nodes from the existing nodes
  616. # Pick uniformly from repeated_nodes (preferential attachment)
  617. targets = _random_subset(repeated_nodes, m, seed)
  618. # Add edges to m nodes from the source.
  619. G.add_edges_from(zip([source] * m, targets))
  620. # Add one node to the list for each new edge just created.
  621. repeated_nodes.extend(targets)
  622. # And the new node "source" has m edges to add to the list.
  623. repeated_nodes.extend([source] * m)
  624. source += 1
  625. return G
  626. @py_random_state(4)
  627. @nx._dispatchable(graphs=None, returns_graph=True)
  628. def dual_barabasi_albert_graph(
  629. n, m1, m2, p, seed=None, initial_graph=None, *, create_using=None
  630. ):
  631. """Returns a random graph using dual Barabási–Albert preferential attachment
  632. A graph of $n$ nodes is grown by attaching new nodes each with either $m_1$
  633. edges (with probability $p$) or $m_2$ edges (with probability $1-p$) that
  634. are preferentially attached to existing nodes with high degree.
  635. Parameters
  636. ----------
  637. n : int
  638. Number of nodes
  639. m1 : int
  640. Number of edges to link each new node to existing nodes with probability $p$
  641. m2 : int
  642. Number of edges to link each new node to existing nodes with probability $1-p$
  643. p : float
  644. The probability of attaching $m_1$ edges (as opposed to $m_2$ edges)
  645. seed : integer, random_state, or None (default)
  646. Indicator of random number generation state.
  647. See :ref:`Randomness<randomness>`.
  648. initial_graph : Graph or None (default)
  649. Initial network for Barabási–Albert algorithm.
  650. A copy of `initial_graph` is used.
  651. It should be connected for most use cases.
  652. If None, starts from an star graph on max(m1, m2) + 1 nodes.
  653. create_using : Graph constructor, optional (default=nx.Graph)
  654. Graph type to create. If graph instance, then cleared before populated.
  655. Multigraph and directed types are not supported and raise a ``NetworkXError``.
  656. Returns
  657. -------
  658. G : Graph
  659. Raises
  660. ------
  661. NetworkXError
  662. If `m1` and `m2` do not satisfy ``1 <= m1,m2 < n``, or
  663. `p` does not satisfy ``0 <= p <= 1``, or
  664. the initial graph number of nodes m0 does not satisfy m1, m2 <= m0 <= n.
  665. References
  666. ----------
  667. .. [1] N. Moshiri "The dual-Barabasi-Albert model", arXiv:1810.10538.
  668. """
  669. create_using = check_create_using(create_using, directed=False, multigraph=False)
  670. if m1 < 1 or m1 >= n:
  671. raise nx.NetworkXError(
  672. f"Dual Barabási–Albert must have m1 >= 1 and m1 < n, m1 = {m1}, n = {n}"
  673. )
  674. if m2 < 1 or m2 >= n:
  675. raise nx.NetworkXError(
  676. f"Dual Barabási–Albert must have m2 >= 1 and m2 < n, m2 = {m2}, n = {n}"
  677. )
  678. if p < 0 or p > 1:
  679. raise nx.NetworkXError(
  680. f"Dual Barabási–Albert network must have 0 <= p <= 1, p = {p}"
  681. )
  682. # For simplicity, if p == 0 or 1, just return BA
  683. if p == 1:
  684. return barabasi_albert_graph(n, m1, seed, create_using=create_using)
  685. elif p == 0:
  686. return barabasi_albert_graph(n, m2, seed, create_using=create_using)
  687. if initial_graph is None:
  688. # Default initial graph : star graph on max(m1, m2) nodes
  689. G = star_graph(max(m1, m2), create_using)
  690. else:
  691. if len(initial_graph) < max(m1, m2) or len(initial_graph) > n:
  692. raise nx.NetworkXError(
  693. f"Barabási–Albert initial graph must have between "
  694. f"max(m1, m2) = {max(m1, m2)} and n = {n} nodes"
  695. )
  696. G = initial_graph.copy()
  697. # Target nodes for new edges
  698. targets = list(G)
  699. # List of existing nodes, with nodes repeated once for each adjacent edge
  700. repeated_nodes = [n for n, d in G.degree() for _ in range(d)]
  701. # Start adding the remaining nodes.
  702. source = len(G)
  703. while source < n:
  704. # Pick which m to use (m1 or m2)
  705. if seed.random() < p:
  706. m = m1
  707. else:
  708. m = m2
  709. # Now choose m unique nodes from the existing nodes
  710. # Pick uniformly from repeated_nodes (preferential attachment)
  711. targets = _random_subset(repeated_nodes, m, seed)
  712. # Add edges to m nodes from the source.
  713. G.add_edges_from(zip([source] * m, targets))
  714. # Add one node to the list for each new edge just created.
  715. repeated_nodes.extend(targets)
  716. # And the new node "source" has m edges to add to the list.
  717. repeated_nodes.extend([source] * m)
  718. source += 1
  719. return G
  720. @py_random_state(4)
  721. @nx._dispatchable(graphs=None, returns_graph=True)
  722. def extended_barabasi_albert_graph(n, m, p, q, seed=None, *, create_using=None):
  723. """Returns an extended Barabási–Albert model graph.
  724. An extended Barabási–Albert model graph is a random graph constructed
  725. using preferential attachment. The extended model allows new edges,
  726. rewired edges or new nodes. Based on the probabilities $p$ and $q$
  727. with $p + q < 1$, the growing behavior of the graph is determined as:
  728. 1) With $p$ probability, $m$ new edges are added to the graph,
  729. starting from randomly chosen existing nodes and attached preferentially at the
  730. other end.
  731. 2) With $q$ probability, $m$ existing edges are rewired
  732. by randomly choosing an edge and rewiring one end to a preferentially chosen node.
  733. 3) With $(1 - p - q)$ probability, $m$ new nodes are added to the graph
  734. with edges attached preferentially.
  735. When $p = q = 0$, the model behaves just like the Barabási–Alber model.
  736. Parameters
  737. ----------
  738. n : int
  739. Number of nodes
  740. m : int
  741. Number of edges with which a new node attaches to existing nodes
  742. p : float
  743. Probability value for adding an edge between existing nodes. p + q < 1
  744. q : float
  745. Probability value of rewiring of existing edges. p + q < 1
  746. seed : integer, random_state, or None (default)
  747. Indicator of random number generation state.
  748. See :ref:`Randomness<randomness>`.
  749. create_using : Graph constructor, optional (default=nx.Graph)
  750. Graph type to create. If graph instance, then cleared before populated.
  751. Multigraph and directed types are not supported and raise a ``NetworkXError``.
  752. Returns
  753. -------
  754. G : Graph
  755. Raises
  756. ------
  757. NetworkXError
  758. If `m` does not satisfy ``1 <= m < n`` or ``1 >= p + q``
  759. References
  760. ----------
  761. .. [1] Albert, R., & Barabási, A. L. (2000)
  762. Topology of evolving networks: local events and universality
  763. Physical review letters, 85(24), 5234.
  764. """
  765. create_using = check_create_using(create_using, directed=False, multigraph=False)
  766. if m < 1 or m >= n:
  767. msg = f"Extended Barabasi-Albert network needs m>=1 and m<n, m={m}, n={n}"
  768. raise nx.NetworkXError(msg)
  769. if p + q >= 1:
  770. msg = f"Extended Barabasi-Albert network needs p + q <= 1, p={p}, q={q}"
  771. raise nx.NetworkXError(msg)
  772. # Add m initial nodes (m0 in barabasi-speak)
  773. G = empty_graph(m, create_using)
  774. # List of nodes to represent the preferential attachment random selection.
  775. # At the creation of the graph, all nodes are added to the list
  776. # so that even nodes that are not connected have a chance to get selected,
  777. # for rewiring and adding of edges.
  778. # With each new edge, nodes at the ends of the edge are added to the list.
  779. attachment_preference = []
  780. attachment_preference.extend(range(m))
  781. # Start adding the other n-m nodes. The first node is m.
  782. new_node = m
  783. while new_node < n:
  784. a_probability = seed.random()
  785. # Total number of edges of a Clique of all the nodes
  786. clique_degree = len(G) - 1
  787. clique_size = (len(G) * clique_degree) / 2
  788. # Adding m new edges, if there is room to add them
  789. if a_probability < p and G.size() <= clique_size - m:
  790. # Select the nodes where an edge can be added
  791. eligible_nodes = [nd for nd, deg in G.degree() if deg < clique_degree]
  792. for i in range(m):
  793. # Choosing a random source node from eligible_nodes
  794. src_node = seed.choice(eligible_nodes)
  795. # Picking a possible node that is not 'src_node' or
  796. # neighbor with 'src_node', with preferential attachment
  797. prohibited_nodes = list(G[src_node])
  798. prohibited_nodes.append(src_node)
  799. # This will raise an exception if the sequence is empty
  800. dest_node = seed.choice(
  801. [nd for nd in attachment_preference if nd not in prohibited_nodes]
  802. )
  803. # Adding the new edge
  804. G.add_edge(src_node, dest_node)
  805. # Appending both nodes to add to their preferential attachment
  806. attachment_preference.append(src_node)
  807. attachment_preference.append(dest_node)
  808. # Adjusting the eligible nodes. Degree may be saturated.
  809. if G.degree(src_node) == clique_degree:
  810. eligible_nodes.remove(src_node)
  811. if G.degree(dest_node) == clique_degree and dest_node in eligible_nodes:
  812. eligible_nodes.remove(dest_node)
  813. # Rewiring m edges, if there are enough edges
  814. elif p <= a_probability < (p + q) and m <= G.size() < clique_size:
  815. # Selecting nodes that have at least 1 edge but that are not
  816. # fully connected to ALL other nodes (center of star).
  817. # These nodes are the pivot nodes of the edges to rewire
  818. eligible_nodes = [nd for nd, deg in G.degree() if 0 < deg < clique_degree]
  819. for i in range(m):
  820. # Choosing a random source node
  821. node = seed.choice(eligible_nodes)
  822. # The available nodes do have a neighbor at least.
  823. nbr_nodes = list(G[node])
  824. # Choosing the other end that will get detached
  825. src_node = seed.choice(nbr_nodes)
  826. # Picking a target node that is not 'node' or
  827. # neighbor with 'node', with preferential attachment
  828. nbr_nodes.append(node)
  829. dest_node = seed.choice(
  830. [nd for nd in attachment_preference if nd not in nbr_nodes]
  831. )
  832. # Rewire
  833. G.remove_edge(node, src_node)
  834. G.add_edge(node, dest_node)
  835. # Adjusting the preferential attachment list
  836. attachment_preference.remove(src_node)
  837. attachment_preference.append(dest_node)
  838. # Adjusting the eligible nodes.
  839. # nodes may be saturated or isolated.
  840. if G.degree(src_node) == 0 and src_node in eligible_nodes:
  841. eligible_nodes.remove(src_node)
  842. if dest_node in eligible_nodes:
  843. if G.degree(dest_node) == clique_degree:
  844. eligible_nodes.remove(dest_node)
  845. else:
  846. if G.degree(dest_node) == 1:
  847. eligible_nodes.append(dest_node)
  848. # Adding new node with m edges
  849. else:
  850. # Select the edges' nodes by preferential attachment
  851. targets = _random_subset(attachment_preference, m, seed)
  852. G.add_edges_from(zip([new_node] * m, targets))
  853. # Add one node to the list for each new edge just created.
  854. attachment_preference.extend(targets)
  855. # The new node has m edges to it, plus itself: m + 1
  856. attachment_preference.extend([new_node] * (m + 1))
  857. new_node += 1
  858. return G
  859. @py_random_state(3)
  860. @nx._dispatchable(graphs=None, returns_graph=True)
  861. def powerlaw_cluster_graph(n, m, p, seed=None, *, create_using=None):
  862. """Holme and Kim algorithm for growing graphs with powerlaw
  863. degree distribution and approximate average clustering.
  864. Parameters
  865. ----------
  866. n : int
  867. the number of nodes
  868. m : int
  869. the number of random edges to add for each new node
  870. p : float,
  871. Probability of adding a triangle after adding a random edge
  872. seed : integer, random_state, or None (default)
  873. Indicator of random number generation state.
  874. See :ref:`Randomness<randomness>`.
  875. create_using : Graph constructor, optional (default=nx.Graph)
  876. Graph type to create. If graph instance, then cleared before populated.
  877. Multigraph and directed types are not supported and raise a ``NetworkXError``.
  878. Notes
  879. -----
  880. The average clustering has a hard time getting above a certain
  881. cutoff that depends on `m`. This cutoff is often quite low. The
  882. transitivity (fraction of triangles to possible triangles) seems to
  883. decrease with network size.
  884. It is essentially the Barabási–Albert (BA) growth model with an
  885. extra step that each random edge is followed by a chance of
  886. making an edge to one of its neighbors too (and thus a triangle).
  887. This algorithm improves on BA in the sense that it enables a
  888. higher average clustering to be attained if desired.
  889. It seems possible to have a disconnected graph with this algorithm
  890. since the initial `m` nodes may not be all linked to a new node
  891. on the first iteration like the BA model.
  892. Raises
  893. ------
  894. NetworkXError
  895. If `m` does not satisfy ``1 <= m <= n`` or `p` does not
  896. satisfy ``0 <= p <= 1``.
  897. References
  898. ----------
  899. .. [1] P. Holme and B. J. Kim,
  900. "Growing scale-free networks with tunable clustering",
  901. Phys. Rev. E, 65, 026107, 2002.
  902. """
  903. create_using = check_create_using(create_using, directed=False, multigraph=False)
  904. if m < 1 or n < m:
  905. raise nx.NetworkXError(f"NetworkXError must have m>1 and m<n, m={m},n={n}")
  906. if p > 1 or p < 0:
  907. raise nx.NetworkXError(f"NetworkXError p must be in [0,1], p={p}")
  908. G = empty_graph(m, create_using) # add m initial nodes (m0 in barabasi-speak)
  909. repeated_nodes = list(G) # list of existing nodes to sample from
  910. # with nodes repeated once for each adjacent edge
  911. source = m # next node is m
  912. while source < n: # Now add the other n-1 nodes
  913. possible_targets = _random_subset(repeated_nodes, m, seed)
  914. # do one preferential attachment for new node
  915. target = possible_targets.pop()
  916. G.add_edge(source, target)
  917. repeated_nodes.append(target) # add one node to list for each new link
  918. count = 1
  919. while count < m: # add m-1 more new links
  920. if seed.random() < p: # clustering step: add triangle
  921. neighborhood = [
  922. nbr
  923. for nbr in G.neighbors(target)
  924. if not G.has_edge(source, nbr) and nbr != source
  925. ]
  926. if neighborhood: # if there is a neighbor without a link
  927. nbr = seed.choice(neighborhood)
  928. G.add_edge(source, nbr) # add triangle
  929. repeated_nodes.append(nbr)
  930. count = count + 1
  931. continue # go to top of while loop
  932. # else do preferential attachment step if above fails
  933. target = possible_targets.pop()
  934. G.add_edge(source, target)
  935. repeated_nodes.append(target)
  936. count = count + 1
  937. repeated_nodes.extend([source] * m) # add source node to list m times
  938. source += 1
  939. return G
  940. @py_random_state(3)
  941. @nx._dispatchable(graphs=None, returns_graph=True)
  942. def random_lobster_graph(n, p1, p2, seed=None, *, create_using=None):
  943. """Returns a random lobster graph.
  944. A lobster is a tree that reduces to a caterpillar when pruning all
  945. leaf nodes. A caterpillar is a tree that reduces to a path graph
  946. when pruning all leaf nodes; setting `p2` to zero produces a caterpillar.
  947. This implementation iterates on the probabilities `p1` and `p2` to add
  948. edges at levels 1 and 2, respectively. Graphs are therefore constructed
  949. iteratively with uniform randomness at each level rather than being selected
  950. uniformly at random from the set of all possible lobsters.
  951. Parameters
  952. ----------
  953. n : int
  954. The expected number of nodes in the backbone
  955. p1 : float
  956. Probability of adding an edge to the backbone
  957. p2 : float
  958. Probability of adding an edge one level beyond backbone
  959. seed : integer, random_state, or None (default)
  960. Indicator of random number generation state.
  961. See :ref:`Randomness<randomness>`.
  962. create_using : Graph constructor, optional (default=nx.Graph)
  963. Graph type to create. If graph instance, then cleared before populated.
  964. Multigraph and directed types are not supported and raise a ``NetworkXError``.
  965. Raises
  966. ------
  967. NetworkXError
  968. If `p1` or `p2` parameters are >= 1 because the while loops would never finish.
  969. """
  970. create_using = check_create_using(create_using, directed=False, multigraph=False)
  971. p1, p2 = abs(p1), abs(p2)
  972. if any(p >= 1 for p in [p1, p2]):
  973. raise nx.NetworkXError("Probability values for `p1` and `p2` must both be < 1.")
  974. # a necessary ingredient in any self-respecting graph library
  975. llen = int(2 * seed.random() * n + 0.5)
  976. L = path_graph(llen, create_using)
  977. # build caterpillar: add edges to path graph with probability p1
  978. current_node = llen - 1
  979. for n in range(llen):
  980. while seed.random() < p1: # add fuzzy caterpillar parts
  981. current_node += 1
  982. L.add_edge(n, current_node)
  983. cat_node = current_node
  984. while seed.random() < p2: # add crunchy lobster bits
  985. current_node += 1
  986. L.add_edge(cat_node, current_node)
  987. return L # voila, un lobster!
  988. @py_random_state(3)
  989. @nx._dispatchable(graphs=None, returns_graph=True)
  990. def random_lobster(n, p1, p2, seed=None, *, create_using=None):
  991. """
  992. .. deprecated:: 3.5
  993. `random_lobster` is a deprecated alias
  994. for `random_lobster_graph`.
  995. Use `random_lobster_graph` instead.
  996. """
  997. import warnings
  998. warnings.warn(
  999. "`random_lobster` is deprecated, use `random_lobster_graph` instead.",
  1000. category=DeprecationWarning,
  1001. stacklevel=2,
  1002. )
  1003. return random_lobster_graph(n, p1, p2, seed=seed, create_using=create_using)
  1004. @py_random_state(1)
  1005. @nx._dispatchable(graphs=None, returns_graph=True)
  1006. def random_shell_graph(constructor, seed=None, *, create_using=None):
  1007. """Returns a random shell graph for the constructor given.
  1008. Parameters
  1009. ----------
  1010. constructor : list of three-tuples
  1011. Represents the parameters for a shell, starting at the center
  1012. shell. Each element of the list must be of the form `(n, m,
  1013. d)`, where `n` is the number of nodes in the shell, `m` is
  1014. the number of edges in the shell, and `d` is the ratio of
  1015. inter-shell (next) edges to intra-shell edges. If `d` is zero,
  1016. there will be no intra-shell edges, and if `d` is one there
  1017. will be all possible intra-shell edges.
  1018. seed : integer, random_state, or None (default)
  1019. Indicator of random number generation state.
  1020. See :ref:`Randomness<randomness>`.
  1021. create_using : Graph constructor, optional (default=nx.Graph)
  1022. Graph type to create. Graph instances are not supported.
  1023. Multigraph and directed types are not supported and raise a ``NetworkXError``.
  1024. Examples
  1025. --------
  1026. >>> constructor = [(10, 20, 0.8), (20, 40, 0.8)]
  1027. >>> G = nx.random_shell_graph(constructor)
  1028. """
  1029. create_using = check_create_using(create_using, directed=False, multigraph=False)
  1030. G = empty_graph(0, create_using)
  1031. glist = []
  1032. intra_edges = []
  1033. nnodes = 0
  1034. # create gnm graphs for each shell
  1035. for n, m, d in constructor:
  1036. inter_edges = int(m * d)
  1037. intra_edges.append(m - inter_edges)
  1038. g = nx.convert_node_labels_to_integers(
  1039. gnm_random_graph(n, inter_edges, seed=seed, create_using=G.__class__),
  1040. first_label=nnodes,
  1041. )
  1042. glist.append(g)
  1043. nnodes += n
  1044. G = nx.operators.union(G, g)
  1045. # connect the shells randomly
  1046. for gi in range(len(glist) - 1):
  1047. nlist1 = list(glist[gi])
  1048. nlist2 = list(glist[gi + 1])
  1049. total_edges = intra_edges[gi]
  1050. edge_count = 0
  1051. while edge_count < total_edges:
  1052. u = seed.choice(nlist1)
  1053. v = seed.choice(nlist2)
  1054. if u == v or G.has_edge(u, v):
  1055. continue
  1056. else:
  1057. G.add_edge(u, v)
  1058. edge_count = edge_count + 1
  1059. return G
  1060. @py_random_state(2)
  1061. @nx._dispatchable(graphs=None, returns_graph=True)
  1062. def random_powerlaw_tree(n, gamma=3, seed=None, tries=100, *, create_using=None):
  1063. """Returns a tree with a power law degree distribution.
  1064. Parameters
  1065. ----------
  1066. n : int
  1067. The number of nodes.
  1068. gamma : float
  1069. Exponent of the power law.
  1070. seed : integer, random_state, or None (default)
  1071. Indicator of random number generation state.
  1072. See :ref:`Randomness<randomness>`.
  1073. tries : int
  1074. Number of attempts to adjust the sequence to make it a tree.
  1075. create_using : Graph constructor, optional (default=nx.Graph)
  1076. Graph type to create. If graph instance, then cleared before populated.
  1077. Multigraph and directed types are not supported and raise a ``NetworkXError``.
  1078. Raises
  1079. ------
  1080. NetworkXError
  1081. If no valid sequence is found within the maximum number of
  1082. attempts.
  1083. Notes
  1084. -----
  1085. A trial power law degree sequence is chosen and then elements are
  1086. swapped with new elements from a powerlaw distribution until the
  1087. sequence makes a tree (by checking, for example, that the number of
  1088. edges is one smaller than the number of nodes).
  1089. """
  1090. create_using = check_create_using(create_using, directed=False, multigraph=False)
  1091. # This call may raise a NetworkXError if the number of tries is succeeded.
  1092. seq = random_powerlaw_tree_sequence(n, gamma=gamma, seed=seed, tries=tries)
  1093. G = degree_sequence_tree(seq, create_using)
  1094. return G
  1095. @py_random_state(2)
  1096. @nx._dispatchable(graphs=None)
  1097. def random_powerlaw_tree_sequence(n, gamma=3, seed=None, tries=100):
  1098. """Returns a degree sequence for a tree with a power law distribution.
  1099. Parameters
  1100. ----------
  1101. n : int,
  1102. The number of nodes.
  1103. gamma : float
  1104. Exponent of the power law.
  1105. seed : integer, random_state, or None (default)
  1106. Indicator of random number generation state.
  1107. See :ref:`Randomness<randomness>`.
  1108. tries : int
  1109. Number of attempts to adjust the sequence to make it a tree.
  1110. Raises
  1111. ------
  1112. NetworkXError
  1113. If no valid sequence is found within the maximum number of
  1114. attempts.
  1115. Notes
  1116. -----
  1117. A trial power law degree sequence is chosen and then elements are
  1118. swapped with new elements from a power law distribution until
  1119. the sequence makes a tree (by checking, for example, that the number of
  1120. edges is one smaller than the number of nodes).
  1121. """
  1122. # get trial sequence
  1123. z = nx.utils.powerlaw_sequence(n, exponent=gamma, seed=seed)
  1124. # round to integer values in the range [0,n]
  1125. zseq = [min(n, max(round(s), 0)) for s in z]
  1126. # another sequence to swap values from
  1127. z = nx.utils.powerlaw_sequence(tries, exponent=gamma, seed=seed)
  1128. # round to integer values in the range [0,n]
  1129. swap = [min(n, max(round(s), 0)) for s in z]
  1130. for _ in swap:
  1131. valid, _ = nx.utils.is_valid_tree_degree_sequence(zseq)
  1132. if valid:
  1133. return zseq
  1134. index = seed.randint(0, n - 1)
  1135. zseq[index] = swap.pop()
  1136. raise nx.NetworkXError(
  1137. f"Exceeded max ({tries}) attempts for a valid tree sequence."
  1138. )
  1139. @py_random_state(3)
  1140. @nx._dispatchable(graphs=None, returns_graph=True)
  1141. def random_kernel_graph(
  1142. n, kernel_integral, kernel_root=None, seed=None, *, create_using=None
  1143. ):
  1144. r"""Returns an random graph based on the specified kernel.
  1145. The algorithm chooses each of the $[n(n-1)]/2$ possible edges with
  1146. probability specified by a kernel $\kappa(x,y)$ [1]_. The kernel
  1147. $\kappa(x,y)$ must be a symmetric (in $x,y$), non-negative,
  1148. bounded function.
  1149. Parameters
  1150. ----------
  1151. n : int
  1152. The number of nodes
  1153. kernel_integral : function
  1154. Function that returns the definite integral of the kernel $\kappa(x,y)$,
  1155. $F(y,a,b) := \int_a^b \kappa(x,y)dx$
  1156. kernel_root: function (optional)
  1157. Function that returns the root $b$ of the equation $F(y,a,b) = r$.
  1158. If None, the root is found using :func:`scipy.optimize.brentq`
  1159. (this requires SciPy).
  1160. seed : integer, random_state, or None (default)
  1161. Indicator of random number generation state.
  1162. See :ref:`Randomness<randomness>`.
  1163. create_using : Graph constructor, optional (default=nx.Graph)
  1164. Graph type to create. If graph instance, then cleared before populated.
  1165. Multigraph and directed types are not supported and raise a ``NetworkXError``.
  1166. Notes
  1167. -----
  1168. The kernel is specified through its definite integral which must be
  1169. provided as one of the arguments. If the integral and root of the
  1170. kernel integral can be found in $O(1)$ time then this algorithm runs in
  1171. time $O(n+m)$ where m is the expected number of edges [2]_.
  1172. The nodes are set to integers from $0$ to $n-1$.
  1173. Examples
  1174. --------
  1175. Generate an Erdős–Rényi random graph $G(n,c/n)$, with kernel
  1176. $\kappa(x,y)=c$ where $c$ is the mean expected degree.
  1177. >>> def integral(u, w, z):
  1178. ... return c * (z - w)
  1179. >>> def root(u, w, r):
  1180. ... return r / c + w
  1181. >>> c = 1
  1182. >>> graph = nx.random_kernel_graph(1000, integral, root)
  1183. See Also
  1184. --------
  1185. gnp_random_graph
  1186. expected_degree_graph
  1187. References
  1188. ----------
  1189. .. [1] Bollobás, Béla, Janson, S. and Riordan, O.
  1190. "The phase transition in inhomogeneous random graphs",
  1191. *Random Structures Algorithms*, 31, 3--122, 2007.
  1192. .. [2] Hagberg A, Lemons N (2015),
  1193. "Fast Generation of Sparse Random Kernel Graphs".
  1194. PLoS ONE 10(9): e0135177, 2015. doi:10.1371/journal.pone.0135177
  1195. """
  1196. create_using = check_create_using(create_using, directed=False, multigraph=False)
  1197. if kernel_root is None:
  1198. import scipy as sp
  1199. def kernel_root(y, a, r):
  1200. def my_function(b):
  1201. return kernel_integral(y, a, b) - r
  1202. return sp.optimize.brentq(my_function, a, 1)
  1203. graph = nx.empty_graph(create_using=create_using)
  1204. graph.add_nodes_from(range(n))
  1205. (i, j) = (1, 1)
  1206. while i < n:
  1207. r = -math.log(1 - seed.random()) # (1-seed.random()) in (0, 1]
  1208. if kernel_integral(i / n, j / n, 1) <= r:
  1209. i, j = i + 1, i + 1
  1210. else:
  1211. j = math.ceil(n * kernel_root(i / n, j / n, r))
  1212. graph.add_edge(i - 1, j - 1)
  1213. return graph