__init__.py 21.3 KB
Newer Older
Tiago Peixoto's avatar
Tiago Peixoto committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#! /usr/bin/env python
# graph_tool.py -- a general graph manipulation python module
#
# Copyright (C) 2007 Tiago de Paula Peixoto <tiago@forked.de>
#
# 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/>.

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

This module includes centrality-related algorithms.
"""

Tiago Peixoto's avatar
Tiago Peixoto committed
26
27
28
29
from .. dl_import import dl_import
dl_import("import libgraph_tool_centrality")

from .. core import _prop
30
import sys, numpy
Tiago Peixoto's avatar
Tiago Peixoto committed
31
32
33
34

__all__ = ["pagerank", "betweenness", "central_point_dominance", "eigentrust",
           "absolute_trust"]

35
def pagerank(g, damping=0.8, prop=None, epslon=1e-6, max_iter=None,
Tiago Peixoto's avatar
Tiago Peixoto committed
36
             ret_iter=False):
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
    r"""
    Calculate the PageRank of each vertex.

    Parameters
    ----------
    g : Graph
        Graph to be used.
    damping : float, optional (default: 0.8)
        Damping factor.
    prop : ProperyMap, optional (default: None)
        Vertex property map to store the PageRank values.
    epslon : 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.
    ret_iter : bool, optional (default: False)
        If true, the total number of iterations is also returned.

    Returns
    -------
58
    pagerank : A vertex property map containing the PageRank values.
59
60
61
62
63
64
65
66
67

    See Also
    --------
    betweenness: betweenness centrality
    eigentrust: eigentrust centrality
    absolute_trust: absolute trust centrality

    Notes
    -----
68
69
70
71
    The value of PageRank [pagerank_wikipedia]_ of vertex v :math:`PR(v)` is
    given interactively by the relation:

    .. math::
72
73

        PR(v) = \frac{1-d}{N} + d \sum_{w \in \Gamma^{-}(v)}
74
                \frac{PR (w)}{d^{+}(w)}
75
76
77
78
79
80
81
82
83
84
85
86

    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.

    The implemented algorithm progressively iterates the above condition, until
    it no longer changes, according to the parameter epslon. It has a
    topology-dependent running time.

    If enabled during compilation, this algorithm runs in parallel.

    Examples
    --------
87
88
    >>> from numpy.random import poisson, seed
    >>> seed(42)
89
90
91
    >>> g = gt.random_graph(100, lambda: (poisson(3), poisson(3)), seed=42)
    >>> pr = gt.pagerank(g)
    >>> print pr.get_array()
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
    [ 1.01514315  0.60117439  0.32514372  0.28        0.2         1.54971179
      0.28        1.0236911   0.33123536  0.4778296   0.62078363  1.25377064
      0.49213262  1.70011842  0.30671734  0.56424761  0.86810689  1.68765055
      0.49551575  0.72837655  0.39240949  1.43802363  0.51563806  0.41983927
      0.37857787  0.45875573  0.97033399  0.38531927  0.54001665  0.89328562
      0.52122532  0.94064256  1.39911631  0.64663655  1.23521006  0.71722741
      0.59460778  0.2         0.63239854  1.86292923  0.2         0.31277737
      0.74650027  0.32415672  0.47975325  1.11611173  0.53433883  0.63352435
      0.23822967  0.93151021  0.5440643   0.69188579  0.97489471  0.51216733
      1.31721331  1.32808547  0.39894203  0.50384137  0.75225633  0.28220146
      1.10818407  0.58685184  1.26437262  0.67929902  0.69678112  1.34428502
      0.61651094  0.43008378  0.7905129   1.35318411  0.2         0.2
      1.6584374   0.98009079  0.27200222  0.3413639   0.23822967  0.27963213
      1.22498499  0.34097559  0.50749002  1.21145838  0.50430676  0.50218939
      0.74232491  0.5335867   0.27254191  0.36031317  0.65344358  0.96712961
      0.53252883  0.86479464  0.59958851  0.82703737  0.68722079  0.52036384
      0.65299724  0.42291513  0.81729152  1.7586996 ]
109
110
111
112
113

    References
    ----------
    .. [pagerank_wikipedia] http://en.wikipedia.org/wiki/Pagerank
    .. [lawrence_pagerank_1998] P. Lawrence, B. Sergey, M. Rajeev, W. Terry,
114
       "The pagerank citation ranking: Bringing order to the web", Technical
