Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.9/dist-packages/networkx/generators/degree_seq.py: 13%

290 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-10-20 07:00 +0000

1"""Generate graphs with a given degree sequence or expected degree sequence. 

2""" 

3 

4import heapq 

5import math 

6from itertools import chain, combinations, zip_longest 

7from operator import itemgetter 

8 

9import networkx as nx 

10from networkx.utils import py_random_state, random_weighted_sample 

11 

12__all__ = [ 

13 "configuration_model", 

14 "directed_configuration_model", 

15 "expected_degree_graph", 

16 "havel_hakimi_graph", 

17 "directed_havel_hakimi_graph", 

18 "degree_sequence_tree", 

19 "random_degree_sequence_graph", 

20] 

21 

22chaini = chain.from_iterable 

23 

24 

25def _to_stublist(degree_sequence): 

26 """Returns a list of degree-repeated node numbers. 

27 

28 ``degree_sequence`` is a list of nonnegative integers representing 

29 the degrees of nodes in a graph. 

30 

31 This function returns a list of node numbers with multiplicities 

32 according to the given degree sequence. For example, if the first 

33 element of ``degree_sequence`` is ``3``, then the first node number, 

34 ``0``, will appear at the head of the returned list three times. The 

35 node numbers are assumed to be the numbers zero through 

36 ``len(degree_sequence) - 1``. 

37 

38 Examples 

39 -------- 

40 

41 >>> degree_sequence = [1, 2, 3] 

42 >>> _to_stublist(degree_sequence) 

43 [0, 1, 1, 2, 2, 2] 

44 

45 If a zero appears in the sequence, that means the node exists but 

46 has degree zero, so that number will be skipped in the returned 

47 list:: 

48 

49 >>> degree_sequence = [2, 0, 1] 

50 >>> _to_stublist(degree_sequence) 

51 [0, 0, 2] 

52 

53 """ 

54 return list(chaini([n] * d for n, d in enumerate(degree_sequence))) 

55 

56 

57def _configuration_model( 

58 deg_sequence, create_using, directed=False, in_deg_sequence=None, seed=None 

59): 

60 """Helper function for generating either undirected or directed 

61 configuration model graphs. 

62 

63 ``deg_sequence`` is a list of nonnegative integers representing the 

64 degree of the node whose label is the index of the list element. 

65 

66 ``create_using`` see :func:`~networkx.empty_graph`. 

67 

68 ``directed`` and ``in_deg_sequence`` are required if you want the 

69 returned graph to be generated using the directed configuration 

70 model algorithm. If ``directed`` is ``False``, then ``deg_sequence`` 

71 is interpreted as the degree sequence of an undirected graph and 

72 ``in_deg_sequence`` is ignored. Otherwise, if ``directed`` is 

73 ``True``, then ``deg_sequence`` is interpreted as the out-degree 

74 sequence and ``in_deg_sequence`` as the in-degree sequence of a 

75 directed graph. 

76 

77 .. note:: 

78 

79 ``deg_sequence`` and ``in_deg_sequence`` need not be the same 

80 length. 

81 

82 ``seed`` is a random.Random or numpy.random.RandomState instance 

83 

84 This function returns a graph, directed if and only if ``directed`` 

85 is ``True``, generated according to the configuration model 

86 algorithm. For more information on the algorithm, see the 

87 :func:`configuration_model` or :func:`directed_configuration_model` 

88 functions. 

89 

90 """ 

91 n = len(deg_sequence) 

92 G = nx.empty_graph(n, create_using) 

93 # If empty, return the null graph immediately. 

94 if n == 0: 

95 return G 

96 # Build a list of available degree-repeated nodes. For example, 

97 # for degree sequence [3, 2, 1, 1, 1], the "stub list" is 

98 # initially [0, 0, 0, 1, 1, 2, 3, 4], that is, node 0 has degree 

99 # 3 and thus is repeated 3 times, etc. 

100 # 

101 # Also, shuffle the stub list in order to get a random sequence of 

102 # node pairs. 

103 if directed: 

104 pairs = zip_longest(deg_sequence, in_deg_sequence, fillvalue=0) 

105 # Unzip the list of pairs into a pair of lists. 

106 out_deg, in_deg = zip(*pairs) 

107 

108 out_stublist = _to_stublist(out_deg) 

109 in_stublist = _to_stublist(in_deg) 

110 

111 seed.shuffle(out_stublist) 

112 seed.shuffle(in_stublist) 

113 else: 

114 stublist = _to_stublist(deg_sequence) 

115 # Choose a random balanced bipartition of the stublist, which 

116 # gives a random pairing of nodes. In this implementation, we 

117 # shuffle the list and then split it in half. 

118 n = len(stublist) 

119 half = n // 2 

120 seed.shuffle(stublist) 

121 out_stublist, in_stublist = stublist[:half], stublist[half:] 

122 G.add_edges_from(zip(out_stublist, in_stublist)) 

123 return G 

124 

125 

126@py_random_state(2) 

127@nx._dispatch(graphs=None) 

128def configuration_model(deg_sequence, create_using=None, seed=None): 

129 """Returns a random graph with the given degree sequence. 

130 

131 The configuration model generates a random pseudograph (graph with 

132 parallel edges and self loops) by randomly assigning edges to 

133 match the given degree sequence. 

134 

135 Parameters 

136 ---------- 

137 deg_sequence : list of nonnegative integers 

138 Each list entry corresponds to the degree of a node. 

139 create_using : NetworkX graph constructor, optional (default MultiGraph) 

140 Graph type to create. If graph instance, then cleared before populated. 

141 seed : integer, random_state, or None (default) 

142 Indicator of random number generation state. 

143 See :ref:`Randomness<randomness>`. 

144 

145 Returns 

146 ------- 

147 G : MultiGraph 

148 A graph with the specified degree sequence. 

149 Nodes are labeled starting at 0 with an index 

150 corresponding to the position in deg_sequence. 

151 

152 Raises 

153 ------ 

154 NetworkXError 

155 If the degree sequence does not have an even sum. 

156 

157 See Also 

158 -------- 

159 is_graphical 

160 

161 Notes 

162 ----- 

163 As described by Newman [1]_. 

164 

165 A non-graphical degree sequence (not realizable by some simple 

166 graph) is allowed since this function returns graphs with self 

167 loops and parallel edges. An exception is raised if the degree 

168 sequence does not have an even sum. 

169 

170 This configuration model construction process can lead to 

171 duplicate edges and loops. You can remove the self-loops and 

172 parallel edges (see below) which will likely result in a graph 

173 that doesn't have the exact degree sequence specified. 

174 

175 The density of self-loops and parallel edges tends to decrease as 

176 the number of nodes increases. However, typically the number of 

177 self-loops will approach a Poisson distribution with a nonzero mean, 

178 and similarly for the number of parallel edges. Consider a node 

179 with *k* stubs. The probability of being joined to another stub of 

180 the same node is basically (*k* - *1*) / *N*, where *k* is the 

181 degree and *N* is the number of nodes. So the probability of a 

182 self-loop scales like *c* / *N* for some constant *c*. As *N* grows, 

183 this means we expect *c* self-loops. Similarly for parallel edges. 

184 

185 References 

186 ---------- 

187 .. [1] M.E.J. Newman, "The structure and function of complex networks", 

188 SIAM REVIEW 45-2, pp 167-256, 2003. 

189 

190 Examples 

191 -------- 

192 You can create a degree sequence following a particular distribution 

193 by using the one of the distribution functions in 

194 :mod:`~networkx.utils.random_sequence` (or one of your own). For 

195 example, to create an undirected multigraph on one hundred nodes 

196 with degree sequence chosen from the power law distribution: 

197 

198 >>> sequence = nx.random_powerlaw_tree_sequence(100, tries=5000) 

199 >>> G = nx.configuration_model(sequence) 

200 >>> len(G) 

201 100 

202 >>> actual_degrees = [d for v, d in G.degree()] 

203 >>> actual_degrees == sequence 

204 True 

205 

206 The returned graph is a multigraph, which may have parallel 

207 edges. To remove any parallel edges from the returned graph: 

208 

209 >>> G = nx.Graph(G) 

210 

211 Similarly, to remove self-loops: 

212 

213 >>> G.remove_edges_from(nx.selfloop_edges(G)) 

214 

215 """ 

216 if sum(deg_sequence) % 2 != 0: 

