__init__.py 41.5 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-2012 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
Tiago Peixoto's avatar
Tiago Peixoto committed
37
   katz
38
   hits
39
   eigentrust
40
   trust_transitivity
41 42 43

Contents
++++++++
44 45
"""

46 47
from __future__ import division, absolute_import, print_function

Tiago Peixoto's avatar
Tiago Peixoto committed
48
from .. dl_import import dl_import
49
dl_import("from . import libgraph_tool_centrality")
Tiago Peixoto's avatar
Tiago Peixoto committed
50

51
from .. import _prop, ungroup_vector_property
Tiago Peixoto's avatar
Tiago Peixoto committed
52 53
import sys
import numpy
Tiago Peixoto's avatar
Tiago Peixoto committed
54 55

__all__ = ["pagerank", "betweenness", "central_point_dominance", "eigentrust",
Tiago Peixoto's avatar
Tiago Peixoto committed
56
           "eigenvector", "katz", "hits", "trust_transitivity"]
Tiago Peixoto's avatar
Tiago Peixoto committed
57

Tiago Peixoto's avatar
Tiago Peixoto committed
58

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

    Parameters
    ----------
66
    g : :class:`~graph_tool.Graph`
67
        Graph to be used.
68
    damping : float, optional (default: 0.85)
69
        Damping factor.
70 71 72 73 74
    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.
75
    prop : :class:`~graph_tool.PropertyMap`, optional (default: None)
76 77
        Vertex property map to store the PageRank values. If supplied, it will
        be used uninitialized.
Tiago Peixoto's avatar
Tiago Peixoto committed
78
    epsilon : float, optional (default: 1e-6)
79 80 81 82 83 84 85 86 87
        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
    -------
88 89
    pagerank : :class:`~graph_tool.PropertyMap`
        A vertex property map containing the PageRank values.
90 91 92 93 94

    See Also
    --------
    betweenness: betweenness centrality
    eigentrust: eigentrust centrality
95 96
    eigenvector: eigenvector centrality
    hits: hubs and authority centralities
97
    trust_transitivity: pervasive trust transitivity
98 99 100

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

    .. math::
105

106 107
        PR(v) = \frac{1-d}{N} + d \sum_{u \in \Gamma^{-}(v)}
                \frac{PR (u)}{d^{+}(u)}
108 109 110 111

    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.

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    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
130
    it no longer changes, according to the parameter epsilon. It has a
131 132 133 134 135 136
    topology-dependent running time.

    If enabled during compilation, this algorithm runs in parallel.

    Examples
    --------
137
    >>> from numpy.random import random, poisson, seed
138
    >>> seed(42)
139
    >>> g = gt.random_graph(100, lambda: (poisson(3), poisson(3)))
140
    >>> pr = gt.pagerank(g)
141
    >>> print(pr.a)
Tiago Peixoto's avatar
Tiago Peixoto committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    [ 0.00865316  0.0054067   0.00406312  0.00426668  0.0015      0.00991696
      0.00550065  0.00936397  0.00347917  0.00731864  0.00689843  0.00286274
      0.00508731  0.01020047  0.00562247  0.00584915  0.02457086  0.00438568
      0.0057385   0.00621745  0.001755    0.0045073   0.0015      0.00225167
      0.00698342  0.00206302  0.01094466  0.001925    0.00710093  0.00519877
      0.00460646  0.00994648  0.01005248  0.00904629  0.00676221  0.00789208
      0.00933103  0.00301154  0.00264951  0.00842812  0.0015      0.00191034
      0.00594069  0.00884372  0.00453417  0.00388987  0.00317433  0.0086067
      0.00385394  0.00672702  0.00258411  0.01468262  0.00454     0.00381159
      0.00402607  0.00451133  0.00480966  0.00811557  0.00571949  0.00317433
      0.00856838  0.00280517  0.00280563  0.00906324  0.00614421  0.0015
      0.00292034  0.00479769  0.00552694  0.00604799  0.0115922   0.0015
      0.00676183  0.00695336  0.01023352  0.01737541  0.00451443  0.00197688
      0.00553866  0.00486233  0.0078653   0.00867599  0.01248092  0.0015
      0.00399605  0.00399605  0.00881571  0.00638008  0.01056944  0.00353724
      0.00249869  0.00684919  0.00241374  0.01061397  0.00673569  0.00590937
      0.01004638  0.00331612  0.00926359  0.00460809]
159 160 161 162 163 164 165 166 167

    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)
168
    >>> print(pr.a)
Tiago Peixoto's avatar
Tiago Peixoto committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    [ 0.00712999  0.00663336  0.00685722  0.00402663  0.00092715  0.01021926
      0.00269502  0.0073301   0.00449892  0.00582793  0.00580542  0.00275149
      0.00676363  0.01157972  0.00486918  0.00616345  0.02506695  0.00607967
      0.00553375  0.00359075  0.00293808  0.00362247  0.00250025  0.00186946
      0.00895516  0.00318147  0.01489786  0.00312436  0.0074751   0.0040342
      0.006254    0.00687051  0.0098073   0.01076278  0.00887077  0.00806759
      0.00969532  0.00252648  0.00278688  0.00972144  0.00148972  0.00215428
      0.00713602  0.00559849  0.00495517  0.00457118  0.00323767  0.01257406
      0.00120179  0.00514838  0.00130655  0.01724465  0.00343819  0.00420962
      0.00297617  0.00588287  0.00657206  0.00775082  0.00758217  0.00433776
      0.00576829  0.00464595  0.00307274  0.00585795  0.00745881  0.00238803
      0.00230431  0.00437046  0.00492464  0.00275414  0.01524646  0.00300867
      0.00816665  0.00548853  0.00874738  0.01871498  0.00216776  0.00245196
      0.00308878  0.00646323  0.01287978  0.00911384  0.01628604  0.0009367
      0.00222119  0.00864202  0.01199119  0.01126539  0.01086846  0.00309224
      0.0020319   0.00659422  0.00226965  0.0134399   0.01094141  0.00732916
      0.00489314  0.0030402   0.00783914  0.00278588]
186 187 188

    References
    ----------
189 190
    .. [pagerank-wikipedia] http://en.wikipedia.org/wiki/Pagerank
    .. [lawrence-pagerank-1998] P. Lawrence, B. Sergey, M. Rajeev, W. Terry,
191
       "The pagerank citation ranking: Bringing order to the web", Technical
192
       report, Stanford University, 1998
193 194 195
    .. [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`
196 197 198 199
    """

    if max_iter == None:
        max_iter = 0
