__init__.py 34.9 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) 2006-2013 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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

    .. doctest:: pagerank

       >>> g = gt.collection.data["polblogs"]
       >>> g = gt.GraphView(g, vfilt=gt.label_largest_component(g))
       >>> pr = gt.pagerank(g)
       >>> gt.graph_draw(g, pos=g.vp["pos"], vertex_fill_color=pr,
       ...               vertex_size=gt.prop_to_size(pr, mi=5, ma=15),
       ...               vorder=pr, output="polblogs_pr.pdf")
       <...>

    .. testcode:: pagerank
       :hide:

       gt.graph_draw(g, pos=g.vp["pos"], vertex_fill_color=pr,
                     vertex_size=gt.prop_to_size(pr, mi=5, ma=15),
                     vorder=pr, output="polblogs_pr.png")


    .. figure:: polblogs_pr.*
       :align: center

       PageRank values of the a political blogs network of [adamic-polblogs]_.
160 161 162

    Now with a personalization vector, and edge weights:

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
    .. doctest:: pagerank

       >>> d = g.degree_property_map("total")
       >>> periphery = d.a <= 2
       >>> p = g.new_vertex_property("double")
       >>> p.a[periphery] = 100
       >>> pr = gt.pagerank(g, pers=p)
       >>> gt.graph_draw(g, pos=g.vp["pos"], vertex_fill_color=pr,
       ...               vertex_size=gt.prop_to_size(pr, mi=5, ma=15),
       ...               vorder=pr, output="polblogs_pr_pers.pdf")
       <...>

    .. testcode:: pagerank
       :hide:

       gt.graph_draw(g, pos=g.vp["pos"], vertex_fill_color=pr,
                     vertex_size=gt.prop_to_size(pr, mi=5, ma=15),
                     vorder=pr, output="polblogs_pr_pers.png")


    .. figure:: polblogs_pr_pers.*
       :align: center

       Personalized PageRank values of the a political blogs network of
       [adamic-polblogs]_, where vertices with very low degree are given
       artificially high scores.
189 190 191

    References
    ----------
192 193
    .. [pagerank-wikipedia] http://en.wikipedia.org/wiki/Pagerank
    .. [lawrence-pagerank-1998] P. Lawrence, B. Sergey, M. Rajeev, W. Terry,
194
       "The pagerank citation ranking: Bringing order to the web", Technical
195
       report, Stanford University, 1998