217 msg = "Invalid degree sequence: sum of degrees must be even, not odd" 

218 raise nx.NetworkXError(msg) 

219 

220 G = nx.empty_graph(0, create_using, default=nx.MultiGraph) 

221 if G.is_directed(): 

222 raise nx.NetworkXNotImplemented("not implemented for directed graphs") 

223 

224 G = _configuration_model(deg_sequence, G, seed=seed) 

225 

226 return G 

227 

228 

229@py_random_state(3) 

230@nx._dispatch(graphs=None) 

231def directed_configuration_model( 

232 in_degree_sequence, out_degree_sequence, create_using=None, seed=None 

233): 

234 """Returns a directed_random graph with the given degree sequences. 

235 

236 The configuration model generates a random directed pseudograph 

237 (graph with parallel edges and self loops) by randomly assigning 

238 edges to match the given degree sequences. 

239 

240 Parameters 

241 ---------- 

242 in_degree_sequence : list of nonnegative integers 

243 Each list entry corresponds to the in-degree of a node. 

244 out_degree_sequence : list of nonnegative integers 

245 Each list entry corresponds to the out-degree of a node. 

246 create_using : NetworkX graph constructor, optional (default MultiDiGraph) 

247 Graph type to create. If graph instance, then cleared before populated. 

248 seed : integer, random_state, or None (default) 

249 Indicator of random number generation state. 

250 See :ref:`Randomness<randomness>`. 

251 

252 Returns 

253 ------- 

254 G : MultiDiGraph 

255 A graph with the specified degree sequences. 

256 Nodes are labeled starting at 0 with an index 

257 corresponding to the position in deg_sequence. 

258 

259 Raises 

260 ------ 

261 NetworkXError 

262 If the degree sequences do not have the same sum. 

263 

264 See Also 

265 -------- 

266 configuration_model 

267 

268 Notes 

269 ----- 

270 Algorithm as described by Newman [1]_. 

271 

272 A non-graphical degree sequence (not realizable by some simple 

273 graph) is allowed since this function returns graphs with self 

274 loops and parallel edges. An exception is raised if the degree 

275 sequences does not have the same sum. 

276 

277 This configuration model construction process can lead to 

278 duplicate edges and loops. You can remove the self-loops and 

279 parallel edges (see below) which will likely result in a graph 

280 that doesn't have the exact degree sequence specified. This 

281 "finite-size effect" decreases as the size of the graph increases. 

282 

283 References 

284 ---------- 

285 .. [1] Newman, M. E. J. and Strogatz, S. H. and Watts, D. J. 

286 Random graphs with arbitrary degree distributions and their applications 

287 Phys. Rev. E, 64, 026118 (2001) 

288 

289 Examples 

290 -------- 

291 One can modify the in- and out-degree sequences from an existing 

292 directed graph in order to create a new directed graph. For example, 

293 here we modify the directed path graph: 

294 

295 >>> D = nx.DiGraph([(0, 1), (1, 2), (2, 3)]) 

296 >>> din = list(d for n, d in D.in_degree()) 

297 >>> dout = list(d for n, d in D.out_degree()) 

298 >>> din.append(1) 

299 >>> dout[0] = 2 

300 >>> # We now expect an edge from node 0 to a new node, node 3. 

301 ... D = nx.directed_configuration_model(din, dout) 

302 

303 The returned graph is a directed multigraph, which may have parallel 

304 edges. To remove any parallel edges from the returned graph: 

305 

306 >>> D = nx.DiGraph(D) 

307 

308 Similarly, to remove self-loops: 

309 

310 >>> D.remove_edges_from(nx.selfloop_edges(D)) 

311 

312 """ 

313 if sum(in_degree_sequence) != sum(out_degree_sequence): 

314 msg = "Invalid degree sequences: sequences must have equal sums" 

315 raise nx.NetworkXError(msg) 

316 

317 if create_using is None: 

318 create_using = nx.MultiDiGraph 

319 

320 G = _configuration_model( 

321 out_degree_sequence, 

322 create_using, 

323 directed=True, 

324 in_deg_sequence=in_degree_sequence, 

325 seed=seed, 

326 ) 

327 

328 name = "directed configuration_model {} nodes {} edges" 

329 return G 

330 

331 

332@py_random_state(1) 