Tiago Peixoto's avatar
Tiago Peixoto committed
200 201
    if prop == None:
        prop = g.new_vertex_property("double")
202 203
        N = len(prop.a)
        prop.a = pers.a[:N] if pers is not None else 1. / g.num_vertices()
Tiago Peixoto's avatar
Tiago Peixoto committed
204
    ic = libgraph_tool_centrality.\
205 206 207
            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
208 209 210 211 212
    if ret_iter:
        return prop, ic
    else:
        return prop

Tiago Peixoto's avatar
Tiago Peixoto committed
213

214 215 216 217 218 219
def betweenness(g, vprop=None, eprop=None, weight=None, norm=True):
    r"""
    Calculate the betweenness centrality for each vertex and edge.

    Parameters
    ----------
220
    g : :class:`~graph_tool.Graph`
221
        Graph to be used.
222
    vprop : :class:`~graph_tool.PropertyMap`, optional (default: None)
223
        Vertex property map to store the vertex betweenness values.
224
    eprop : :class:`~graph_tool.PropertyMap`, optional (default: None)
225
        Edge property map to store the edge betweenness values.
226
    weight : :class:`~graph_tool.PropertyMap`, optional (default: None)
227 228 229 230 231 232
        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
233 234
    vertex_betweenness : A vertex property map with the vertex betweenness values.
    edge_betweenness : An edge property map with the edge betweenness values.
235 236 237 238 239 240

    See Also
    --------
    central_point_dominance: central point dominance of the graph
    pagerank: PageRank centrality
    eigentrust: eigentrust centrality
241 242
    eigenvector: eigenvector centrality
    hits: hubs and authority centralities
243
    trust_transitivity: pervasive trust transitivity
244 245 246 247 248

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

249 250
    .. math::

251 252 253 254 255 256 257 258 259
        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`.

260
    The algorithm used here is defined in [brandes-faster-2001]_, and has a
261 262 263 264 265 266 267
    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
    --------
268 269
    >>> from numpy.random import poisson, seed
    >>> seed(42)
270
    >>> g = gt.random_graph(100, lambda: (poisson(3), poisson(3)))
271
    >>> vb, eb = gt.betweenness(g)
272
    >>> print(vb.a)
Tiago Peixoto's avatar
Tiago Peixoto committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
    [ 0.04889806  0.07181892  0.0256799   0.02885791  0.          0.05060927
      0.04490836  0.03763462  0.02033383  0.03163202  0.02641248  0.03171598
      0.03771112  0.02194663  0.0374907   0.01072567  0.          0.03079281
      0.05409258  0.00163434  0.00051978  0.01045902  0.          0.00796784
      0.0494527   0.00647576  0.03708252  0.00304503  0.0663657   0.03903257
      0.03305169  0.          0.07787098  0.03938866  0.08577116  0.020183
      0.06024004  0.01004935  0.0443127   0.06397736  0.          0.00363548
      0.01742486  0.03216543  0.01918144  0.02059159  0.          0.01476213
      0.          0.0466751   0.01072612  0.10288046  0.00563973  0.03850413
      0.00629595  0.01292137  0.0537963   0.04454985  0.01227018  0.00729488
      0.02092959  0.02308238  0.00712703  0.02193975  0.03823342  0.
      0.00995364  0.04023839  0.0312708   0.0111312   0.00228516  0.
      0.09659583  0.01327402  0.05792071  0.08606828  0.0143541   0.00221604
      0.02144698  0.          0.04023879  0.00715758  0.          0.
      0.02348452  0.00760922  0.01486521  0.08132792  0.0382674   0.03078318
      0.00430209  0.01772787  0.02280666  0.0373011   0.03077511  0.02871265
      0.          0.01044655  0.04415432  0.04447525]
290 291 292

    References
    ----------
293 294
    .. [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
295
       centrality", Journal of Mathematical Sociology, 2001, :doi:`10.1080/0022250X.2001.9990249`
296
    """
