__init__.py 27.7 KB
Newer Older
Tiago Peixoto's avatar
Tiago Peixoto committed
1
#! /usr/bin/env python
2
# -*- coding: utf-8 -*-
Tiago Peixoto's avatar
Tiago Peixoto committed
3
#
4 5
# graph_tool -- a general graph manipulation python module
#
Tiago Peixoto's avatar
Tiago Peixoto committed
6
# Copyright (C) 2007-2011 Tiago de Paula Peixoto <tiago@skewed.de>
Tiago Peixoto's avatar
Tiago Peixoto committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

21
"""
22 23
``graph_tool.centrality`` - Centrality measures
-----------------------------------------------
24 25

This module includes centrality-related algorithms.
26 27 28 29 30 31 32 33 34 35

Summary
+++++++

.. autosummary::
   :nosignatures:

   pagerank
   betweenness
   central_point_dominance
36
   eigenvector
37
   eigentrust
38
   trust_transitivity
39 40 41

Contents
++++++++
42 43
"""

Tiago Peixoto's avatar
Tiago Peixoto committed
44 45 46
from .. dl_import import dl_import
dl_import("import libgraph_tool_centrality")

47
from .. import _prop, ungroup_vector_property
Tiago Peixoto's avatar
Tiago Peixoto committed
48 49
import sys
import numpy
Tiago Peixoto's avatar
Tiago Peixoto committed
50 51

__all__ = ["pagerank", "betweenness", "central_point_dominance", "eigentrust",
52
           "eigenvector", "trust_transitivity"]
Tiago Peixoto's avatar
Tiago Peixoto committed
53

Tiago Peixoto's avatar
Tiago Peixoto committed
54

55 56
def pagerank(g, damping=0.85, pers=None, weight=None, prop=None, epsilon=1e-6,
             max_iter=None, ret_iter=False):
