graph_vertex_similarity.hh 4.03 KB
Newer Older
1 2
// graph-tool -- a general graph modification and manipulation thingy
//
Tiago Peixoto's avatar
Tiago Peixoto committed
3
// Copyright (C) 2006-2019 Tiago de Paula Peixoto <tiago@skewed.de>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#ifndef GRAPH_VERTEX_SIMILARITY_HH
#define GRAPH_VERTEX_SIMILARITY_HH

#include "graph_util.hh"

namespace graph_tool
{
using namespace std;
using namespace boost;

28 29
template <class Graph, class Vertex, class Mark, class Weight>
double dice(Vertex u, Vertex v, Mark& mark, Weight& weight, Graph& g)
30
{
31 32
    typename property_traits<Weight>::value_type count = 0, ku = 0, kv = 0;
    for (auto e : out_edges_range(u, g))
33
    {
34 35 36 37 38 39 40 41 42 43 44
        auto w = weight[e];
        mark[target(e, g)] += w;
        ku += w;
    }
    for (auto e : out_edges_range(v, g))
    {
        auto w = weight[e];
        auto dw = std::min(w, mark[target(e, g)]);
        mark[target(e, g)] -= dw;
        count += dw;
        kv += w;
45 46
    }
    for (auto w : adjacent_vertices_range(u, g))
47 48
        mark[w] = 0;
    return 2 * count / double(ku + kv);
49 50
}

51 52
template <class Graph, class Vertex, class Mark, class Weight>
double jaccard(Vertex u, Vertex v, Mark& mark, Weight& weight, Graph& g)
53
{
54 55
    typename property_traits<Weight>::value_type count = 0, total = 0;
    for (auto e : out_edges_range(u, g))
56
    {
57 58 59
        auto w = weight[e];
        mark[target(e, g)] += w;
        total += w;
60 61
    }

62
    for (auto e : out_edges_range(v, g))
63
    {
64 65 66 67 68
       auto w = weight[e];
       auto dw = std::min(w, mark[target(e, g)]);
       count += dw;
       mark[target(e, g)] -= dw;
       total += w - dw;
69 70 71
    }

    for (auto w : adjacent_vertices_range(u, g))
72
        mark[w] = 0;
73 74 75
    return count / double(total);
}

76 77
template <class Graph, class Vertex, class Mark, class Weight>
double inv_log_weighted(Vertex u, Vertex v, Mark& mark, Weight& weight, Graph& g)
78 79
{
    double count = 0;
80 81
    for (auto e : out_edges_range(u, g))
        mark[target(e, g)] += weight[e];
82 83
    for (auto w : adjacent_vertices_range(v, g))
    {
84
        if (mark[w] > 0)
85
        {
86
            if (graph_tool::is_directed(g))
87
                count += mark[w] / log(in_degreeS()(w, g, weight));
88
            else
89
                count += mark[w] / log(out_degreeS()(w, g, weight));
90 91 92
        }
    }
    for (auto w : adjacent_vertices_range(u, g))
93
        mark[w] = 0;
94 95 96 97
    return count;
}


98 99
template <class Graph, class VMap, class Sim, class Weight>
void all_pairs_similarity(Graph& g, VMap s, Sim&& f, Weight& weight)
100
{
101 102
    vector<typename property_traits<Weight>::value_type>
        mask(num_vertices(g));
Tiago Peixoto's avatar
Tiago Peixoto committed
103 104 105 106 107 108 109 110
    #pragma omp parallel if (num_vertices(g) > OPENMP_MIN_THRESH) \
        firstprivate(mask)
    parallel_vertex_loop_no_spawn
        (g,
         [&](auto v)
         {
             s[v].resize(num_vertices(g));
             for (auto w : vertices_range(g))
111
                 s[v][w] = f(v, w, mask, weight);
Tiago Peixoto's avatar
Tiago Peixoto committed
112
         });
113 114
}

115 116 117
template <class Graph, class Vlist, class Slist, class Sim, class Weight>
void some_pairs_similarity(Graph& g, Vlist& vlist, Slist& slist, Sim&& f,
                           Weight& weight)
118
{
119 120
    vector<typename property_traits<Weight>::value_type>
        mark(num_vertices(g));
Tiago Peixoto's avatar
Tiago Peixoto committed
121
    #pragma omp parallel if (num_vertices(g) > OPENMP_MIN_THRESH) \
122
        firstprivate(mark)
Tiago Peixoto's avatar
Tiago Peixoto committed
123 124 125 126 127 128
    parallel_loop_no_spawn
        (vlist,
         [&](size_t i, const auto& val)
         {
             size_t u = val[0];
             size_t v = val[1];
129
             slist[i] = f(u, v, mark, weight);
Tiago Peixoto's avatar
Tiago Peixoto committed
130
         });
131 132 133 134 135
}

} // graph_tool namespace

#endif // GRAPH_VERTEX_SIMILARITY_HH