__init__.py 47.1 KB
Newer Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024

    References
    ----------
    .. [lattice] http://en.wikipedia.org/wiki/Square_lattice

    """

    g = Graph(directed=False)
    libgraph_tool_generation.lattice(g._Graph__graph, shape, periodic)
    return g


def geometric_graph(points, radius, ranges=None):
    r"""
    Generate a geometric network form a set of N-dimensional points.

    Parameters
    ----------
    points : list or :class:`~numpy.ndarray`
        List of points. This must be a two-dimensional array, where the rows are
        coordinates in a N-dimensional space.
    radius : float
        Pairs of points with an euclidean distance lower than this parameters
        will be connected.
1025
    ranges : list or :class:`~numpy.ndarray` (optional, default: ``None``)
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
        If provided, periodic boundary conditions will be assumed, and the
        values of this parameter it will be used as the ranges in all
        dimensions. It must be a two-dimensional array, where each row will
        cointain the lower and upper bound of each dimension.

    Returns
    -------
    geometric_graph : :class:`~graph_tool.Graph`
        The generated graph.
    pos : :class:`~graph_tool.PropertyMap`
        A vertex property map with the position of each vertex.

    Notes
    -----
    A geometric graph [geometric-graph]_ is generated by connecting points
    embedded in a N-dimensional euclidean space which are at a distance equal to
    or smaller than a given radius.

    See Also
    --------
    triangulation: 2D or 3D triangulation
    random_graph: random graph generation
    lattice : N-dimensional square lattice

    Examples
    --------
    >>> from numpy.random import seed, random
    >>> seed(42)
    >>> points = random((500, 2)) * 4
    >>> g, pos = gt.geometric_graph(points, 0.3)
1056
    >>> gt.graph_draw(g, pos=pos, output_size=(300,300), output="geometric.pdf")
1057
1058
    <...>
    >>> g, pos = gt.geometric_graph(points, 0.3, [(0,4), (0,4)])
1059
    >>> gt.graph_draw(g, output_size=(300,300), output="geometric_periodic.pdf")
1060
1061
    <...>

1062
1063
    .. image:: geometric.*
    .. image:: geometric_periodic.*
1064
1065
1066
1067
1068
1069
1070

    *Left:* Geometric network with random points. *Right:* Same network, but
     with periodic boundary conditions.

    References
    ----------
    .. [geometric-graph] Jesper Dall and Michael Christensen, "Random geometric
Tiago Peixoto's avatar
Tiago Peixoto committed
1071
       graphs", Phys. Rev. E 66, 016121 (2002), :doi:`10.1103/PhysRevE.66.016121`
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094

    """

    g = Graph(directed=False)
    pos = g.new_vertex_property("vector<double>")
    if type(points) != numpy.ndarray:
        points = numpy.array(points)
    if len(points.shape) < 2:
        raise ValueError("points list must be a two-dimensional array!")
    if ranges is not None:
        periodic = True
        if type(ranges) != numpy.ndarray:
            ranges = numpy.array(ranges, dtype="float")
        else:
            ranges = array(ranges, dtype="float")
    else:
        periodic = False
        ranges = ()

    libgraph_tool_generation.geometric(g._Graph__graph, points, float(radius),
                                       ranges, periodic,
                                       _prop("v", g, pos))
    return g, pos
1095
1096
1097
1098
1099
1100
1101
1102
1103


def price_network(N, m=1, c=None, gamma=1, directed=True, seed_graph=None):
    r"""A generalized version of Price's -- or Barabási-Albert if undirected -- preferential attachment network model.

    Parameters
    ----------
    N : int
        Size of the network.
1104
    m : int (optional, default: ``1``)
1105
        Out-degree of newly added vertices.
1106
    c : float (optional, default: ``1 if directed == True else 0``)
1107
1108
        Constant factor added to the probability of a vertex receiving an edge
        (see notes below).
1109
    gamma : float (optional, default: ``1``)
1110
        Preferential attachment power (see notes below).
1111
    directed : bool (optional, default: ``True``)
1112
1113
        If ``True``, a Price network is generated. If ``False``, a
        Barabási-Albert network is generated.
1114
    seed_graph : :class:`~graph_tool.Graph` (optional, default: ``None``)
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
        If provided, this graph will be used as the starting point of the
        algorithm.

    Returns
    -------
    price_graph : :class:`~graph_tool.Graph`
        The generated graph.

    Notes
    -----

    The (generalized) [price]_ network is either a directed or undirected graph
    (the latter is called a Barabási-Albert network), generated dynamically by
    at each step adding a new vertex, and connecting it to :math:`m` other
1129
    vertices, chosen with probability :math:`\pi` defined as:
