Commit 441495fa authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Use range-based iteration in centrality algorithms

parent 6daae30b
......@@ -90,15 +90,14 @@ struct get_closeness
get_vertex_dists(g, v, vertex_index, dist_map, weights, comp_size);
closeness[v] = 0;
typename graph_traits<Graph>::vertex_iterator v2, v_end;
for (tie(v2, v_end) = vertices(g); v2 != v_end; ++v2)
for (auto v2 : vertices_range(g))
{
if (*v2 != v && dist_map[*v2] != numeric_limits<val_type>::max())
if (v2 != v && dist_map[v2] != numeric_limits<val_type>::max())
{
if (!harmonic)
closeness[v] += dist_map[*v2];
closeness[v] += dist_map[v2];
else
closeness[v] += 1. / dist_map[*v2];
closeness[v] += 1. / dist_map[v2];
}
}
if (!harmonic)
......
......@@ -63,22 +63,19 @@ struct get_eigenvector
schedule(runtime) if (N > 100) reduction(+:norm)
for (i = 0; i < N; ++i)
{
typename graph_traits<Graph>::vertex_descriptor v =
vertex(i, g);
auto v = vertex(i, g);
if (v == graph_traits<Graph>::null_vertex())
continue;
c_temp[v] = 0;
typename in_or_out_edge_iteratorS<Graph>::type e, e_end;
for (tie(e, e_end) = in_or_out_edge_iteratorS<Graph>::get_edges(v, g);
e != e_end; ++e)
for (const auto& e : in_or_out_edges_range(v, g))
{
typename graph_traits<Graph>::vertex_descriptor s;
if (is_directed::apply<Graph>::type::value)
s = source(*e,g);
s = source(e, g);
else
s = target(*e,g);
c_temp[v] += get(w, *e) * c[s];
s = target(e, g);
c_temp[v] += get(w, e) * c[s];
}
norm += power(c_temp[v], 2);
}
......@@ -89,8 +86,7 @@ struct get_eigenvector
schedule(runtime) if (N > 100) reduction(+:delta)
for (i = 0; i < N; ++i)
{
typename graph_traits<Graph>::vertex_descriptor v =
vertex(i, g);
auto v = vertex(i, g);
if (v == graph_traits<Graph>::null_vertex())
continue;
c_temp[v] /= norm;
......@@ -111,8 +107,7 @@ struct get_eigenvector
schedule(runtime) if (N > 100)
for (i = 0; i < N; ++i)
{
typename graph_traits<Graph>::vertex_descriptor v =
vertex(i, g);
auto v = vertex(i, g);
if (v == graph_traits<Graph>::null_vertex())
continue;
c[v] = c_temp[v];
......
......@@ -57,7 +57,7 @@ struct get_hits
schedule(runtime) if (N > 100)
for (i = 0; i < N; ++i)
{
typename graph_traits<Graph>::vertex_descriptor v = vertex(i, g);
auto v = vertex(i, g);
if (v == graph_traits<Graph>::null_vertex())
continue;
x[v] = 1.0 / V;
......@@ -81,26 +81,23 @@ struct get_hits
continue;
x_temp[v] = 0;
typename in_or_out_edge_iteratorS<Graph>::type ie, ie_end;
for (tie(ie, ie_end) = in_or_out_edge_iteratorS<Graph>::get_edges(v, g);
ie != ie_end; ++ie)
for (const auto& ie : in_or_out_edges_range(v, g))
{
typename graph_traits<Graph>::vertex_descriptor s =
source(*ie, g);
auto s = source(ie, g);
if (is_directed::apply<Graph>::type::value)
s = source(*ie, g);
s = source(ie, g);
else
s = target(*ie,g);
x_temp[v] += get(w, *ie) * y[s];
s = target(ie, g);
x_temp[v] += get(w, ie) * y[s];
}
x_norm += power(x_temp[v], 2);
y_temp[v] = 0;
typename graph_traits<Graph>::out_edge_iterator e, e_end;
for (tie(e, e_end) = out_edges(v, g); e != e_end; ++e)
for (const auto& e : out_edges_range(v, g))
{
typename graph_traits<Graph>::vertex_descriptor s = target(*e, g);
y_temp[v] += get(w, *e) * x[s];
auto s = target(e, g);
y_temp[v] += get(w, e) * x[s];
}
y_norm += power(y_temp[v], 2);
}
......@@ -112,8 +109,7 @@ struct get_hits
schedule(runtime) if (N > 100) reduction(+:delta)
for (i = 0; i < N; ++i)
{
typename graph_traits<Graph>::vertex_descriptor v =
vertex(i, g);
auto v = vertex(i, g);
if (v == graph_traits<Graph>::null_vertex())
continue;
x_temp[v] /= x_norm;
......@@ -135,8 +131,7 @@ struct get_hits
schedule(runtime) if (N > 100)
for (i = 0; i < N; ++i)
{
typename graph_traits<Graph>::vertex_descriptor v =
vertex(i, g);
auto v = vertex(i, g);
if (v == graph_traits<Graph>::null_vertex())
continue;
x[v] = x_temp[v];
......
......@@ -59,22 +59,19 @@ struct get_katz
schedule(runtime) if (N > 100)
for (i = 0; i < N; ++i)
{
typename graph_traits<Graph>::vertex_descriptor v =
vertex(i, g);
auto v = vertex(i, g);
if (v == graph_traits<Graph>::null_vertex())
continue;
c_temp[v] = get(beta, v);
typename in_or_out_edge_iteratorS<Graph>::type e, e_end;
for (tie(e, e_end) = in_or_out_edge_iteratorS<Graph>::get_edges(v, g);
e != e_end; ++e)
for (const auto& e : in_or_out_edges_range(v, g))
{
typename graph_traits<Graph>::vertex_descriptor s;
if (is_directed::apply<Graph>::type::value)
s = source(*e, g);
s = source(e, g);
else
s = target(*e, g);
c_temp[v] += alpha * get(w, *e) * c[s];
s = target(e, g);
c_temp[v] += alpha * get(w, e) * c[s];
}
}
......@@ -83,8 +80,7 @@ struct get_katz
schedule(runtime) if (N > 100) reduction(+:delta)
for (i = 0; i < N; ++i)
{
typename graph_traits<Graph>::vertex_descriptor v =
vertex(i, g);
auto v = vertex(i, g);
if (v == graph_traits<Graph>::null_vertex())
continue;
delta += abs(c_temp[v] - c[v]);
......@@ -102,8 +98,7 @@ struct get_katz
schedule(runtime) if (N > 100)
for (i = 0; i < N; ++i)
{
typename graph_traits<Graph>::vertex_descriptor v =
vertex(i, g);
auto v = vertex(i, g);
if (v == graph_traits<Graph>::null_vertex())
continue;
c_temp[v] = c[v];
......
......@@ -46,13 +46,12 @@ struct get_pagerank
schedule(runtime) if (N > 100)
for (i = 0; i < N; ++i)
{
typename graph_traits<Graph>::vertex_descriptor v = vertex(i, g);
auto v = vertex(i, g);
if (v == graph_traits<Graph>::null_vertex())
continue;
put(deg, v, 0);
typename graph_traits<Graph>::out_edge_iterator e, e_end;
for (tie(e, e_end) = out_edges(v, g); e!= e_end; ++e)
put(deg, v, get(deg, v) + get(weight, *e));
for (const auto& e : out_edges_range(v, g))
put(deg, v, get(deg, v) + get(weight, e));
}
rank_type delta = epsilon + 1;
......@@ -65,22 +64,18 @@ struct get_pagerank
schedule(runtime) if (N > 100) reduction(+:delta)
for (i = 0; i < N; ++i)
{
typename graph_traits<Graph>::vertex_descriptor v =
vertex(i, g);
auto v = vertex(i, g);
if (v == graph_traits<Graph>::null_vertex())
continue;
rank_type r = 0;
typename in_or_out_edge_iteratorS<Graph>::type e, e_end;
for (tie(e, e_end) = in_or_out_edge_iteratorS<Graph>::get_edges(v, g);
e != e_end; ++e)
for (const auto& e : in_or_out_edges_range(v, g))
{
typename graph_traits<Graph>::vertex_descriptor s;
if (is_directed::apply<Graph>::type::value)
s = source(*e, g);
s = source(e, g);
else
s = target(*e, g);
r += (get(rank, s) * get(weight, *e)) / get(deg, s);
s = target(e, g);
r += (get(rank, s) * get(weight, e)) / get(deg, s);
}
put(r_temp, v, (1.0 - d) * get(pers, v) + d * r);
......@@ -99,8 +94,7 @@ struct get_pagerank
schedule(runtime) if (N > 100)
for (i = 0; i < N; ++i)
{
typename graph_traits<Graph>::vertex_descriptor v =
vertex(i, g);
auto v = vertex(i, g);
if (v == graph_traits<Graph>::null_vertex())
continue;
put(rank, v, get(r_temp, v));
......
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