57 58 59 60 61
    r"""
    Calculate the PageRank of each vertex.

    Parameters
    ----------
62
    g : :class:`~graph_tool.Graph`
63
        Graph to be used.
64
    damping : float, optional (default: 0.85)
65
        Damping factor.
66 67 68 69 70
    pers : :class:`~graph_tool.PropertyMap`, optional (default: None)
        Personalization vector. If omitted, a constant value of :math:`1/N`
        will be used.
    weight : :class:`~graph_tool.PropertyMap`, optional (default: None)
        Edge weights. If omitted, a constant value of 1 will be used.
71
    prop : :class:`~graph_tool.PropertyMap`, optional (default: None)
72
        Vertex property map to store the PageRank values.
Tiago Peixoto's avatar
Tiago Peixoto committed
73
    epsilon : float, optional (default: 1e-6)
74 75 76 77 78 79 80 81 82
        Convergence condition. The iteration will stop if the total delta of all
        vertices are below this value.
    max_iter : int, optional (default: None)
        If supplied, this will limit the total number of iterations.
    ret_iter : bool, optional (default: False)
        If true, the total number of iterations is also returned.

    Returns
    -------
83 84
    pagerank : :class:`~graph_tool.PropertyMap`
        A vertex property map containing the PageRank values.
85 86 87 88 89

    See Also
    --------
    betweenness: betweenness centrality
    eigentrust: eigentrust centrality
90
    trust_transitivity: pervasive trust transitivity
91 92 93

    Notes
    -----
Tiago Peixoto's avatar
Tiago Peixoto committed
94 95
    The value of PageRank [pagerank-wikipedia]_ of vertex v, :math:`PR(v)`, is
    given iteratively by the relation:
96 97

    .. math::
98

99 100
        PR(v) = \frac{1-d}{N} + d \sum_{u \in \Gamma^{-}(v)}
                \frac{PR (u)}{d^{+}(u)}
101 102 103 104

    where :math:`\Gamma^{-}(v)` are the in-neighbours of v, :math:`d^{+}(w)` is
    the out-degree of w, and d is a damping factor.

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    If a personalization property :math:`p(v)` is given, the definition becomes:

    .. math::

        PR(v) = (1-d)p(v) + d \sum_{u \in \Gamma^{-}(v)}
                \frac{PR (u)}{d^{+}(u)}

    If edge weights are also given, the equation is then generalized to:

    .. math::

        PR(v) = (1-d)p(v) + d \sum_{u \in \Gamma^{-}(v)}
                \frac{PR (u) w_{u\to v}}{d^{+}(u)}

    where :math:`d^{+}(u)=\sum_{y}A_{u,y}w_{u\to y}` is redefined to be the sum
    of the weights of the out-going edges from u.

    The implemented algorithm progressively iterates the above equations, until
Tiago Peixoto's avatar
Tiago Peixoto committed
123
    it no longer changes, according to the parameter epsilon. It has a
124 125 126 127 128 129
    topology-dependent running time.

    If enabled during compilation, this algorithm runs in parallel.

    Examples
    --------
130
    >>> from numpy.random import random, poisson, seed
131
    >>> seed(42)
132
    >>> g = gt.random_graph(100, lambda: (poisson(3), poisson(3)))
133
    >>> pr = gt.pagerank(g)
134
    >>> print pr.a
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    [ 0.00867754  0.00729246  0.00363279  0.00668265  0.0015      0.00859964
      0.00449637  0.00961946  0.01295288  0.00882362  0.00719256  0.00280697
      0.00518114  0.01047904  0.00569656  0.00519058  0.00759745  0.00700835
      0.00870244  0.00522561  0.00233159  0.00236035  0.0015      0.00255374
      0.00872139  0.00227483  0.00686341  0.001755    0.00488567  0.01045994
      0.00393206  0.00988283  0.01376133  0.00721883  0.01429166  0.00752748
      0.01846797  0.00674401  0.00412138  0.00842639  0.0015      0.00233159
      0.00306271  0.01902149  0.0099247   0.00428981  0.00215072  0.01123842
      0.00236035  0.00768803  0.00463719  0.01130437  0.00392423  0.00491263
      0.00899519  0.00680983  0.0091988   0.00459334  0.00809094  0.00881614
      0.01381946  0.00489171  0.00425249  0.01957383  0.00708763  0.0015
      0.00418613  0.00607306  0.01287535  0.00639268  0.01578391  0.0015
      0.01541987  0.00860721  0.01378758  0.0173314   0.00775072  0.00247939
      0.00524088  0.00686587  0.00436895  0.00755964  0.00708251  0.0015
      0.00226438  0.00184085  0.00555171  0.01159494  0.01297596  0.00460887
      0.00406717  0.00578091  0.00548516  0.01197071  0.00674202  0.01011666
      0.01072786  0.00646937  0.01430012  0.01483996]
152 153 154 155 156 157 158 159 160 161

    Now with a personalization vector, and edge weights:

    >>> w = g.new_edge_property("double")
    >>> w.a = random(g.num_edges())
    >>> p = g.new_vertex_property("double")
    >>> p.a = random(g.num_vertices())
    >>> p.a /= p.a.sum()
    >>> pr = gt.pagerank(g, pers=p, weight=w)
    >>> print pr.a
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    [ 0.00761942  0.00761689  0.00418938  0.00947758  0.00092715  0.00349991
      0.00811226  0.00448968  0.01209889  0.01384828  0.00600036  0.00221745
      0.00432908  0.01036427  0.00536132  0.00692364  0.00575216  0.00750936
      0.00924536  0.00461255  0.00422277  0.00055639  0.00250025  0.00289125
      0.00925617  0.003356    0.00642017  0.00298276  0.00571097  0.01115541
      0.00452616  0.01670105  0.01788592  0.00580217  0.01350007  0.00837655
      0.01535733  0.00497981  0.00436008  0.01324374  0.00148972  0.00287379
      0.00408663  0.02785282  0.00790422  0.00491795  0.00070143  0.00789247
      0.00033551  0.00777089  0.00278393  0.00801468  0.00452296  0.00378295
      0.00642244  0.00698618  0.01069855  0.0019177   0.00742151  0.00872767
      0.01868187  0.00442359  0.00593616  0.01517386  0.00712472  0.00238803
      0.00468324  0.0024983   0.011788    0.00577489  0.01242015  0.00300867
      0.01390361  0.00796192  0.00822753  0.01062897  0.00815637  0.00332914
      0.00911336  0.00915715  0.00945334  0.00880299  0.00758402  0.0009367
      0.00378413  0.00174124  0.00283594  0.00929262  0.01090867  0.00460206
      0.00341061  0.00699703  0.00232131  0.01244958  0.00731098  0.01288061
      0.00820259  0.00430521  0.01633379  0.0119308 ]
179 180 181

    References
    ----------
182 183
    .. [pagerank-wikipedia] http://en.wikipedia.org/wiki/Pagerank
    .. [lawrence-pagerank-1998] P. Lawrence, B. Sergey, M. Rajeev, W. Terry,
184
       "The pagerank citation ranking: Bringing order to the web", Technical
185
       report, Stanford University, 1998
186 187 188
    .. [Langville-survey-2005] A. N. Langville, C. D. Meyer, "A Survey of
       Eigenvector Methods for Web Information Retrieval", SIAM Review, vol. 47,
       no. 1, pp. 135-161, 2005, :DOI:`10.1137/S0036144503424786`
189 190 191 192
    """

    if max_iter == None:
        max_iter = 0
