random_graphs.py 43 KB

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