115
116
117
118
119
       report, Stanford University, 1998
    """

    if max_iter == None:
        max_iter = 0
Tiago Peixoto's avatar
Tiago Peixoto committed
120
121
122
123
124
125
126
127
128
129
    if prop == None:
        prop = g.new_vertex_property("double")
    ic = libgraph_tool_centrality.\
            get_pagerank(g._Graph__graph, _prop("v", g, prop), damping, epslon,
                         max_iter)
    if ret_iter:
        return prop, ic
    else:
        return prop

130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
def betweenness(g, vprop=None, eprop=None, weight=None, norm=True):
    r"""
    Calculate the betweenness centrality for each vertex and edge.

    Parameters
    ----------
    g : Graph
        Graph to be used.
    vprop : ProperyMap, optional (default: None)
        Vertex property map to store the vertex betweenness values.
    eprop : ProperyMap, optional (default: None)
        Edge property map to store the edge betweenness values.
    weight : ProperyMap, optional (default: None)
        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
    -------
149
150
151
152
    vertex_betweenness : A vertex property map with the vertex betweenness
                         values.
    edge_betweenness : An edge property map with the edge betweenness
                       values.
153
154
155
156
157
158
159
160
161
162
163
164

    See Also
    --------
    central_point_dominance: central point dominance of the graph
    pagerank: PageRank centrality
    eigentrust: eigentrust centrality
    absolute_trust: absolute trust centrality

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

165
166
    .. math::