Tiago Peixoto's avatar
Tiago Peixoto committed
193 194 195
    if prop == None:
        prop = g.new_vertex_property("double")
    ic = libgraph_tool_centrality.\
196 197 198
            get_pagerank(g._Graph__graph, _prop("v", g, prop),
                         _prop("v", g, pers), _prop("e", g, weight),
                         damping, epsilon, max_iter)
Tiago Peixoto's avatar
Tiago Peixoto committed
199 200 201 202 203
    if ret_iter:
        return prop, ic
    else:
        return prop

Tiago Peixoto's avatar
Tiago Peixoto committed
204

205 206 207 208 209 210
def betweenness(g, vprop=None, eprop=None, weight=None, norm=True):
    r"""
    Calculate the betweenness centrality for each vertex and edge.

    Parameters
    ----------
211
    g : :class:`~graph_tool.Graph`
212
        Graph to be used.
213
    vprop : :class:`~graph_tool.PropertyMap`, optional (default: None)
214
        Vertex property map to store the vertex betweenness values.
215
    eprop : :class:`~graph_tool.PropertyMap`, optional (default: None)
216
        Edge property map to store the edge betweenness values.
217
    weight : :class:`~graph_tool.PropertyMap`, optional (default: None)
218 219 220 221 222 223
        Edge property map corresponding to the weight value of each edge.
    norm : bool, optional (default: True)
        Whether or not the betweenness values should be normalized.

    Returns
    -------
Tiago Peixoto's avatar
Tiago Peixoto committed
224 225
    vertex_betweenness : A vertex property map with the vertex betweenness values.
    edge_betweenness : An edge property map with the edge betweenness values.
226 227 228 229 230 231

    See Also
    --------
    central_point_dominance: central point dominance of the graph
    pagerank: PageRank centrality
    eigentrust: eigentrust centrality
232
    trust_transitivity: pervasive trust transitivity
233 234 235 236 237

    Notes
    -----
    Betweenness centrality of a vertex :math:`C_B(v)` is defined as,

238 239
    .. math::

240 241 242 243 244 245 246 247 248
        C_B(v)= \sum_{s \neq v \neq t \in V \atop s \neq t}
                \frac{\sigma_{st}(v)}{\sigma_{st}}

    where :math:`\sigma_{st}` is the number of shortest geodesic paths from s to
    t, and :math:`\sigma_{st}(v)` is the number of shortest geodesic paths from
    s to t that pass through a vertex v.  This may be normalised by dividing
    through the number of pairs of vertices not including v, which is
    :math:`(n-1)(n-2)/2`.

249
    The algorithm used here is defined in [brandes-faster-2001]_, and has a
250 251 252 253 254 255 256
    complexity of :math:`O(VE)` for unweighted graphs and :math:`O(VE + V(V+E)
    \log V)` for weighted graphs. The space complexity is :math:`O(VE)`.

    If enabled during compilation, this algorithm runs in parallel.

    Examples
    --------
257 258
    >>> from numpy.random import poisson, seed
    >>> seed(42)
259
    >>> g = gt.random_graph(100, lambda: (poisson(3), poisson(3)))
260
    >>> vb, eb = gt.betweenness(g)
261
    >>> print vb.a
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
    [ 0.02412512  0.08233748  0.01789612  0.03997773  0.          0.03476439
      0.03490215  0.02812729  0.05385124  0.01614861  0.01894254  0.03552189
      0.01648793  0.02743878  0.02243743  0.0052126   0.          0.02648145
      0.05045875  0.01670867  0.00027069  0.00235053  0.          0.00424986
      0.02153982  0.01635659  0.03177692  0.00152088  0.02099129  0.05311383
      0.00802715  0.          0.06129706  0.03148129  0.10265395  0.02762436
      0.05323276  0.028209    0.01641328  0.03547918  0.          0.00998142
      0.01043084  0.06810444  0.01435047  0.02884138  0.          0.02336079
      0.          0.09098673  0.02911358  0.05909676  0.01314448  0.0304931
      0.01315283  0.03795536  0.02756845  0.020655    0.00268182  0.0151298
      0.05597887  0.04095171  0.00853146  0.06176456  0.03165429  0.          0.0114488
      0.05435076  0.04046437  0.0122299   0.03417369  0.          0.05979601
      0.01502672  0.05908044  0.09567821  0.01206665  0.01259951  0.02381255
      0.          0.01840359  0.00708731  0.          0.          0.01274922
      0.00385357  0.          0.11470908  0.04903743  0.03336991  0.01270614
      0.00448173  0.02163365  0.06685948  0.01032924  0.02400925  0.          0.0300603
      0.03220004  0.03932736]
279 280 281

    References
    ----------
282 283
    .. [betweenness-wikipedia] http://en.wikipedia.org/wiki/Centrality#Betweenness_centrality
    .. [brandes-faster-2001] U. Brandes, "A faster algorithm for betweenness
Tiago Peixoto's avatar
Tiago Peixoto committed
284
       centrality", Journal of Mathematical Sociology, 2001, :doi:`10.1080/0022250X.2001.9990249`
285
    """
