degree_seq.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. """Generate graphs with a given degree sequence or expected degree sequence.
  2. """
  3. import heapq
  4. import math
  5. from itertools import chain, combinations, zip_longest
  6. from operator import itemgetter
  7. import networkx as nx
  8. from networkx.utils import py_random_state, random_weighted_sample
  9. __all__ = [
  10. "configuration_model",
  11. "directed_configuration_model",
  12. "expected_degree_graph",
  13. "havel_hakimi_graph",
  14. "directed_havel_hakimi_graph",
  15. "degree_sequence_tree",
  16. "random_degree_sequence_graph",
  17. ]
  18. chaini = chain.from_iterable
  19. def _to_stublist(degree_sequence):
  20. """Returns a list of degree-repeated node numbers.
  21. ``degree_sequence`` is a list of nonnegative integers representing
  22. the degrees of nodes in a graph.
  23. This function returns a list of node numbers with multiplicities
  24. according to the given degree sequence. For example, if the first
  25. element of ``degree_sequence`` is ``3``, then the first node number,
  26. ``0``, will appear at the head of the returned list three times. The
  27. node numbers are assumed to be the numbers zero through
  28. ``len(degree_sequence) - 1``.
  29. Examples
  30. --------
  31. >>> degree_sequence = [1, 2, 3]
  32. >>> _to_stublist(degree_sequence)
  33. [0, 1, 1, 2, 2, 2]
  34. If a zero appears in the sequence, that means the node exists but
  35. has degree zero, so that number will be skipped in the returned
  36. list::
  37. >>> degree_sequence = [2, 0, 1]
  38. >>> _to_stublist(degree_sequence)
  39. [0, 0, 2]
  40. """
  41. return list(chaini([n] * d for n, d in enumerate(degree_sequence)))
  42. def _configuration_model(
  43. deg_sequence, create_using, directed=False, in_deg_sequence=None, seed=None
  44. ):
  45. """Helper function for generating either undirected or directed
  46. configuration model graphs.
  47. ``deg_sequence`` is a list of nonnegative integers representing the
  48. degree of the node whose label is the index of the list element.
  49. ``create_using`` see :func:`~networkx.empty_graph`.
  50. ``directed`` and ``in_deg_sequence`` are required if you want the
  51. returned graph to be generated using the directed configuration
  52. model algorithm. If ``directed`` is ``False``, then ``deg_sequence``
  53. is interpreted as the degree sequence of an undirected graph and
  54. ``in_deg_sequence`` is ignored. Otherwise, if ``directed`` is
  55. ``True``, then ``deg_sequence`` is interpreted as the out-degree
  56. sequence and ``in_deg_sequence`` as the in-degree sequence of a
  57. directed graph.
  58. .. note::
  59. ``deg_sequence`` and ``in_deg_sequence`` need not be the same
  60. length.
  61. ``seed`` is a random.Random or numpy.random.RandomState instance
  62. This function returns a graph, directed if and only if ``directed``
  63. is ``True``, generated according to the configuration model
  64. algorithm. For more information on the algorithm, see the
  65. :func:`configuration_model` or :func:`directed_configuration_model`
  66. functions.
  67. """
  68. n = len(deg_sequence)
  69. G = nx.empty_graph(n, create_using)
  70. # If empty, return the null graph immediately.
  71. if n == 0:
  72. return G
  73. # Build a list of available degree-repeated nodes. For example,
  74. # for degree sequence [3, 2, 1, 1, 1], the "stub list" is
  75. # initially [0, 0, 0, 1, 1, 2, 3, 4], that is, node 0 has degree
  76. # 3 and thus is repeated 3 times, etc.
  77. #
  78. # Also, shuffle the stub list in order to get a random sequence of
  79. # node pairs.
  80. if directed:
  81. pairs = zip_longest(deg_sequence, in_deg_sequence, fillvalue=0)
  82. # Unzip the list of pairs into a pair of lists.
  83. out_deg, in_deg = zip(*pairs)
  84. out_stublist = _to_stublist(out_deg)
  85. in_stublist = _to_stublist(in_deg)
  86. seed.shuffle(out_stublist)
  87. seed.shuffle(in_stublist)
  88. else:
  89. stublist = _to_stublist(deg_sequence)
  90. # Choose a random balanced bipartition of the stublist, which
  91. # gives a random pairing of nodes. In this implementation, we
  92. # shuffle the list and then split it in half.
  93. n = len(stublist)
  94. half = n // 2
  95. seed.shuffle(stublist)
  96. out_stublist, in_stublist = stublist[:half], stublist[half:]
  97. G.add_edges_from(zip(out_stublist, in_stublist))
  98. return G
  99. @py_random_state(2)
  100. def configuration_model(deg_sequence, create_using=None, seed=None):
  101. """Returns a random graph with the given degree sequence.
  102. The configuration model generates a random pseudograph (graph with
  103. parallel edges and self loops) by randomly assigning edges to
  104. match the given degree sequence.
  105. Parameters
  106. ----------
  107. deg_sequence : list of nonnegative integers
  108. Each list entry corresponds to the degree of a node.
  109. create_using : NetworkX graph constructor, optional (default MultiGraph)
  110. Graph type to create. If graph instance, then cleared before populated.
  111. seed : integer, random_state, or None (default)
  112. Indicator of random number generation state.
  113. See :ref:`Randomness<randomness>`.
  114. Returns
  115. -------
  116. G : MultiGraph
  117. A graph with the specified degree sequence.
  118. Nodes are labeled starting at 0 with an index
  119. corresponding to the position in deg_sequence.
  120. Raises
  121. ------
  122. NetworkXError
  123. If the degree sequence does not have an even sum.
  124. See Also
  125. --------
  126. is_graphical
  127. Notes
  128. -----
  129. As described by Newman [1]_.
  130. A non-graphical degree sequence (not realizable by some simple
  131. graph) is allowed since this function returns graphs with self
  132. loops and parallel edges. An exception is raised if the degree
  133. sequence does not have an even sum.
  134. This configuration model construction process can lead to
  135. duplicate edges and loops. You can remove the self-loops and
  136. parallel edges (see below) which will likely result in a graph
  137. that doesn't have the exact degree sequence specified.
  138. The density of self-loops and parallel edges tends to decrease as
  139. the number of nodes increases. However, typically the number of
  140. self-loops will approach a Poisson distribution with a nonzero mean,
  141. and similarly for the number of parallel edges. Consider a node
  142. with *k* stubs. The probability of being joined to another stub of
  143. the same node is basically (*k* - *1*) / *N*, where *k* is the
  144. degree and *N* is the number of nodes. So the probability of a
  145. self-loop scales like *c* / *N* for some constant *c*. As *N* grows,
  146. this means we expect *c* self-loops. Similarly for parallel edges.
  147. References
  148. ----------
  149. .. [1] M.E.J. Newman, "The structure and function of complex networks",
  150. SIAM REVIEW 45-2, pp 167-256, 2003.
  151. Examples
  152. --------
  153. You can create a degree sequence following a particular distribution
  154. by using the one of the distribution functions in
  155. :mod:`~networkx.utils.random_sequence` (or one of your own). For
  156. example, to create an undirected multigraph on one hundred nodes
  157. with degree sequence chosen from the power law distribution:
  158. >>> sequence = nx.random_powerlaw_tree_sequence(100, tries=5000)
  159. >>> G = nx.configuration_model(sequence)
  160. >>> len(G)
  161. 100
  162. >>> actual_degrees = [d for v, d in G.degree()]
  163. >>> actual_degrees == sequence
  164. True
  165. The returned graph is a multigraph, which may have parallel
  166. edges. To remove any parallel edges from the returned graph:
  167. >>> G = nx.Graph(G)
  168. Similarly, to remove self-loops:
  169. >>> G.remove_edges_from(nx.selfloop_edges(G))
  170. """
  171. if sum(deg_sequence) % 2 != 0:
  172. msg = "Invalid degree sequence: sum of degrees must be even, not odd"
  173. raise nx.NetworkXError(msg)
  174. G = nx.empty_graph(0, create_using, default=nx.MultiGraph)
  175. if G.is_directed():
  176. raise nx.NetworkXNotImplemented("not implemented for directed graphs")
  177. G = _configuration_model(deg_sequence, G, seed=seed)
  178. return G
  179. @py_random_state(3)
  180. def directed_configuration_model(
  181. in_degree_sequence, out_degree_sequence, create_using=None, seed=None
  182. ):
  183. """Returns a directed_random graph with the given degree sequences.
  184. The configuration model generates a random directed pseudograph
  185. (graph with parallel edges and self loops) by randomly assigning
  186. edges to match the given degree sequences.
  187. Parameters
  188. ----------
  189. in_degree_sequence : list of nonnegative integers
  190. Each list entry corresponds to the in-degree of a node.
  191. out_degree_sequence : list of nonnegative integers
  192. Each list entry corresponds to the out-degree of a node.
  193. create_using : NetworkX graph constructor, optional (default MultiDiGraph)
  194. Graph type to create. If graph instance, then cleared before populated.
  195. seed : integer, random_state, or None (default)
  196. Indicator of random number generation state.
  197. See :ref:`Randomness<randomness>`.
  198. Returns
  199. -------
  200. G : MultiDiGraph
  201. A graph with the specified degree sequences.
  202. Nodes are labeled starting at 0 with an index
  203. corresponding to the position in deg_sequence.
  204. Raises
  205. ------
  206. NetworkXError
  207. If the degree sequences do not have the same sum.
  208. See Also
  209. --------
  210. configuration_model
  211. Notes
  212. -----
  213. Algorithm as described by Newman [1]_.
  214. A non-graphical degree sequence (not realizable by some simple
  215. graph) is allowed since this function returns graphs with self
  216. loops and parallel edges. An exception is raised if the degree
  217. sequences does not have the same sum.
  218. This configuration model construction process can lead to
  219. duplicate edges and loops. You can remove the self-loops and
  220. parallel edges (see below) which will likely result in a graph
  221. that doesn't have the exact degree sequence specified. This
  222. "finite-size effect" decreases as the size of the graph increases.
  223. References
  224. ----------
  225. .. [1] Newman, M. E. J. and Strogatz, S. H. and Watts, D. J.
  226. Random graphs with arbitrary degree distributions and their applications
  227. Phys. Rev. E, 64, 026118 (2001)
  228. Examples
  229. --------
  230. One can modify the in- and out-degree sequences from an existing
  231. directed graph in order to create a new directed graph. For example,
  232. here we modify the directed path graph:
  233. >>> D = nx.DiGraph([(0, 1), (1, 2), (2, 3)])
  234. >>> din = list(d for n, d in D.in_degree())
  235. >>> dout = list(d for n, d in D.out_degree())
  236. >>> din.append(1)
  237. >>> dout[0] = 2
  238. >>> # We now expect an edge from node 0 to a new node, node 3.
  239. ... D = nx.directed_configuration_model(din, dout)
  240. The returned graph is a directed multigraph, which may have parallel
  241. edges. To remove any parallel edges from the returned graph:
  242. >>> D = nx.DiGraph(D)
  243. Similarly, to remove self-loops:
  244. >>> D.remove_edges_from(nx.selfloop_edges(D))
  245. """
  246. if sum(in_degree_sequence) != sum(out_degree_sequence):
  247. msg = "Invalid degree sequences: sequences must have equal sums"
  248. raise nx.NetworkXError(msg)
  249. if create_using is None:
  250. create_using = nx.MultiDiGraph
  251. G = _configuration_model(
  252. out_degree_sequence,
  253. create_using,
  254. directed=True,
  255. in_deg_sequence=in_degree_sequence,
  256. seed=seed,
  257. )
  258. name = "directed configuration_model {} nodes {} edges"
  259. return G
  260. @py_random_state(1)
  261. def expected_degree_graph(w, seed=None, selfloops=True):
  262. r"""Returns a random graph with given expected degrees.
  263. Given a sequence of expected degrees $W=(w_0,w_1,\ldots,w_{n-1})$
  264. of length $n$ this algorithm assigns an edge between node $u$ and
  265. node $v$ with probability
  266. .. math::
  267. p_{uv} = \frac{w_u w_v}{\sum_k w_k} .
  268. Parameters
  269. ----------
  270. w : list
  271. The list of expected degrees.
  272. selfloops: bool (default=True)
  273. Set to False to remove the possibility of self-loop edges.
  274. seed : integer, random_state, or None (default)
  275. Indicator of random number generation state.
  276. See :ref:`Randomness<randomness>`.
  277. Returns
  278. -------
  279. Graph
  280. Examples
  281. --------
  282. >>> z = [10 for i in range(100)]
  283. >>> G = nx.expected_degree_graph(z)
  284. Notes
  285. -----
  286. The nodes have integer labels corresponding to index of expected degrees
  287. input sequence.
  288. The complexity of this algorithm is $\mathcal{O}(n+m)$ where $n$ is the
  289. number of nodes and $m$ is the expected number of edges.
  290. The model in [1]_ includes the possibility of self-loop edges.
  291. Set selfloops=False to produce a graph without self loops.
  292. For finite graphs this model doesn't produce exactly the given
  293. expected degree sequence. Instead the expected degrees are as
  294. follows.
  295. For the case without self loops (selfloops=False),
  296. .. math::
  297. E[deg(u)] = \sum_{v \ne u} p_{uv}
  298. = w_u \left( 1 - \frac{w_u}{\sum_k w_k} \right) .
  299. NetworkX uses the standard convention that a self-loop edge counts 2
  300. in the degree of a node, so with self loops (selfloops=True),
  301. .. math::
  302. E[deg(u)] = \sum_{v \ne u} p_{uv} + 2 p_{uu}
  303. = w_u \left( 1 + \frac{w_u}{\sum_k w_k} \right) .
  304. References
  305. ----------
  306. .. [1] Fan Chung and L. Lu, Connected components in random graphs with
  307. given expected degree sequences, Ann. Combinatorics, 6,
  308. pp. 125-145, 2002.
  309. .. [2] Joel Miller and Aric Hagberg,
  310. Efficient generation of networks with given expected degrees,
  311. in Algorithms and Models for the Web-Graph (WAW 2011),
  312. Alan Frieze, Paul Horn, and Paweł Prałat (Eds), LNCS 6732,
  313. pp. 115-126, 2011.
  314. """
  315. n = len(w)
  316. G = nx.empty_graph(n)
  317. # If there are no nodes are no edges in the graph, return the empty graph.
  318. if n == 0 or max(w) == 0:
  319. return G
  320. rho = 1 / sum(w)
  321. # Sort the weights in decreasing order. The original order of the
  322. # weights dictates the order of the (integer) node labels, so we
  323. # need to remember the permutation applied in the sorting.
  324. order = sorted(enumerate(w), key=itemgetter(1), reverse=True)
  325. mapping = {c: u for c, (u, v) in enumerate(order)}
  326. seq = [v for u, v in order]
  327. last = n
  328. if not selfloops:
  329. last -= 1
  330. for u in range(last):
  331. v = u
  332. if not selfloops:
  333. v += 1
  334. factor = seq[u] * rho
  335. p = min(seq[v] * factor, 1)
  336. while v < n and p > 0:
  337. if p != 1:
  338. r = seed.random()
  339. v += math.floor(math.log(r, 1 - p))
  340. if v < n:
  341. q = min(seq[v] * factor, 1)
  342. if seed.random() < q / p:
  343. G.add_edge(mapping[u], mapping[v])
  344. v += 1
  345. p = q
  346. return G
  347. def havel_hakimi_graph(deg_sequence, create_using=None):
  348. """Returns a simple graph with given degree sequence constructed
  349. using the Havel-Hakimi algorithm.
  350. Parameters
  351. ----------
  352. deg_sequence: list of integers
  353. Each integer corresponds to the degree of a node (need not be sorted).
  354. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  355. Graph type to create. If graph instance, then cleared before populated.
  356. Directed graphs are not allowed.
  357. Raises
  358. ------
  359. NetworkXException
  360. For a non-graphical degree sequence (i.e. one
  361. not realizable by some simple graph).
  362. Notes
  363. -----
  364. The Havel-Hakimi algorithm constructs a simple graph by
  365. successively connecting the node of highest degree to other nodes
  366. of highest degree, resorting remaining nodes by degree, and
  367. repeating the process. The resulting graph has a high
  368. degree-associativity. Nodes are labeled 1,.., len(deg_sequence),
  369. corresponding to their position in deg_sequence.
  370. The basic algorithm is from Hakimi [1]_ and was generalized by
  371. Kleitman and Wang [2]_.
  372. References
  373. ----------
  374. .. [1] Hakimi S., On Realizability of a Set of Integers as
  375. Degrees of the Vertices of a Linear Graph. I,
  376. Journal of SIAM, 10(3), pp. 496-506 (1962)
  377. .. [2] Kleitman D.J. and Wang D.L.
  378. Algorithms for Constructing Graphs and Digraphs with Given Valences
  379. and Factors Discrete Mathematics, 6(1), pp. 79-88 (1973)
  380. """
  381. if not nx.is_graphical(deg_sequence):
  382. raise nx.NetworkXError("Invalid degree sequence")
  383. p = len(deg_sequence)
  384. G = nx.empty_graph(p, create_using)
  385. if G.is_directed():
  386. raise nx.NetworkXError("Directed graphs are not supported")
  387. num_degs = [[] for i in range(p)]
  388. dmax, dsum, n = 0, 0, 0
  389. for d in deg_sequence:
  390. # Process only the non-zero integers
  391. if d > 0:
  392. num_degs[d].append(n)
  393. dmax, dsum, n = max(dmax, d), dsum + d, n + 1
  394. # Return graph if no edges
  395. if n == 0:
  396. return G
  397. modstubs = [(0, 0)] * (dmax + 1)
  398. # Successively reduce degree sequence by removing the maximum degree
  399. while n > 0:
  400. # Retrieve the maximum degree in the sequence
  401. while len(num_degs[dmax]) == 0:
  402. dmax -= 1
  403. # If there are not enough stubs to connect to, then the sequence is
  404. # not graphical
  405. if dmax > n - 1:
  406. raise nx.NetworkXError("Non-graphical integer sequence")
  407. # Remove largest stub in list
  408. source = num_degs[dmax].pop()
  409. n -= 1
  410. # Reduce the next dmax largest stubs
  411. mslen = 0
  412. k = dmax
  413. for i in range(dmax):
  414. while len(num_degs[k]) == 0:
  415. k -= 1
  416. target = num_degs[k].pop()
  417. G.add_edge(source, target)
  418. n -= 1
  419. if k > 1:
  420. modstubs[mslen] = (k - 1, target)
  421. mslen += 1
  422. # Add back to the list any nonzero stubs that were removed
  423. for i in range(mslen):
  424. (stubval, stubtarget) = modstubs[i]
  425. num_degs[stubval].append(stubtarget)
  426. n += 1
  427. return G
  428. def directed_havel_hakimi_graph(in_deg_sequence, out_deg_sequence, create_using=None):
  429. """Returns a directed graph with the given degree sequences.
  430. Parameters
  431. ----------
  432. in_deg_sequence : list of integers
  433. Each list entry corresponds to the in-degree of a node.
  434. out_deg_sequence : list of integers
  435. Each list entry corresponds to the out-degree of a node.
  436. create_using : NetworkX graph constructor, optional (default DiGraph)
  437. Graph type to create. If graph instance, then cleared before populated.
  438. Returns
  439. -------
  440. G : DiGraph
  441. A graph with the specified degree sequences.
  442. Nodes are labeled starting at 0 with an index
  443. corresponding to the position in deg_sequence
  444. Raises
  445. ------
  446. NetworkXError
  447. If the degree sequences are not digraphical.
  448. See Also
  449. --------
  450. configuration_model
  451. Notes
  452. -----
  453. Algorithm as described by Kleitman and Wang [1]_.
  454. References
  455. ----------
  456. .. [1] D.J. Kleitman and D.L. Wang
  457. Algorithms for Constructing Graphs and Digraphs with Given Valences
  458. and Factors Discrete Mathematics, 6(1), pp. 79-88 (1973)
  459. """
  460. in_deg_sequence = nx.utils.make_list_of_ints(in_deg_sequence)
  461. out_deg_sequence = nx.utils.make_list_of_ints(out_deg_sequence)
  462. # Process the sequences and form two heaps to store degree pairs with
  463. # either zero or nonzero out degrees
  464. sumin, sumout = 0, 0
  465. nin, nout = len(in_deg_sequence), len(out_deg_sequence)
  466. maxn = max(nin, nout)
  467. G = nx.empty_graph(maxn, create_using, default=nx.DiGraph)
  468. if maxn == 0:
  469. return G
  470. maxin = 0
  471. stubheap, zeroheap = [], []
  472. for n in range(maxn):
  473. in_deg, out_deg = 0, 0
  474. if n < nout:
  475. out_deg = out_deg_sequence[n]
  476. if n < nin:
  477. in_deg = in_deg_sequence[n]
  478. if in_deg < 0 or out_deg < 0:
  479. raise nx.NetworkXError(
  480. "Invalid degree sequences. Sequence values must be positive."
  481. )
  482. sumin, sumout, maxin = sumin + in_deg, sumout + out_deg, max(maxin, in_deg)
  483. if in_deg > 0:
  484. stubheap.append((-1 * out_deg, -1 * in_deg, n))
  485. elif out_deg > 0:
  486. zeroheap.append((-1 * out_deg, n))
  487. if sumin != sumout:
  488. raise nx.NetworkXError(
  489. "Invalid degree sequences. Sequences must have equal sums."
  490. )
  491. heapq.heapify(stubheap)
  492. heapq.heapify(zeroheap)
  493. modstubs = [(0, 0, 0)] * (maxin + 1)
  494. # Successively reduce degree sequence by removing the maximum
  495. while stubheap:
  496. # Remove first value in the sequence with a non-zero in degree
  497. (freeout, freein, target) = heapq.heappop(stubheap)
  498. freein *= -1
  499. if freein > len(stubheap) + len(zeroheap):
  500. raise nx.NetworkXError("Non-digraphical integer sequence")
  501. # Attach arcs from the nodes with the most stubs
  502. mslen = 0
  503. for i in range(freein):
  504. if zeroheap and (not stubheap or stubheap[0][0] > zeroheap[0][0]):
  505. (stubout, stubsource) = heapq.heappop(zeroheap)
  506. stubin = 0
  507. else:
  508. (stubout, stubin, stubsource) = heapq.heappop(stubheap)
  509. if stubout == 0:
  510. raise nx.NetworkXError("Non-digraphical integer sequence")
  511. G.add_edge(stubsource, target)
  512. # Check if source is now totally connected
  513. if stubout + 1 < 0 or stubin < 0:
  514. modstubs[mslen] = (stubout + 1, stubin, stubsource)
  515. mslen += 1
  516. # Add the nodes back to the heaps that still have available stubs
  517. for i in range(mslen):
  518. stub = modstubs[i]
  519. if stub[1] < 0:
  520. heapq.heappush(stubheap, stub)
  521. else:
  522. heapq.heappush(zeroheap, (stub[0], stub[2]))
  523. if freeout < 0:
  524. heapq.heappush(zeroheap, (freeout, target))
  525. return G
  526. def degree_sequence_tree(deg_sequence, create_using=None):
  527. """Make a tree for the given degree sequence.
  528. A tree has #nodes-#edges=1 so
  529. the degree sequence must have
  530. len(deg_sequence)-sum(deg_sequence)/2=1
  531. """
  532. # The sum of the degree sequence must be even (for any undirected graph).
  533. degree_sum = sum(deg_sequence)
  534. if degree_sum % 2 != 0:
  535. msg = "Invalid degree sequence: sum of degrees must be even, not odd"
  536. raise nx.NetworkXError(msg)
  537. if len(deg_sequence) - degree_sum // 2 != 1:
  538. msg = (
  539. "Invalid degree sequence: tree must have number of nodes equal"
  540. " to one less than the number of edges"
  541. )
  542. raise nx.NetworkXError(msg)
  543. G = nx.empty_graph(0, create_using)
  544. if G.is_directed():
  545. raise nx.NetworkXError("Directed Graph not supported")
  546. # Sort all degrees greater than 1 in decreasing order.
  547. #
  548. # TODO Does this need to be sorted in reverse order?
  549. deg = sorted((s for s in deg_sequence if s > 1), reverse=True)
  550. # make path graph as backbone
  551. n = len(deg) + 2
  552. nx.add_path(G, range(n))
  553. last = n
  554. # add the leaves
  555. for source in range(1, n - 1):
  556. nedges = deg.pop() - 2
  557. for target in range(last, last + nedges):
  558. G.add_edge(source, target)
  559. last += nedges
  560. # in case we added one too many
  561. if len(G) > len(deg_sequence):
  562. G.remove_node(0)
  563. return G
  564. @py_random_state(1)
  565. def random_degree_sequence_graph(sequence, seed=None, tries=10):
  566. r"""Returns a simple random graph with the given degree sequence.
  567. If the maximum degree $d_m$ in the sequence is $O(m^{1/4})$ then the
  568. algorithm produces almost uniform random graphs in $O(m d_m)$ time
  569. where $m$ is the number of edges.
  570. Parameters
  571. ----------
  572. sequence : list of integers
  573. Sequence of degrees
  574. seed : integer, random_state, or None (default)
  575. Indicator of random number generation state.
  576. See :ref:`Randomness<randomness>`.
  577. tries : int, optional
  578. Maximum number of tries to create a graph
  579. Returns
  580. -------
  581. G : Graph
  582. A graph with the specified degree sequence.
  583. Nodes are labeled starting at 0 with an index
  584. corresponding to the position in the sequence.
  585. Raises
  586. ------
  587. NetworkXUnfeasible
  588. If the degree sequence is not graphical.
  589. NetworkXError
  590. If a graph is not produced in specified number of tries
  591. See Also
  592. --------
  593. is_graphical, configuration_model
  594. Notes
  595. -----
  596. The generator algorithm [1]_ is not guaranteed to produce a graph.
  597. References
  598. ----------
  599. .. [1] Moshen Bayati, Jeong Han Kim, and Amin Saberi,
  600. A sequential algorithm for generating random graphs.
  601. Algorithmica, Volume 58, Number 4, 860-910,
  602. DOI: 10.1007/s00453-009-9340-1
  603. Examples
  604. --------
  605. >>> sequence = [1, 2, 2, 3]
  606. >>> G = nx.random_degree_sequence_graph(sequence, seed=42)
  607. >>> sorted(d for n, d in G.degree())
  608. [1, 2, 2, 3]
  609. """
  610. DSRG = DegreeSequenceRandomGraph(sequence, seed)
  611. for try_n in range(tries):
  612. try:
  613. return DSRG.generate()
  614. except nx.NetworkXUnfeasible:
  615. pass
  616. raise nx.NetworkXError(f"failed to generate graph in {tries} tries")
  617. class DegreeSequenceRandomGraph:
  618. # class to generate random graphs with a given degree sequence
  619. # use random_degree_sequence_graph()
  620. def __init__(self, degree, rng):
  621. if not nx.is_graphical(degree):
  622. raise nx.NetworkXUnfeasible("degree sequence is not graphical")
  623. self.rng = rng
  624. self.degree = list(degree)
  625. # node labels are integers 0,...,n-1
  626. self.m = sum(self.degree) / 2.0 # number of edges
  627. try:
  628. self.dmax = max(self.degree) # maximum degree
  629. except ValueError:
  630. self.dmax = 0
  631. def generate(self):
  632. # remaining_degree is mapping from int->remaining degree
  633. self.remaining_degree = dict(enumerate(self.degree))
  634. # add all nodes to make sure we get isolated nodes
  635. self.graph = nx.Graph()
  636. self.graph.add_nodes_from(self.remaining_degree)
  637. # remove zero degree nodes
  638. for n, d in list(self.remaining_degree.items()):
  639. if d == 0:
  640. del self.remaining_degree[n]
  641. if len(self.remaining_degree) > 0:
  642. # build graph in three phases according to how many unmatched edges
  643. self.phase1()
  644. self.phase2()
  645. self.phase3()
  646. return self.graph
  647. def update_remaining(self, u, v, aux_graph=None):
  648. # decrement remaining nodes, modify auxiliary graph if in phase3
  649. if aux_graph is not None:
  650. # remove edges from auxiliary graph
  651. aux_graph.remove_edge(u, v)
  652. if self.remaining_degree[u] == 1:
  653. del self.remaining_degree[u]
  654. if aux_graph is not None:
  655. aux_graph.remove_node(u)
  656. else:
  657. self.remaining_degree[u] -= 1
  658. if self.remaining_degree[v] == 1:
  659. del self.remaining_degree[v]
  660. if aux_graph is not None:
  661. aux_graph.remove_node(v)
  662. else:
  663. self.remaining_degree[v] -= 1
  664. def p(self, u, v):
  665. # degree probability
  666. return 1 - self.degree[u] * self.degree[v] / (4.0 * self.m)
  667. def q(self, u, v):
  668. # remaining degree probability
  669. norm = max(self.remaining_degree.values()) ** 2
  670. return self.remaining_degree[u] * self.remaining_degree[v] / norm
  671. def suitable_edge(self):
  672. """Returns True if and only if an arbitrary remaining node can
  673. potentially be joined with some other remaining node.
  674. """
  675. nodes = iter(self.remaining_degree)
  676. u = next(nodes)
  677. return any(v not in self.graph[u] for v in nodes)
  678. def phase1(self):
  679. # choose node pairs from (degree) weighted distribution
  680. rem_deg = self.remaining_degree
  681. while sum(rem_deg.values()) >= 2 * self.dmax**2:
  682. u, v = sorted(random_weighted_sample(rem_deg, 2, self.rng))
  683. if self.graph.has_edge(u, v):
  684. continue
  685. if self.rng.random() < self.p(u, v): # accept edge
  686. self.graph.add_edge(u, v)
  687. self.update_remaining(u, v)
  688. def phase2(self):
  689. # choose remaining nodes uniformly at random and use rejection sampling
  690. remaining_deg = self.remaining_degree
  691. rng = self.rng
  692. while len(remaining_deg) >= 2 * self.dmax:
  693. while True:
  694. u, v = sorted(rng.sample(list(remaining_deg.keys()), 2))
  695. if self.graph.has_edge(u, v):
  696. continue
  697. if rng.random() < self.q(u, v):
  698. break
  699. if rng.random() < self.p(u, v): # accept edge
  700. self.graph.add_edge(u, v)
  701. self.update_remaining(u, v)
  702. def phase3(self):
  703. # build potential remaining edges and choose with rejection sampling
  704. potential_edges = combinations(self.remaining_degree, 2)
  705. # build auxiliary graph of potential edges not already in graph
  706. H = nx.Graph(
  707. [(u, v) for (u, v) in potential_edges if not self.graph.has_edge(u, v)]
  708. )
  709. rng = self.rng
  710. while self.remaining_degree:
  711. if not self.suitable_edge():
  712. raise nx.NetworkXUnfeasible("no suitable edges left")
  713. while True:
  714. u, v = sorted(rng.choice(list(H.edges())))
  715. if rng.random() < self.q(u, v):
  716. break
  717. if rng.random() < self.p(u, v): # accept edge
  718. self.graph.add_edge(u, v)
  719. self.update_remaining(u, v, aux_graph=H)