Commit 4a5bc229 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

clustering.local_clustering(): Fix bug with directed graphs

parent c8d01f45
...@@ -34,25 +34,19 @@ using namespace graph_tool; ...@@ -34,25 +34,19 @@ using namespace graph_tool;
boost::python::tuple global_clustering(GraphInterface& g) boost::python::tuple global_clustering(GraphInterface& g)
{ {
double c, c_err; double c, c_err;
bool directed = g.get_directed();
g.set_directed(false);
run_action<graph_tool::detail::never_directed>() run_action<graph_tool::detail::never_directed>()
(g, std::bind(get_global_clustering(), std::placeholders::_1, (g, std::bind(get_global_clustering(), std::placeholders::_1,
std::ref(c), std::ref(c_err)))(); std::ref(c), std::ref(c_err)))();
g.set_directed(directed);
return boost::python::make_tuple(c, c_err); return boost::python::make_tuple(c, c_err);
} }
void local_clustering(GraphInterface& g, boost::any prop) void local_clustering(GraphInterface& g, boost::any prop)
{ {
bool directed = g.get_directed(); run_action<>()
g.set_directed(false);
run_action<graph_tool::detail::never_directed>()
(g, std::bind(set_clustering_to_property(), (g, std::bind(set_clustering_to_property(),
std::placeholders::_1, std::placeholders::_1,
std::placeholders::_2), std::placeholders::_2),
writable_vertex_scalar_properties())(prop); writable_vertex_scalar_properties())(prop);
g.set_directed(directed);
} }
using namespace boost::python; using namespace boost::python;
......
...@@ -46,7 +46,7 @@ using namespace boost; ...@@ -46,7 +46,7 @@ using namespace boost;
template <class Graph, class VProp> template <class Graph, class VProp>
pair<int,int> pair<int,int>
get_triangles(typename graph_traits<Graph>::vertex_descriptor v, VProp& mark, get_triangles(typename graph_traits<Graph>::vertex_descriptor v, VProp& mark,
const Graph &g) const Graph& g)
{ {
size_t triangles = 0; size_t triangles = 0;
...@@ -74,7 +74,10 @@ get_triangles(typename graph_traits<Graph>::vertex_descriptor v, VProp& mark, ...@@ -74,7 +74,10 @@ get_triangles(typename graph_traits<Graph>::vertex_descriptor v, VProp& mark,
mark[n] = false; mark[n] = false;
size_t k = out_degree(v, g); size_t k = out_degree(v, g);
return make_pair(triangles / 2, (k * (k - 1)) / 2); if (is_directed::apply<Graph>::type::value)
return make_pair(triangles, (k * (k - 1)));
else
return make_pair(triangles / 2, (k * (k - 1)) / 2);
} }
...@@ -124,7 +127,6 @@ struct set_clustering_to_property ...@@ -124,7 +127,6 @@ struct set_clustering_to_property
void operator()(const Graph& g, ClustMap clust_map) const void operator()(const Graph& g, ClustMap clust_map) const
{ {
typedef typename property_traits<ClustMap>::value_type c_type; typedef typename property_traits<ClustMap>::value_type c_type;
typename get_undirected_graph<Graph>::type ug(g);
vector<bool> mask(num_vertices(g), false); vector<bool> mask(num_vertices(g), false);
#pragma omp parallel if (num_vertices(g) > OPENMP_MIN_THRESH) \ #pragma omp parallel if (num_vertices(g) > OPENMP_MIN_THRESH) \
...@@ -133,7 +135,7 @@ struct set_clustering_to_property ...@@ -133,7 +135,7 @@ struct set_clustering_to_property
(g, (g,
[&](auto v) [&](auto v)
{ {
auto triangles = get_triangles(v, mask, ug); // get from ug auto triangles = get_triangles(v, mask, g);
double clustering = (triangles.second > 0) ? double clustering = (triangles.second > 0) ?
double(triangles.first)/triangles.second : double(triangles.first)/triangles.second :
0.0; 0.0;
......
...@@ -130,10 +130,10 @@ def local_clustering(g, prop=None, undirected=True): ...@@ -130,10 +130,10 @@ def local_clustering(g, prop=None, undirected=True):
:doi:`10.1038/30918` :doi:`10.1038/30918`
""" """
if prop == None: if prop is None:
prop = g.new_vertex_property("double") prop = g.new_vertex_property("double")
if g.is_directed() and undirected: if g.is_directed() and undirected:
g = GraphView(g, directed=False) g = GraphView(g, directed=False, skip_properties=True)
_gt.local_clustering(g._Graph__graph, _prop("v", g, prop)) _gt.local_clustering(g._Graph__graph, _prop("v", g, prop))
return prop return prop
...@@ -191,6 +191,8 @@ def global_clustering(g): ...@@ -191,6 +191,8 @@ def global_clustering(g):
:doi:`10.1137/S003614450342480` :doi:`10.1137/S003614450342480`
""" """
if g.is_directed():
g = GraphView(g, directed=False, skip_properties=True)
c = _gt.global_clustering(g._Graph__graph) c = _gt.global_clustering(g._Graph__graph)
return c return c
......
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