333@nx._dispatch(graphs=None) 

334def expected_degree_graph(w, seed=None, selfloops=True): 

335 r"""Returns a random graph with given expected degrees. 

336 

337 Given a sequence of expected degrees $W=(w_0,w_1,\ldots,w_{n-1})$ 

338 of length $n$ this algorithm assigns an edge between node $u$ and 

339 node $v$ with probability 

340 

341 .. math:: 

342 

343 p_{uv} = \frac{w_u w_v}{\sum_k w_k} . 

344 

345 Parameters 

346 ---------- 

347 w : list 

348 The list of expected degrees. 

349 selfloops: bool (default=True) 

350 Set to False to remove the possibility of self-loop edges. 

351 seed : integer, random_state, or None (default) 

352 Indicator of random number generation state. 

353 See :ref:`Randomness<randomness>`. 

354 

355 Returns 

356 ------- 

357 Graph 

358 

359 Examples 

360 -------- 

361 >>> z = [10 for i in range(100)] 

362 >>> G = nx.expected_degree_graph(z) 

363 

364 Notes 

365 ----- 

366 The nodes have integer labels corresponding to index of expected degrees 

367 input sequence. 

368 

369 The complexity of this algorithm is $\mathcal{O}(n+m)$ where $n$ is the 

370 number of nodes and $m$ is the expected number of edges. 

371 

372 The model in [1]_ includes the possibility of self-loop edges. 

373 Set selfloops=False to produce a graph without self loops. 

374 

375 For finite graphs this model doesn't produce exactly the given 

376 expected degree sequence. Instead the expected degrees are as 

377 follows. 

378 

379 For the case without self loops (selfloops=False), 

380 

381 .. math:: 

382 

383 E[deg(u)] = \sum_{v \ne u} p_{uv} 

384 = w_u \left( 1 - \frac{w_u}{\sum_k w_k} \right) . 

385 

386 

387 NetworkX uses the standard convention that a self-loop edge counts 2 

388 in the degree of a node, so with self loops (selfloops=True), 

389 

390 .. math:: 

391 

392 E[deg(u)] = \sum_{v \ne u} p_{uv} + 2 p_{uu} 

393 = w_u \left( 1 + \frac{w_u}{\sum_k w_k} \right) . 

394 

395 References 

396 ---------- 

397 .. [1] Fan Chung and L. Lu, Connected components in random graphs with 

398 given expected degree sequences, Ann. Combinatorics, 6, 

399 pp. 125-145, 2002. 

400 .. [2] Joel Miller and Aric Hagberg, 

401 Efficient generation of networks with given expected degrees, 

402 in Algorithms and Models for the Web-Graph (WAW 2011), 

403 Alan Frieze, Paul Horn, and Paweł Prałat (Eds), LNCS 6732, 

404 pp. 115-126, 2011. 

405 """ 

406 n = len(w) 

407 G = nx.empty_graph(n) 

408 

409 # If there are no nodes are no edges in the graph, return the empty graph. 

410 if n == 0 or max(w) == 0: 

411 return G 

412 

413 rho = 1 / sum(w) 

414 # Sort the weights in decreasing order. The original order of the 

415 # weights dictates the order of the (integer) node labels, so we 

416 # need to remember the permutation applied in the sorting. 

417 order = sorted(enumerate(w), key=itemgetter(1), reverse=True) 

418 mapping = {c: u for c, (u, v) in enumerate(order)} 

419 seq = [v for u, v in order] 

420 last = n 

421 if not selfloops: 

422 last -= 1 

423 for u in range(last): 

424 v = u 

425 if not selfloops: 

426 v += 1 

427 factor = seq[u] * rho 

428 p = min(seq[v] * factor, 1) 

429 while v < n and p > 0: 

430 if p != 1: 

431 r = seed.random() 

432 v += math.floor(math.log(r, 1 - p)) 

433 if v < n: 

434 q = min(seq[v] * factor, 1) 

435 if seed.random() < q / p: 

436 G.add_edge(mapping[u], mapping[v]) 

437 v += 1 

438 p = q 

439 return G 

440 

441 

442@nx._dispatch(graphs=None) 

443def havel_hakimi_graph(deg_sequence, create_using=None): 