Tiago Peixoto's avatar
Tiago Peixoto committed
286 287 288 289 290 291 292 293 294 295 296 297 298
    if vprop == None:
        vprop = g.new_vertex_property("double")
    if eprop == None:
        eprop = g.new_edge_property("double")
    if weight != None and weight.value_type() != eprop.value_type():
        nw = g.new_edge_property(eprop.value_type())
        g.copy_property(weight, nw)
        weight = nw
    libgraph_tool_centrality.\
            get_betweenness(g._Graph__graph, _prop("e", g, weight),
                            _prop("e", g, eprop), _prop("v", g, vprop), norm)
    return vprop, eprop

Tiago Peixoto's avatar
Tiago Peixoto committed
299

Tiago Peixoto's avatar
Tiago Peixoto committed
300
def central_point_dominance(g, betweenness):
301 302 303 304 305 306
    r"""
    Calculate the central point dominance of the graph, given the betweenness
    centrality of each vertex.

    Parameters
    ----------
307
    g : :class:`~graph_tool.Graph`
308
        Graph to be used.
309
    betweenness : :class:`~graph_tool.PropertyMap`
310 311 312 313 314
        Vertex property map with the betweenness centrality values. The values
        must be normalized.

    Returns
    -------
315 316
    cp : float
        The central point dominance.
317 318 319 320 321 322 323 324

    See Also
    --------
    betweenness: betweenness centrality

    Notes
    -----
    Let :math:`v^*` be the vertex with the largest relative betweenness
325
    centrality; then, the central point dominance [freeman-set-1977]_ is defined
326 327
    as:

328 329
    .. math::

330 331 332 333 334 335 336 337 338
        C'_B = \frac{1}{|V|-1} \sum_{v} C_B(v^*) - C_B(v)

    where :math:`C_B(v)` is the normalized betweenness centrality of vertex
    v. The value of :math:`C_B` lies in the range [0,1].

    The algorithm has a complexity of :math:`O(V)`.

    Examples
    --------
339 340
    >>> from numpy.random import poisson, seed
    >>> seed(42)
341
    >>> g = gt.random_graph(100, lambda: (poisson(3), poisson(3)))
342 343
    >>> vb, eb = gt.betweenness(g)
    >>> print gt.central_point_dominance(g, vb)
344
    0.0888588565184
345 346 347

    References
    ----------
348
    .. [freeman-set-1977] Linton C. Freeman, "A Set of Measures of Centrality
Tiago Peixoto's avatar
Tiago Peixoto committed
349 350
       Based on Betweenness", Sociometry, Vol. 40, No. 1,  pp. 35-41, 1977,
       `http://www.jstor.org/stable/3033543 <http://www.jstor.org/stable/3033543>`_
351 352
    """