Tiago Peixoto's avatar
Tiago Peixoto committed
297 298 299 300 301 302 303 304 305 306 307 308 309
    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
310

Tiago Peixoto's avatar
Tiago Peixoto committed
311
def central_point_dominance(g, betweenness):
312 313 314 315 316 317
    r"""
    Calculate the central point dominance of the graph, given the betweenness
    centrality of each vertex.

    Parameters
    ----------
318
    g : :class:`~graph_tool.Graph`
319
        Graph to be used.
320
    betweenness : :class:`~graph_tool.PropertyMap`
321 322 323 324 325
        Vertex property map with the betweenness centrality values. The values
        must be normalized.

    Returns
    -------
326 327
    cp : float
        The central point dominance.
328 329 330 331 332 333 334 335

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

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

339 340
    .. math::

341 342 343 344 345 346 347 348 349
        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
    --------
350 351
    >>> from numpy.random import poisson, seed
    >>> seed(42)
352
    >>> g = gt.random_graph(100, lambda: (poisson(3), poisson(3)))
353
    >>> vb, eb = gt.betweenness(g)
354
    >>> print(gt.central_point_dominance(g, vb))
Tiago Peixoto's avatar
Tiago Peixoto committed
355
    0.0766473408634
356 357 358

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

Tiago Peixoto's avatar
Tiago Peixoto committed
364
    return libgraph_tool_centrality.\
365
           get_central_point_dominance(g._Graph__graph,
Tiago Peixoto's avatar
Tiago Peixoto committed
366 367
                                       _prop("v", g, betweenness))

368

369 370 371 372 373 374 375 376 377
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.
378
    weight : :class:`~graph_tool.PropertyMap` (optional, default: ``None``)
379 380
        Edge property map with the edge weights.
    vprop : :class:`~graph_tool.PropertyMap`, optional (default: ``None``)
381 382
        Vertex property map where the values of eigenvector must be stored. If
        provided, it will be used uninitialized.
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
    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
400
    hits: hubs and authority centralities
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
    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)
434
    >>> print(x[0])
Tiago Peixoto's avatar
Tiago Peixoto committed
435
    0.0160851991895
436
    >>> print(x[1].a)
Tiago Peixoto's avatar
Tiago Peixoto committed
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
    [ 0.1376411   0.07207366  0.02727508  0.05805304  0.          0.10690994
      0.04315491  0.01040908  0.02300252  0.08874163  0.04968119  0.06718114
      0.05526028  0.20449371  0.02337425  0.07581173  0.19993899  0.14718912
      0.08464664  0.08474977  0.          0.04843894  0.          0.0089388
      0.16831573  0.00138653  0.11741616  0.          0.13455019  0.03642682
      0.06729803  0.06229526  0.08937098  0.05693976  0.0793375   0.04076743
      0.22176891  0.07717256  0.00518048  0.05722748  0.          0.00055799
      0.04541778  0.06420469  0.06189998  0.08011859  0.05377224  0.29979873
      0.01211309  0.15503588  0.02804072  0.1692873   0.01420732  0.02507
      0.02959899  0.02702304  0.1652933   0.01434992  0.1073001   0.04582697
      0.04618913  0.0220902   0.01421926  0.09891276  0.04522928  0.
      0.00236599  0.07686829  0.03243909  0.00346715  0.1954776   0.
      0.25583217  0.11710921  0.07804282  0.21188464  0.04800656  0.00321866
      0.0552824   0.11204116  0.11420818  0.24071304  0.15451676  0.
      0.00475456  0.10680434  0.17054333  0.18945499  0.15673649  0.03405238
      0.01653319  0.02563015  0.00186129  0.12061027  0.11449362  0.11114196
      0.06779788  0.00595725  0.09127559  0.02380386]
454 455 456 457 458 459 460 461 462 463 464 465 466

    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`


    """

