__init__.py 45.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
    -------
    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)
1025
    >>> gt.graph_draw(g, pos=pos, output_size=(300,300), output="geometric.pdf")
1026
1027
    <...>
    >>> g, pos = gt.geometric_graph(points, 0.3, [(0,4), (0,4)])
1028
    >>> gt.graph_draw(g, output_size=(300,300), output="geometric_periodic.pdf")
1029
1030
    <...>

1031
1032
    .. image:: geometric.*
    .. image:: geometric_periodic.*
1033
1034
1035
1036
1037
1038
1039

    *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
1040
       graphs", Phys. Rev. E 66, 016121 (2002), :doi:`10.1103/PhysRevE.66.016121`
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063

    """

    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
1064
1065
1066
1067
1068
1069
1070
1071
1072


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.
1073
    m : int (optional, default: ``1``)
1074
        Out-degree of newly added vertices.
1075
    c : float (optional, default: ``1 if directed == True else 0``)
1076
1077
        Constant factor added to the probability of a vertex receiving an edge
        (see notes below).
1078
    gamma : float (optional, default: ``1``)
1079
        Preferential attachment power (see notes below).
1080
    directed : bool (optional, default: ``True``)
1081
1082
        If ``True``, a Price network is generated. If ``False``, a
        Barabási-Albert network is generated.
1083
    seed_graph : :class:`~graph_tool.Graph` (optional, default: ``None``)
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
        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
1098
    vertices, chosen with probability :math:`\pi` defined as:
1099
1100
1101

    .. math::

1102
        \pi \propto k^\gamma + c
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120

    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).

1121
1122
1123
1124
1125
1126
1127
    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.

1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
    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)
1140
1141
1142
    >>> gt.graph_draw(g, pos=gt.sfdp_layout(g, epsilon=1e-3, cooling_step=0.99),
    ...               vertex_fill_color=g.vertex_index, vertex_size=2,
    ...               edge_pen_width=1, output="price-network.png")
1143
1144
    <...>
    >>> g = gt.price_network(100000, c=0.1)
1145
1146
1147
    >>> gt.graph_draw(g, pos=gt.sfdp_layout(g, epsilon=1e-3, cooling_step=0.99),
    ...               vertex_fill_color=g.vertex_index, vertex_size=2,
    ...               edge_pen_width=1, output="price-network-broader.png")
1148
1149
    <...>

1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
    .. 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.
1161
1162
1163
1164
1165
1166
1167
1168


    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
1169
       :doi:`10.1098/rstb.1925.0002`
1170
1171
1172
    .. [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
1173
       :doi:`10.1002/asi.4630270505`
1174
    .. [barabasi-albert] Barabási, A.-L., and Albert, R., "Emergence of
Tiago Peixoto's avatar
Tiago Peixoto committed
1175
1176
       scaling in random networks", Science, 286, 509, 1999,
       :doi:`10.1126/science.286.5439.509`
1177
1178
    .. [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
1179
       :doi:`10.1080/00018730110112519`
1180
1181
1182
1183
1184
1185
    """

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

    if seed_graph is None:
1186
1187
1188
        g = Graph(directed=directed)
        if c > 0:
            g.add_vertex()
1189
        else:
1190
1191
            g.add_vertex(2)
            g.add_edge(g.vertex(1), g.vertex(0))
1192
1193
1194
1195
1196
1197
        N -= g.num_vertices()
    else:
        g = seed_graph
    seed = numpy.random.randint(0, sys.maxint)
    libgraph_tool_generation.price(g._Graph__graph, N, gamma, c, m, seed)
    return g