Tiago Peixoto's avatar
Tiago Peixoto committed
353
    return libgraph_tool_centrality.\
354
           get_central_point_dominance(g._Graph__graph,
Tiago Peixoto's avatar
Tiago Peixoto committed
355 356
                                       _prop("v", g, betweenness))

357

358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
def eigenvector(g, weight=None, vprop=None, epsilon=1e-6, max_iter=None):
    r"""
    Calculate the eigenvector centrality of each vertex in the graph, as well as
    the largest eigenvalue.

    Parameters
    ----------
    g : :class:`~graph_tool.Graph`
        Graph to be used.
    weights : :class:`~graph_tool.PropertyMap` (optional, default: ``None``)
        Edge property map with the edge weights.
    vprop : :class:`~graph_tool.PropertyMap`, optional (default: ``None``)
        Vertex property map where the values of eigenvector must be stored.
    epsilon : float, optional (default: ``1e-6``)
        Convergence condition. The iteration will stop if the total delta of all
        vertices are below this value.
    max_iter : int, optional (default: ``None``)
        If supplied, this will limit the total number of iterations.

    Returns
    -------
    eigenvalue : float
        The largest eigenvalue of the (weighted) adjacency matrix.
    eigenvector : :class:`~graph_tool.PropertyMap`
        A vertex property map containing the eigenvector values.

    See Also
    --------
    betweenness: betweenness centrality
    pagerank: PageRank centrality
    trust_transitivity: pervasive trust transitivity

    Notes
    -----

    The eigenvector centrality :math:`\mathbf{x}` is the eigenvector of the
    (weighted) adjacency matrix with the largest eigenvalue :math:`\lambda`,
    i.e. it is the solution of

    .. math::

        \mathbf{A}\mathbf{x} = \lambda\mathbf{x},


    where :math:`\mathbf{A}` is the (weighted) adjacency matrix and
    :math:`\lambda` is the largest eigenvalue.

    The algorithm uses the power method which has a topology-dependent complexity of
    :math:`O\left(N\times\frac{-\log\epsilon}{\log|\lambda_1/\lambda_2|}\right)`,
    where :math:`N` is the number of vertices, :math:`\epsilon` is the ``epsilon``
    parameter, and :math:`\lambda_1` and :math:`\lambda_2` are the largest and
    second largest eigenvalues of the (weighted) adjacency matrix, respectively.

    If enabled during compilation, this algorithm runs in parallel.

    Examples
    --------
    >>> from numpy.random import poisson, random, seed
    >>> seed(42)
    >>> g = gt.random_graph(100, lambda: (poisson(3), poisson(3)))
    >>> w = g.new_edge_property("double")
    >>> w.a = random(g.num_edges()) * 42
    >>> x = gt.eigenvector(g, w)
    >>> print x[0]
422
    0.0144966901652
423
    >>> print x[1].a
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
    [ 0.06930562  0.15259089  0.02186899  0.16605389  0.          0.03329417
      0.15389839  0.05956241  0.02364241  0.14555449  0.12223866  0.04284837
      0.02930074  0.12362235  0.08914056  0.06613553  0.04127041  0.06894616
      0.11768146  0.10576438  0.03184658  0.01097733  0.          0.00244834
      0.12373674  0.04521477  0.06729261  0.          0.0119752   0.09613809
      0.05371652  0.0760725   0.26342747  0.06901843  0.12261332  0.07428021
      0.16074381  0.04316101  0.01382325  0.07976272  0.          0.02358784
      0.04087066  0.22404933  0.05037626  0.07311321  0.00118498  0.10162138
      0.00695398  0.15475802  0.03601454  0.12771193  0.00204537  0.00850904
      0.06338004  0.11785637  0.171928    0.00577511  0.08163299  0.02358024
      0.25112631  0.05618482  0.00868661  0.15998601  0.11513557  0.
      0.07307991  0.06984483  0.1043859   0.15786829  0.12612902  0.
      0.11247494  0.06210605  0.07547538  0.09774456  0.01932     0.01132741
      0.07459586  0.14437367  0.06477981  0.13211211  0.04886609  0.
      0.09043424  0.00644994  0.0112379   0.10472704  0.2077468   0.04146317
      0.04745921  0.07442587  0.00757032  0.18575827  0.09975438  0.11682436
      0.22233397  0.08072367  0.13527766  0.16050936]
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463

    References
    ----------

    .. [eigenvector-centrality] http://en.wikipedia.org/wiki/Centrality#Eigenvector_centrality
    .. [power-method] http://en.wikipedia.org/wiki/Power_iteration
    .. [langville-survey-2005] A. N. Langville, C. D. Meyer, "A Survey of
       Eigenvector Methods for Web Information Retrieval", SIAM Review, vol. 47,
       no. 1, pp. 135-161, 2005, :DOI:`10.1137/S0036144503424786`


    """

    if vprop == None:
        vprop = g.new_vertex_property("double")
    if max_iter is None:
        max_iter = 0
    ee = libgraph_tool_centrality.\
         get_eigenvector(g._Graph__graph, _prop("e", g, weight),
                         _prop("v", g, vprop), epsilon, max_iter)
    return ee, vprop