467
    if vprop is None:
468
        vprop = g.new_vertex_property("double")
469
        vprop.a = 1. / g.num_vertices()
470 471 472 473 474 475 476 477
    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
478 479
def katz(g, alpha=0.01, beta=None, weight=None, vprop=None, epsilon=1e-6, max_iter=None):
    r"""
Tiago Peixoto's avatar
Tiago Peixoto committed
480
    Calculate the Katz centrality of each vertex in the graph.
Tiago Peixoto's avatar
Tiago Peixoto committed
481 482 483 484 485 486 487 488

    Parameters
    ----------
    g : :class:`~graph_tool.Graph`
        Graph to be used.
    weight : :class:`~graph_tool.PropertyMap` (optional, default: ``None``)
        Edge property map with the edge weights.
    alpha : float, optional (default: ``0.01``)
489 490
        Free parameter :math:`\alpha`. This must be smaller than the inverse of
        the largest eigenvalue of the adjacency matrix.
Tiago Peixoto's avatar
Tiago Peixoto committed
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
    beta : :class:`~graph_tool.PropertyMap`, optional (default: ``None``)
        Vertex property map where the local personalization values. If not
        provided, the global value of 1 will be used.
    vprop : :class:`~graph_tool.PropertyMap`, optional (default: ``None``)
        Vertex property map where the values of eigenvector must be stored. If
        provided, it will be used uninitialized.
    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
    -------
    centrality : :class:`~graph_tool.PropertyMap`
        A vertex property map containing the Katz centrality values.

    See Also
    --------
    betweenness: betweenness centrality
    pagerank: PageRank centrality
    eigenvector: eigenvector centrality
    hits: hubs and authority centralities
    trust_transitivity: pervasive trust transitivity

    Notes
    -----

    The Katz centrality :math:`\mathbf{x}` is the solution of the nonhomogeneous
    linear system

    .. math::

        \mathbf{x} = \alpha\mathbf{A}\mathbf{x} + \mathbf{\beta},


    where :math:`\mathbf{A}` is the (weighted) adjacency matrix and
    :math:`\mathbf{\beta}` is the personalization vector (if not supplied,
    :math:`\mathbf{\beta} = \mathbf{1}` is assumed).

    The algorithm uses successive iterations of the equation above, which has a
    topology-dependent convergence complexity.

    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
    >>> beta = g.new_vertex_property("double")
    >>> beta.a = random(g.num_vertices())
    >>> x = gt.katz(g, 1.2, beta, w)
    >>> print(x.a)
    [  1.37641115e-01   7.20736590e-02   2.72750802e-02   5.80530330e-02
       2.01730812e-46   1.06909945e-01   4.31549123e-02   1.04090757e-02
       2.30025193e-02   8.87416158e-02   4.96811868e-02   6.71811510e-02
       5.52602884e-02   2.04493707e-01   2.33742500e-02   7.58117387e-02
       1.99938991e-01   1.47189139e-01   8.46466442e-02   8.47497818e-02
       1.51237905e-45   4.84389342e-02   5.44009790e-46   8.93879711e-03
       1.68315739e-01   1.38653230e-03   1.17416178e-01   5.86769582e-45
       1.34550186e-01   3.64268155e-02   6.72980215e-02   6.22952571e-02
       8.93709730e-02   5.69397613e-02   7.93375017e-02   4.07674261e-02
       2.21768916e-01   7.71725431e-02   5.18047531e-03   5.72274765e-02
       3.24137925e-46   5.57993342e-04   4.54177794e-02   6.42046867e-02
       6.18999821e-02   8.01185834e-02   5.37722396e-02   2.99798712e-01
       1.21130890e-02   1.55035898e-01   2.80407226e-02   1.69287315e-01
       1.42073265e-02   2.50699989e-02   2.95989919e-02   2.70230452e-02
       1.65293284e-01   1.43499144e-02   1.07300107e-01   4.58269685e-02
       4.61891303e-02   2.20902054e-02   1.42192559e-02   9.89127698e-02
       4.52292816e-02   5.19593979e-46   2.36598546e-03   7.68682863e-02
       3.24390891e-02   3.46714702e-03   1.95477600e-01   6.54634726e-46
       2.55832162e-01   1.17109207e-01   7.80428298e-02   2.11884617e-01
       4.80065642e-02   3.21866466e-03   5.52824029e-02   1.12041157e-01
       1.14208195e-01   2.40713033e-01   1.54516765e-01   2.03810664e-46
       4.75455657e-03   1.06804336e-01   1.70543325e-01   1.89454987e-01
       1.56736484e-01   3.40523749e-02   1.65331867e-02   2.56301436e-02
       1.86129309e-03   1.20610273e-01   1.14493631e-01   1.11141961e-01
       6.77978870e-02   5.95724763e-03   9.12755850e-02   2.38038610e-02]

    References
    ----------

    .. [katz-centrality] http://en.wikipedia.org/wiki/Katz_centrality
    .. [katz-new] L. Katz, "A new status index derived from sociometric analysis",
       Psychometrika 18, Number 1, 39-43, 1953, :DOI:`10.1007/BF02289026`
    """

    if vprop == None:
        vprop = g.new_vertex_property("double")
        N = len(vprop.a)
        vprop.a = beta.a[:N] if beta is not None else 1.
    if max_iter is None:
        max_iter = 0
    ee = libgraph_tool_centrality.\
         get_katz(g._Graph__graph, _prop("e", g, weight), _prop("v", g, vprop),
         _prop("v", beta, vprop), float(alpha), epsilon, max_iter)
    return vprop


