graph_blockmodel_covariates.hh 6.31 KB
Newer Older
1
2
3
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// graph-tool -- a general graph modification and manipulation thingy
//
// Copyright (C) 2006-2016 Tiago de Paula Peixoto <tiago@skewed.de>
//
// 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_BLOCKMODEL_COVARIATE_HH
#define GRAPH_BLOCKMODEL_COVARIATE_HH

#include <cmath>
#include <iostream>
#include <queue>
#include <boost/math/special_functions/zeta.hpp>
#include <boost/functional/hash.hpp>

#include "config.h"
#include <unordered_set>
#include <unordered_map>
#include <tuple>

#ifdef HAVE_SPARSEHASH
#include SPARSEHASH_INCLUDE(dense_hash_set)
#include SPARSEHASH_INCLUDE(dense_hash_map)
#endif

namespace graph_tool
{

#ifdef HAVE_SPARSEHASH
using google::dense_hash_set;
using google::dense_hash_map;
#endif

using namespace boost;

// this will label each edge covariate in a consecutive range [0, C-1]

struct ec_hist
{
    template <class Graph, class EVMap, class EMap>
    void operator()(Graph&g, const EVMap& ev, EMap& ec) const
    {
        typedef typename property_traits<EVMap>::value_type val_t;
        unordered_map<val_t, size_t> ehist;
        for (auto e : edges_range(g))
        {
            auto iter = ehist.find(ev[e]);
            size_t x;
            if (iter == ehist.end())
            {
                x = ehist.size();
                ehist[ev[e]] = x;
            }
            else
            {
                x = iter->second;
            }
            ec[e] = x;
        }
    }
};

// this will split an edge-valued graph into layers

struct split_graph
{
    template <class Graph, class ECMap, class EMap, class VMap, class VVMap, class BMap>
    void operator()(Graph& g, ECMap& ec, VMap& b, EMap& eweight, VMap& vweight,
                    VVMap& vc, VVMap& vmap,
                    std::vector<std::reference_wrapper<GraphInterface>>& us,
                    std::vector<std::reference_wrapper<VMap>>& ub,
                    std::vector<std::reference_wrapper<VMap>>& uvweight,
                    std::vector<std::reference_wrapper<EMap>>& ueweight,
                    std::vector<BMap>& block_map,
                    std::vector<std::reference_wrapper<VMap>>& block_rmap,
                    std::vector<std::reference_wrapper<VMap>>& uvmap) const
    {
        std::vector<unordered_map<size_t, size_t>> vhmap(num_vertices(g));

        auto get_v = [&] (size_t v, size_t l) -> size_t
        {
            auto iter = vhmap[v].find(l);
            if (iter == vhmap[v].end())
            {
                size_t u = add_vertex(us[l].get().GetGraph());
                vhmap[v][l] = u;
                size_t pos = lower_bound(vc[v].begin(), vc[v].end(), l) - vc[v].begin();
                vc[v].insert(vc[v].begin() + pos, l);
                vmap[v].insert(vmap[v].begin() + pos, u);
                uvmap[l].get()[u] = v;
                uvweight[l].get()[u] = vweight[v];
                size_t r =  b[v];
                size_t u_r;

                if (block_map.size() <= l + 1)
                {
                    size_t n = block_map.size();
                    block_map.resize(l + 2);
#ifdef HAVE_SPARSEHASH
                    for (size_t i = n; i < block_map.size(); ++i)
                    {
                        block_map[i].set_empty_key(numeric_limits<size_t>::max());
                        block_map[i].set_deleted_key(numeric_limits<size_t>::max() - 1);
                    }
#endif
                }
                auto& bmap = block_map[l + 1];
                auto riter = bmap.find(r);
                if (riter == bmap.end())
                {
                    u_r = bmap.size();
                    bmap[r] = u_r;
                    block_rmap[l].get()[u_r] = r;
                }
                else
                {
                    u_r = riter->second;
                }
                ub[l].get()[u] = u_r;
                return u;
            }
            else
            {
                return iter->second;
            }
        };

        for (auto e : edges_range(g))
        {
            auto s = source(e, g);
            auto t = target(e, g);
            size_t l = ec[e];

            auto u_s = get_v(s, l);
            auto u_t = get_v(t, l);
            auto ne = add_edge(u_s, u_t, us[l].get().GetGraph()).first;
            ueweight[l].get()[ne] = eweight[e];
        }
    }
};

#ifdef HAVE_SPARSEHASH
    typedef vector<dense_hash_map<size_t, size_t, std::hash<size_t>>> bmap_t;
#else
    typedef vector<unordered_map<size_t, size_t>> bmap_t;
#endif


template <class BlockState>
size_t get_block_map(BlockState& state, typename bmap_t::value_type& bmap,
                     size_t r)
{
    size_t r_u;
    #pragma omp critical (covariate_block_map)
    {
        auto iter = bmap.find(r);
        if (iter == bmap.end())
        {
            if (state.free_blocks.empty())
            {
                r_u = bmap.size();
            }
            else
            {
                r_u = state.free_blocks.back();
                state.free_blocks.pop_back();
            }
            bmap[r] = r_u;
            state.block_rmap[r_u] = r;
        }
        else
        {
            r_u = iter->second;
        }
        assert(r_u < num_vertices(state.bg));
    }
    // assert(state.block_rmap[r_u] == r);
    // assert(r_u < num_vertices(state.bg));
    return r_u;
}

template <class BlockState>
void remove_block_map(BlockState& state, typename bmap_t::value_type& bmap,
                      size_t r)
{
    #pragma omp critical (covariate_block_map)
    {
        auto iter = bmap.find(r);
        if (iter != bmap.end()) // another thread may have removed it already
        {
            state.free_blocks.push_back(iter->second);
            bmap.erase(iter);
        }
        // assert(bmap.find(r) == bmap.end());
    }
}


}; // graph_tool namespace

#endif // GRAPH_BLOCKMODEL_COVARIATE_HH