Tiago Peixoto's avatar
Tiago Peixoto committed
464
def eigentrust(g, trust_map, vprop=None, norm=False, epsilon=1e-6, max_iter=0,
Tiago Peixoto's avatar
Tiago Peixoto committed
465
               ret_iter=False):
466 467 468 469 470
    r"""
    Calculate the eigentrust centrality of each vertex in the graph.

    Parameters
    ----------
471
    g : :class:`~graph_tool.Graph`
472
        Graph to be used.
473
    trust_map : :class:`~graph_tool.PropertyMap`
474
        Edge property map with the values of trust associated with each
475
        edge. The values must lie in the range [0,1].
476
    vprop : :class:`~graph_tool.PropertyMap`, optional (default: ``None``)
477
        Vertex property map where the values of eigentrust must be stored.
478
    norm : bool, optional (default:  ``False``)
479
        Norm eigentrust values so that the total sum equals 1.
480
    epsilon : float, optional (default: ``1e-6``)
481 482
        Convergence condition. The iteration will stop if the total delta of all
        vertices are below this value.
483
    max_iter : int, optional (default: ``None``)
484
        If supplied, this will limit the total number of iterations.
485
    ret_iter : bool, optional (default: ``False``)
486 487 488 489
        If true, the total number of iterations is also returned.

    Returns
    -------
490 491
    eigentrust : :class:`~graph_tool.PropertyMap`
        A vertex property map containing the eigentrust values.
492 493 494 495 496

    See Also
    --------
    betweenness: betweenness centrality
    pagerank: PageRank centrality
497
    trust_transitivity: pervasive trust transitivity
498 499 500

    Notes
    -----
501
    The eigentrust [kamvar-eigentrust-2003]_ values :math:`t_i` correspond the
502 503
    following limit

504 505
    .. math::

506 507 508 509 510
        \mathbf{t} = \lim_{n\to\infty} \left(C^T\right)^n \mathbf{c}

    where :math:`c_i = 1/|V|` and the elements of the matrix :math:`C` are the
    normalized trust values:

511 512
    .. math::

513 514 515 516 517 518 519 520 521 522
        c_{ij} = \frac{\max(s_{ij},0)}{\sum_{j} \max(s_{ij}, 0)}

    The algorithm has a topology-dependent complexity.

    If enabled during compilation, this algorithm runs in parallel.

    Examples
    --------
    >>> from numpy.random import poisson, random, seed
    >>> seed(42)
523
    >>> g = gt.random_graph(100, lambda: (poisson(3), poisson(3)))
524
    >>> trust = g.new_edge_property("double")
525
    >>> trust.a = random(g.num_edges())*42
526
    >>> t = gt.eigentrust(g, trust, norm=True)
527
    >>> print t.a
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
    [ 0.01047704  0.00916079  0.00329728  0.02183509  0.          0.00570515
      0.01795154  0.00745325  0.01556352  0.02103068  0.00766733  0.0017898
      0.00477604  0.02075469  0.00596795  0.00777273  0.00486571  0.00922539
      0.00947171  0.00808096  0.00159634  0.00039577  0.          0.00170102
      0.01221233  0.00105577  0.00499953  0.          0.00104692  0.02124238
      0.00460677  0.02377163  0.03682221  0.00788995  0.01588396  0.01090949
      0.01738658  0.00629269  0.00252249  0.01494837  0.          0.00118236
      0.00086828  0.04746846  0.01541295  0.00777287  0.00014366  0.01079173
      0.00025071  0.0110701   0.0041122   0.00931269  0.0003396   0.00085525
      0.00715489  0.01178172  0.01968206  0.00086696  0.01155932  0.01291423
      0.03650986  0.00409654  0.00635913  0.02706258  0.01329375  0.          0.004781
      0.00393831  0.0159743   0.01175119  0.01924371  0.          0.01624875
      0.01213196  0.01327894  0.02122698  0.01120801  0.00261826  0.01197119
      0.01071532  0.01065604  0.00891656  0.00394593  0.          0.00519856
      0.00013063  0.00206336  0.01471883  0.02029324  0.00793793  0.00560927
      0.00944371  0.00205155  0.01610597  0.009506    0.0211548   0.01688137
      0.00731456  0.0175582   0.02643281]
545 546 547

    References
    ----------
548
    .. [kamvar-eigentrust-2003] S. D. Kamvar, M. T. Schlosser, H. Garcia-Molina
549 550
       "The eigentrust algorithm for reputation management in p2p networks",
       Proceedings of the 12th international conference on World Wide Web,
Tiago Peixoto's avatar
Tiago Peixoto committed
551
       Pages: 640 - 651, 2003, :doi:`10.1145/775152.775242`
552 553
    """