593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
def hits(g, weight=None, xprop=None, yprop=None, epsilon=1e-6, max_iter=None):
    r"""
    Calculate the authority and hub centralities of each vertex in the graph.

    Parameters
    ----------
    g : :class:`~graph_tool.Graph`
        Graph to be used.
    weight : :class:`~graph_tool.PropertyMap` (optional, default: ``None``)
        Edge property map with the edge weights.
    xprop : :class:`~graph_tool.PropertyMap`, optional (default: ``None``)
        Vertex property map where the authority centrality must be stored.
    yprop : :class:`~graph_tool.PropertyMap`, optional (default: ``None``)
        Vertex property map where the hub centrality 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
    -------
    eig : `float`
        The largest eigenvalue of the cocitation matrix.
    x : :class:`~graph_tool.PropertyMap`
        A vertex property map containing the authority centrality values.
    y : :class:`~graph_tool.PropertyMap`
        A vertex property map containing the hub centrality values.

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

    Notes
    -----

    The Hyperlink-Induced Topic Search (HITS) centrality assigns hub
    (:math:`\mathbf{y}`) and authority (:math:`\mathbf{x}`) centralities to the
    vertices, following:

    .. math::

        \begin{align}
            \mathbf{x} &= \alpha\mathbf{A}\mathbf{y} \\
            \mathbf{y} &= \beta\mathbf{A}^T\mathbf{x}
        \end{align}


    where :math:`\mathbf{A}` is the (weighted) adjacency matrix and
    :math:`\lambda = 1/(\alpha\beta)` is the largest eigenvalue of the
    cocitation matrix, :math:`\mathbf{A}\mathbf{A}^T`. (Without loss of
    generality, we set :math:`\beta=1` in the algorithm.)

    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) cocitation 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
    >>> l, x, y = gt.hits(g, w)
    >>> print(l)
    8.1281860004e-05
    >>> print(x.a)
    [  3.24207627e-02   9.86207526e-02   1.35737601e-03   2.81221883e-03
       0.00000000e+00   3.50637929e-02   6.07494974e-03   1.73442186e-02
       7.70292609e-02   3.16281170e-02   6.23685289e-03   5.33251236e-03
       3.90261094e-03   1.39799492e-01   3.32727532e-03   2.75600277e-02
       4.17864911e-03   1.35434601e-01   1.12371826e-01   3.14487794e-02
       1.56239625e-03   1.53154844e-02   0.00000000e+00   9.76595823e-03
       6.84470944e-02   3.99230637e-03   1.61380128e-02   6.30396302e-03
       6.03036275e-02   1.32849969e-02   3.04151276e-02   5.42617854e-02
       2.08833632e-02   2.28460202e-02   7.57731579e-02   1.83496779e-02
       4.73479252e-01   9.24456456e-02   6.05629566e-04   6.52238551e-02
       0.00000000e+00   8.29910892e-03   1.13757465e-02   4.83645107e-02
       2.71118703e-02   5.49281707e-02   1.26313788e-03   1.55217802e-01
       1.19145685e-02   5.68602825e-02   4.09272093e-02   6.21803861e-02
       2.79433626e-03   6.33529895e-03   1.74347486e-02   4.77049040e-02
       2.29321775e-01   9.82639314e-05   1.33196598e-01   1.07649933e-03
       2.24082303e-02   2.90035582e-03   4.40055377e-03   1.81697665e-01
       7.04846456e-03   0.00000000e+00   7.86454159e-03   7.11419961e-02
       2.56300819e-02   2.56393002e-03   1.38263616e-01   0.00000000e+00
       2.97294623e-01   3.87958584e-01   1.57869881e-02   1.78305749e-02
       4.25241895e-02   8.25617611e-04   9.42672676e-03   1.12595761e-01
       5.96375228e-02   3.60860657e-01   2.13119143e-02   0.00000000e+00
       1.17954701e-04   2.64968422e-03   5.35828471e-65   1.82261998e-01
       2.23512354e-01   1.18366359e-01   5.23661102e-02   1.33577328e-04
       1.38032617e-02   5.00359873e-02   7.12945214e-03   4.82585969e-03
       8.28225880e-02   2.45545154e-02   3.93940652e-02   2.36085882e-02]
    >>> print(y.a)
    [  1.19518911e+01   4.24393415e+01   1.99799643e+00   2.21936973e+00
       4.05229016e+00   1.96921433e+00   5.28773128e+01   3.07583159e+00
       3.84349214e+00   1.43864706e-01   1.15485811e+01   3.88897379e+01
       1.25350058e+01   8.23442356e-01   5.16533892e+00   5.82076701e-01
       0.00000000e+00   2.49809577e+01   3.01041295e+00   1.62691697e-01
       2.07143530e+00   3.04855423e-01   4.29357896e+00   6.67497836e-01
       6.87288592e-01   4.79338810e+00   1.91391421e+00   9.79201735e-01
       5.05465736e+00   6.14454206e+00   1.74858481e+00   0.00000000e+00
       3.73904255e+00   5.60767290e-01   1.09558455e+01   8.41912714e+00
       1.43428505e+00   2.08906862e+01   2.95186438e+00   1.21143763e+00
       1.57869686e+01   3.59363866e+00   1.64801081e-03   2.99040323e+00
       7.22166777e-02   3.08057330e+00   0.00000000e+00   6.03006855e-63
       0.00000000e+00   2.52297825e+01   3.54764499e+00   8.31117522e-01
       1.79062457e+00   1.33432369e+01   8.55091617e-04   6.34751541e+00
       2.59640589e+00   6.62572431e+00   8.55178204e-02   5.27425893e-01
       4.33163271e+00   1.12133638e+00   1.34099527e+00   1.71416121e+01
       1.24989675e+01   2.76622179e+00   2.88210334e-01   8.36393997e+00
       2.93852144e-01   9.31043745e-01   9.47642397e-02   7.38290147e+00
       5.91868714e+00   4.66993445e-01   1.98366671e+00   9.30041719e+00
       4.53580404e-01   1.45961552e+00   1.07607675e+01   0.00000000e+00
       1.50664001e+01   3.05884574e+00   0.00000000e+00   7.37716446e-01
       8.67607706e-01   3.96919920e-01   6.28437918e-01   4.05469431e+01
       1.05754629e+00   7.36234170e+00   7.89914973e+00   9.30338044e-02
       5.47835232e+00   7.54663318e+00   2.48594880e+00   5.16658324e-01
       0.00000000e+00   6.17005885e+00   9.42499389e+00   1.45784289e+00]

    References
    ----------

    .. [hits-algorithm] http://en.wikipedia.org/wiki/HITS_algorithm
    .. [kleinberg-authoritative] J. Kleinberg, "Authoritative sources in a
       hyperlinked environment", Journal of the ACM 46 (5): 604–632, 1999,
       :DOI:`10.1145/324133.324140`.
    .. [power-method] http://en.wikipedia.org/wiki/Power_iteration
    """

    if xprop is None:
        xprop = g.new_vertex_property("double")
    if yprop is None:
        yprop = g.new_vertex_property("double")
    if max_iter is None:
        max_iter = 0
    l = libgraph_tool_centrality.\
         get_hits(g._Graph__graph, _prop("e", g, weight), _prop("v", g, xprop),
                  _prop("v", g, yprop), epsilon, max_iter)
    return 1. / l, xprop, yprop


