Commit cfd37051 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

graph_clustering.hh: improve speed of triangle counting

parent 1f792136
...@@ -49,7 +49,7 @@ auto get_triangles(typename graph_traits<Graph>::vertex_descriptor v, ...@@ -49,7 +49,7 @@ auto get_triangles(typename graph_traits<Graph>::vertex_descriptor v,
EWeight& eweight, VProp& mark, const Graph& g) EWeight& eweight, VProp& mark, const Graph& g)
{ {
typedef typename property_traits<EWeight>::value_type val_t; typedef typename property_traits<EWeight>::value_type val_t;
val_t triangles = 0, w1 = 0, w2 = 0, k = 0; val_t triangles = 0, k = 0;
for (auto e : out_edges_range(v, g)) for (auto e : out_edges_range(v, g))
{ {
...@@ -66,15 +66,17 @@ auto get_triangles(typename graph_traits<Graph>::vertex_descriptor v, ...@@ -66,15 +66,17 @@ auto get_triangles(typename graph_traits<Graph>::vertex_descriptor v,
auto n = target(e, g); auto n = target(e, g);
if (n == v) if (n == v)
continue; continue;
w1 = eweight[e]; auto m = mark[n];
mark[n] = 0;
val_t t = 0;
for (auto e2 : out_edges_range(n, g)) for (auto e2 : out_edges_range(n, g))
{ {
auto n2 = target(e2, g); auto n2 = target(e2, g);
if (n2 == n) if (mark[n2] > 0)
continue; t += eweight[e2];
w2 = eweight[e2];
triangles += mark[n2] * w1 * w2;
} }
triangles += t * eweight[e];
mark[n] = m;
} }
for (auto n : adjacent_vertices_range(v, g)) for (auto n : adjacent_vertices_range(v, g))
...@@ -96,6 +98,7 @@ struct get_global_clustering ...@@ -96,6 +98,7 @@ struct get_global_clustering
typedef typename property_traits<EWeight>::value_type val_t; typedef typename property_traits<EWeight>::value_type val_t;
val_t triangles = 0, n = 0; val_t triangles = 0, n = 0;
vector<val_t> mask(num_vertices(g), 0); vector<val_t> mask(num_vertices(g), 0);
vector<std::pair<val_t, val_t>> ret(num_vertices(g));
#pragma omp parallel if (num_vertices(g) > OPENMP_MIN_THRESH) \ #pragma omp parallel if (num_vertices(g) > OPENMP_MIN_THRESH) \
firstprivate(mask) reduction(+:triangles, n) firstprivate(mask) reduction(+:triangles, n)
...@@ -112,17 +115,17 @@ struct get_global_clustering ...@@ -112,17 +115,17 @@ struct get_global_clustering
// "jackknife" variance // "jackknife" variance
c_err = 0.0; c_err = 0.0;
double cerr = 0.0; double cerr = 0.0;
#pragma omp parallel if (num_vertices(g) > OPENMP_MIN_THRESH) \ #pragma omp parallel if (num_vertices(g) > OPENMP_MIN_THRESH) \
firstprivate(mask) reduction(+:cerr) reduction(+:cerr)
parallel_vertex_loop_no_spawn parallel_vertex_loop_no_spawn
(g, (g,
[&](auto v) [&](auto v)
{ {
auto temp = get_triangles(v, eweight, mask, g); auto cl = double(triangles - ret[v].first) /
double cl = double(triangles - temp.first) / (n - ret[v].second);
(n - temp.second);
cerr += power(c - cl, 2); cerr += power(c - cl, 2);
}); });
c_err = sqrt(cerr); c_err = sqrt(cerr);
} }
}; };
......
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