444 """Returns a simple graph with given degree sequence constructed 

445 using the Havel-Hakimi algorithm. 

446 

447 Parameters 

448 ---------- 

449 deg_sequence: list of integers 

450 Each integer corresponds to the degree of a node (need not be sorted). 

451 create_using : NetworkX graph constructor, optional (default=nx.Graph) 

452 Graph type to create. If graph instance, then cleared before populated. 

453 Directed graphs are not allowed. 

454 

455 Raises 

456 ------ 

457 NetworkXException 

458 For a non-graphical degree sequence (i.e. one 

459 not realizable by some simple graph). 

460 

461 Notes 

462 ----- 

463 The Havel-Hakimi algorithm constructs a simple graph by 

464 successively connecting the node of highest degree to other nodes 

465 of highest degree, resorting remaining nodes by degree, and 

466 repeating the process. The resulting graph has a high 

467 degree-associativity. Nodes are labeled 1,.., len(deg_sequence), 

468 corresponding to their position in deg_sequence. 

469 

470 The basic algorithm is from Hakimi [1]_ and was generalized by 

471 Kleitman and Wang [2]_. 

472 

473 References 

474 ---------- 

475 .. [1] Hakimi S., On Realizability of a Set of Integers as 

476 Degrees of the Vertices of a Linear Graph. I, 

477 Journal of SIAM, 10(3), pp. 496-506 (1962) 

478 .. [2] Kleitman D.J. and Wang D.L. 

479 Algorithms for Constructing Graphs and Digraphs with Given Valences 

480 and Factors Discrete Mathematics, 6(1), pp. 79-88 (1973) 

481 """ 

482 if not nx.is_graphical(deg_sequence): 

483 raise nx.NetworkXError("Invalid degree sequence") 

484 

485 p = len(deg_sequence) 

486 G = nx.empty_graph(p, create_using) 

487 if G.is_directed(): 

488 raise nx.NetworkXError("Directed graphs are not supported") 

489 num_degs = [[] for i in range(p)] 

490 dmax, dsum, n = 0, 0, 0 

491 for d in deg_sequence: 

492 # Process only the non-zero integers 

493 if d > 0: 

494 num_degs[d].append(n) 

495 dmax, dsum, n = max(dmax, d), dsum + d, n + 1 

496 # Return graph if no edges 

497 if n == 0: 

498 return G 

499 

500 modstubs = [(0, 0)] * (dmax + 1) 

501 # Successively reduce degree sequence by removing the maximum degree 

502 while n > 0: 

503 # Retrieve the maximum degree in the sequence 

504 while len(num_degs[dmax]) == 0: 

505 dmax -= 1 

506 # If there are not enough stubs to connect to, then the sequence is 

507 # not graphical 

508 if dmax > n - 1: 

509 raise nx.NetworkXError("Non-graphical integer sequence") 

510 

511 # Remove largest stub in list 

512 source = num_degs[dmax].pop() 

513 n -= 1 

514 # Reduce the next dmax largest stubs 

515 mslen = 0 

516 k = dmax 

517 for i in range(dmax): 

518 while len(num_degs[k]) == 0: 

519 k -= 1 

520 target = num_degs[k].pop() 

521 G.add_edge(source, target) 

522 n -= 1 

523 if k > 1: 

524 modstubs[mslen] = (k - 1, target) 

525 mslen += 1 

526 # Add back to the list any nonzero stubs that were removed 

527 for i in range(mslen): 

528 (stubval, stubtarget) = modstubs[i] 

529 num_degs[stubval].append(stubtarget) 

530 n += 1 

531 

532 return G 

533 

534 

535@nx._dispatch(graphs=None) 

536def directed_havel_hakimi_graph(in_deg_sequence, out_deg_sequence, create_using=None): 