167
168
169
170
171
172
173
174
175
        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`.

176
    The algorithm used here is defined in [brandes_faster_2001]_, and has a
177
178
179
180
181
182
183
    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
    --------
184
185
    >>> from numpy.random import poisson, seed
    >>> seed(42)
186
187
188
    >>> g = gt.random_graph(100, lambda: (poisson(3), poisson(3)), seed=42)
    >>> vb, eb = gt.betweenness(g)
    >>> print vb.get_array()
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
    [ 0.03536033  0.03251351  0.00813873  0.00496977  0.          0.08339989
      0.00948258  0.05751528  0.00236377  0.00868464  0.04443961  0.04691023
      0.01768388  0.          0.01130552  0.01277964  0.04223144  0.05040177
      0.01202611  0.0012722   0.00828095  0.11598601  0.01864867  0.01412404
      0.03343004  0.01772387  0.04780278  0.01351748  0.03616999  0.09074218
      0.          0.          0.03901368  0.02526396  0.07471888  0.00219886
      0.          0.          0.01062083  0.07973799  0.          0.01410051
      0.02025676  0.          0.00988767  0.07519014  0.          0.06380861
      0.          0.01954769  0.04576145  0.04151243  0.          0.04198926
      0.0462918   0.07353227  0.00606605  0.02597097  0.02566416  0.00196642
      0.06240786  0.02996611  0.03252566  0.01451141  0.05149852  0.
      0.03582571  0.04600123  0.03776439  0.03326425  0.          0.
      0.11568858  0.01361223  0.00515358  0.007151    0.00241302  0.00271168
      0.01780978  0.01867583  0.02020758  0.01254292  0.00054971  0.00698211
      0.02359226  0.0385241   0.00157871  0.00576513  0.04173662  0.03233332
      0.0208791   0.02286212  0.04366053  0.03701801  0.02142117  0.03099565
      0.02555676  0.03365458  0.03542124  0.06174975]
206
207
208
209
210
211
212

    References
    ----------
    .. [betweenness_wikipedia] http://en.wikipedia.org/wiki/Centrality#Betweenness_centrality
    .. [brandes_faster_2001] U. Brandes, "A faster algorithm for betweenness
       centrality",  Journal of Mathematical Sociology, 2001
    """
Tiago Peixoto's avatar
Tiago Peixoto committed
213
214
215
216
217
218
219
220
221
222
223
224
225
226
    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

def central_point_dominance(g, betweenness):
227
228
229
230
231
232
233
234
235
236
237
238
239
240
    r"""
    Calculate the central point dominance of the graph, given the betweenness
    centrality of each vertex.

    Parameters
    ----------
    g : Graph
        Graph to be used.
    betweenness : ProperyMap
        Vertex property map with the betweenness centrality values. The values
        must be normalized.

    Returns
    -------
241
242
    cp : float
        The central point dominance.
243
244
245
246
247
248
249
250

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

    Notes
    -----
    Let :math:`v^*` be the vertex with the largest relative betweenness
251
    centrality; then, the central point dominance [freeman_set_1977]_ is defined
252
253
    as:

254
255
    .. math::

256
257
258
259
260
261
262
263
264
        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
    --------
265
266
    >>> from numpy.random import poisson, seed
    >>> seed(42)
267
268
269
    >>> g = gt.random_graph(100, lambda: (poisson(3), poisson(3)), seed=42)
    >>> vb, eb = gt.betweenness(g)
    >>> print gt.central_point_dominance(g, vb)
270
    0.0902382147799
271
272
273
274
275
276
277

    References
    ----------
    .. [freeman_set_1977] Linton C. Freeman, "A Set of Measures of Centrality
       Based on Betweenness", Sociometry, Vol. 40, No. 1,  pp. 35-41 (1977)
    """

Tiago Peixoto's avatar
Tiago Peixoto committed
278
    return libgraph_tool_centrality.\
279
           get_central_point_dominance(g._Graph__graph,
Tiago Peixoto's avatar
Tiago Peixoto committed
280
281
                                       _prop("v", g, betweenness))

282
283

def eigentrust(g, trust_map, vprop=None, norm=False, epslon=1e-6, max_iter=0,
Tiago Peixoto's avatar
Tiago Peixoto committed
284
               ret_iter=False):
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
    r"""
    Calculate the eigentrust centrality of each vertex in the graph.

    Parameters
    ----------
    g : Graphs
        Graph to be used.
    trust_map : ProperyMap
        Edge property map with the values of trust associated with each
        edge. The values must not lie in the range [0,1].
    vprop : PropertyMap, optional (default: None)
        Vertex property map where the values of eigentrust must be stored.
    norm : bool, optional (default: false)
        Norm eigentrust values so that the total sum equals 1.
    epslon : 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.
    ret_iter : bool, optional (default: False)
        If true, the total number of iterations is also returned.

    Returns
    -------
309
    eigentrust : A vertex property map containing the eigentrust values.
310
311
312
313
314
315
316
317
318
319
320
321

    See Also
    --------
    betweenness: betweenness centrality
    pagerank: PageRank centrality
    absolute_trust: absolute trust centrality

    Notes
    -----
    The eigentrust _[kamvar_eigentrust_2003] values :math:`t_i` correspond the
    following limit

322
323
    .. math::

324
325
326
327
328
        \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:

329
330
    .. math::

331
332
333
334
335
336
337
338
339
340
341
342
343
        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)
    >>> g = gt.random_graph(100, lambda: (poisson(3), poisson(3)), seed=42)
    >>> trust = g.new_edge_property("double")
    >>> trust.get_array()[:] = random(g.num_edges())*42
344
    >>> t = gt.eigentrust(g, trust, norm=True)
345
    >>> print t.get_array()
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
    [  1.86170852e-02   3.54163528e-03   6.09712602e-04   0.00000000e+00
       0.00000000e+00   3.49545179e-02   0.00000000e+00   2.59814288e-02
       8.41396546e-04   4.78599541e-03   1.01228999e-02   1.43178181e-02
       2.24766294e-03   1.80046830e-02   3.55639433e-03   4.24974765e-03
       1.11631004e-02   3.12332820e-02   6.70174456e-03   1.09689726e-02
       5.42202976e-03   2.51547994e-02   6.87197775e-03   3.90316493e-03
       2.81858126e-03   6.26514036e-03   1.12322993e-02   4.35905749e-03
       1.86938930e-02   1.93055029e-02   3.25522183e-03   9.48081499e-03
       1.84882500e-02   8.17367673e-03   4.02113149e-02   1.07092572e-02
       1.02184616e-02   0.00000000e+00   4.21126174e-03   3.97005433e-02
       0.00000000e+00   6.23025347e-04   1.92797472e-02   5.22705075e-04
       4.07751175e-03   2.11704089e-02   7.49484415e-03   8.10935540e-03
       9.47352873e-05   1.74518912e-02   1.18865927e-02   8.49808309e-03
       8.07449129e-03   6.04464513e-03   1.31497182e-02   1.61277706e-02
       3.45965628e-03   9.28003800e-03   5.81189874e-03   2.67273946e-03
       1.33359267e-02   3.99664807e-03   1.45641237e-02   2.06551771e-03
       1.89334085e-02   2.44376969e-02   7.44521415e-03   6.35266998e-03
       9.90439343e-03   2.61315207e-02   0.00000000e+00   0.00000000e+00
       4.08351424e-02   1.21805039e-02   3.45041723e-03   1.84601840e-03
       1.09623699e-03   2.37115682e-03   1.70221593e-02   4.57709422e-03
       4.21193747e-03   2.26493986e-02   3.92636239e-03   2.42441556e-03
       7.41276227e-03   7.01899189e-03   3.30982461e-03   4.18470116e-04
       8.46801514e-03   9.05050341e-03   5.09784610e-03   3.20304076e-02
       6.71276214e-03   5.26109355e-03   5.29170118e-03   3.46248974e-03
       1.10436337e-02   2.20158077e-03   1.26859707e-02   2.25728004e-02]
371
372
373
374
375
376
377
378
379

    References
    ----------
    .. [kamvar_eigentrust_2003] S. D. Kamvar, M. T. Schlosser, H. Garcia-Molina
       "The eigentrust algorithm for reputation management in p2p networks",
       Proceedings of the 12th international conference on World Wide Web,
       Pages: 640 - 651, 2003
    """

Tiago Peixoto's avatar
Tiago Peixoto committed
380
381
    if vprop == None:
        vprop = g.new_vertex_property("double")
382
383
384
385
386
387
388
389
390
391
392
    i = libgraph_tool_centrality.\
           get_eigentrust(g._Graph__graph, _prop("e", g, trust_map),
                          _prop("v", g, vprop), epslon, max_iter)
    if norm:
        vprop.get_array()[:] /= sum(vprop.get_array())

    if ret_iter:
        return vprop, i
    else:
        return vprop

393
394
def absolute_trust(g, trust_map, source=None, vprop=None, n_iter=100,
                   reversed=False, seed=None):
395
    r"""
396
397
    Samples the absolute trust centrality of each vertex in the graph, or only
    for a given source, if one is provided.
398
399
400
401
402
403
404
405

    Parameters
    ----------
    g : Graphs
        Graph to be used.
    trust_map : ProperyMap
        Edge property map with the values of trust associated with each
        edge. The values must lie in the range [0,1].
406
407
408
    source : Vertex, optional (default: None)
        A vertex which is used the as the sole source for gathering trust
        values, instead of all the vertices in the graph.
409
410
    vprop : PropertyMap, optional (default: None)
        Vertex property map where the values of eigentrust must be stored.
411
412
413
    n_iter : int, optional (default: 100)
        Number of iterations (independent self-avoiding walks) per source
        vertex.
414
415
416
417
    reversed : bool, optional (default: False)
        Calculates the "reversed" trust instead: The direction of the edges are
        inverted, but the path weighting is preserved in the original direction
        (see Notes below).
418
419
420
421
422
423
    seed : int, optional (default: None)
         The initializing seed for the random number generator. If not supplied
         a different random value will be chosen each time.

    Returns
    -------
424
425
    absolute_trust : PropertyMap
        A vertex property map containing the absolute trust vector from the
426
        corresponding vertex to the rest of the network. Each element i of the
427
428
        vector is the trust value of the vertex with index i, from the given
        vertex.
429

430
431
        If the parameter "source" is specified, the values of the
        property map are scalars, instead of vectors.
432

433
434
435
436
437
438
439
440
441
442
    See Also
    --------
    eigentrust: eigentrust centrality
    betweenness: betweenness centrality
    pagerank: PageRank centrality

    Notes
    -----
    The absolute trust between vertices i and j is defined as

443
444
    .. math::

Tiago Peixoto's avatar
Tiago Peixoto committed
445
446
        t_{ij} = \frac{1}{\sum_{\{i\to j\}}w_{\{i\to j\}}}\sum_{\{i\to j\}}
                 w_{\{i\to j\}} \prod_{e\in \{i\to j\}}c_e
447

Tiago Peixoto's avatar
Tiago Peixoto committed
448
449
450
451
    where the sum is taken over all paths from i to j (without loops),
    :math:`c_e` is the direct trust value associated with edge e, and
    :math:`w_{\{i\to j\}}` is the weight of a given path, which is defined as

452
453
    .. math::

Tiago Peixoto's avatar
Tiago Peixoto committed
454
455
456
       w_{\{i\to j\}} = \prod_{e\in \{i\to j\}}\{\delta_{t(e),j}(1-c_e) + c_e\},

    such that the direct trust of the last edge on the path is not considered.
457

458
459
460
461
462
463
464
465
    The algorithm performs only an approximation of the above measure, by doing
    several self-avoiding random walks per source vertex, and computing the
    trust for all different paths found. Each complete walk is done by one of
    three types of biased choices, which govern the probability of following a
    specific edge: 1. With probability proportional to an edge's trust value;
    2. With probability proportional to the complement of an edge's trust value
    :math:`(1-t_{e})`; 3. All edges with equal probability. The parameter
    "n_iter" controls how many walks of each type are performed.
466
467
468
469
470
471
472
473
474
475

    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)), seed=42)
    >>> trust = g.new_edge_property("double")
    >>> trust.get_array()[:] = random(g.num_edges())
Tiago Peixoto's avatar
Tiago Peixoto committed
476
    >>> t = gt.absolute_trust(g, trust)
477
    >>> print array(t[g.vertex(10)])
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
    [  4.34269834e-01   1.84086073e-02   2.71138999e-03   0.00000000e+00
       0.00000000e+00   2.34151772e-01   0.00000000e+00   2.74629641e-01
       3.82398108e-02   3.51593064e-03   0.00000000e+00   2.25062582e-03
       7.34404022e-02   3.02164679e-02   1.10468968e-01   3.36602968e-01
       1.43712489e-01   1.09583017e-02   9.97860841e-03   6.20179844e-03
       3.92453395e-03   5.93950226e-01   2.42696614e-01   1.10854185e-02
       5.30694827e-02   8.71279579e-02   1.71495793e-01   4.53369014e-01
       4.08880943e-01   2.43222424e-01   2.31048774e-02   1.97243934e-01
       2.28690841e-02   3.89067069e-01   5.65879939e-01   2.64268559e-01
       1.02377787e-02   0.00000000e+00   2.24908740e-01   1.37897878e-01
       0.00000000e+00   3.84890236e-02   1.36013255e-01   1.13238046e-01
       4.42613337e-02   1.54047730e-01   7.26844315e-04   9.74848849e-01
       2.01388742e-03   2.98355979e-01   5.06984528e-01   1.99649387e-01
       2.77657386e-02   4.29460962e-01   1.02848076e-01   4.89428001e-02
       8.00972222e-02   1.44641703e-01   5.47481542e-02   6.50547255e-01
       8.27105379e-02   1.39682293e-01   1.53401434e-01   1.86305773e-03
       1.10090329e-01   1.66675096e-01   6.19209248e-01   4.98140945e-01
       2.94691587e-01   2.72221755e-01   0.00000000e+00   0.00000000e+00
       3.26706417e-01   4.91620740e-02   1.97732120e-01   5.43678807e-01
       3.22718844e-01   3.99852058e-03   1.28240520e-01   5.80458086e-02
       6.15485883e-04   2.65672284e-01   1.44784379e-01   5.08912552e-02
       1.02175200e-01   2.88217333e-01   4.58941581e-01   2.21592754e-04
       2.43926919e-01   2.29463331e-01   2.23860692e-02   3.96450827e-01
       1.50189446e-01   3.84811442e-02   1.43428384e-01   1.96921016e-02
       4.77979398e-01   7.78812012e-02   5.56363349e-01   2.12033381e-01]
503
    """
Tiago Peixoto's avatar
Tiago Peixoto committed
504
505
506
507

    if seed != 0:
        seed = numpy.random.randint(0, sys.maxint)
    if vprop == None:
508
509
510
511
512
513
514
        if source == None:
            vprop = g.new_vertex_property("vector<double>")
        else:
            vprop = g.new_vertex_property("double")

    if source != None:
        vprop_temp = vprop
Tiago Peixoto's avatar
Tiago Peixoto committed
515
        vprop = g.new_vertex_property("vector<double>")
516
517
518
519
        source = g.vertex_index[source]
    else:
        source = -1

520
521
522
523
    if reversed:
        g.stash_filter(reversed=True)
        g.set_reversed(True)

524
    libgraph_tool_centrality.\
525
526
            get_absolute_trust(g._Graph__graph, source,
                               _prop("e", g, trust_map), _prop("v", g, vprop),
527
                               n_iter, reversed, seed)
528
529
530
    if reversed:
        g.pop_filter(reversed=True)

531
532
533
534
    if source != -1:
        vprop_temp.get_array()[:] = numpy.array(vprop[g.vertex(source)])
        vprop = vprop_temp

535
    return vprop
Tiago Peixoto's avatar
Tiago Peixoto committed
536