degree_seq.py 30 KB

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