537 """Returns a directed graph with the given degree sequences. 

538 

539 Parameters 

540 ---------- 

541 in_deg_sequence : list of integers 

542 Each list entry corresponds to the in-degree of a node. 

543 out_deg_sequence : list of integers 

544 Each list entry corresponds to the out-degree of a node. 

545 create_using : NetworkX graph constructor, optional (default DiGraph) 

546 Graph type to create. If graph instance, then cleared before populated. 

547 

548 Returns 

549 ------- 

550 G : DiGraph 

551 A graph with the specified degree sequences. 

552 Nodes are labeled starting at 0 with an index 

553 corresponding to the position in deg_sequence 

554 

555 Raises 

556 ------ 

557 NetworkXError 

558 If the degree sequences are not digraphical. 

559 

560 See Also 

561 -------- 

562 configuration_model 

563 

564 Notes 

565 ----- 

566 Algorithm as described by Kleitman and Wang [1]_. 

567 

568 References 

569 ---------- 

570 .. [1] D.J. Kleitman and D.L. Wang 

571 Algorithms for Constructing Graphs and Digraphs with Given Valences 

572 and Factors Discrete Mathematics, 6(1), pp. 79-88 (1973) 

573 """ 

574 in_deg_sequence = nx.utils.make_list_of_ints(in_deg_sequence) 

575 out_deg_sequence = nx.utils.make_list_of_ints(out_deg_sequence) 

576 

577 # Process the sequences and form two heaps to store degree pairs with 

578 # either zero or nonzero out degrees 

579 sumin, sumout = 0, 0 

580 nin, nout = len(in_deg_sequence), len(out_deg_sequence) 

581 maxn = max(nin, nout) 

582 G = nx.empty_graph(maxn, create_using, default=nx.DiGraph) 

583 if maxn == 0: 

584 return G 

585 maxin = 0 

586 stubheap, zeroheap = [], [] 

587 for n in range(maxn): 

588 in_deg, out_deg = 0, 0 

589 if n < nout: 

590 out_deg = out_deg_sequence[n] 

591 if n < nin: 

592 in_deg = in_deg_sequence[n] 

593 if in_deg < 0 or out_deg < 0: 

594 raise nx.NetworkXError( 

595 "Invalid degree sequences. Sequence values must be positive." 

596 ) 

597 sumin, sumout, maxin = sumin + in_deg, sumout + out_deg, max(maxin, in_deg) 

598 if in_deg > 0: 

599 stubheap.append((-1 * out_deg, -1 * in_deg, n)) 

600 elif out_deg > 0: 

601 zeroheap.append((-1 * out_deg, n)) 

602 if sumin != sumout: 

603 raise nx.NetworkXError( 

604 "Invalid degree sequences. Sequences must have equal sums." 

605 ) 

606 heapq.heapify(stubheap) 

607 heapq.heapify(zeroheap) 

608 

609 modstubs = [(0, 0, 0)] * (maxin + 1) 

610 # Successively reduce degree sequence by removing the maximum 

611 while stubheap: 

612 # Remove first value in the sequence with a non-zero in degree 

613 (freeout, freein, target) = heapq.heappop(stubheap) 

614 freein *= -1 

615 if freein > len(stubheap) + len(zeroheap): 

616 raise nx.NetworkXError("Non-digraphical integer sequence") 

617 

618 # Attach arcs from the nodes with the most stubs 

619 mslen = 0 

620 for i in range(freein): 

621 if zeroheap and (not stubheap or stubheap[0][0] > zeroheap[0][0]): 

622 (stubout, stubsource) = heapq.heappop(zeroheap) 

623 stubin = 0 

624 else: 

625 (stubout, stubin, stubsource) = heapq.heappop(stubheap) 

626 if stubout == 0: 

627 raise nx.NetworkXError("Non-digraphical integer sequence") 

628 G.add_edge(stubsource, target) 

629 # Check if source is now totally connected 

630 if stubout + 1 < 0 or stubin < 0: 

631 modstubs[mslen] = (stubout + 1, stubin, stubsource) 

632 mslen += 1 

633 

634 # Add the nodes back to the heaps that still have available stubs 

635 for i in range(mslen): 

636 stub = modstubs[i] 

637 if stub[1] < 0: 

638 heapq.heappush(stubheap, stub) 

639 else: 

640 heapq.heappush(zeroheap, (stub[0], stub[2])) 

641 if freeout < 0: 

642 heapq.heappush(zeroheap, (freeout, target)) 

643 

644 return G 

645 

646 

647@nx._dispatch(graphs=None) 

648def degree_sequence_tree(deg_sequence, create_using=None): 

649 """Make a tree for the given degree sequence. 

650 

651 A tree has #nodes-#edges=1 so 

652 the degree sequence must have 

653 len(deg_sequence)-sum(deg_sequence)/2=1 

654 """ 

