Commit 354c0a1a authored by Tiago Peixoto's avatar Tiago Peixoto

vertex_similarity(): Add salton, hub-promoted, hub-suppressed,...

vertex_similarity(): Add salton, hub-promoted, hub-suppressed, resource-allocation and leicht-holme-newman
parent 9137c542
Pipeline #493 failed with stage
in 444 minutes and 11 seconds
...@@ -49,6 +49,63 @@ void get_dice_similarity(GraphInterface& gi, boost::any as, boost::any weight) ...@@ -49,6 +49,63 @@ void get_dice_similarity(GraphInterface& gi, boost::any as, boost::any weight)
(gi.get_graph_view(), as, weight); (gi.get_graph_view(), as, weight);
} }
void get_salton_similarity(GraphInterface& gi, boost::any as, boost::any weight)
{
if (weight.empty())
weight = ecmap_t();
gt_dispatch<>()
([&](auto& g, auto& s, auto& w)
{
all_pairs_similarity(g, s,
[&](auto u, auto v, auto& mask, auto& w)
{
return salton(u, v, mask, w, g);
}, w);
},
all_graph_views(), vertex_floating_vector_properties(),
weight_props_t())
(gi.get_graph_view(), as, weight);
}
void get_hub_promoted_similarity(GraphInterface& gi, boost::any as, boost::any weight)
{
if (weight.empty())
weight = ecmap_t();
gt_dispatch<>()
([&](auto& g, auto& s, auto& w)
{
all_pairs_similarity(g, s,
[&](auto u, auto v, auto& mask, auto& w)
{
return hub_promoted(u, v, mask, w, g);
}, w);
},
all_graph_views(), vertex_floating_vector_properties(),
weight_props_t())
(gi.get_graph_view(), as, weight);
}
void get_hub_suppressed_similarity(GraphInterface& gi, boost::any as, boost::any weight)
{
if (weight.empty())
weight = ecmap_t();
gt_dispatch<>()
([&](auto& g, auto& s, auto& w)
{
all_pairs_similarity(g, s,
[&](auto u, auto v, auto& mask, auto& w)
{
return hub_suppressed(u, v, mask, w, g);
}, w);
},
all_graph_views(), vertex_floating_vector_properties(),
weight_props_t())
(gi.get_graph_view(), as, weight);
}
void get_dice_similarity_pairs(GraphInterface& gi, python::object opairs, void get_dice_similarity_pairs(GraphInterface& gi, python::object opairs,
python::object osim, boost::any weight) python::object osim, boost::any weight)
{ {
...@@ -71,6 +128,72 @@ void get_dice_similarity_pairs(GraphInterface& gi, python::object opairs, ...@@ -71,6 +128,72 @@ void get_dice_similarity_pairs(GraphInterface& gi, python::object opairs,
(gi.get_graph_view(), weight); (gi.get_graph_view(), weight);
} }
void get_salton_similarity_pairs(GraphInterface& gi, python::object opairs,
python::object osim, boost::any weight)
{
multi_array_ref<int64_t,2> pairs = get_array<int64_t,2>(opairs);
multi_array_ref<double,1> sim = get_array<double,1>(osim);
if (weight.empty())
weight = ecmap_t();
gt_dispatch<>()
([&](auto& g, auto w)
{
some_pairs_similarity(g, pairs, sim,
[&](auto u, auto v, auto& mask, auto& w)
{
return salton(u, v, mask, w, g);
}, w);
},
all_graph_views(), weight_props_t())
(gi.get_graph_view(), weight);
}
void get_hub_promoted_similarity_pairs(GraphInterface& gi, python::object opairs,
python::object osim, boost::any weight)
{
multi_array_ref<int64_t,2> pairs = get_array<int64_t,2>(opairs);
multi_array_ref<double,1> sim = get_array<double,1>(osim);
if (weight.empty())
weight = ecmap_t();
gt_dispatch<>()
([&](auto& g, auto w)
{
some_pairs_similarity(g, pairs, sim,
[&](auto u, auto v, auto& mask, auto& w)
{
return hub_promoted(u, v, mask, w, g);
}, w);
},
all_graph_views(), weight_props_t())
(gi.get_graph_view(), weight);
}
void get_hub_suppressed_similarity_pairs(GraphInterface& gi, python::object opairs,
python::object osim, boost::any weight)
{
multi_array_ref<int64_t,2> pairs = get_array<int64_t,2>(opairs);
multi_array_ref<double,1> sim = get_array<double,1>(osim);
if (weight.empty())
weight = ecmap_t();
gt_dispatch<>()
([&](auto& g, auto w)
{
some_pairs_similarity(g, pairs, sim,
[&](auto u, auto v, auto& mask, auto& w)
{
return hub_suppressed(u, v, mask, w, g);
}, w);
},
all_graph_views(), weight_props_t())
(gi.get_graph_view(), weight);
}
void get_jaccard_similarity(GraphInterface& gi, boost::any as, boost::any weight) void get_jaccard_similarity(GraphInterface& gi, boost::any as, boost::any weight)
{ {
if (weight.empty()) if (weight.empty())
...@@ -156,14 +279,113 @@ void get_inv_log_weight_similarity_pairs(GraphInterface& gi, ...@@ -156,14 +279,113 @@ void get_inv_log_weight_similarity_pairs(GraphInterface& gi,
(gi.get_graph_view(), weight); (gi.get_graph_view(), weight);
} }
void get_r_allocation_similarity(GraphInterface& gi, boost::any as,
boost::any weight)
{
if (weight.empty())
weight = ecmap_t();
gt_dispatch<>()
([&](auto& g, auto& s, auto w)
{
all_pairs_similarity(g, s,
[&](auto u, auto v, auto& mask, auto w)
{
return r_allocation(u, v, mask, w, g);
}, w);
},
all_graph_views(), vertex_floating_vector_properties(),
weight_props_t())
(gi.get_graph_view(), as, weight);
}
void get_r_allocation_similarity_pairs(GraphInterface& gi,
python::object opairs,
python::object osim,
boost::any weight)
{
multi_array_ref<int64_t,2> pairs = get_array<int64_t,2>(opairs);
multi_array_ref<double,1> sim = get_array<double,1>(osim);
if (weight.empty())
weight = ecmap_t();
gt_dispatch<>()
([&](auto& g, auto w)
{
some_pairs_similarity(g, pairs, sim,
[&](auto u, auto v, auto& mask, auto w)
{
return r_allocation(u, v, mask, w, g);
}, w);
},
all_graph_views(), weight_props_t())
(gi.get_graph_view(), weight);
}
void get_leicht_holme_newman_similarity(GraphInterface& gi, boost::any as,
boost::any weight)
{
if (weight.empty())
weight = ecmap_t();
gt_dispatch<>()
([&](auto& g, auto& s, auto w)
{
all_pairs_similarity(g, s,
[&](auto u, auto v, auto& mask, auto w)
{
return leicht_holme_newman(u, v, mask, w, g);
}, w);
},
all_graph_views(), vertex_floating_vector_properties(),
weight_props_t())
(gi.get_graph_view(), as, weight);
}
void get_leicht_holme_newman_similarity_pairs(GraphInterface& gi,
python::object opairs,
python::object osim,
boost::any weight)
{
multi_array_ref<int64_t,2> pairs = get_array<int64_t,2>(opairs);
multi_array_ref<double,1> sim = get_array<double,1>(osim);
if (weight.empty())
weight = ecmap_t();
gt_dispatch<>()
([&](auto& g, auto w)
{
some_pairs_similarity(g, pairs, sim,
[&](auto u, auto v, auto& mask, auto w)
{
return leicht_holme_newman(u, v, mask, w, g);
}, w);
},
all_graph_views(), weight_props_t())
(gi.get_graph_view(), weight);
}
void export_vertex_similarity() void export_vertex_similarity()
{ {
python::def("dice_similarity", &get_dice_similarity); python::def("dice_similarity", &get_dice_similarity);
python::def("dice_similarity_pairs", &get_dice_similarity_pairs); python::def("dice_similarity_pairs", &get_dice_similarity_pairs);
python::def("salton_similarity", &get_salton_similarity);
python::def("salton_similarity_pairs", &get_salton_similarity_pairs);
python::def("hub_promoted_similarity", &get_hub_promoted_similarity);
python::def("hub_promoted_similarity_pairs", &get_hub_promoted_similarity_pairs);
python::def("hub_suppressed_similarity", &get_hub_suppressed_similarity);
python::def("hub_suppressed_similarity_pairs", &get_hub_suppressed_similarity_pairs);
python::def("jaccard_similarity", &get_jaccard_similarity); python::def("jaccard_similarity", &get_jaccard_similarity);
python::def("jaccard_similarity_pairs", &get_jaccard_similarity_pairs); python::def("jaccard_similarity_pairs", &get_jaccard_similarity_pairs);
python::def("inv_log_weight_similarity", &get_inv_log_weight_similarity); python::def("inv_log_weight_similarity", &get_inv_log_weight_similarity);
python::def("inv_log_weight_similarity_pairs", python::def("inv_log_weight_similarity_pairs",
&get_inv_log_weight_similarity_pairs); &get_inv_log_weight_similarity_pairs);
python::def("r_allocation_similarity", &get_r_allocation_similarity);
python::def("r_allocation_similarity_pairs",
&get_r_allocation_similarity_pairs);
python::def("leicht_holme_newman_similarity", &get_leicht_holme_newman_similarity);
python::def("leicht_holme_newman_similarity_pairs",
&get_leicht_holme_newman_similarity_pairs);
}; };
...@@ -26,7 +26,7 @@ using namespace std; ...@@ -26,7 +26,7 @@ using namespace std;
using namespace boost; using namespace boost;
template <class Graph, class Vertex, class Mark, class Weight> template <class Graph, class Vertex, class Mark, class Weight>
double dice(Vertex u, Vertex v, Mark& mark, Weight& weight, Graph& g) auto common_neighbors(Vertex u, Vertex v, Mark& mark, Weight& weight, Graph& g)
{ {
typename property_traits<Weight>::value_type count = 0, ku = 0, kv = 0; typename property_traits<Weight>::value_type count = 0, ku = 0, kv = 0;
for (auto e : out_edges_range(u, g)) for (auto e : out_edges_range(u, g))
...@@ -45,9 +45,49 @@ double dice(Vertex u, Vertex v, Mark& mark, Weight& weight, Graph& g) ...@@ -45,9 +45,49 @@ double dice(Vertex u, Vertex v, Mark& mark, Weight& weight, Graph& g)
} }
for (auto w : adjacent_vertices_range(u, g)) for (auto w : adjacent_vertices_range(u, g))
mark[w] = 0; mark[w] = 0;
return std::make_tuple(count, ku, kv);
}
template <class Graph, class Vertex, class Mark, class Weight>
double dice(Vertex u, Vertex v, Mark& mark, Weight& weight, Graph& g)
{
typename property_traits<Weight>::value_type count = 0, ku = 0, kv = 0;
std::tie(count, ku, kv) = common_neighbors(u, v, mark, weight, g);
return 2 * count / double(ku + kv); return 2 * count / double(ku + kv);
} }
template <class Graph, class Vertex, class Mark, class Weight>
double salton(Vertex u, Vertex v, Mark& mark, Weight& weight, Graph& g)
{
typename property_traits<Weight>::value_type count = 0, ku = 0, kv = 0;
std::tie(count, ku, kv) = common_neighbors(u, v, mark, weight, g);
return count / sqrt(ku * kv);
}
template <class Graph, class Vertex, class Mark, class Weight>
double hub_promoted(Vertex u, Vertex v, Mark& mark, Weight& weight, Graph& g)
{
typename property_traits<Weight>::value_type count = 0, ku = 0, kv = 0;
std::tie(count, ku, kv) = common_neighbors(u, v, mark, weight, g);
return count / double(std::max(ku, kv));
}
template <class Graph, class Vertex, class Mark, class Weight>
double hub_suppressed(Vertex u, Vertex v, Mark& mark, Weight& weight, Graph& g)
{
typename property_traits<Weight>::value_type count = 0, ku = 0, kv = 0;
std::tie(count, ku, kv) = common_neighbors(u, v, mark, weight, g);
return count / double(std::min(ku, kv));
}
template <class Graph, class Vertex, class Mark, class Weight>
double leicht_holme_newman(Vertex u, Vertex v, Mark& mark, Weight& weight, Graph& g)
{
typename property_traits<Weight>::value_type count = 0, ku = 0, kv = 0;
std::tie(count, ku, kv) = common_neighbors(u, v, mark, weight, g);
return count / double(ku * kv);
}
template <class Graph, class Vertex, class Mark, class Weight> template <class Graph, class Vertex, class Mark, class Weight>
double jaccard(Vertex u, Vertex v, Mark& mark, Weight& weight, Graph& g) double jaccard(Vertex u, Vertex v, Mark& mark, Weight& weight, Graph& g)
{ {
...@@ -79,21 +119,47 @@ double inv_log_weighted(Vertex u, Vertex v, Mark& mark, Weight& weight, Graph& g ...@@ -79,21 +119,47 @@ double inv_log_weighted(Vertex u, Vertex v, Mark& mark, Weight& weight, Graph& g
double count = 0; double count = 0;
for (auto e : out_edges_range(u, g)) for (auto e : out_edges_range(u, g))
mark[target(e, g)] += weight[e]; mark[target(e, g)] += weight[e];
for (auto w : adjacent_vertices_range(v, g)) for (auto e : out_edges_range(v, g))
{ {
if (mark[w] > 0) auto w = weight[e];
{ auto dw = std::min(w, mark[target(e, g)]);
if (graph_tool::is_directed(g)) if (mark[target(e, g)] > 0)
count += mark[w] / log(in_degreeS()(w, g, weight)); {
else if (graph_tool::is_directed(g))
count += mark[w] / log(out_degreeS()(w, g, weight)); count += dw / log(in_degreeS()(target(e, g), g, weight));
} else
count += dw / log(out_degreeS()(target(e, g), g, weight));
}
mark[target(e, g)] -= dw;
} }
for (auto w : adjacent_vertices_range(u, g)) for (auto w : adjacent_vertices_range(u, g))
mark[w] = 0; mark[w] = 0;
return count; return count;
} }
template <class Graph, class Vertex, class Mark, class Weight>
double r_allocation(Vertex u, Vertex v, Mark& mark, Weight& weight, Graph& g)
{
double count = 0;
for (auto e : out_edges_range(u, g))
mark[target(e, g)] += weight[e];
for (auto e : out_edges_range(v, g))
{
auto w = weight[e];
auto dw = std::min(w, mark[target(e, g)]);
if (mark[target(e, g)] > 0)
{
if (graph_tool::is_directed(g))
count += dw / double(in_degreeS()(target(e, g), g, weight));
else
count += dw / double(out_degreeS()(target(e, g), g, weight));
}
mark[target(e, g)] -= dw;
}
for (auto w : adjacent_vertices_range(u, g))
mark[w] = 0;
return count;
}
template <class Graph, class VMap, class Sim, class Weight> template <class Graph, class VMap, class Sim, class Weight>
void all_pairs_similarity(Graph& g, VMap s, Sim&& f, Weight& weight) void all_pairs_similarity(Graph& g, VMap s, Sim&& f, Weight& weight)
......
This diff is collapsed.
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