graph_vertex_similarity.hh 3.62 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-2017 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
// 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;

template <class Graph, class Vertex, class Mark>
double dice(Vertex u, Vertex v, bool self_loop, Mark& mark, Graph& g)
{
    size_t count = 0;
    for (auto w : adjacent_vertices_range(u, g))
        mark[w] = true;
    if (self_loop)
        mark[u] = true;
    for (auto w : adjacent_vertices_range(v, g))
    {
        if (mark[w])
            count++;
    }
    for (auto w : adjacent_vertices_range(u, g))
        mark[w] = false;
    if (self_loop)
        mark[u] = false;
    return 2 * count / double(out_degree(u, g) + out_degree(v, g));
}

template <class Graph, class Vertex, class Mark>
double jaccard(Vertex u, Vertex v, bool self_loop, Mark& mark, Graph& g)
{
    size_t count = 0, total = 0;
    for (auto w : adjacent_vertices_range(u, g))
    {
        mark[w] = true;
        total++;
    }

    if (self_loop)
        mark[u] = true;

    for (auto w : adjacent_vertices_range(v, g))
    {
        if (mark[w])
            count++;
        else
            total++;
    }

    for (auto w : adjacent_vertices_range(u, g))
        mark[w] = false;
    if (self_loop)
        mark[u] = false;
    return count / double(total);
}

template <class Graph, class Vertex, class Mark>
double inv_log_weighted(Vertex u, Vertex v, Mark& mark, Graph& g)
{
    double count = 0;
    for (auto w : adjacent_vertices_range(u, g))
        mark[w] = true;
    for (auto w : adjacent_vertices_range(v, g))
    {
        if (mark[w])
        {
86
            if (graph_tool::is_directed(g))
87
88
89
90
91
92
93
94
95
96
97
98
99
100
                count += 1. / log(in_degreeS()(w, g));
            else
                count += 1. / log(out_degree(w, g));
        }
    }
    for (auto w : adjacent_vertices_range(u, g))
        mark[w] = false;
    return count;
}


template <class Graph, class VMap, class Sim>
void all_pairs_similarity(Graph& g, VMap s, Sim&& f)
{
Tiago Peixoto's avatar
Tiago Peixoto committed
101
102
103
104
105
106
107
108
109
110
111
    vector<bool> mask(num_vertices(g), false);
    #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))
                 s[v][w] = f(v, w, mask);
         });
112
113
114
115
116
117
}

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

} // graph_tool namespace

#endif // GRAPH_VERTEX_SIMILARITY_HH