Commit 713c9854 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Fix assortativity for undirected graphs (ticket #52)

parent b96e8035
......@@ -21,6 +21,7 @@
#include <tr1/unordered_set>
#include "shared_map.hh"
#include "histogram.hh"
#include "graph_util.hh"
namespace graph_tool
{
......@@ -37,11 +38,14 @@ struct get_assortativity_coefficient
double& r_err) const
{
const Graph& g = *gp;
size_t n_edges = 0;
int e_kk = 0;
tr1::unordered_map<double,int> a, b;
SharedMap<tr1::unordered_map<double,int> > sa(a), sb(b);
typedef typename mpl::if_<typename is_directed::apply<Graph>::type,
size_t, double>::type count_t;
count_t c = (is_directed::apply<Graph>::type::value) ? 1.0 : 0.5;
count_t n_edges = 0;
count_t e_kk = 0;
tr1::unordered_map<double,count_t> a, b;
SharedMap<tr1::unordered_map<double,count_t> > sa(a), sb(b);
int i, N = num_vertices(g);
#pragma omp parallel for default(shared) private(i) firstprivate(sa,sb)\
schedule(dynamic) reduction(+:e_kk, n_edges)
......@@ -56,11 +60,11 @@ struct get_assortativity_coefficient
for (tie(e,e_end) = out_edges(v, g); e != e_end; ++e)
{
double k2 = deg(target(*e, g), g);
sa[k1]++;
sb[k2]++;
if (k1 == k2)
e_kk++;
n_edges++;
e_kk += c;
sa[k1] += c;
sb[k2] += c;
n_edges += c;
}
}
......@@ -98,7 +102,7 @@ struct get_assortativity_coefficient
tl1 -= 1;
tl1 /= n_edges - 1;
double rl = (tl1 - tl2)/(1.0 - tl2);
err += (r-rl)*(r-rl);
err += (r-rl)*(r-rl)*c;
}
}
r_err = sqrt(err);
......@@ -115,10 +119,15 @@ struct get_scalar_assortativity_coefficient
double& r_err) const
{
const Graph& g = *gp;
size_t n_edges = 0;
typedef typename mpl::if_<typename is_directed::apply<Graph>::type,
size_t, double>::type count_t;
count_t c = (is_directed::apply<Graph>::type::value) ? 1.0 : 0.5;
count_t n_edges = 0;
double e_xy = 0.0;
tr1::unordered_map<double,int> a, b;
SharedMap<tr1::unordered_map<double,int> > sa(a), sb(b);
tr1::unordered_map<double,count_t> a, b;
SharedMap<tr1::unordered_map<double,count_t> > sa(a), sb(b);
int i, N = num_vertices(g);
#pragma omp parallel for default(shared) private(i) firstprivate(sa,sb)\
......@@ -134,10 +143,10 @@ struct get_scalar_assortativity_coefficient
for (tie(e,e_end) = out_edges(v, g); e != e_end; ++e)
{
double k2 = deg(target(*e,g),g);
sa[k1]++;
sb[k2]++;
e_xy += k1*k2;
n_edges++;
sa[k1] += c;
sb[k2] += c;
e_xy += k1*k2*c;
n_edges += c;
}
}
......@@ -187,7 +196,7 @@ struct get_scalar_assortativity_coefficient
rl = (t1l - avg_al*avg_bl)/(dal*dbl);
else
rl = (t1l - avg_al*avg_bl);
err += (r-rl)*(r-rl);
err += (r-rl)*(r-rl)*c;
}
}
r_err = sqrt(err);
......
......@@ -337,6 +337,7 @@ BOOST_PYTHON_MODULE(libgraph_tool_core)
variant_from_python<GraphInterface::degree_t>();
to_python_converter<pair<string,bool>, pair_to_tuple<string,bool> >();
to_python_converter<pair<size_t,size_t>, pair_to_tuple<size_t,size_t> >();
to_python_converter<pair<double,double>, pair_to_tuple<double,double> >();
pair_from_tuple<double,double>();
pair_from_tuple<size_t,size_t>();
#ifdef HAVE_SCIPY
......
......@@ -35,6 +35,23 @@ namespace graph_tool
{
using namespace boost;
//
// Metaprogramming
// ===============
// useful metafunction to determine whether a graph is directed or not
struct is_directed
{
template <class Graph>
struct apply
{
typedef is_convertible<typename graph_traits<Graph>::directed_category,
directed_tag> type;
};
};
// This will count "by hand" the number of vertices on a graph. Always O(V).
struct HardNumVertices
{
......
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