1130
1131
1132

    .. math::

1133
        \pi \propto k^\gamma + c
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151

    where :math:`k` is the in-degree of the vertex (or simply the degree in the
    undirected case). If :math:`\gamma=1`, the tail of resulting in-degree
    distribution of the directed case is given by

    .. math::

        P_{k_\text{in}} \sim k_\text{in}^{-(2 + c/m)},

    or for the undirected case

    .. math::

        P_{k} \sim k^{-(3 + c/m)}.

    However, if :math:`\gamma \ne 1`, the in-degree distribution is not
    scale-free (see [dorogovtsev-evolution]_ for details).

1152
1153
1154
1155
1156
1157
1158
    Note that if `seed_graph` is not given, the algorithm will *always* start
    with one node if :math:`c > 0`, or with two nodes with a link between them
    otherwise. If :math:`m > 1`, the degree of the newly added vertices will be
    vary dynamically as :math:`m'(t) = \min(m, N(t))`, where :math:`N(t)` is the
    number of vertices added so far. If this behaviour is undesired, a proper
    seed graph with :math:`N \ge m` vertices must be provided.

1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
    This algorithm runs in :math:`O(N\log N)` time.

    See Also
    --------
    triangulation: 2D or 3D triangulation
    random_graph: random graph generation
    lattice : N-dimensional square lattice
    geometric_graph : N-dimensional geometric network

    Examples
    --------
    >>> g = gt.price_network(100000)
Tiago Peixoto's avatar
Tiago Peixoto committed
1171
    >>> gt.graph_draw(g, pos=gt.sfdp_layout(g, epsilon=1e-2, cooling_step=0.95),
1172
1173
    ...               vertex_fill_color=g.vertex_index, vertex_size=2,
    ...               edge_pen_width=1, output="price-network.png")
1174
1175
    <...>
    >>> g = gt.price_network(100000, c=0.1)
Tiago Peixoto's avatar
Tiago Peixoto committed
1176
    >>> gt.graph_draw(g, pos=gt.sfdp_layout(g, epsilon=1e-2, cooling_step=0.95),
1177
1178
    ...               vertex_fill_color=g.vertex_index, vertex_size=2,
    ...               edge_pen_width=1, output="price-network-broader.png")
1179
1180
    <...>

1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
    .. figure:: price-network.png
        :align: center

        Price network with :math:`N=10^5` nodes and :math:`c=1`.  The colors
        represent the order in which vertices were added.

    .. figure:: price-network-broader.png
        :align: center

        Price network with :math:`N=10^5` nodes and :math:`c=0.1`.  The colors
        represent the order in which vertices were added.
1192
1193
1194
1195
1196
1197
1198
1199


    References
    ----------

    .. [yule] Yule, G. U. "A Mathematical Theory of Evolution, based on the
       Conclusions of Dr. J. C. Willis, F.R.S.". Philosophical Transactions of
       the Royal Society of London, Ser. B 213: 21–87, 1925,
Tiago Peixoto's avatar
Tiago Peixoto committed
1200
       :doi:`10.1098/rstb.1925.0002`
1201
1202
1203
    .. [price] Derek De Solla Price, "A general theory of bibliometric and other
       cumulative advantage processes", Journal of the American Society for
       Information Science, Volume 27, Issue 5, pages 292–306, September 1976,
Tiago Peixoto's avatar
Tiago Peixoto committed
1204
       :doi:`10.1002/asi.4630270505`
1205
    .. [barabasi-albert] Barabási, A.-L., and Albert, R., "Emergence of
Tiago Peixoto's avatar
Tiago Peixoto committed
1206
1207
       scaling in random networks", Science, 286, 509, 1999,
       :doi:`10.1126/science.286.5439.509`
1208
1209
    .. [dorogovtsev-evolution] S. N. Dorogovtsev and J. F. F. Mendes, "Evolution
       of networks", Advances in Physics, 2002, Vol. 51, No. 4, 1079-1187,
Tiago Peixoto's avatar
Tiago Peixoto committed
1210
       :doi:`10.1080/00018730110112519`
1211
1212
1213
1214
1215
1216
    """

    if c is None:
        c = 1 if directed else 0

    if seed_graph is None:
1217
1218
1219
        g = Graph(directed=directed)
        if c > 0:
            g.add_vertex()
1220
        else:
1221
1222
            g.add_vertex(2)
            g.add_edge(g.vertex(1), g.vertex(0))
1223
1224
1225
        N -= g.num_vertices()
    else:
        g = seed_graph
1226
    seed = numpy.random.randint(0, sys.maxsize)
1227
1228
    libgraph_tool_generation.price(g._Graph__graph, N, gamma, c, m, seed)
    return g
1229