Tiago Peixoto's avatar
Tiago Peixoto committed
554 555
    if vprop == None:
        vprop = g.new_vertex_property("double")
556 557
    i = libgraph_tool_centrality.\
           get_eigentrust(g._Graph__graph, _prop("e", g, trust_map),
Tiago Peixoto's avatar
Tiago Peixoto committed
558
                          _prop("v", g, vprop), epsilon, max_iter)
559 560 561 562 563 564 565 566
    if norm:
        vprop.get_array()[:] /= sum(vprop.get_array())

    if ret_iter:
        return vprop, i
    else:
        return vprop

Tiago Peixoto's avatar
Tiago Peixoto committed
567

568
def trust_transitivity(g, trust_map, source=None, target=None, vprop=None):
569
    r"""
570 571
    Calculate the pervasive trust transitivity between chosen (or all) vertices
    in the graph.
572 573 574

    Parameters
    ----------
575
    g : :class:`~graph_tool.Graph`
576
        Graph to be used.
577
    trust_map : :class:`~graph_tool.PropertyMap`
578 579
        Edge property map with the values of trust associated with each
        edge. The values must lie in the range [0,1].
Tiago Peixoto's avatar
Tiago Peixoto committed
580
    source : :class:`~graph_tool.Vertex` (optional, default: None)
581
        Source vertex. All trust values are computed relative to this vertex.
582
        If left unspecified, the trust values for all sources are computed.
Tiago Peixoto's avatar
Tiago Peixoto committed
583
    target : :class:`~graph_tool.Vertex` (optional, default: None)
584 585 586
        The only target for which the trust value will be calculated. If left
        unspecified, the trust values for all targets are computed.
    vprop : :class:`~graph_tool.PropertyMap` (optional, default: None)
587 588
        A vertex property map where the values of transitive trust must be
        stored.
589 590 591

    Returns
    -------
592 593 594 595 596 597 598 599
    trust_transitivity : :class:`~graph_tool.PropertyMap` or float
        A vertex vector property map containing, for each source vertex, a
        vector with the trust values for the other vertices. If only one of
        `source` or `target` is specified, this will be a single-valued vertex
        property map containing the trust vector from/to the source/target
        vertex to/from the rest of the network. If both `source` and `target`
        are specified, the result is a single float, with the corresponding
        trust value for the target.
600

601 602 603 604 605 606 607 608
    See Also
    --------
    eigentrust: eigentrust centrality
    betweenness: betweenness centrality
    pagerank: PageRank centrality

    Notes
    -----
Tiago Peixoto's avatar
Tiago Peixoto committed
609
    The pervasive trust transitivity between vertices i and j is defined as
610

611 612
    .. math::

613 614
        t_{ij} = \frac{\sum_m A_{m,j} w^2_{G\setminus\{j\}}(i\to m)c_{m,j}}
                 {\sum_m A_{m,j} w_{G\setminus\{j\}}(i\to m)}
615

616 617 618
    where :math:`A_{ij}` is the adjacency matrix, :math:`c_{ij}` is the direct
    trust from i to j, and :math:`w_G(i\to j)` is the weight of the path with
    maximum weight from i to j, computed as
Tiago Peixoto's avatar
Tiago Peixoto committed
619

620 621
    .. math::

622
       w_G(i\to j) = \prod_{e\in i\to j} c_e.
623

624 625
    The algorithm measures the transitive trust by finding the paths with
    maximum weight, using Dijkstra's algorithm, to all in-neighbours of a given
626
    target. This search needs to be performed repeatedly for every target, since
627 628 629 630 631 632 633
    it needs to be removed from the graph first. For each given source, the
    resulting complexity is therefore :math:`O(N^2\log N)` for all targets, and
    :math:`O(N\log N)` for a single target. For a given target, the complexity
    for obtaining the trust from all given sources is :math:`O(kN\log N)`, where
    :math:`k` is the in-degree of the target. Thus, the complexity for obtaining
    the complete trust matrix is :math:`O(EN\log N)`, where :math:`E` is the
    number of edges in the network.
634 635 636 637 638 639 640

    If enabled during compilation, this algorithm runs in parallel.

    Examples
    --------
    >>> from numpy.random import poisson, random, seed
    >>> seed(42)
641
    >>> g = gt.random_graph(100, lambda: (poisson(3), poisson(3)))
642
    >>> trust = g.new_edge_property("double")
643
    >>> trust.a = random(g.num_edges())
644
    >>> t = gt.trust_transitivity(g, trust, source=g.vertex(0))
645
    >>> print t.a
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
    [ 1.          0.05067451  0.0289009   0.08405839  0.          0.03545782
      0.07538031  0.22379499  0.05854478  0.07125532  0.05587198  0.02518448
      0.02466963  0.15362921  0.06018343  0.05567524  0.27358358  0.05275904
      0.03729045  0.09492006  0.05238415  0.00737096  0.          0.00510552
      0.08817629  0.03785795  0.02446122  0.          0.00738919  0.05119031
      0.34527065  0.03114924  0.07197484  0.47425602  0.03472084  0.04662672
      0.05560849  0.02364994  0.01215894  0.31788601  0.          0.03879942
      0.01857147  0.06889803  0.04377159  0.08730759  0.00246369  0.39098931
      0.0046694   0.04502054  0.02463968  0.02645761  0.00707666  0.00472783
      0.02984028  0.1148617   0.0572029   0.00675625  0.02800698  0.12556046
      0.08124247  0.05626964  0.06027068  0.04269226  0.07518783  0.
      0.02654427  0.013547    0.35126413  0.05338214  0.02683736  0.
      0.03967174  0.15764245  0.03434911  0.06712879  0.01081633  0.01438191
      0.22453002  0.13426137  0.11622003  0.18696878  0.02270863  0.
      0.09096154  0.00622639  0.01807092  0.06891202  0.05429632  0.03123112
      0.04239384  0.06347529  0.00980117  0.08103904  0.06022526  0.09774797
      0.0442361   0.03511221  0.09291923  0.10395899]
Tiago Peixoto's avatar
Tiago Peixoto committed
663 664 665

    References
    ----------
666 667 668
    .. [richters-trust-2010] Oliver Richters and Tiago P. Peixoto, "Trust
       Transitivity in Social Networks," PLoS ONE 6, no. 4:
       e1838 (2011), :doi:`10.1371/journal.pone.0018384`
Tiago Peixoto's avatar
Tiago Peixoto committed
669

670
    """
Tiago Peixoto's avatar
Tiago Peixoto committed
671 672

    if vprop == None:
673
        vprop = g.new_vertex_property("vector<double>")
674

675 676 677 678
    if target == None:
        target = -1
    else:
        target = g.vertex_index[target]
679

680 681 682 683 684
    if source == None:
        source = -1
    else:
        source = g.vertex_index[source]

685
    libgraph_tool_centrality.\
686 687 688 689
            get_trust_transitivity(g._Graph__graph, source, target,
                                   _prop("e", g, trust_map),
                                   _prop("v", g, vprop))
    if target != -1 or source != -1:
690
        vprop = ungroup_vector_property(vprop, [0])[0]
691
    if target != -1 and source != -1:
692
        return vprop.a[target]
693
    return vprop