Tiago Peixoto's avatar
Tiago Peixoto committed
742
def eigentrust(g, trust_map, vprop=None, norm=False, epsilon=1e-6, max_iter=0,
Tiago Peixoto's avatar
Tiago Peixoto committed
743
               ret_iter=False):
744 745 746 747 748
    r"""
    Calculate the eigentrust centrality of each vertex in the graph.

    Parameters
    ----------
749
    g : :class:`~graph_tool.Graph`
750
        Graph to be used.
751
    trust_map : :class:`~graph_tool.PropertyMap`
752
        Edge property map with the values of trust associated with each
753
        edge. The values must lie in the range [0,1].
754
    vprop : :class:`~graph_tool.PropertyMap`, optional (default: ``None``)
755
        Vertex property map where the values of eigentrust must be stored.
756
    norm : bool, optional (default:  ``False``)
757
        Norm eigentrust values so that the total sum equals 1.
758
    epsilon : float, optional (default: ``1e-6``)
759 760
        Convergence condition. The iteration will stop if the total delta of all
        vertices are below this value.
761
    max_iter : int, optional (default: ``None``)
762
        If supplied, this will limit the total number of iterations.
763
    ret_iter : bool, optional (default: ``False``)
764 765 766 767
        If true, the total number of iterations is also returned.

    Returns
    -------
768 769
    eigentrust : :class:`~graph_tool.PropertyMap`
        A vertex property map containing the eigentrust values.
770 771 772 773 774

    See Also
    --------
    betweenness: betweenness centrality
    pagerank: PageRank centrality
775
    trust_transitivity: pervasive trust transitivity
776 777 778

    Notes
    -----
779
    The eigentrust [kamvar-eigentrust-2003]_ values :math:`t_i` correspond the
780 781
    following limit

782 783
    .. math::

784 785 786 787 788
        \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:

789 790
    .. math::

791 792 793 794 795 796 797 798 799 800
        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)
801
    >>> g = gt.random_graph(100, lambda: (poisson(3), poisson(3)))
802
    >>> trust = g.new_edge_property("double")
803
    >>> trust.a = random(g.num_edges())*42
804
    >>> t = gt.eigentrust(g, trust, norm=True)
805
    >>> print(t.a)
Tiago Peixoto's avatar
Tiago Peixoto committed
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
    [  1.12095562e-02   3.97280231e-03   1.31675503e-02   9.61282478e-03
       0.00000000e+00   1.73295741e-02   3.53395497e-03   1.06203582e-02
       1.36906165e-03   8.64587777e-03   1.12049516e-02   3.18891993e-03
       9.28265221e-03   2.25294315e-02   3.24795656e-03   9.16555333e-03
       5.68412465e-02   6.79686311e-03   6.37474649e-03   6.04696712e-03
       0.00000000e+00   8.51131034e-03   0.00000000e+00   1.09336777e-03
       1.49885187e-02   1.09327367e-04   3.73928902e-02   0.00000000e+00
       1.74638522e-02   8.21101864e-03   5.79876899e-03   1.34905262e-02
       1.71525132e-02   2.25425503e-02   1.04184903e-02   1.05537922e-02
       1.34096247e-02   2.82760533e-03   4.31713918e-04   7.39114668e-03
       0.00000000e+00   2.21328121e-05   8.79050007e-03   7.08148889e-03
       5.88651144e-03   7.45401425e-03   5.66098580e-03   2.80738199e-02
       2.41472197e-03   1.00673881e-02   2.29910658e-03   3.23790630e-02
       3.02136064e-03   2.25030440e-03   3.53325357e-03   6.90672383e-03
       1.01692058e-02   1.03783022e-02   1.22476413e-02   4.82453065e-03
       1.15878890e-02   3.41943633e-03   1.57958469e-03   6.56648121e-03
       1.28152141e-02   0.00000000e+00   1.29192164e-03   9.35867476e-03
       3.89329603e-03   1.78002682e-03   2.81987911e-02   0.00000000e+00
       1.74943514e-02   6.24079508e-03   1.57572103e-02   3.77119257e-02
       4.78552984e-03   3.30463136e-04   5.60118687e-03   5.75656186e-03
       2.65412905e-02   1.59663210e-02   2.88844192e-02   0.00000000e+00
       7.87754853e-04   1.76957899e-02   3.19907905e-02   1.94650690e-02
       1.32052233e-02   3.57577093e-03   7.09968545e-04   8.70787481e-03
       1.24901391e-04   2.61215462e-02   2.25923034e-02   1.10928239e-02
       9.39210737e-03   5.61073138e-04   1.59987179e-02   3.02799309e-03]
831 832 833

    References
    ----------
834
    .. [kamvar-eigentrust-2003] S. D. Kamvar, M. T. Schlosser, H. Garcia-Molina
835 836
       "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
837
       Pages: 640 - 651, 2003, :doi:`10.1145/775152.775242`
838 839
    """

