Commit b7188ade authored by Tiago Peixoto's avatar Tiago Peixoto

Add support for a single source in absolute trust code

parent 0f15b1fe
......@@ -32,8 +32,8 @@ using namespace std;
using namespace boost;
using namespace graph_tool;
void absolute_trust(GraphInterface& g, boost::any c, boost::any t,
double epslon, size_t max_iter, size_t seed)
void absolute_trust(GraphInterface& g, int64_t source, boost::any c,
boost::any t, double epslon, size_t max_iter, size_t seed)
{
rng_t rng(static_cast<rng_t::result_type>(seed));
......@@ -44,8 +44,8 @@ void absolute_trust(GraphInterface& g, boost::any c, boost::any t,
run_action<>()
(g, bind<void>
(get_eigentrust(),
_1, g.GetVertexIndex(), g.GetEdgeIndex(), _2,
(get_absolute_trust(),
_1, g.GetVertexIndex(), g.GetEdgeIndex(), source, _2,
_3, epslon, max_iter, ref(rng)),
edge_floating_properties(),
vertex_floating_vector_properties())(c,t);
......
......@@ -29,13 +29,13 @@ namespace graph_tool
using namespace std;
using namespace boost;
struct get_eigentrust
struct get_absolute_trust
{
template <class Graph, class VertexIndex, class EdgeIndex, class TrustMap,
class InferredTrustMap>
void operator()(Graph& g, VertexIndex vertex_index, EdgeIndex edge_index,
TrustMap c, InferredTrustMap t, double epslon,
size_t max_iter, rng_t& rng)
int64_t source, TrustMap c, InferredTrustMap t,
double epslon, size_t max_iter, rng_t& rng)
const
{
typedef typename property_traits<TrustMap>::value_type c_type;
......@@ -50,10 +50,12 @@ struct get_eigentrust
// init inferred trust t
int i, N = num_vertices(g);
#pragma omp parallel for default(shared) private(i) \
schedule(dynamic)
for (i = 0; i < N; ++i)
schedule(dynamic)
for (i = (source == -1) ? 0 : source;
i < ((source == -1) ? N : source + 1); ++i)
{
typename graph_traits<Graph>::vertex_descriptor v = vertex(i, g);
typename graph_traits<Graph>::vertex_descriptor v =
vertex(i, g);
if (v == graph_traits<Graph>::null_vertex())
continue;
t[v].resize(N);
......@@ -66,10 +68,10 @@ struct get_eigentrust
while (delta >= epslon)
{
delta = 0;
int i, N = num_vertices(g);
#pragma omp parallel for default(shared) private(i) \
schedule(dynamic) reduction(+:delta)
for (i = 0; i < N; ++i)
for (i = (source == -1) ? 0 : source;
i < ((source == -1) ? N : source + 1); ++i)
{
typename graph_traits<Graph>::vertex_descriptor v =
vertex(i, g);
......@@ -96,6 +98,8 @@ struct get_eigentrust
target(*e,g);
if (v_mark[v][vertex_index[t]] <= iter)
{
if (c[*e] <= 0)
continue;
out_es.push_back(*e);
if (out_prob.empty())
out_prob.push_back(c[*e]);
......@@ -141,7 +145,8 @@ struct get_eigentrust
#pragma omp parallel for default(shared) private(i) \
schedule(dynamic)
for (i = 0; i < N; ++i)
for (i = (source == -1) ? 0 : source;
i < ((source == -1) ? N : source + 1); ++i)
{
typename graph_traits<Graph>::vertex_descriptor v = vertex(i, g);
if (v == graph_traits<Graph>::null_vertex())
......
......@@ -27,7 +27,7 @@ from .. dl_import import dl_import
dl_import("import libgraph_tool_centrality")
from .. core import _prop
import numpy
import sys, numpy
__all__ = ["pagerank", "betweenness", "central_point_dominance", "eigentrust",
"absolute_trust"]
......@@ -379,10 +379,11 @@ def eigentrust(g, trust_map, vprop=None, norm=False, epslon=1e-6, max_iter=0,
else:
return vprop
def absolute_trust(g, trust_map, vprop=None, epslon=0.1, max_iter=None,
seed=None, ret_iter=False)
def absolute_trust(g, trust_map, source=None, vprop=None, epslon=0.1,
max_iter=None, seed=None, ret_iter=False):
r"""
Samples the absolute trust centrality of each vertex in the graph.
Samples the absolute trust centrality of each vertex in the graph, or only
for a given source, if one is provided.
Parameters
----------
......@@ -391,6 +392,9 @@ def absolute_trust(g, trust_map, vprop=None, epslon=0.1, max_iter=None,
trust_map : ProperyMap
Edge property map with the values of trust associated with each
edge. The values must lie in the range [0,1].
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.
vprop : PropertyMap, optional (default: None)
Vertex property map where the values of eigentrust must be stored.
epslon : float, optional (default: 0.1)
......@@ -410,6 +414,9 @@ def absolute_trust(g, trust_map, vprop=None, epslon=0.1, max_iter=None,
corresponding vertex to the rest of the network. Each element i of the
vector is the trust value of the vertex with index i, from the given vertex.
If the parameter "source" is specified, the values of the property map are
scalars, instead of vectors.
See Also
--------
eigentrust: eigentrust centrality
......@@ -464,10 +471,29 @@ def absolute_trust(g, trust_map, vprop=None, epslon=0.1, max_iter=None,
if seed != 0:
seed = numpy.random.randint(0, sys.maxint)
if vprop == None:
if source == None:
vprop = g.new_vertex_property("vector<double>")
else:
vprop = g.new_vertex_property("double")
if source != None:
vprop_temp = vprop
vprop = g.new_vertex_property("vector<double>")
source = g.vertex_index[source]
else:
source = -1
if max_iter == None:
max_iter = 0
ic = libgraph_tool_centrality.\
get_absolute_trust(g._Graph__graph, _prop("e", g, trust_map),
_prop("v", g, vprop), epslon, max_iter, seed)
get_absolute_trust(g._Graph__graph, source,
_prop("e", g, trust_map), _prop("v", g, vprop),
epslon, max_iter, seed)
if source != -1:
vprop_temp.get_array()[:] = numpy.array(vprop[g.vertex(source)])
vprop = vprop_temp
if ret_iter:
return vprop, ic
else:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment