| 644 | @patch_docstring(nxa.random_shell_graph) |
| 645 | @py_random_state(1) |
| 646 | def random_shell_graph(constructor, seed=None): |
| 647 | G = empty_graph(0) |
| 648 | |
| 649 | glist = [] |
| 650 | intra_edges = [] |
| 651 | nnodes = 0 |
| 652 | # create gnm graphs for each shell |
| 653 | for n, m, d in constructor: |
| 654 | inter_edges = int(m * d) |
| 655 | intra_edges.append(m - inter_edges) |
| 656 | g = nx.convert_node_labels_to_integers( |
| 657 | gnm_random_graph(n, inter_edges, seed=seed), first_label=nnodes |
| 658 | ) |
| 659 | glist.append(g) |
| 660 | nnodes += n |
| 661 | G = nx.operators.union(G, g) |
| 662 | |
| 663 | # connect the shells randomly |
| 664 | for gi in range(len(glist) - 1): |
| 665 | nlist1 = list(glist[gi]) |
| 666 | nlist2 = list(glist[gi + 1]) |
| 667 | total_edges = intra_edges[gi] |
| 668 | edge_count = 0 |
| 669 | while edge_count < total_edges: |
| 670 | u = seed.choice(nlist1) |
| 671 | v = seed.choice(nlist2) |
| 672 | if u == v or G.has_edge(u, v): |
| 673 | continue |
| 674 | else: |
| 675 | G.add_edge(u, v) |
| 676 | edge_count = edge_count + 1 |
| 677 | return G |
| 678 | |
| 679 | |
| 680 | @patch_docstring(nxa.random_powerlaw_tree) |