Tiago Peixoto's avatar
Tiago Peixoto committed
840 841
    if vprop == None:
        vprop = g.new_vertex_property("double")
842 843
    i = libgraph_tool_centrality.\
           get_eigentrust(g._Graph__graph, _prop("e", g, trust_map),
Tiago Peixoto's avatar
Tiago Peixoto committed
844
                          _prop("v", g, vprop), epsilon, max_iter)
845 846 847 848 849 850 851 852
    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
853

854
def trust_transitivity(g, trust_map, source=None, target=None, vprop=None):
855
    r"""
856 857
    Calculate the pervasive trust transitivity between chosen (or all) vertices
    in the graph.
858 859 860

    Parameters
    ----------
861
    g : :class:`~graph_tool.Graph`
862
        Graph to be used.
863
    trust_map : :class:`~graph_tool.PropertyMap`
864 865
        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
866
    source : :class:`~graph_tool.Vertex` (optional, default: None)
867
        Source vertex. All trust values are computed relative to this vertex.
868
        If left unspecified, the trust values for all sources are computed.
Tiago Peixoto's avatar
Tiago Peixoto committed
869
    target : :class:`~graph_tool.Vertex` (optional, default: None)
870 871 872
        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)
873 874
        A vertex property map where the values of transitive trust must be
        stored.
875 876 877

    Returns
    -------
878 879 880 881 882 883 884 885
    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.
886

887 888 889 890 891 892 893 894
    See Also
    --------
    eigentrust: eigentrust centrality
    betweenness: betweenness centrality
    pagerank: PageRank centrality

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

897 898
    .. math::

899 900
        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)}
901

902 903 904
    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
905

906 907
    .. math::

908
       w_G(i\to j) = \prod_{e\in i\to j} c_e.
909

910 911
    The algorithm measures the transitive trust by finding the paths with
    maximum weight, using Dijkstra's algorithm, to all in-neighbours of a given
912
    target. This search needs to be performed repeatedly for every target, since
913 914 915 916 917 918 919
    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.
920 921 922 923 924 925 926

    If enabled during compilation, this algorithm runs in parallel.

    Examples
    --------
    >>> from numpy.random import poisson, random, seed
    >>> seed(42)
927
    >>> g = gt.random_graph(100, lambda: (poisson(3), poisson(3)))
928
    >>> trust = g.new_edge_property("double")
929
    >>> trust.a = random(g.num_edges())
930
    >>> t = gt.trust_transitivity(g, trust, source=g.vertex(0))
931
    >>> print(t.a)
Tiago Peixoto's avatar
Tiago Peixoto committed
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
    [  1.00000000e+00   9.59916062e-02   4.27717883e-02   7.70755875e-02
       0.00000000e+00   2.04476926e-01   5.55315822e-02   2.82854665e-02
       5.08479257e-02   1.68128402e-01   3.28567434e-02   7.39525583e-02
       1.34463196e-01   8.83740756e-02   1.79990535e-01   7.08809615e-02
       6.37757645e-02   7.24187957e-02   4.83082241e-02   9.90676983e-02
       0.00000000e+00   6.50497060e-02   0.00000000e+00   1.77344948e-02
       1.08677897e-01   1.00958718e-03   4.49524961e-02   0.00000000e+00
       1.64902280e-01   4.31492976e-02   2.19446085e-01   3.00890381e-02
       6.86750847e-02   2.72460575e-02   3.57314594e-02   4.87776483e-02
       4.11748930e-01   7.91396467e-02   2.54835127e-03   3.01711432e-01
       0.00000000e+00   4.14406224e-04   4.24794624e-02   9.14096554e-02
       4.17528677e-01   3.79112573e-02   1.16489950e-01   5.18112902e-02
       8.49111259e-03   5.26399996e-02   2.45690139e-02   7.51435125e-02
       5.62381854e-02   2.90115777e-02   2.72543383e-02   1.46877163e-01
       7.81446822e-02   1.24417763e-02   1.01337976e-01   9.92776442e-02
       3.14622176e-02   1.20097319e-01   3.30335980e-02   4.61757040e-02
       1.01085599e-01   0.00000000e+00   4.44660446e-03   6.31066845e-02
       1.94702084e-02   8.45343379e-04   4.82190327e-02   0.00000000e+00
       6.60346087e-02   7.44581695e-02   6.19535229e-02   1.82072422e-01
       1.45366611e-02   2.59020075e-02   2.52208295e-02   6.80519730e-02
       6.74671969e-02   1.14198914e-01   5.12493343e-02   0.00000000e+00
       6.33427008e-03   1.42290348e-01   6.90459437e-02   1.00565411e-01
       5.88966867e-02   3.28157280e-02   2.80046903e-02   2.41520032e-01
       8.45879329e-04   6.76633672e-02   6.05080467e-02   9.12575826e-02
       1.97789973e-02   6.40885493e-02   4.80934526e-02   1.28787181e-02]
Tiago Peixoto's avatar
Tiago Peixoto committed
957 958 959

    References
    ----------
960 961 962
    .. [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
963

964
    """
Tiago Peixoto's avatar
Tiago Peixoto committed
965 966

    if vprop == None:
967
        vprop = g.new_vertex_property("vector<double>")
968

969 970 971 972
    if target == None:
        target = -1
    else:
        target = g.vertex_index[target]
973

974 975 976 977 978
    if source == None:
        source = -1
    else:
        source = g.vertex_index[source]

979
    libgraph_tool_centrality.\
980 981 982 983
            get_trust_transitivity(g._Graph__graph, source, target,
                                   _prop("e", g, trust_map),
                                   _prop("v", g, vprop))
    if target != -1 or source != -1:
984
        vprop = ungroup_vector_property(vprop, [0])[0]
985
    if target != -1 and source != -1:
986
        return vprop.a[target]
987
    return vprop
988