655 # The sum of the degree sequence must be even (for any undirected graph). 

656 degree_sum = sum(deg_sequence) 

657 if degree_sum % 2 != 0: 

658 msg = "Invalid degree sequence: sum of degrees must be even, not odd" 

659 raise nx.NetworkXError(msg) 

660 if len(deg_sequence) - degree_sum // 2 != 1: 

661 msg = ( 

662 "Invalid degree sequence: tree must have number of nodes equal" 

663 " to one less than the number of edges" 

664 ) 

665 raise nx.NetworkXError(msg) 

666 G = nx.empty_graph(0, create_using) 

667 if G.is_directed(): 

668 raise nx.NetworkXError("Directed Graph not supported") 

669 

670 # Sort all degrees greater than 1 in decreasing order. 

671 # 

672 # TODO Does this need to be sorted in reverse order? 

673 deg = sorted((s for s in deg_sequence if s > 1), reverse=True) 

674 

675 # make path graph as backbone 

676 n = len(deg) + 2 

677 nx.add_path(G, range(n)) 

678 last = n 

679 

680 # add the leaves 

681 for source in range(1, n - 1): 

682 nedges = deg.pop() - 2 

683 for target in range(last, last + nedges): 

684 G.add_edge(source, target) 

685 last += nedges 

686 

687 # in case we added one too many 

688 if len(G) > len(deg_sequence): 

689 G.remove_node(0) 

690 return G 

691 

692 

693@py_random_state(1) 

694@nx._dispatch(graphs=None) 

695def random_degree_sequence_graph(sequence, seed=None, tries=10): 

696 r"""Returns a simple random graph with the given degree sequence. 

697 

698 If the maximum degree $d_m$ in the sequence is $O(m^{1/4})$ then the 

699 algorithm produces almost uniform random graphs in $O(m d_m)$ time 

700 where $m$ is the number of edges. 

701 

702 Parameters 

703 ---------- 

704 sequence : list of integers 

705 Sequence of degrees 

706 seed : integer, random_state, or None (default) 

707 Indicator of random number generation state. 

708 See :ref:`Randomness<randomness>`. 

709 tries : int, optional 

710 Maximum number of tries to create a graph 

711 

712 Returns 

713 ------- 

714 G : Graph 

715 A graph with the specified degree sequence. 

716 Nodes are labeled starting at 0 with an index 

717 corresponding to the position in the sequence. 

718 

719 Raises 

720 ------ 

721 NetworkXUnfeasible 

722 If the degree sequence is not graphical. 

723 NetworkXError 

724 If a graph is not produced in specified number of tries 

725 

726 See Also 

727 -------- 

728 is_graphical, configuration_model 

729 

730 Notes 

731 ----- 

732 The generator algorithm [1]_ is not guaranteed to produce a graph. 

733 

734 References 

735 ---------- 

736 .. [1] Moshen Bayati, Jeong Han Kim, and Amin Saberi, 

737 A sequential algorithm for generating random graphs. 

738 Algorithmica, Volume 58, Number 4, 860-910, 

739 DOI: 10.1007/s00453-009-9340-1 

740 

741 Examples 

742 -------- 

743 >>> sequence = [1, 2, 2, 3] 

744 >>> G = nx.random_degree_sequence_graph(sequence, seed=42) 

745 >>> sorted(d for n, d in G.degree()) 

746 [1, 2, 2, 3] 

747 """ 

748 DSRG = DegreeSequenceRandomGraph(sequence, seed) 

749 for try_n in range(tries): 

750 try: 

751 return DSRG.generate() 

752 except nx.NetworkXUnfeasible: 

753 pass 

754 raise nx.NetworkXError(f"failed to generate graph in {tries} tries") 

755 

756 

757class DegreeSequenceRandomGraph: 

758 # class to generate random graphs with a given degree sequence 

759 # use random_degree_sequence_graph() 

760 def __init__(self, degree, rng): 

761 if not nx.is_graphical(degree): 

762 raise nx.NetworkXUnfeasible("degree sequence is not graphical") 

763 self.rng = rng 

764 self.degree = list(degree) 

765 # node labels are integers 0,...,n-1 

