|
| 1 | +Basic static network exploration |
| 2 | +================================ |
| 3 | + |
| 4 | +This tutorial introduces a few simple functions for static undirected networks. |
| 5 | +All examples assume that you already installed Reticula with ``pip install |
| 6 | +reticula`` and have ``ret`` imported as the usual alias:: |
| 7 | + |
| 8 | + >>> import reticula as ret |
| 9 | + |
| 10 | +Generating a random network |
| 11 | +--------------------------- |
| 12 | + |
| 13 | +We start by creating a random :math:`G(n,p)` network. The function |
| 14 | +:py:func:`random_gnp_graph` requires a pseudo random number generator, created |
| 15 | +here with :py:func:`mersenne_twister`:: |
| 16 | + |
| 17 | + >>> gen = ret.mersenne_twister(42) |
| 18 | + >>> g = ret.random_gnp_graph[ret.int64](n=100, p=0.02, random_state=gen) |
| 19 | + >>> g |
| 20 | + <undirected_network[int64] with 100 verts and 110 edges> |
| 21 | + |
| 22 | +Inspecting the network |
| 23 | +---------------------- |
| 24 | + |
| 25 | +The vertices and edges can be retrieved as Python lists:: |
| 26 | + |
| 27 | + >>> g.vertices()[:5] |
| 28 | + [0, 1, 2, 3, 4] |
| 29 | + >>> g.edges()[:3] |
| 30 | + [undirected_edge[int64](0, 16), undirected_edge[int64](0, 20), ...] |
| 31 | + |
| 32 | +Several helper functions report basic properties such as the number of vertices |
| 33 | +or edges and the network density:: |
| 34 | + |
| 35 | + >>> len(g.vertices()) |
| 36 | + 100 |
| 37 | + >>> len(g.edges()) |
| 38 | + 110 |
| 39 | + >>> ret.density(g) |
| 40 | + 0.022223 |
| 41 | + |
| 42 | +You can probe the edges of the network, for example to find out how which nodes |
| 43 | +are connected by a specific edge. |
| 44 | + |
| 45 | +Mutator vertices are those vertices that can change the state of the other |
| 46 | +nodes through a specific edge, and mutated vertices are those whose internal |
| 47 | +state can change because of mutators. For undirected_network, all nodes |
| 48 | +incident to an edge can act as mutators or mutated vertices:: |
| 49 | + |
| 50 | + >>> e = g.edges()[0] |
| 51 | + >>> e |
| 52 | + undirected_edge[int64](0, 16) |
| 53 | + >>> e.mutator_verts() |
| 54 | + [0, 16] |
| 55 | + >>> e.mutated_verts() |
| 56 | + [0, 16] |
| 57 | + >>> e.incident_verts() |
| 58 | + [0, 16] |
| 59 | + |
| 60 | +This is not the case for directed networks, where the mutator and mutated |
| 61 | +vertices can be different. |
| 62 | + |
| 63 | + >>> e = ret.directed_edge[int64](0, 16) |
| 64 | + >>> e |
| 65 | + directed_edge[int64](0, 16) |
| 66 | + >>> e.mutator_verts() |
| 67 | + [0] |
| 68 | + >>> e.tail() |
| 69 | + 0 |
| 70 | + >>> e.mutated_verts() |
| 71 | + [16] |
| 72 | + >>> e.head() |
| 73 | + 16 |
| 74 | + >>> e.incident_verts() |
| 75 | + [0, 16] |
| 76 | + |
| 77 | + |
| 78 | +Connected components |
| 79 | +-------------------- |
| 80 | + |
| 81 | +The function :py:func:`connected_components` returns all components of an |
| 82 | +undirected network:: |
| 83 | + |
| 84 | + >>> comps = ret.connected_components(g) |
| 85 | + >>> lcc = max(comps, key=len) |
| 86 | + >>> lcc |
| 87 | + <component[int64] of 93 nodes: {99, 96, 95, 94, 92, 91, 90, 89, 88, 87, ...})> |
| 88 | + >>> len(lcc) |
| 89 | + 93 |
| 90 | + |
| 91 | +If you are only interested in the largest component, you can directly use |
| 92 | +:py:func:`largest_connected_component`:: |
| 93 | + |
| 94 | + >> lcc = ret.largest_connected_component(g) |
| 95 | + >> lcc |
| 96 | + <component[int64] of 93 nodes: {99, 96, 95, 94, 92, 91, 90, 89, 88, 87, ...})> |
| 97 | + |
| 98 | + |
| 99 | +Degrees and neighbourhoods |
| 100 | +-------------------------- |
| 101 | + |
| 102 | +Degree information for each vertex is accessible via :py:func:`degree`:: |
| 103 | + |
| 104 | + >>> ret.degree(g, 0) |
| 105 | + 4 |
| 106 | + |
| 107 | +Since graph g is undirected, the degree of a vertex is the same as its in- and |
| 108 | +out-degree:: |
| 109 | + |
| 110 | + >>> ret.in_degree(g, 0) |
| 111 | + 4 |
| 112 | + >>> ret.out_degree(g, 0) |
| 113 | + 4 |
| 114 | + >>> ret.incident_degree(g, 0) |
| 115 | + 4 |
| 116 | + |
| 117 | +You can also directly generate the degree sequence of the network, using |
| 118 | +:py:func:`degree_sequence`:: |
| 119 | + |
| 120 | + >>> ret.degree_sequence(g) |
| 121 | + [4, 1, 0, 6, 1, 2, 1, 0, ...] |
| 122 | + >>> ret.in_degree_sequence(g) |
| 123 | + [4, 1, 0, 6, 1, 2, 1, 0, ...] |
| 124 | + >>> ret.out_degree_sequence(g) |
| 125 | + [4, 1, 0, 6, 1, 2, 1, 0, ...] |
| 126 | + >>> ret.incident_degree_sequence(g) |
| 127 | + [4, 1, 0, 6, 1, 2, 1, 0, ...] |
| 128 | + |
| 129 | +You can get a list of neighbours of a node:: |
| 130 | + |
| 131 | + >>> g.neighbours(0) |
| 132 | + [20, 51, 31, 16] |
| 133 | + |
| 134 | + |
| 135 | +Similar to before, you can also get the successors and predecessors, the |
| 136 | +directed in- and out-neighbours, of a vertex:: |
| 137 | + |
| 138 | + >>> g.successors(0) |
| 139 | + [20, 51, 31, 16] |
| 140 | + >>> g.prdecessors(0) |
| 141 | + [20, 51, 31, 16] |
| 142 | + |
| 143 | + |
| 144 | +These simple building blocks will let you start exploring static networks in |
| 145 | +Reticula. |
0 commit comments