Returns the growing network (GN) digraph with `n` nodes. The GN graph is built by adding nodes one at a time with a link to one previously added node. The target node for the link is chosen with probability based on degree. The default attachment kernel is a linear function of the
(n, kernel=None, create_using=None, seed=None)
| 28 | @py_random_state(3) |
| 29 | @nx._dispatchable(graphs=None, returns_graph=True) |
| 30 | def gn_graph(n, kernel=None, create_using=None, seed=None): |
| 31 | """Returns the growing network (GN) digraph with `n` nodes. |
| 32 | |
| 33 | The GN graph is built by adding nodes one at a time with a link to one |
| 34 | previously added node. The target node for the link is chosen with |
| 35 | probability based on degree. The default attachment kernel is a linear |
| 36 | function of the degree of a node. |
| 37 | |
| 38 | The graph is always a (directed) tree. |
| 39 | |
| 40 | Parameters |
| 41 | ---------- |
| 42 | n : int |
| 43 | The number of nodes for the generated graph. |
| 44 | kernel : function |
| 45 | The attachment kernel. |
| 46 | create_using : NetworkX graph constructor, optional (default DiGraph) |
| 47 | Graph type to create. If graph instance, then cleared before populated. |
| 48 | seed : integer, random_state, or None (default) |
| 49 | Indicator of random number generation state. |
| 50 | See :ref:`Randomness<randomness>`. |
| 51 | |
| 52 | Examples |
| 53 | -------- |
| 54 | To create the undirected GN graph, use the :meth:`~DiGraph.to_directed` |
| 55 | method:: |
| 56 | |
| 57 | >>> D = nx.gn_graph(10) # the GN graph |
| 58 | >>> G = D.to_undirected() # the undirected version |
| 59 | |
| 60 | To specify an attachment kernel, use the `kernel` keyword argument:: |
| 61 | |
| 62 | >>> D = nx.gn_graph(10, kernel=lambda x: x**1.5) # A_k = k^1.5 |
| 63 | |
| 64 | References |
| 65 | ---------- |
| 66 | .. [1] P. L. Krapivsky and S. Redner, |
| 67 | Organization of Growing Random Networks, |
| 68 | Phys. Rev. E, 63, 066123, 2001. |
| 69 | """ |
| 70 | G = empty_graph(1, create_using, default=nx.DiGraph) |
| 71 | if not G.is_directed(): |
| 72 | raise nx.NetworkXError("create_using must indicate a Directed Graph") |
| 73 | |
| 74 | if kernel is None: |
| 75 | |
| 76 | def kernel(x): |
| 77 | return x |
| 78 | |
| 79 | if n == 1: |
| 80 | return G |
| 81 | |
| 82 | G.add_edge(1, 0) # get started |
| 83 | ds = [1, 1] # degree sequence |
| 84 | |
| 85 | for source in range(2, n): |
| 86 | # compute distribution from kernel and degree |
| 87 | dist = [kernel(d) for d in ds] |
searching dependent graphs…