196 197 198
    .. [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`
199 200 201
    .. [adamic-polblogs] L. A. Adamic and N. Glance, "The political blogosphere
       and the 2004 US Election", in Proceedings of the WWW-2005 Workshop on the
       Weblogging Ecosystem (2005). :DOI:`10.1145/1134271.1134277`
202 203 204 205
    """

    if max_iter == None:
        max_iter = 0
Tiago Peixoto's avatar
Tiago Peixoto committed
206 207
    if prop == None:
        prop = g.new_vertex_property("double")
208 209
        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
210
    ic = libgraph_tool_centrality.\
211 212 213
            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
214 215 216 217 218
    if ret_iter:
        return prop, ic
    else:
        return prop

Tiago Peixoto's avatar
Tiago Peixoto committed
219

220 221 222 223 224 225
def betweenness(g, vprop=None, eprop=None, weight=None, norm=True):
    r"""
    Calculate the betweenness centrality for each vertex and edge.

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

    See Also
    --------
    central_point_dominance: central point dominance of the graph
    pagerank: PageRank centrality
    eigentrust: eigentrust centrality
247 248
    eigenvector: eigenvector centrality
    hits: hubs and authority centralities
249
    trust_transitivity: pervasive trust transitivity
250 251 252 253 254

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

255 256
    .. math::

257 258 259 260 261 262 263 264 265
        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`.

266
    The algorithm used here is defined in [brandes-faster-2001]_, and has a
267 268 269 270 271 272 273
    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
    --------
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298

    .. doctest:: betweenness

       >>> g = gt.collection.data["polblogs"]
       >>> g = gt.GraphView(g, vfilt=gt.label_largest_component(g))
       >>> vp, ep = gt.betweenness(g)
       >>> gt.graph_draw(g, pos=g.vp["pos"], vertex_fill_color=vp,
       ...               vertex_size=gt.prop_to_size(vp, mi=5, ma=15),
       ...               edge_pen_width=gt.prop_to_size(ep, mi=0.5, ma=5),
       ...               vorder=vp, output="polblogs_betweenness.pdf")
       <...>

    .. testcode:: betweenness
       :hide:

       gt.graph_draw(g, pos=g.vp["pos"], vertex_fill_color=vp,
                     vertex_size=gt.prop_to_size(vp, mi=5, ma=15),
                     edge_pen_width=gt.prop_to_size(ep, mi=0.5, ma=5),
                     vorder=vp, output="polblogs_betweenness.png")


    .. figure:: polblogs_betweenness.*
       :align: center

       Betweenness values of the a political blogs network of [adamic-polblogs]_.
299 300 301

    References
    ----------
302 303
    .. [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
304
       centrality", Journal of Mathematical Sociology, 2001, :doi:`10.1080/0022250X.2001.9990249`
305 306 307
    .. [adamic-polblogs] L. A. Adamic and N. Glance, "The political blogosphere
       and the 2004 US Election", in Proceedings of the WWW-2005 Workshop on the
       Weblogging Ecosystem (2005). :DOI:`10.1145/1134271.1134277`
308
    """
Tiago Peixoto's avatar
Tiago Peixoto committed
309 310 311 312 313 314 315 316 317 318 319 320 321
    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
322

Tiago Peixoto's avatar
Tiago Peixoto committed
323
def central_point_dominance(g, betweenness):
324 325 326 327 328 329
    r"""
    Calculate the central point dominance of the graph, given the betweenness
    centrality of each vertex.

    Parameters
    ----------
330
    g : :class:`~graph_tool.Graph`
331
        Graph to be used.
332
    betweenness : :class:`~graph_tool.PropertyMap`
333 334 335 336 337
        Vertex property map with the betweenness centrality values. The values
        must be normalized.

    Returns
    -------
338 339
    cp : float
        The central point dominance.
340 341 342 343 344 345 346 347

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

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

351 352
    .. math::

353 354 355 356 357 358 359 360 361
        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
    --------
362 363 364 365 366 367

    >>> g = gt.collection.data["polblogs"]
    >>> g = gt.GraphView(g, vfilt=gt.label_largest_component(g))
    >>> vp, ep = gt.betweenness(g)
    >>> print(gt.central_point_dominance(g, vp))
    0.11610685614353008
368 369 370

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

Tiago Peixoto's avatar
Tiago Peixoto committed
376
    return libgraph_tool_centrality.\
377
           get_central_point_dominance(g._Graph__graph,
Tiago Peixoto's avatar
Tiago Peixoto committed
378 379
                                       _prop("v", g, betweenness))

380

381 382 383 384 385 386 387 388 389
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.
390
    weight : :class:`~graph_tool.PropertyMap` (optional, default: ``None``)
391 392
        Edge property map with the edge weights.
    vprop : :class:`~graph_tool.PropertyMap`, optional (default: ``None``)
393 394
        Vertex property map where the values of eigenvector must be stored. If
        provided, it will be used uninitialized.
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
    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
412
    hits: hubs and authority centralities
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
    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
    --------
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
    .. testsetup:: eigenvector

       np.random.seed(42)

    .. doctest:: eigenvector

       >>> g = gt.collection.data["polblogs"]
       >>> g = gt.GraphView(g, vfilt=gt.label_largest_component(g))
       >>> w = g.new_edge_property("double")
       >>> w.a = np.random.random(len(w.a)) * 42
       >>> ee, x = gt.eigenvector(g, w)
       >>> print(ee)
       0.0013713102236792602
       >>> gt.graph_draw(g, pos=g.vp["pos"], vertex_fill_color=x,
       ...               vertex_size=gt.prop_to_size(x, mi=5, ma=15),
       ...               vorder=x, output="polblogs_eigenvector.pdf")
       <...>

    .. testcode:: eigenvector
       :hide:

       gt.graph_draw(g, pos=g.vp["pos"], vertex_fill_color=x,
                     vertex_size=gt.prop_to_size(x, mi=5, ma=15),
                     vorder=x, output="polblogs_eigenvector.png")


    .. figure:: polblogs_eigenvector.*
       :align: center

       Eigenvector values of the a political blogs network of
       [adamic-polblogs]_, with random weights attributed to the edges.
471 472 473 474 475 476 477 478 479

    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`
480 481 482
    .. [adamic-polblogs] L. A. Adamic and N. Glance, "The political blogosphere
       and the 2004 US Election", in Proceedings of the WWW-2005 Workshop on the
       Weblogging Ecosystem (2005). :DOI:`10.1145/1134271.1134277`
483 484 485

    """

486
    if vprop is None:
487
        vprop = g.new_vertex_property("double")
488
        vprop.a = 1. / g.num_vertices()
489 490 491 492 493 494 495 496
    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
497 498
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
499
    Calculate the Katz centrality of each vertex in the graph.
Tiago Peixoto's avatar
Tiago Peixoto committed
500 501 502 503 504 505 506 507

    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``)
508 509
        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
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
    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
    --------
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
    .. testsetup:: katz

       np.random.seed(42)

    .. doctest:: katz

       >>> g = gt.collection.data["polblogs"]
       >>> g = gt.GraphView(g, vfilt=gt.label_largest_component(g))
       >>> w = g.new_edge_property("double")
       >>> w.a = np.random.random(len(w.a)) * 42
       >>> x = gt.katz(g, weight=w)
       >>> gt.graph_draw(g, pos=g.vp["pos"], vertex_fill_color=x,
       ...               vertex_size=gt.prop_to_size(x, mi=5, ma=15),
       ...               vorder=x, output="polblogs_katz.pdf")
       <...>

    .. testcode:: katz
       :hide:

       gt.graph_draw(g, pos=g.vp["pos"], vertex_fill_color=x,
                     vertex_size=gt.prop_to_size(x, mi=5, ma=15),
                     vorder=x, output="polblogs_katz.png")


    .. figure:: polblogs_katz.*
       :align: center

       Katz centrality values of the a political blogs network of
       [adamic-polblogs]_, with random weights attributed to the edges.
Tiago Peixoto's avatar
Tiago Peixoto committed
586 587 588 589 590 591 592

    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`
593 594 595
    .. [adamic-polblogs] L. A. Adamic and N. Glance, "The political blogosphere
       and the 2004 US Election", in Proceedings of the WWW-2005 Workshop on the
       Weblogging Ecosystem (2005). :DOI:`10.1145/1134271.1134277`
Tiago Peixoto's avatar
Tiago Peixoto committed
596 597 598 599 600 601 602 603 604 605 606 607 608 609
    """

    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


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

    .. doctest:: hits

       >>> g = gt.collection.data["polblogs"]
       >>> g = gt.GraphView(g, vfilt=gt.label_largest_component(g))
       >>> ee, x, y = gt.hits(g)
       >>> gt.graph_draw(g, pos=g.vp["pos"], vertex_fill_color=x,
       ...               vertex_size=gt.prop_to_size(x, mi=5, ma=15),
       ...               vorder=x, output="polblogs_hits_auths.pdf")
       <...>
       >>> gt.graph_draw(g, pos=g.vp["pos"], vertex_fill_color=y,
       ...               vertex_size=gt.prop_to_size(y, mi=5, ma=15),
       ...               vorder=y, output="polblogs_hits_hubs.pdf")
       <...>

    .. testcode:: hits
       :hide:

       gt.graph_draw(g, pos=g.vp["pos"], vertex_fill_color=x,
                     vertex_size=gt.prop_to_size(x, mi=5, ma=15),
                     vorder=x, output="polblogs_hits_auths.png")
       gt.graph_draw(g, pos=g.vp["pos"], vertex_fill_color=y,
                     vertex_size=gt.prop_to_size(y, mi=5, ma=15),
                     vorder=y, output="polblogs_hits_hubs.png")


    .. figure:: polblogs_hits_auths.*
       :align: left

       HITS authority values of the a political blogs network of
       [adamic-polblogs]_.

    .. figure:: polblogs_hits_hubs.*
       :align: right

       HITS hub values of the a political blogs network of [adamic-polblogs]_.
712 713 714 715 716 717

    References
    ----------

    .. [hits-algorithm] http://en.wikipedia.org/wiki/HITS_algorithm
    .. [kleinberg-authoritative] J. Kleinberg, "Authoritative sources in a
718
       hyperlinked environment", Journal of the ACM 46 (5): 604-632, 1999,
719 720
       :DOI:`10.1145/324133.324140`.
    .. [power-method] http://en.wikipedia.org/wiki/Power_iteration
721 722 723
    .. [adamic-polblogs] L. A. Adamic and N. Glance, "The political blogosphere
       and the 2004 US Election", in Proceedings of the WWW-2005 Workshop on the
       Weblogging Ecosystem (2005). :DOI:`10.1145/1134271.1134277`
724 725 726 727 728 729 730 731 732 733 734 735 736 737
    """

    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
738
def eigentrust(g, trust_map, vprop=None, norm=False, epsilon=1e-6, max_iter=0,
Tiago Peixoto's avatar
Tiago Peixoto committed
739
               ret_iter=False):
740 741 742 743 744
    r"""
    Calculate the eigentrust centrality of each vertex in the graph.

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

    Returns
    -------
764 765
    eigentrust : :class:`~graph_tool.PropertyMap`
        A vertex property map containing the eigentrust values.
766 767 768 769 770

    See Also
    --------
    betweenness: betweenness centrality
    pagerank: PageRank centrality
771
    trust_transitivity: pervasive trust transitivity
772 773 774

    Notes
    -----
775
    The eigentrust [kamvar-eigentrust-2003]_ values :math:`t_i` correspond the
776 777
    following limit

778 779
    .. math::

780 781 782 783 784
        \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:

785 786
    .. math::

787 788 789 790 791 792 793 794
        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
    --------
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825

    .. testsetup:: eigentrust

       np.random.seed(42)

    .. doctest:: eigentrust

       >>> g = gt.collection.data["polblogs"]
       >>> g = gt.GraphView(g, vfilt=gt.label_largest_component(g))
       >>> w = g.new_edge_property("double")
       >>> w.a = np.random.random(len(w.a)) * 42
       >>> t = gt.eigentrust(g, w)
       >>> gt.graph_draw(g, pos=g.vp["pos"], vertex_fill_color=t,
       ...               vertex_size=gt.prop_to_size(t, mi=5, ma=15),
       ...               vorder=t, output="polblogs_eigentrust.pdf")
       <...>

    .. testcode:: eigentrust
       :hide:

       gt.graph_draw(g, pos=g.vp["pos"], vertex_fill_color=t,
                     vertex_size=gt.prop_to_size(t, mi=5, ma=15),
                     vorder=t, output="polblogs_eigentrust.png")


    .. figure:: polblogs_eigentrust.*
       :align: center

       Eigentrust values of the a political blogs network of
       [adamic-polblogs]_, with random weights attributed to the edges.

826 827 828

    References
    ----------
829
    .. [kamvar-eigentrust-2003] S. D. Kamvar, M. T. Schlosser, H. Garcia-Molina
830 831
       "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
832
       Pages: 640 - 651, 2003, :doi:`10.1145/775152.775242`
833 834 835
    .. [adamic-polblogs] L. A. Adamic and N. Glance, "The political blogosphere
       and the 2004 US Election", in Proceedings of the WWW-2005 Workshop on the
       Weblogging Ecosystem (2005). :DOI:`10.1145/1134271.1134277`
836 837
    """

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

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

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

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

885 886 887 888 889 890 891 892
    See Also
    --------
    eigentrust: eigentrust centrality
    betweenness: betweenness centrality
    pagerank: PageRank centrality

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

895 896
    .. math::

897 898
        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)}