766 self.m = sum(self.degree) / 2.0 # number of edges 

767 try: 

768 self.dmax = max(self.degree) # maximum degree 

769 except ValueError: 

770 self.dmax = 0 

771 

772 def generate(self): 

773 # remaining_degree is mapping from int->remaining degree 

774 self.remaining_degree = dict(enumerate(self.degree)) 

775 # add all nodes to make sure we get isolated nodes 

776 self.graph = nx.Graph() 

777 self.graph.add_nodes_from(self.remaining_degree) 

778 # remove zero degree nodes 

779 for n, d in list(self.remaining_degree.items()): 

780 if d == 0: 

781 del self.remaining_degree[n] 

782 if len(self.remaining_degree) > 0: 

783 # build graph in three phases according to how many unmatched edges 

784 self.phase1() 

785 self.phase2() 

786 self.phase3() 

787 return self.graph 

788 

789 def update_remaining(self, u, v, aux_graph=None): 

790 # decrement remaining nodes, modify auxiliary graph if in phase3 

791 if aux_graph is not None: 

792 # remove edges from auxiliary graph 

793 aux_graph.remove_edge(u, v) 

794 if self.remaining_degree[u] == 1: 

795 del self.remaining_degree[u] 

796 if aux_graph is not None: 

797 aux_graph.remove_node(u) 

798 else: 

799 self.remaining_degree[u] -= 1 

800 if self.remaining_degree[v] == 1: 

801 del self.remaining_degree[v] 

802 if aux_graph is not None: 

803 aux_graph.remove_node(v) 

804 else: 

805 self.remaining_degree[v] -= 1 

806 

807 def p(self, u, v): 

808 # degree probability 

809 return 1 - self.degree[u] * self.degree[v] / (4.0 * self.m) 

810 

811 def q(self, u, v): 

812 # remaining degree probability 

813 norm = max(self.remaining_degree.values()) ** 2 

814 return self.remaining_degree[u] * self.remaining_degree[v] / norm 

815 

816 def suitable_edge(self): 

817 """Returns True if and only if an arbitrary remaining node can 

818 potentially be joined with some other remaining node. 

819 

820 """ 

821 nodes = iter(self.remaining_degree) 

822 u = next(nodes) 

823 return any(v not in self.graph[u] for v in nodes) 

824 

825 def phase1(self): 

826 # choose node pairs from (degree) weighted distribution 

827 rem_deg = self.remaining_degree 

828 while sum(rem_deg.values()) >= 2 * self.dmax**2: 

829 u, v = sorted(random_weighted_sample(rem_deg, 2, self.rng)) 

830 if self.graph.has_edge(u, v): 

831 continue 

832 if self.rng.random() < self.p(u, v): # accept edge 

833 self.graph.add_edge(u, v) 

834 self.update_remaining(u, v) 

835 

836 def phase2(self): 

837 # choose remaining nodes uniformly at random and use rejection sampling 

838 remaining_deg = self.remaining_degree 

839 rng = self.rng 

840 while len(remaining_deg) >= 2 * self.dmax: 

841 while True: 

842 u, v = sorted(rng.sample(list(remaining_deg.keys()), 2)) 

843 if self.graph.has_edge(u, v): 

844 continue 

845 if rng.random() < self.q(u, v): 

846 break 

847 if rng.random() < self.p(u, v): # accept edge 

848 self.graph.add_edge(u, v) 

849 self.update_remaining(u, v) 

850 

851 def phase3(self): 

852 # build potential remaining edges and choose with rejection sampling 

853 potential_edges = combinations(self.remaining_degree, 2) 

854 # build auxiliary graph of potential edges not already in graph 

855 H = nx.Graph( 

856 [(u, v) for (u, v) in potential_edges if not self.graph.has_edge(u, v)] 

857 ) 

858 rng = self.rng 

859 while self.remaining_degree: 

860 if not self.suitable_edge(): 

861 raise nx.NetworkXUnfeasible("no suitable edges left") 

862 while True: 

863 u, v = sorted(rng.choice(list(H.edges()))) 

864 if rng.random() < self.q(u, v): 

865 break 

866 if rng.random() < self.p(u, v): # accept edge 

867 self.graph.add_edge(u, v) 

868 self.update_remaining(u, v, aux_graph=H)