899

900 901 902
    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
903

904 905
    .. math::

906
       w_G(i\to j) = \prod_{e\in i\to j} c_e.
907

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

    If enabled during compilation, this algorithm runs in parallel.

    Examples
    --------
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
    .. testsetup:: trust_transitivity

       np.random.seed(42)

    .. doctest:: trust_transitivity

       >>> g = gt.collection.data["polblogs"]
       >>> g = gt.GraphView(g, vfilt=gt.label_largest_component(g))
       >>> g = gt.Graph(g, prune=True)
       >>> w = g.new_edge_property("double")
       >>> w.a = np.random.random(len(w.a))
       >>> g.vp["label"][g.vertex(42)]
       'blogforamerica.com'
       >>> t = gt.trust_transitivity(g, w, source=g.vertex(42))
       >>> gt.graph_draw(g, pos=g.vp["pos"], vertex_fill_color=t,
       ...               vertex_size=gt.prop_to_size(t, mi=5, ma=15),
       ...               vorder=t, output="polblogs_trust_transitivity.pdf")
       <...>

    .. testcode:: trust_transitivity
       :hide:

       gt.graph_draw(g, pos=g.vp["pos"], vertex_fill_color=t,
                     vertex_size=gt.prop_to_size(t, mi=5, ma=15),
                     vorder=t, output="polblogs_trust_transitivity.png")


    .. figure:: polblogs_trust_transitivity.*
       :align: center

       Trust transitivity values from source vertex 42 of the a political blogs
       network of [adamic-polblogs]_, with random weights attributed to the
       edges.
Tiago Peixoto's avatar
Tiago Peixoto committed
956 957 958

    References
    ----------
959 960 961
    .. [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`
962 963 964
    .. [adamic-polblogs] L. A. Adamic and N. Glance, "The political blogosphere
       and the 2004 US Election", in Proceedings of the WWW-2005 Workshop on the
       Weblogging Ecosystem (2005). :DOI:`10.1145/1134271.1134277`
Tiago Peixoto's avatar
Tiago Peixoto committed
965

966
    """
Tiago Peixoto's avatar
Tiago Peixoto committed
967 968

    if vprop == None:
969
        vprop = g.new_vertex_property("vector<double>")
970

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

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

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